Install PyTorch on Raspberry Pi 4 - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
PyTorch on Raspberry Pi 4

Install PyTorch on a Raspberry Pi 4.

Introduction.

This page will guide you through the installation of PyTorch 1.7.0, 1.7.1 or 1.8.0, TorchVision and Caffe2 on a Raspberry Pi 4 with a 64-bit operating system.

PyTorch is a software library specially developed for deep learning. It consumes a lot of resources of your Pi. You can run PyTorch on a Raspberry Pi 4, but don't expect miracles. It runs your models, if not too complicated, but it can't train new models. Nor can it perform so-called transfer learning.

PyTorch runs on Python. A C++ API is available, but we have not tested it. If you want your PyTorch models to run fast on your Raspberry Pi, consider exporting your model to ONNX. Once saved as an ONNX model, you can import it into an ARM-friendly C++ framework such as ncnn or MNN. It will speed up your model considerably.

If you have Ubuntu on your Raspberry Pi, there are special compiled wheels for Ubuntu 18.04 or 20.04.
We only have a guide for the Raspberry Pi 64-bit operating system. As PyTorch evolves, building it on a simple 32-bit machine is getting more and more difficult. The JIT trace, for instance, will not work on a 32-bit OS. Besides, most of our visitors today use a 64-bit operating system on their Raspberry Pi for their deep learning models because it is faster and more robust.
We discuss two installations, one with a Python 3 wheel. The other method is the build from scratch. Unfortunately, there is no official pip3 wheel available for the Raspberry Pi 4. However, we created these wheels and put them on GitHub for your convenience.

Version check.
Please check your operating system before installing PyTorch on your Raspberry 64-bit OS. Run the command uname -a and verify your version with the screen dump below.

VersionCheck_64

You also need to check your C++ compiler version with the command gcc -v. It must also be an aarch64-linux-gnu version, as shown in the screenshot. If you have a 64-bit operating system, but your gcc version is different from the one given above, reinstall the whole operating system with the latest version. The guide is found here: Install 64 bit OS on Raspberry Pi 4. A 64-bit C ++ compiler is needed as we are going to build libraries. Even if you use Python wheels, gcc is called behind the curtains.
Also important, the zram swap size of more than 3 Gbyte after installation according to our instructions. Less is no problem as long as you don't build PyTorch from scratch.

The wheel.

PyTorch is build by Ninja. It takes about 3 hours to complete the whole build. We have posted the wheels on our GitHub page. Feel free to use these. With all the tedious work already done, it takes now only a few minutes to install PyTorch on your Nano. For the diehards, the complete procedure is covered later in this manual.
Pytorch 1.8 for Python 3.
The whole procedure is found below. The wheel was too large to store at GitHub, so Google drive is used. Please make sure you have latest pip3 and python3 version installed, otherwise, pip may come with the message ".whl is not a supported wheel on this platform".
Python check.
Check your Python3 version. Each version needs a unique wheel. Currently, the Raspberry Pi 64-bit operating system uses Python 3.7.3. So you need to download torch-1.7.x-cp37-cp37m-linux_aarch64.whl. Undoubtedly, the Python version will upgrade over time and you will need a different wheel. See out GitHub page for all the wheels.

Python version check



# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies (if not already onboard)
$ sudo apt-get install python3-pip libopenblas-dev libopenmpi-dev libomp-dev
# upgrade setuptools 47.1.1 -> 54.0.0
$ sudo -H pip3 install --upgrade setuptools
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1oJITMYYGrlJ66Jw62-PHQLSZO7PT7Wg8
# install PyTorch 1.8.0
$ sudo -H pip3 install torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies (if not already onboard)
$ sudo apt-get install python3-pip libopenblas-dev libopenmpi-dev libomp-dev
# upgrade setuptools 47.1.1 -> 54.0.0
$ sudo -H pip3 install --upgrade setuptools
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1gkcHqdAhpuDEhgeVlLZ3Eeshi_wDveK_
# install PyTorch 1.7.1
$ sudo -H pip3 install torch-1.7.1a0-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.7.1a0-cp37-cp37m-linux_aarch64.whl
# get a fresh start (remember, the 64-bit OS is still under development)
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies (if not already onboard)
$ sudo apt-get install python3-pip libopenblas-dev libopenmpi-dev libomp-dev
# upgrade setuptools 47.1.1 -> 54.0.0
$ sudo -H pip3 install --upgrade setuptools
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1AV_Ci9b6r2Dm-xMLXwsBx_W1da6vN3lc
# install PyTorch 1.7.0
$ sudo -H pip3 install torch-1.7.0a0-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.7.0a0-cp37-cp37m-linux_aarch64.whl
After a successful installation, you can check PyTorch with the following commands.

PyTorch 1.8.0 succes


Installation from scratch.

Install PyTorch 1.8 for Python 3.

Building PyTorch from scratch is relatively easy. Install some dependencies first, then download the zip from GitHub and finally build the software. The whole procedure takes about 3 hours and is described below. As mentioned before, make sure you have enough RAM available. Minimum a total of 5.5 GByte (swap plus RAM) is needed.
Most important, modify the version number in the file ~/pytorch/version.txt from 1.7.0 to 1.7.1, if you install version PyTorch 1.7.1. It seems the developers forget the adjust this number on GitHub. If you don't change the number, you end up with a torch-1.7.0a0-cp37-cp37m-linux_aarch64.whl wheel, suggesting you have the old version still on your machine.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libopenmpi-dev libomp-dev
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow ccache
$ sudo -H pip3 install -U setuptools
# download PyTorch with all its libraries
$ git clone -b v1.8.0 --depth 1 --recursive https://github.com/pytorch/pytorch.git
$ cd pytorch
# one command to install several dependencies in one go
# installs future, numpy, pyyaml, requests
# setuptools, six, typing_extensions, dataclasses
$ sudo pip3 install -r requirements.txt
# set some temporary environment variables for the Ninja build
# remember, don't close the window as it will delete these variables
$ export BUILD_CAFFE2_OPS=OFF
$ export USE_FBGEMM=OFF
$ export USE_FAKELOWP=OFF
$ export BUILD_TEST=OFF
$ export USE_MKLDNN=OFF
$ export USE_NNPACK=ON
$ export USE_XNNPACK=ON
$ export USE_QNNPACK=ON
$ export MAX_JOBS=4
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist
$ sudo pip3 install torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libopenmpi-dev libomp-dev
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow ccache
$ sudo -H pip3 install -U setuptools
# download PyTorch with all its libraries
$ git clone -b v1.7.1 --depth 1 --recursive https://github.com/pytorch/pytorch.git
$ cd pytorch
# alter version number from 1.7.0 to 1.7.1 and close with <Ctrl>+<X>,<Y>,<Enter>
$ nano version.txt
# one command to install several dependencies in one go
# installs future, numpy, pyyaml, requests
# setuptools, six, typing_extensions, dataclasses
$ sudo pip3 install -r requirements.txt
# set some temporary environment variables for the Ninja build
# remember, don't close the window as it will delete these variables
$ export BUILD_CAFFE2_OPS=OFF
$ export USE_FBGEMM=OFF
$ export USE_FAKELOWP=OFF
$ export BUILD_TEST=OFF
$ export USE_MKLDNN=OFF
$ export USE_NNPACK=ON
$ export USE_XNNPACK=ON
$ export USE_QNNPACK=ON
$ export MAX_JOBS=4
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist
$ sudo pip3 install torch-1.7.1a0-cp37-cp37m-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libopenmpi-dev libomp-dev
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow ccache
$ sudo -H pip3 install -U setuptools
# download PyTorch with all its libraries
$ git clone -b v1.7.0 --depth 1 --recursive https://github.com/pytorch/pytorch.git
$ cd pytorch
# one command to install several dependencies in one go
# installs future, numpy, pyyaml, requests
# setuptools, six, typing_extensions, dataclasses
$ sudo pip3 install -r requirements.txt
# set some temporary environment variables for the Ninja build
# remember, don't close the window as it will delete these variables
$ export BUILD_CAFFE2_OPS=OFF
$ export USE_FBGEMM=OFF
$ export USE_FAKELOWP=OFF
$ export BUILD_TEST=OFF
$ export USE_MKLDNN=OFF
$ export USE_NNPACK=ON
$ export USE_XNNPACK=ON
$ export USE_QNNPACK=ON
$ export MAX_JOBS=4
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist
$ sudo pip3 install torch-1.7.0a0-cp37-cp37m-linux_aarch64.whl
After successful installation, you can check PyTorch with the commands given at the end of the previous section.

