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.

Last updated: March 20, 2022

Introduction.

This page will guide you through the installation of PyTorch 1.11.0, or an earlier version, TorchVision, LibTorch 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.

Tip.

noneRegularly, we get the question if we have an SD image of a Raspberry Pi 4 with pre-installed frameworks and deep-learning examples.
We are happy to comply with this request. Please, find a complete working Raspberry Pi 4 dedicated to deep learning on our GitHub page. Download the zip file from our GDrive site, unzip and flash the image on a 16 GB SD-card, and enjoy!
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.
PyTorch 1.11.
PyTorch 1.11 has the usual improvements and bug fixes. Take a look at the changelog.
PyTorch 1.10.
PyTorch 1.10 has the usual improvements and bug fixes. Please note, some operations have different behavior compared to version 1.9. Take a look at the changelog.
PyTorch 1.9.
Some warnings about version 1.9.0. As seen here, quite a few changes are made to the software since the last version. Not all operations and declarations are supported anymore. It can cause backward compatibility issues when your 1.8 networks are running on this new version.

Installation by 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.

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. The Raspberry Pi 64-bit operating system uses Python 3.7.3. So you need to download torch-1.X.Y-cp37-cp37m-linux_aarch64.whl. If you have the latest Raspberry Pi Bullseye OS, your Python version is 3.9.2.
Undoubtedly, the Python version will upgrade over time and you will need a different wheel. See out GitHub page for all the wheels.

BullseyeBuster


# 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 libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown

Buster OS

# download the wheel
$ gdown https://drive.google.com/uc?id=1gAxP9q94pMeHQ1XOvLHqjEcmgyxjlY_R
# install PyTorch 1.11.0
$ sudo -H pip3 install torch-1.11.0a0+gitbc2c6ed-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.11.0a0+gitbc2c6ed-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download the wheel
$ gdown https://drive.google.com/uc?id=1ilCdwQX7bq72OW2WF26Og90OpqFX5g_-
# install PyTorch 1.11.0
$ sudo -H pip3 install torch-1.11.0a0+gitbc2c6ed-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torch-1.11.0a0+gitbc2c6ed-cp39-cp39-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 libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown

Buster OS

# download the wheel
$ gdown https://drive.google.com/uc?id=1y-X1UGC43xUfnycJcuDp7tlyFi8QHxvs
# install PyTorch 1.10.0
$ sudo -H pip3 install torch-1.10.0a0+git36449ea-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.10.0a0+git36449ea-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download the wheel
$ gdown https://drive.google.com/uc?id=10zJmU1f7PoxN8ARZol4mtbwok4G23XKZ
# install PyTorch 1.10.0
$ sudo -H pip3 install torch-1.10.0a0+git36449ea-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torch-1.10.0a0+git36449ea-cp39-cp39-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 libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown

Buster OS

# download the wheel
$ gdown https://drive.google.com/uc?id=18KztAf3W_kqFYP2f9uqLj3HBdhoMvVe4
# install PyTorch 1.9.0
$ sudo -H pip3 install torch-1.9.0a0+gitd69c22d-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torch-1.9.0a0+gitd69c22d-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download the wheel
$ gdown https://drive.google.com/uc?id=1p9Sp5YmCJwIkmQIWkpuIkE4NgeYPOr-U
# install PyTorch 1.9.0
$ sudo -H pip3 install torch-1.9.0a0+gitd69c22d-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torch-1.9.0a0+gitd69c22d-cp39-cp39-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 libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown

Buster OS

# download the wheel
$ gdown https://drive.google.com/uc?id=1JF3x556B1ZPe7rp3ogyZiCJPCDsu57RY
# 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

