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 deep learning framework on a Raspberry Pi 4.

Paddle Lite
Last updated: June 5, 2023

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.

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 32 GB SD-card, and enjoy!
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 getconf LONG_BIT and verify your version.

Version check

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.
Buster 64-bit OS
# 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
$ pip3 install six requests wheel pyyaml
# download the wheel
$ wget https://github.com/Qengineering/Paddle-Raspberry-Pi/raw/main/paddlepaddle-2.4.2-cp39-cp39-linux_aarch64.whl
# install Paddle
$ sudo -H pip3 install paddlepaddle-2.4.2-cp39-cp39-linux_aarch64.whl
# clean up
$ rm paddlepaddle-2.4.2-cp39-cp39-linux_aarch64.whl
If everything went well, you will get the following screen dump.

Paddle_242_installation_completed

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

Paddle_242_installation_check

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 5 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

If you boot from an USB device and you swap size exceeds the 2048 MByte, it often happens that the dphys-swapfile is not initialized at boot time due to boot priorities. You have to manual re-activate dphys-swapfile by the following commands after each new boot. Since you will probably only be using dphys-swapfile during this installation, this is not problematic. Better than installing single-line commands in different initialization files for just one session.
# after each boot up
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
Zram+dphys-swap

Before we can start building the Paddle framework, we need to install the Clang compiler. The GNU compiler has problems compiling the C++ code.
For those interested, a structure with one semi-dynamic and one pure dynamic member (__u32 resereved[8] and _u32 link_mode_mask[] in linux/ethtool.h) can not be allocated with malloc in GNU. It results in an error flexible array member ‘ethtool_link_settings::link_mode_masks’ not at end of ‘struct gloo::getInterfaceSpeedGLinkSettings(int, ifreq*)::’.
The Clang compiler does not have this problem because it treats the semi-dynamic member as a static member.
Together with Clang, we will use Ninja instead of Make. Ninja supports more command line arguments than Make.
The last dependency needed is patchelf. Let's install it, if it hasn't already been done in the past.
# install Clang and a two dependencies
$ sudo apt-get install clang ninja-build patchelf
Now that the tools are in place, it's time to download Paddle. We use version 2.4.2. No doubt there will be newer releases over time. You can check the GitHub repo for newer versions first.
# download Paddle
$ git clone -b v2.4.2 --depth=1 https://github.com/PaddlePaddle/Paddle.git
$ cd Paddle/python
# install dependencies
$ pip3 install -r requirements.txt
Once downloaded, and all the additional software is in place, we must adapt two files before Paddle is ready to compile.
The first alteration is in the file Paddle/cmake/flags.cmake. Here, we disable the -Werror flag. The -Werror flag converts a warning to an error. The commendable aim will not hold in practice. You need exactly the same compiler (version) and environment as the developers. Otherwise, you always get warnings. Because warnings are not errors, these are not eminent to stop the compilation.

PaddleWerror

The second modification is in the file Paddle/cmake/version.cmake. Here, we must replace the "0.0.0" release numbers with the version you just downloaded. If not, you will end up with a 0.0.0 release, a number not recognized by other software using the Paddle framework.

PaddleVersion

At last, we can begin building Paddle. It's more or less the usual procedure of making a build directory en run cmake with several options.
However, before the cmake command, be sure you have selected the clang compilers in your terminal.
# select clang
$ export CC=clang
$ export CXX=clang++
# get a working directory
$ mkdir build
$ cd build
# run cmake
$ cmake -D PY_VERSION=3.9 \
        -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 \
        -G Ninja ..
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 8192. It is done with the ulimit command. Once set, ninja is called.
# increase the number of open files
$ ulimit -n 8192
# run with ninja
$ ninja
Towards the end, your compilation may stall.
PaddleStalls

Most likely, the cause can be found in a not completed command in the make file. Please check if the nodesX.cc are filled, in the folder Paddle/paddle/fluid/eager/api/generated/fluid_generated/nodes
If not, just replace the empty files with the corresponding filled tmp ones. nodes.tmp.h replaces nodes.h, nodes1.tmp.cc -> nodes1.cc etc.

PaddleStallsFolders

Now that the files are populated, restart the compilation by issuing the ninja command again.
Be patient and you will now end up with a working wheel.

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
Pip3 install paddle RPi

You can test your installation with the following command.

PipInstallSucces


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.

PaddleHub supports a wide variety of deep learning models. All come with a little Python script, involving the Paddle framework. The code naturally depends on other libraries as well. Some libraries are used all the time, think of protobuf. Others are only called by a specific deep learning model, like easydict, for instance. The full list is given below, as far as we could get it from the code. It's up to you which library you want to install. When a library is missing in the future, the PaddleHub script will let you know. You can then always install it afterwards. By the way, given the fast rate of updates, the list will be soon outdated. It forces you installing the missing dependencies anyway when running the scripts.
# the dependencies
$ sudo apt-get install python3-matplotlib
$ pip3 install --user --upgrade pip
$ pip3 install packaging
$ 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 filelock
$ pip3 install easydict
$ pip3 install gitpython
$ pip3 install pyyaml
$ pip3 install pyzmq
$ pip3 install colorama
$ pip3 install colorlog
$ pip3 install tqdm
$ pip3 install nltk
$ pip3 install pandas
$ pip3 install gunicorn
$ pip3 install paddlenlp
During the installation of various software packages, pip3 complains about missing OpenCV. Sometimes it is even referred to as an error. Don't be tempted to install OpenCV with pip! As long as you have installed OpenCV according to our guide, no problems will arise. Python3 will import OpenCV. It is only the missing entry in the pip3 database that generates these kinds of warnings. And remember, two different versions of OpenCV on one machine is a recipe for problems!
With most of the dependencies installed, now is the time to install the latest version of PaddleHub. At the time of writing (January 2021) the standard pip3 installation will get you a version 1.8.3. It will not work flawlessly with the newly installed PaddlePaddle version 2.0.0. You need at least version 2.0.0 for your PaddleHub. The best way is to provide a nonexistent number and see what the latest version is. Then you can install this version. The screen dump below shows how it works.

Pip3_Latest_version

If this method doesn't work, you can always find the latest version number on the GitHub PaddleHub page.

none
# install PaddleHub version 2.0.0 as being the latest version.
# without dependencies (we just did this manually)
$ pip3 install -U paddlehub==2.0.0 --no-deps
Test PaddleHub.
Let's test PaddleHub. You can find all the models on this site, or at GitHub. Google translate is your friend here.

PaddleHubSite

We are going to test the face-mask detection. The first step is to download the model, as indicated on the website. Next step is running the model. That's all, you don't have to write any code at all. The screen dumps say more than a thousand words.

InstallHubModel

RunHubModel

All your models are stored in the hide folder /home/pi/.paddlehub/modules as you can see below.

HubFolder



If you had to reinstall dphys-swapfile, it's time to uninstall it again. This way you will extend the life of your SD card.
# remove the dphys-swapfile (if installed)
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile

Conversion to Paddle Lite.

The last step in our PaddlePaddle tour is to convert 'regular' deep learning models (so-called fluid models) to Paddle Lite models, used in embedded systems such as a Raspberry Pi 4, or Jetson Nano. Since the conversion is done by the Paddle Lite software, this topic is covered on that page. However, keep in mind that you have a fully functional PaddlePaddle and PaddleHub on your computer to generate the necessary files.

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