numpy pytorch 接口对应_用树莓派4b构建深度学习应用(四)PyTorch篇
前言
上回我们安装了OpenCV 4.4,相信对源码编译库文件有了一定的了解,这篇我们进一步在树莓派上编译并安装 Pytorch 的最新版本。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 -sPyTorch 安装环境依赖
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-numpy2. 切换虚拟环境
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=12. 安装库文件
pip3 install numpy pyyamlTip: 务必确认一下虚拟环境下,已经安装了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/protobuf4. 生成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.git2. 选择对应版本
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_wheelTip: 编译如遇到以上错误信息,是由于源码中有两处变量类型错误,需要用 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/yolov52. 软链接到 OpenCV
cd ~/my_envs/pytorch/lib/python3.7/site-packages ln -s /usr/local/lib/python3.7/site-packages/cv2 cv2Tip: 若要删除软链接,用 rm -rf ./cv2 即可,要注意的是千万别在最后添加 /。3. 安装依赖库
pip install tqdm pip install matplotlib pip install scipy4. 图像推理
测试用最小的模型 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(安装时要注意版本匹配)。
下一篇预告
我们将开始安装 Tensorflow 的开发环境, 并运行一下 tensorflow lite, 看一下裸板树莓派推理的极限速度, 敬请期待...欢迎扫码关注,更多分享总结
以上是生活随笔为你收集整理的numpy pytorch 接口对应_用树莓派4b构建深度学习应用(四)PyTorch篇的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: router vue 动态改变url_V
- 下一篇: 人工智能,机器学习,深度学习入门好文,强