or Bullseye OS
# download the wheel
$ gdown https://drive.google.com/uc?id=1E4bP9NAG5pDSXGWYPGsJ5uzFBq47VN14
# install PyTorch 1.8.0
$ sudo -H pip3 install torch-1.8.0a0+37c1f4a-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torch-1.8.0a0+37c1f4a-cp39-cp39-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 libjpeg-dev libopenblas-dev libopenmpi-dev libomp-dev
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
$ sudo -H pip3 install Cython
# install gdown to download from Google drive
$ sudo -H pip3 install gdown

Buster OS

# download the wheel
$ gdown https://drive.google.com/uc?id=1o2PjvvKkHpYx7QMfMLUoNck7ZiE_TGgR
# 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_11_0_Succes2


Installation from scratch.

Install PyTorch 1.11 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 hours and is described below.
Swap memory.
The whole compilation of PyTorch requires a 8 GB of RAM. Most likely you have to enlarge your allocated swap memory. As you already know, it's done by modifing the limits in two files. First, you have to set CONF_MAXSWAP in /sbin/dphys-swapfile. Next the CONF_SWAPSIZE in /etc/dphys-swapfile need to be set.
Check your free disk space with $ df -h. Obviously, if your drive still has 2 GB free, your swap space is at most 2 GB. Also take into account the size of the newly generated software, and you will end up with a necessary free disk space of 9 to 10 GB.
Close the session with a $ sudo reboot.

Clang compiler.

Before the build can begin, some preparations are required. First, you must have the latest clang compiler. There is a constant stream of issues with the GNU compiler. Usually, it has to do with poor support of the NEON architecture of the ARM cores, causing floating points to be erroneous or truncated.

ClangRPi

The clang compiler doesn't seem to have a problem with the code at all, so time to use clang this time. We know there are people disliking clang. The GNU compiler used to be superior compared to clang, but those days are long gone. Today, both compilers perform almost identically.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the clang compiler
$ sudo apt-get install clang
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
# download PyTorch with all its libraries
$ git clone -b v1.11.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_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist

Buster OS

$ sudo -H pip3 install torch-1.11.0a0+gitbc2c6ed-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
$ sudo -H pip3 install torch-1.11.0a0+gitbc2c6ed-cp39-cp39-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the clang compiler
$ sudo apt-get install clang
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
# download PyTorch with all its libraries
$ git clone -b v1.10.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_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist

Buster OS

$ sudo -H pip3 install torch-1.10.0a0+git36449ea-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
$ sudo -H pip3 install torch-1.10.0a0+git36449ea-cp39-cp39-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the clang compiler
$ sudo apt-get install clang
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
# download PyTorch with all its libraries
$ git clone -b v1.9.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_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist

Buster OS

$ sudo -H pip3 install torch-1.9.0a0+gitd69c22d-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
$ sudo -H pip3 install torch1.9.0a0+gitd69c22d-cp39-cp39-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the clang compiler
$ sudo apt-get install clang
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
# 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_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist

Buster OS

$ sudo pip3 install torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
$ sudo -H pip3 install torch-1.8.0a0+37c1f4a-cp39-cp39-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the clang compiler
$ sudo apt-get install clang
# the dependencies
$ sudo apt-get install ninja-build git cmake
$ sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
$ sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
$ sudo -H pip3 install -U --user wheel mock pillow
# above 58.3.0 you get version issues
$ sudo -H pip3 install setuptools==58.3.0
# 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_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build, if necessary
$ python3 setup.py clean
# start the build
$ python3 setup.py bdist_wheel
# install the wheel
$ cd dist

Buster OS

$ sudo pip3 install torch-1.7.0a0-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
$ sudo -H pip3 install torch-1.7.0a0-cp39-cp39-linux_aarch64.whl
After successful installation, you can check PyTorch with the commands given at the end of the previous section.

PyTorch_1_11_0_Succes

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.
Also, reduce the swap space to the normal size of 100 MByte, to avoid premature wear of the SD card.
# 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.11.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

Buster OS