PyTorch_1_8_0_Success2

One word about OpenCV. PyTorch has the option the use OpenCV. However, it links hardcoded with the OpenCV version found during the build. As soon as you upgrade your OpenCV, PyTorch will stop working since it can't find the old OpenCV version. Given OpenCV enthusiasm to release at least two or three versions a year, it seems not wise to link PyTorch with OpenCV. Otherwise, you will be forced to recompile PyTorch or manually create a whole bunch of symbolic links to the old libraries.

After a successful installation, many files are no longer needed. Removing them will give you about 3.6 GB of disk space.
# remove the whole folder
$ sudo rm -rf ~/pytorch

TorchVision.

Install torchvision on Raspberry Pi 4.

Torchvision is a collection of frequent used datasets, architectures and image algorithms. The installation is simple when you use one of our wheels found on GitHub. You can also build torchvision from scratch. In that case, you only have to download the version of your choice from the official GitHub page, modify the version number at line 32 in setup.py and issue the command $ python3 setup.py bdist_wheel.
Torchvision assumes PyTorch is installed on your machine on the forehand.

Used with PyTorch 1.8.0
# the dependencies
$ sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download TorchVision 0.9.0
$ gdown https://drive.google.com/uc?id=1OSMSaokFF-NoGSCi2NsybVX7o_QBLw-q
# install TorchVision 0.9.0
$ sudo -H pip3 install torchvision-0.9.0a0+01dfa8e-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.9.0a0+01dfa8e-cp37-cp37m-linux_aarch64.whl
Used with PyTorch 1.7.1
# the dependencies
$ sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download TorchVision 0.8.2
$ gdown https://drive.google.com/uc?id=1Yyc0sp4AXZ_Q5ZygsGB5iGNvC9EmJ-E6
# install TorchVision 0.8.2
$ sudo -H pip3 install torchvision-0.8.2a0+2f40a48-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.8.2a0+2f40a48-cp37-cp37m-linux_aarch64.whl
Used with PyTorch 1.7.0
# the dependencies
$ sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown
# copy binairy
$ sudo cp ~/.local/bin/gdown /usr/local/bin/gdown
# download TorchVision 0.8.1
$ gdown https://drive.google.com/uc?id=1z-Fvo9bvZRI_2K5hBn5LdsvENuio9H8W
# install TorchVision 0.8.1
$ sudo -H pip3 install torchvision-0.8.1a0+45f960c-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.8.1a0+45f960c-cp37-cp37m-linux_aarch64.whl
After installation you may want to check torchvision by verifying the release version.

TorchVision_0_9_0_Succes

Caffe2.

Install Caffe2 on Raspberry Pi 4.

PyTorch comes with Caffe2 on board. In other words, if you have PyTorch installed, you have also installed Caffe2 on your Raspberry Pi 4. Together with two conversion tools. Before using Caffe2, most of the time protobuf needs to be updated. Let's do it right away now.
# update protobuf (3.15.5)
$ sudo -H pip3 install -U protobuf
You can check the installation of Caffe2 with a few Python instructions.

none

You can find all Caffe2 binaries at the following locations.

LocationCaffe2_Rpi
LocationCaffe2_Conv_Rpi
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content