上回我们安装了OpenCV 4.4,相信对源码编译库文件有了一定的了解,这篇我们进一步在树莓派上编译并安装 Pytorch 的最新版本。
PyTorch 1.6 版本增加了许多新的 API、用于性能改进和性能分析的工具、以及对基于分布式数据并行(Distributed Data Parallel, DDP)和基于远程过程调用(Remote Procedure Call, RPC)的分布式训练的重大更新。部分更新亮点包括:
编译 torch 需要花费大量的内存,在低于 2g 或以下内存的树莓派上,可以通过增加虚拟内存来防止OOM,4g 或 8g 的版本的树莓派可跳过这步。
sudo nano /etc/dphys-swapfil
设置 4g 的交换内存,文件内容如下:
# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
# use under either modified/non-advertising BSD or GPL license
# this file is sourced with . so full normal sh syntax applies
# the default settings are added as commented out CONF_*=* lines
# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap
# set size to absolute value, leaving empty (default) then uses computed value
# you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=4096
保存退出,重启服务生效。
sudo service dphys-swapfile restart
查看一下 swap 是否已调整。
swapon -s
首先安装一些编译需要的依赖库:
sudo apt-get install libopenblas-dev cython3 libatlas-base-dev m4 libblas-dev cmake
sudo apt-get install python3-dev python3-yaml python3-setuptools python3-wheel python3-pillow python3-numpy
deactivate # 退出之前 OpenCV 的虚拟环境
# 创建新的虚拟环境
virtualenv -p python3 ~/my_envs/pytorch
source ~/my_envs/pytorch/bin/activate
export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
pip3 install numpy pyyaml
Tip:
务必确认一下虚拟环境下,已经安装了numpy。没有numpy的话也能成功编译,但是编译出来的PyTorch 不支持numpy。PyTorch was compiled without NumPy support。
# 查询所要编译的版本 git branch -a
git tag
git checkout v1.6.0
git submodule update --init --recursive
git submodule update --remote third_party/protobufgit clone https://github.com/pytorch/pytorch.git
cd pytorch
python3 setup.py bdist_wheel
接下来就是历时 5 个多小时漫长的编译过程了,如果说之前编译 OpenCV 只是去喝杯咖啡就能回来继续,那编译 PyTorch 的时间都够去好好睡上一觉了
顺便安装一个CPU 温度和使用率工具s-tui,来监测一下系统状态。
sudo pip install s-tui --ignore-installed
sudo s-tui
持续满负荷状态:
cd dist
pip3 install ./torch-1.6.0a0+b31f58d-cp37-cp37m-linux_armv7l.whl
看到如下信息,就代表安装成功了。
git clone https://github.com/pytorch/vision.git
pytorch 1.6 对应的 torchvision 是 0.7 的版本,checkout 出来,并安装 PIL 支持。
pip3 install pillow
cd vision
git checkout v0.7.0-rc4
git submodule update --init --recursive
python3 setup.py bdist_wheel
Tip:
编译如遇到以上错误信息,是由于源码中有两处变量类型错误,需要用 size_t 强制类型转换一下。修改对应的 seekable_buffer.cpp 和 util.cpp 文件即可。
cd dist
pip3 install ./torchvision-0.7.0a0+78ed10c-cp37-cp37m-linux_armv7l.whl
搞定!
git clone https://github.com/ultralytics/yolov5
cd ~/my_envs/pytorch/lib/python3.7/site-packages
ln -s /usr/local/lib/python3.7/site-packages/cv2 cv2
Tip:
若要删除软链接,用 rm -rf ./cv2 即可,要注意的是千万别在最后添加 /。
pip install tqdm
pip install matplotlib
pip install scipy
测试用最小的模型 yolov5s 对两张图片进行目标检测,识别率还不错,但速度一般,一张 3.8 秒,一张 2.8 秒,大约 0.3fps,后续我们可以对比一下openvino 加速的效果。
cd yolov5
python3 detect.py --source ./inference/images/ --weights weights/yolov5s.pt --conf 0.5
到这里,树莓派里的 pytorch1.6 已经可以正常工作了。
新型冠状病毒肺炎(Corona Virus Disease 2019,COVID-19),简称“新冠肺炎”,世界卫生组织命名为“2019冠状病毒病” [1-2] ,是指2019新型冠状病毒感染导致的肺炎。