# download TorchVision 0.12.0
$ gdown https://drive.google.com/uc?id=1btEPxKYfL6OP1Xaedly-ttfPTH-mu4fp
# install TorchVision 0.12.0
$ sudo -H pip3 install torchvision-0.12.0a0+9b5a3fe-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.12.0a0+9b5a3fe-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download TorchVision 0.12.0
$ gdown https://drive.google.com/uc?id=1oDsJEHoVNEXe53S9f1zEzx9UZCFWbExh
# install TorchVision 0.12.0
$ sudo -H pip3 install torchvision-0.12.0a0+9b5a3fe-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torchvision-0.12.0a0+9b5a3fe-cp39-cp39-linux_aarch64.whl
Used with PyTorch 1.10.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

Buster OS

# download TorchVision 0.12.0
$ gdown https://drive.google.com/uc?id=1uQgjrucNSfpliGBWeKUOuo4XGWiBaxQx
# install TorchVision 0.12.0
$ sudo -H pip3 install torchvision-0.12.0a0+09c5ddd-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.12.0a0+09c5ddd-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download TorchVision 0.12.0
$ gdown https://drive.google.com/uc?id=1nX4uSoubwq8NB9k-2DF6LbPyiqwocgEW
# install TorchVision 0.12.0
$ sudo -H pip3 install torchvision-0.12.0a0+57a77c4-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torchvision-0.12.0a0+57a77c4-cp39-cp39-linux_aarch64.whl
Used with PyTorch 1.9.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

Buster OS

# download TorchVision 0.10.0
$ gdown https://drive.google.com/uc?id=1XyYG6hHh7QYaLrvE-mByvhNK_0rLOd0G
# install TorchVision 0.10.0
$ sudo -H pip3 install torchvision-0.10.0a0+300a8a4-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.10.0a0+300a8a4-cp37-cp37m-linux_aarch64.whl

or Bullseye OS
# download TorchVision 0.10.0
$ gdown https://drive.google.com/uc?id=13fKq1V-zLbLBWPw3zP4EZxyMXyOiJWPi
# install TorchVision 0.10.0
$ sudo -H pip3 install torchvision-0.10.0a0+300a8a4-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torchvision-0.10.0a0+300a8a4-cp39-cp39-linux_aarch64.whl
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

Buster OS

# download TorchVision 0.9.0
$ gdown https://drive.google.com/uc?id=1isEX4w4oegkYpv7WalvYPkTpPCcI4CUR
# 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

or Bullseye OS
# download TorchVision 0.9.0
$ gdown https://drive.google.com/uc?id=1mqNqUC1t0MekF5h1WCEqt3soKWhc6Py4
# install TorchVision 0.9.0
$ sudo -H pip3 torchvision-0.9.0a0+01dfa8e-cp39-cp39-linux_aarch64.whl
# clean up
$ rm torchvision-0.9.0a0+01dfa8e-cp39-cp39-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

Buster OS

# download TorchVision 0.8.0
$ gdown https://drive.google.com/uc?id=1H3YLkOicAN78tBLAmzCx2lt_WBjjmFCz
# install TorchVision 0.8.0
$ sudo -H pip3 install torchvision-0.8.0a0+45f960c-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm torchvision-0.8.0a0+45f960c-cp37-cp37m-linux_aarch64.whl
After installation you may want to check torchvision by verifying the release version.

TorchVision_0_10_0_Succes

LibTorch.

Install LibTorch on Raspberry Pi 4.

The native language of PyTorch is Python. For a reason.
AI scientists want to mold their deep learning models and analyses the outcomes without the hassle of dedicated software programming.
Python is well suited for this job. Most people can understand and modify a Python program in a few weeks, while it takes years to grasp the subtleties of the (low-level) C++ language. If you are new to deep learning and PyTorch, we strongly recommend using Python.

