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

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

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

You can test your installation with the following command.

TestPaddlePaddle


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 (Januari 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