树莓派4B安装Pytorch, torchvision

Install Pytorch

  • Raspberry Pi 4B
  • Linux raspberrypi 4.19.75-v7l+ #1270 SMP Tue Sep 24 18:51:41 BST 2019 armv7l GNU/Linux
  • 2G RAM
  • 16G DISK

增加交换内存

  1. 关闭内存交换: sudo dphys-swapfile swapoff

  2. 修改配置文件 sudo vim /etc/dphys-swapfile,设置CONF_SWAPSIZE=4096

    这里虽然设置为4096,但free -m 查看仍然只有2G交换内存

  3. 开启内存交换:sudo dphys-swapfile swapon

  4. 检查:free -m,若swap无变换可尝试重启

Pytorch依赖项

sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-yaml libatlas-base-dev
sudo apt-get install cython3 libatlas-base-dev m4 libblas-dev cmake
sudo apt-get install python3-dev python3-setuptools python3-wheel python3-pillow python3-numpy

pip3 install numpy pyyaml

For raspberry pi 4 there may be an issue with the gcc and g++ version. Install an older version and use it

sudo apt-get install gcc-4.9 g++-4.9

设置环境变量

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

Download Pytorch

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch

--recursive:循环克隆项目,即将该项目引用的项目同时克隆

国内GitHub clone太慢:

  • 尝试码云镜像 ,虽然下载Pytorch仓库很快,但由于依赖库仍然是GitHub地址,依然无法clone 成功

  • 启动Clash代理git config --global http.proxy socks5://127.0.0.1:7890,提速明显

git checkout v1.6.0

git submodule update --init  --recursive
git submodule update --remote third_party/protobuf

Build Pytorch

python3 setup.py bdist_wheel

这样将build一个wheel文件,保存在pytorch/dist

cd dist
pip3 install ./torch-1.6.0-xxx_linux_armv71.whl

Install

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

Check

python3
>>> import torch
>>> import numpy as np
>>> a = torch.from_numpy(np.random.randn(1, 100))
>>> print(a)

Install Torchvision

git clone https://github.com/pytorch/vision.git
pip3 install pillow
cd vision
git checkout v0.7.0-rc4	# pytorch 1.6 对应 torchvision 0.7
git submodule update --init --recursive
python3 setup.py bdist_wheel

ERROR

/home/pi/Pytorch/vision/torchvision/csrc/cpu/decoder/defs.h:12:10: fatal error: libavcodec/avcodec.h: 没有 那个文件或目录
#include <libavcodec/avcodec.h>
       ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

setup.py中178行if has_ffmpeg改为if False

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

参考:

**已编译whl安装包: **

  • torch v1.6.0 + torchvision v0.7.0
  • torch v1.7.0 + torchvision v0.8.0

务必使用对应版本!微云 code: tiktak

comments powered by Disqus