There are more things to know before starting your C++ adventure.
  • The precompiled LibTorch is only suitable for an x86_64 machine. There is no aarch64 version, we had to build it from scratch.

  • The C++ documentation is of low quality. You have a brief explanation of the function calls, but a good guide on installing LibTorch and what to do if something goes wrong is missing. (By the way, most frameworks have the same lack of documentation.)

  • Compilation times are significant on a Raspberry Pi 4. A simple example.cpp, shown later, takes about a minute to build. It is better to use cross-compilation techniques if you are serious about programming in C++. Waiting more than a minute to correct a simple typo is frustrating.

  • The core of PyTorch is built with an old GCC compiler, not using the 2011 C++ naming convention for strings. To use the static build LibTorch, you must use the macro _GLIBCXX_USE_CXX11_ABI, which may conflict with other parts of your C++ program. If you want to use LibTorch, better use the dynamic build version. It can work smoothly with other C++ packages, such as ROS.

  • After three days of toil, we still couldn't get the static build LibTorch to work on the Raspberry Pi. By scrolling through the forums, you will read the same questions and the same (poor) solutions about LibTorch static links over and over.

  • There are problems with the GCC optimization of intrinsics of the NEON registers of the ARM core, as shown above. It forces us to use the clang compiler. Therefore, when using the LibTorch API, you may be forced to use the clang compiler as well.

That said, let's start building the LibTorch C++ API.
There are two possible ways to install LibTorch on your Raspberry Pi. The first method is to download the tar.xz file from our GitHub and extract it. All necessary libraries and headers are installed, as seen in the screenshot below.

LibTorchFolderRPi

The files are placed in the folder named pytorch. To avoid conflicts, make sure you don't have a folder with the same name in the directory where the tar.gz file is to be unzipped. The file structure is identical to the original libtorch-cxx11-abi-shared-with-deps-1.10.1+cpu.zip, found on the PyTorch installation page.
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown

Buster OS

$ gdown https://drive.google.com/uc?id=1sj2V0RONtSLHHjmQ904U82YjdV0TM5bf
$ sudo tar -xf libtorch-1.11.0-Buster-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.11.0-Buster-aarch64-CPU.tar.gz

Bullseye

$ gdown https://drive.google.com/uc?id=1-kTx72-6eePrXVjFtJN-3ntu1DCI8FuD
$ sudo tar -xf libtorch-1.11.0-Bullseye-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.11.0-Bullseye-aarch64-CPU.tar.gz
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown

Buster OS

$ gdown https://drive.google.com/uc?id=1nC_gWNjFirZz74wV4z7t2un4ktCtlRpI
$ sudo tar -xf libtorch-1.10.0-Buster-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.10.0-Buster-aarch64-CPU.tar.gz

Bullseye

$ gdown https://drive.google.com/uc?id=1z58SN6Inh3ka_jFs97LefBuvfGq9o1to
$ sudo tar -xf libtorch-1.10.0-Bullseye-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.10.0-Bullseye-aarch64-CPU.tar.gz
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown

Buster OS

$ gdown https://drive.google.com/uc?id=1yhFMxngRq_RY2DPWwqZ8wwNmzBBjzrHR
$ sudo tar -xf libtorch-1.9.0-Buster-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.9.0-Buster-aarch64-CPU.tar.gz

Bullseye

$ gdown https://drive.google.com/uc?id=1My9PPoa9oYv7ZaPdsoXq4FyqRCHHA_4c
$ sudo tar -xf libtorch-1.9.0-Bullseye-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.9.0-Bullseye-aarch64-CPU.tar.gz
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown

Buster OS

$ gdown https://drive.google.com/uc?id=1kpAb7qIGD0ICy8HYHDGMiY0Yj-K-eGAP
$ sudo tar -xf libtorch-1.8.0-Buster-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.8.0-Buster-aarch64-CPU.tar.gz

Bullseye

