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

Install Paddle 2.0.0 deep learning framework on a Raspberry Pi 4.

Paddle Lite

Introduction.

The Chinese counterpart of Google is Baidu. Just like Googles TensorFlow, Baidu has the open-source deep learning software library, called PaddlePaddle. An acronym for PArallel Distributed Deep LEarning. The framework is impressive; support over 100 different models and more than 200 pre-trained models (often with code) are found in their zoo.
Paddle (Lite) supports a lot of different hardware like CUDA, TensorRT, OpenCL, Mali GPU, Huawei NPU, Rockchip NPU, Baidu XPU, MediaTek APU and FPGA.
The Chinese language may be a barrier for some people. For those, Google Translate is your friend. Most documents are easy to read once Google has translated them.

Just like TensorFlow, Paddle consumes a lot of resources. Often more than found on a little device like a Raspberry Pi. So don't expect to train models. Also, you cannot run all pre-trained models due to the amount of memory required.
The most likely reason for installing Paddle on a Raspberry Pi is the conversion to Paddle Lite models, which are better equipped for the use in small environments.
Due to incompatibility between the CPU (armv8) and the compiler (arm-linux-gnueabihf), Paddle cannot be installed on a Raspberry Pi 4 with a 32-bit operating system. The generated library uses registers (VFPV3) missing in the armv8. Replacing a compiler can be a real nightmare. Best to take a new SD card and install the latest Raspberry 64-bit OS.

Version check.
Please check your operating system before installing Paddle on your Raspberry Pi 4. Run the command uname -a and verify your version with the screen dump below.

Version_32_64

In case of a 64-bit operating system, please check also your C++ compiler with the command gcc -v. It must also be an aarch64-linux-gnu version. In case of a different gcc version, reinstall the whole operating system with the latest version. The guide is found here: Install 64 bit OS on Raspberry Pi 4. You must have a 64-bit C ++ compiler as we are going to build the Paddle libraries.

VersionCheck_64

OpenCV dependency.

First of all, you need to have OpenCV installed on your Raspberry Pi. Paddle needs OpenCV for image rendering. It will install opencv-python <= 4.2.0.32 from its wheel. Besides the known discouragement of an OpenCV pip installation, this version is not available in any of the pypi and piwheels databases, thereby falling back to version 3.4.11.45, something you don't want, due to its avalanche of outdated dependencies.

Just keep things simple. Check which OpenCV version Python 3 imports. If needed, install a newer version according to our guide.

OpenCV_Version_Paddle

We even have removed the OpenCV requirement from our wheel, just to prevent installing double versions on your Raspberry Pi. Remember, space on the SD card is also precious.

Paddle pip3 installation.

The pip3 is by far the easiest and most reliable installation of Paddle on your Raspberry Pi 4. If you plan to use Paddle with Python3 of course. The official Paddle installation does not support AARCH64 wheels. That's why we've put a wheel on our GitHub page that makes installation easy. Follow the instruction below. By the way, it may take a while to build the scipy library if needed.
# a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install dependencies
$ sudo apt-get install cmake wget
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install liblapack-dev patchelf gfortran
$ sudo -H pip3 install Cython
$ sudo -H pip3 install -U setuptools
$ pip3 install six requests wheel pyyaml
# upgrade version 3.0.0 -> 3.13.0
$ pip3 install -U protobuf
# download the wheel
$ wget https://github.com/Qengineering/Paddle-Raspberry-Pi/raw/main/paddlepaddle-2.0.0-cp37-cp37m-linux_aarch64.whl
# install Paddle
$ sudo -H pip3 install paddlepaddle-2.0.0-cp37-cp37m-linux_aarch64.whl
# clean up
$ rm paddlepaddle-2.0.0-cp37-cp37m-linux_aarch64.whl
If everything went well, you will get the following screen dump.

pip3 installation completed

It may happen that the installation is closed with a warning about the PATH. If so, add the location (~ / .local / bin) at the end of the .bashrc file with $ sudo nano .bashrc as shown below.

Add path to bashrc

You can check the Paddle installation on your Raspberry Pi as follows.

Paddle pip installation succes

Paddle installation from scratch.

You can build the Paddle deep learning framework from scratch, if you don't want to use the python wheel or if you need the C++ API inference library. The whole procedure takes about 3 hours and will use approximately 20 GByte of your disk. Obviously, a 64GB SD card will be required as a minimum and, again, you can only compile Paddle on a Raspberry Pi 4 with a 64-bit operating system.

