preface

Last time we installed OpenCV 4.4, we are confident that we have some knowledge of the source compiler library files. In this article we will build and install the latest version of Pytorch on raspberry PI.

New features in PyTorch 1.6

PyTorch 1.6 adds a number of new apis, tools for performance improvement and analysis, and support for Distributed Data Parallel (DDP) and Remote Procedure Call (RPC) based applications. RPC) major update to distributed training. Some of the highlights include:

  • Native support for automatic mixed-precision training (AMP) can increase the speed of large model training by 50-60% with just a few lines of new code.
  • Added native support for TensorPipe for tensoraware
  • Complex Tensor support has been added to the front-end API
  • New analysis tools provide tension-scale memory consumption information
  • Several improvements and new features for distributed data parallel training and remote procedure calls

(Optional) Adding swap memory

Compiling the Torch takes a lot of memory, so you can skip this step by adding virtual memory on the OOM, 4G or 8GB versions of the Raspberry PI to prevent this step.

1. Modify the configuration file

sudo nano /etc/dphys-swapfil
Copy the code

Set the swap memory to 4 GB. The file content is as follows:

# /etc/dphys-swapfile - user settings for dphys-swapfile package # author Neil Franklin, 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=4096Copy the code

Save the Settings and exit. Restart the service to take effect.

sudo service dphys-swapfile restart
Copy the code

Check whether swap has been adjusted.

swapon -s
Copy the code

PyTorch installation environment dependencies

1. Install dependencies

Start by installing some dependency libraries for compilation:

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
Copy the code

2. Switch the virtual environment

Deactivate # deactivate the virtual environment before exiting OpenCV#Create a new virtual environment
virtualenv -p python3 ~/my_envs/pytorch
source ~/my_envs/pytorch/bin/activate
Copy the code

Compile and install PyTorch

1. Set the configuration items

export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
Copy the code

2. Install library files

pip3 install numpy pyyaml
Copy the code

Tip:

Make sure that numpy is installed in the virtual environment. It can be compiled without Numpy, but the compiled PyTorch does not support Numpy. PyTorch was compiled without NumPy support.

3. Download the source code and support library

git clone https://github.com/pytorch/pytorch.git
cd pytorch
#Query the version to buildGit branch -a git tag Git checkout v1.6.0 git submodule update --init --recursive git submodule update --remote third_party/protobufCopy the code

4. Generate the WHL installation package

python3 setup.py bdist_wheel 
Copy the code

What followed was a long compilation that took more than five hours, and while OpenCV was just a coffee break, PyTorch was long enough to get a good night’s sleep

Install a CPU temperature and usage tool, S-TUI, to monitor system status.

sudo pip install s-tui  --ignore-installed
sudo s-tui
Copy the code

A state of continuous full load:

5. Install PyTorch

CD Dist Pip3 install. / Torch -1.6.0 A0 + b31f58D-cp37-cp37M-linux_armv7L.whlCopy the code

If the following information is displayed, the installation is successful.

Compile and install Torchvision

1. Download source code

git clone https://github.com/pytorch/vision.git
Copy the code

2. Select a version

Pytorch 1.6 corresponds to TorchVision which is a version of 0.7, checkout out, and install PIL support.

Pip3 Install Pillow CD Vision Git checkout v0.7.0-rc4 Git submodule update --init --recursive python3 setup.py bdist_wheelCopy the code

Tip:

Error: the source code contains two variable types that need to be cast using size_t. Modify the seekabLE_buffer. CPP and util. CPP files.

3. Install TorchVision

CD Dist Pip3 install. / TorchVision-0.7.0A0 + 78ED10C-cp37-cp37M-linux_armv7L.whlCopy the code

Done!

Run yolo v5

1. Clone yolov5 source code

git clone https://github.com/ultralytics/yolov5
Copy the code

2. Soft link to OpenCV

CD ~ / my_envs pytorch/lib/python3.7 / site - packages ln -s/usr/local/lib/python3.7 / site - packages/cv2 cv2Copy the code

Tip:

To remove soft links, use rm -rf./cv2. Don’t add/at the end.

3. Install the dependent libraries

pip install tqdm
pip install matplotlib
pip install scipy
Copy the code

4. Image reasoning

In the test, the smallest model Yolov5s was used for target detection of the two images. The recognition rate was not bad, but the speed was average, 3.8 seconds for one image and 2.8 seconds for another, about 0.3 FPS. Subsequently, we can compare the acceleration effect of OpenVino.

CD yolov5 python3 detect.py --source./inference/images/ --weights weights/yolov5s.pt --conf 0.5Copy the code

At this point, pytorch1.6 in raspberry PI is working.

Data download

To skip the lengthy build process, download WHL directly and install it with PIP Install. Based on the version of Python 3.7, in addition to PyTorch 1.6 + TorchVision 0.7, I compiled the latest PyTorch 1.7 + TorchVision 0.8 (note the version match when installing).

The public account “Deep Awakening”, the background reply: “RPI04”, can obtain the download link.


Next up

We’ll start installing the Tensorflow development environment and run Tensorflow Lite to see the ultimate speed of bareboard Raspberry PI reasoning, so stay tuned…