一、安装系统
1、下载Download ZIP文件。
Raspbian系统下载地址:https://www.raspberrypi.org/downloads/raspbian/
2、格式化TF卡(新卡可以不格式化处理):
下载格式化工具:SD card formatter
将TF卡插入读卡器并连接电脑,打开软件,选择需要格式化的卡,点击Format,其他选项默认。
3、系统镜像写入tf卡
下载写入工具:win32diskimager
解压下载的ZIP包得到一个.img的镜像文件(解压路径不能含有中文)。
使用win32diskimager]软件,选择设备和解压的.img的镜像文件,点击写入。
二、树莓派无键盘鼠标配置ssh和wifi
1、将系统镜像写入空白卡。
2.于 TF卡/boot/目录下新建并命名一个ssh的文件:
3.新建并命名 wpa_supplicant.conf进行wifi配置:
3.新建并命名 wpa_supplicant.conf进行wifi配置:
文件中写入如下配置内容;
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB
network={
ssid="wifi name" //WiFi名字,不能有中文
psk="wifi password" //你的WiFi密码
key_mgmt=WPA-PSK
}
4.完成后,将TF卡插入raspberry pi 并启动电源。默认用户名:pi 密码:raspberry
三、树莓派4b上安装tensorflow2.0+keras
1、在/usr/local/下新建文件夹tf_pi
mkdir tf_pi
2.安装虚拟环境
python3 -m pip install virtualenv
virtualenv env
source env/bin/activate
3、安装各种环境
sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev
python3 -m pip install keras_applications==1.0.8 --no-deps
python3 -m pip install keras_preprocessing==1.1.0 --no-deps
python3 -m pip install h5py==2.9.0
sudo apt-get install -y openmpi-bin libopenmpi-dev
sudo apt-get install -y libatlas-base-dev
python3 -m pip install -U six wheel mock
4.安装tensorflow
wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.0.0/tensorflow-2.0.0-cp37-none-linux_armv7l.whl
python3 -m pip uninstall tensorflow
python3 -m pip install tensorflow-2.0.0-cp37-none-linux_armv7l.whl
5.重启设备后,启动虚拟环境
cd tf_pi
source env/bin/activate
6、试一下(应该可以显示2.0.0):
7.没有问题的话安装keras(下面的install keras以外的4步其实都没啥用,不过为防万一还是写在这里)(记住还是要在虚拟环境下安装)
sudo apt-get install libhdf5-serial-dev
pip3 install h5py
pip3 install pillowimutils
pip3 install scipy--no-cache-dir
pip3 install keras
9、安装JDK
sudo apt-get purge openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre
10.安装PyCharm(从PyCharm官网下载下来,解压后按照Install-Linux-tar.txt执行即可)
11.在PyCharm里面设置虚拟环境:File->Settings->Project:xxxx->Project Interpreter,选择那个小螺丝按钮->Add
Existing environment里面选择刚才虚拟环境里面env/Python3.7,另外建议把【Make available to all projects】选上,以后就不用选了。
12.到此环境应该设置完毕了,写入如下手写数字识别代码体验下
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets
(x, y), (x_val, y_val) = datasets.mnist.load_data()
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.
y = tf.convert_to_tensor(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
train_dataset = train_dataset.batch(200)
model = keras.Sequential([
layers.Dense(512, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(10)])
optimizer = optimizers.SGD(learning_rate=0.001)
def train_epoch(epoch):
# Step4.loop
for step, (x, y) in enumerate(train_dataset):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28*28))
# Step1. compute output
# [b, 784] => [b, 10]
out = model(x)
# Step2. compute loss
loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]
# Step3. optimize and update w1, w2, w3, b1, b2, b3
grads = tape.gradient(loss, model.trainable_variables)
# w' = w - lr * grad
optimizer.apply_gradients(zip(grads, model.trainable_variables))
if step % 100 == 0:
print(epoch, step, 'loss:', loss.numpy())
def train():
for epoch in range(30):
train_epoch(epoch)
if __name__ == '__main__':
train()
参考:
树莓派中安装JDK
安装TensorFlow2.0