The installation of Paddle requires at least 6 GByte of RAM. If you have a Raspberry Pi 4 with 8 GByte RAM, you in the clear. Otherwise, you need some sort of swapping. With 4 GByte of RAM, you can increase the upper limit zram as discussed here.
Best served are Raspberry Pi's with 2 or 1 GByte with temporarily swapping to the flashcard. The software will be removed at the end of the procedure in order not to use the SD card unnecessarily. To exceed the 2048 MByte limit normally found in the /etc/dphys-swapfile, you must first increase this maximum in the /sbin/dphys-swapfile. Everything is demonstrated once more in the slideshow below.
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# enlarge the max swapsize to 4096
$ sudo nano /sbin/dphys-swapfile
# give the required size of 4096
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot
Check if you have 6 GByte of memory with $ free -m.

6GB memory

As usual, the next step is to install the dependencies. The entire list is shown below.
# a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install dependencies
$ sudo apt-get install cmake wget curl unrar swig
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install liblapack-dev patchelf gfortran
$ sudo -H pip3 install Cython
$ sudo -H pip3 install -U setuptools
$ pip3 install six requests wheel pyyaml
$ pip3 install numpy
# upgrade version 3.0.0 -> 3.13.0
$ pip3 install -U protobuf
$ pip3 install matplotlib
$ pip3 install nltk
# scipy may take a while to compile
$ pip3 install scipy
With all the dependencies in place, you can now download the Paddle package. On our GitHub page, we posted a package special adapted for the Raspberry Pi 4 (aarch64). You can explore the differences with the original software with the compare button.

GitHub Compare

As can be seen in the overview, there are three modifications made. First, the already mentioned, inclusion of OpenCV in the requirements is been removed. Second, to let PaddleHub work, the version number should not be 0.0.0 as now happens. That's why version 2.0.0 is hardcoded given for now. In the future, when this version is labelled stable, it shall not be necessary any more because the version routine will generate a workable number. Last, OpenBLAS needs extra directives not given yet by the cmake file (cmake/external/openblas.cmake). We have inserted then.
Feel free to fork the latest Paddle version from their GitHub page and modify the above files in your own fork to get the very latest software.
# get Paddle
$ git clone https://github.com/Qengineering/Paddle.git
$ cd Paddle
# get a working directory
$ mkdir build
$ cd build
# run cmake
$ cmake -D PY_VERSION=3.7 \
        -D WITH_PYTHON=ON \
        -D WITH_CONTRIB=OFF \
        -D WITH_MKL=OFF \
        -D WITH_MKLDNN=OFF \
        -D WITH_XBYAK=OFF \
        -D WITH_TESTING=OFF \
        -D WITH_ARM=ON \
        -D ON_INFER=ON \
        -D CMAKE_BUILD_TYPE=Release ..
As you can see in the parameter list above, both are generated, a Python wheel and an inference library.

PaddleCmakeRdyRPi

Before we can start the build, the number of files, open at the same time, need to be increased from the default 1024 to 2048. It is done with the ulimit command. Once set make is called with just one core ($ make -j1). Even with two, you will end up crashing due to the smothering memory swapping.
# increase the number of open files
$ ulimit -n 2048
# run with one core
$ make -j1
PaddlePythonRdyRPi

Find your Python installation wheel here. It's identical to the one we have on our GitHub page. Given, of course, you haven't forked a later version of Paddle.

Python install folder

You can now install the Python Paddle version with the wheel as described above.
If you had to install the dphys-swapfile service, it's now time to delete it. This way you will extend the life of your SD card.
# install Paddle Python
$ sudo -H pip3 install python/dist/paddlepaddle-2.0.0-cp37-cp37m-linux_aarch64.whl
# remove the dphys-swapfile
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile

Paddle C++ API library.

The installation of the Paddle C ++ API library follows the exact same steps as the installation of the Python version. If you have set the parameter -D ON_INFER=ON, the libraries are in the following location.

API_location_Rpi


PaddleHub.

PaddleHub is a collection of script files that load and deploy the pre-trained Paddle models on your Raspberry Pi with one click, as we'll see.
PaddleHub uses the Paddle framework so you must have this installed first. Obviously you must have a working internet connection to access the Paddle server.

The installation can be done with just one pip. However, you will be dragged into an OpenCV version 3 with its many incompatible dependencies as described earlier in this guide. Better to install all the dependencies and PaddleHub separately, one by one.
We assume you have OpenCV already working for your Paddle framework, so there is no need to install it again here.

It is a good idea to check the list of dependencies to make sure it is still up to date with the latest version of PaddleHub. The requirements.txt is located at the root of the GitHub page here.
# install all dependencies
$ pip3 install pre-commit
$ pip3 install protobuf
$ pip3 install yapf
$ pip3 install six
$ pip3 install flask
$ pip3 install flake8
$ pip3 install visualdl
$ pip3 install cma
$ pip3 install sentencepiece
$ pip3 install colorlog
$ pip3 install tqdm
$ pip3 install nltk
$ pip3 install pandas
$ pip3 install gunicorn
# install PaddleHub without dependencies
$ pip3 install -U paddlehub --no-deps
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
Back to content