用树莓派4b构建深度学习应用(四)PyTorch篇

    科技2026-06-13  4

    前言

    上回我们安装了OpenCV 4.4,相信对源码编译库文件有了一定的了解,这篇我们进一步在树莓派上编译并安装 Pytorch 的最新版本。

    文章目录

    前言PyTorch 1.6 的新特性增加交换内存(可选)1. 修改配置文件 PyTorch 安装环境依赖1. 安装依赖2. 切换虚拟环境 编译安装 PyTorch1. 设置配置项2. 安装库文件3. 下载源码及支持库4. 生成whl安装包5. 安装 PyTorch 编译安装 Torchvision1. 下载源码2. 选择对应版本3. 安装 TorchVision 运行 yolo v51. 克隆 yolov5 源码2. 软链接到 OpenCV3. 安装依赖库4. 图像推理 资料下载下一篇预告

    PyTorch 1.6 的新特性

    PyTorch 1.6 版本增加了许多新的 API、用于性能改进和性能分析的工具、以及对基于分布式数据并行(Distributed Data Parallel, DDP)和基于远程过程调用(Remote Procedure Call, RPC)的分布式训练的重大更新。部分更新亮点包括:

    原生支持自动混合精度训练(AMP, automatic mixed-precision training),只需增加几行新代码就可以提高大型模型训练50-60% 的速度。为 tensor-aware 增加对 TensorPipe 的原生支持在前端 API 增加了对 complex tensor 的支持新的分析工具提供了张量级的内存消耗信息针对分布式数据并行训练和远程过程调用的多项改进和新功能

    增加交换内存(可选)

    编译 torch 需要花费大量的内存,在低于 2g 或以下内存的树莓派上,可以通过增加虚拟内存来防止OOM,4g 或 8g 的版本的树莓派可跳过这步。

    1. 修改配置文件

    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

    PyTorch 安装环境依赖

    1. 安装依赖

    首先安装一些编译需要的依赖库:

    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

    2. 切换虚拟环境

    deactivate # 退出之前 OpenCV 的虚拟环境 # 创建新的虚拟环境 virtualenv -p python3 ~/my_envs/pytorch source ~/my_envs/pytorch/bin/activate

    编译安装 PyTorch

    1. 设置配置项

    export NO_CUDA=1 export NO_DISTRIBUTED=1 export NO_MKLDNN=1 export NO_NNPACK=1 export NO_QNNPACK=1

    2. 安装库文件

    pip3 install numpy pyyaml

    Tip:

    务必确认一下虚拟环境下,已经安装了numpy。没有numpy的话也能成功编译,但是编译出来的PyTorch 不支持numpy。PyTorch was compiled without NumPy support。

    3. 下载源码及支持库

    git clone https://github.com/pytorch/pytorch.git cd pytorch # 查询所要编译的版本 git branch -a git tag git checkout v1.6.0 git submodule update --init --recursive git submodule update --remote third_party/protobuf

    4. 生成whl安装包

    python3 setup.py bdist_wheel

    接下来就是历时 5 个多小时漫长的编译过程了,如果说之前编译 OpenCV 只是去喝杯咖啡就能回来继续,那编译 PyTorch 的时间都够去好好睡上一觉了

    顺便安装一个CPU 温度和使用率工具s-tui,来监测一下系统状态。

    sudo pip install s-tui --ignore-installed sudo s-tui

    持续满负荷状态:

    5. 安装 PyTorch

    cd dist pip3 install ./torch-1.6.0a0+b31f58d-cp37-cp37m-linux_armv7l.whl

    看到如下信息,就代表安装成功了。

    编译安装 Torchvision

    1. 下载源码

    git clone https://github.com/pytorch/vision.git

    2. 选择对应版本

    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 文件即可。

    3. 安装 TorchVision

    cd dist pip3 install ./torchvision-0.7.0a0+78ed10c-cp37-cp37m-linux_armv7l.whl

    搞定!

    运行 yolo v5

    1. 克隆 yolov5 源码

    git clone https://github.com/ultralytics/yolov5

    2. 软链接到 OpenCV

    cd ~/my_envs/pytorch/lib/python3.7/site-packages ln -s /usr/local/lib/python3.7/site-packages/cv2 cv2

    Tip:

    若要删除软链接,用 rm -rf ./cv2 即可,要注意的是千万别在最后添加 /。

    3. 安装依赖库

    pip install tqdm pip install matplotlib pip install scipy

    4. 图像推理

    测试用最小的模型 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 已经可以正常工作了。

    资料下载

    若想跳过冗长的编译过程,可以直接下载whl,然后用 pip install 进行安装即可。基于 python 3.7 的版本,除了 pytorch 1.6 + torchvision 0.7,我还编译了最新的 pytorch 1.7 + torchvision 0.8(安装时要注意版本匹配)。

    公众号“深度觉醒”,后台回复:“rpi04”,可获取下载链接。


    下一篇预告

    我们将开始安装 Tensorflow 的开发环境, 并运行一下 tensorflow lite, 看一下裸板树莓派推理的极限速度, 敬请期待…


    Processed: 0.014, SQL: 9