$ gdown https://drive.google.com/uc?id=1PrqFOPHxrbLGEMuxBTxYY-Ubjk-l88_Z
$ sudo tar -xf libtorch-1.8.0-Bullseye-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.8.0-Bullseye-aarch64-CPU.tar.gz
# install gdown to download from Google drive, if not done yet
$ sudo -H pip3 install gdown

Buster OS

$ gdown https://drive.google.com/uc?id=1XpdE-AG4QMz1HE8bDdpiKw1Fpl3Em0kK
$ sudo tar -xf libtorch-1.7.0-Buster-aarch64-CPU.tar.gz
# clean up
$ rm libtorch-1.7.0-Buster-aarch64-CPU.tar.gz
The other way is to compile the LibTorch C++ API from scratch. The whole procedure is almost identical to the original Python installation. Follow the instructions to compile and install the libraries from scratch. If you want static libraries (libtorch.a), set the environment flag BUILD_SHARED_LIBS=OFF. As mentioned, we couldn't get the static libraries to work on the Raspberry Pi. Better to use the dynamic libraries (libtorch.so).
# First, download and install the dependencies and
# your PyTorch version of your choice as specified above.
# Follow all steps up until the environment variables.
$ cd ~/pytorch
# now set the temporary environment variables for LibTorch
# 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_NUMPY=ON
$ export USE_OPENCV=OFF
$ export USE_NCCL=OFF
$ export USE_SYSTEM_NCCL=OFF
$ export BUILD_SHARED_LIBS=ON
$ PATH=/usr/lib/ccache:$PATH
# set clang compiler
$ export CC=clang
$ export CXX=clang++
# clean up the previous build
$ python3 setup.py clean
# start the build
$ python3 ./tools/build_libtorch.py
libTorchSuccesRPi

With the build completed, there are several new and modified folders. The new build folder lives the ~/pytorch root, as seen in the screen dump below.
Also, the torch folder is modified if you look at the date (14/01/22). Again, in the torch folder are several modified directories like bin, include and lib.

libTorchRPiFolders

When using the C++ API, you only need the bin, include, lib and share folder in the pytorch/torch directory. If you like, all other folders and files can be removed, saving a lot of space. Don't forget the many hidden files. You end up with the same structure as shown in the tar.gz installation above.

StrippedTorchRPi

example-app.cpp.
Time to test the LibTorch installation with the famous example-app from the PyTorch C++ site.

						
						#include <iostream>
						#include <torch/torch.h>
						
						using namespace std;
						
						int main()
						{
						    torch::Tensor tensor = torch::rand({2, 3});
						    cout << tensor << endl;
						    cout << torch::hypot(torch::tensor(1.),torch::tensor(1.))<< endl;
						
						    auto t = torch::ones({3, 3}, torch::dtype(torch::kFloat32));
						    cout << "t:\n" << t << endl;
						    cout << "t.exp():\n" << t.exp() << endl;
						        
						    return 0;
						}
						
We're using the CMake file on the same PyTorch page. We only stripped the Windows MSVC branch as it is not needed in a Linux environment.
Save the file as CMakeLists.txt in the same folder as where you have placed your example-app.cpp.

						
						cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
						project(example-app)
						
						set(CMAKE_SYSTEM_NAME Linux)
						set(CMAKE_SYSTEM_PROCESSOR arm)
						set(CMAKE_C_COMPILER clang)
						set(CMAKE_CXX_COMPILER clang++)
						
						find_package(Torch REQUIRED)
						set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
						
						add_executable(example-app example-app.cpp)
						target_link_libraries(example-app "${TORCH_LIBRARIES}")
						set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
						    
						
Then create a folder called build and in this directory create the application with the following commands.
# make the build folder
$ mkdir build
$ cd build
$ cmake -D CMAKE_PREFIX_PATH=/home/pi/pytorch ..
$ cmake --build . --config Release

ExampleAppRPi

More information about the C++ API Library can be found on the PyTorch site. You can also find guides on how to transfer your TorchScript to C++ here.


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
Raspberry 64 OS
Raspberry 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content