Install ARMnn deep learning framework on a Raspberry Pi 4.
This page guides you through the installation of the ARMnn framework on a Raspberry Pi 4. The given C++ code examples are written in the Code::Blocks IDE for the Raspberry Pi 4. We only guide you through the basics, so in the end, you can build your application. For more information on the ARMnn library see: https://developer.arm.com/solutions/machine-learning-on-arm/developer-material.
ARMnn is the largest framework we've come across. More than 3.1 Gbyte of disk space is not small. The main reason for this memory usage is the dependencies of some large libraries, such as TensorFlow and Boost. ARM recommends cross-compilation. A fast, powerful computer generates the code, which is then transferred to the Raspberry Pi. However, cross-code development can be complicated when it comes to debugging. Constantly exchanging files, to see if your code is working properly, is not user-friendly.
Let begin with the well known fresh start and then download the required software.
# fresh start in the morning
$ sudo apt-get update
$ sudo apt-get upgrade
# some tools
$ sudo apt-get install scons git wget
$ sudo apt-get install autoconf
$ sudo apt-get install libtool
# make the directory
$ mkdir armnn-pi
$ cd armnn-pi
$ export BASEDIR=`pwd`
# get the ARM libraries
$ git clone https://github.com/Arm-software/ComputeLibrary.git
$ git clone https://github.com/Arm-software/armnn
# and the dependencies
$ wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2
$ git clone -b v3.5.0 https://github.com/google/protobuf.git
$ git clone https://github.com/google/flatbuffers.git
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ git checkout 590d6eef7e91a6a7392c8ffffb7b58f2e0c8bc6b
One word about the line export BASEDIR=`pwd`. Here you define a variable BASEDIR with the string of the print working directory, your current folder. The variable is lost when the operating system is closed. So, if you continue working on the installation after a restart, first set this variable by cd arm-pi && export BASEDIR = `pwd`.
The first step to installing ARMnn on the Raspberry Pi is to build ARM's Compute Library. The ARMnn framework uses this library to optimize these deep learning routines on ARM CPUs and GPUs. It takes about 33 minutes on a Raspberry Pi 4 to build the entire library.
$ cd $BASEDIR/ComputeLibrary
$ scons -j 4 extra_cxx_flags="-fPIC" \
Werror=0 debug=0 asserts=0 neon=1 \
opencl=0 os=linux arch=armv7a examples=1
The next library is Boost. Most people have Boost already on their system. Often loaded with sudo apt-get install libboost-dev. However, the ARMnn framework needs the static build C++ library with all its headers. In this case, we need to build the Boost library from scratch. First, unpack the library and get some administration done.
# unpack the tar (± 3 min)
$ cd $BASEDIR
$ tar xf boost_1_64_0.tar.bz2
# run the scripts
$ cd $BASEDIR/boost_1_64_0/tools/build
$ ./b2 install --prefix=$BASEDIR/boost.build
# incorporate the bin dir into PATH
$ export PATH=$BASEDIR/boost.build/bin:$PATH
Note the line export PATH=$ BASEDIR/boost.build/bin: $PATH. Here you include the directory with executables, like b2, in the PATH system variable. This link will be lost when the operating system is closed. So keep working on installing of boost to completion.
Before the actual build, we need to set the correct compiler in the b2 project file. This is done after copying the project-config.jam file with nano. Please follow the instructions.
# copy the user-config to project-config
$ cp $BASEDIR/boost_1_64_0/tools/build/example/user-config.jam \
# change the directory
$ cd $BASEDIR/boost_1_64_0
# start editor
$ nano project-config.jam
# add the line using gcc : arm : arm-linux-gnueabihf-g++ ; in the project-config
# save with <Ctrl+X>, <Y>, <ENTER>
With the proper compiler given, the b2 tool can build the Boost library. Some warnings can be generated, but they can all be ignored.
$ b2 --build-dir=$BASEDIR/boost_1_64_0/build -j 4 \
--with-program_options install \
After Boost, Google's protobuffer library is installed. This library must also be built statically, with all its headers in place. So, even earlier installed with a sudo apt-get install protobuf-compiler, they have to be compiled again before the ARMnn framework can use them.This action takes 15 minutes.
$ cd $BASEDIR/protobuf
$ git submodule update --init --recursive
$ ./configure --prefix=$BASEDIR/protobuf-host
$ make -j4
$ make install
$ make clean
Two more libraries to go. The first one is TensorFlow, the second are the FlatBuffers, used by TensorFlow Lite. Compile both with the following commands. It takes about 5 minutes to complete.
$ cd $BASEDIR/tensorflow
$ ../armnn/scripts/generate_tensorflow_protobuf.sh \
$ cd $BASEDIR/flatbuffers
$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
$ make -j4
With all the dependencies in place, we can finally build the ARMnn framework. The procedure is identical to the one used to install OpenCV. First, you create a build directory, next you run a cmake with the options followed by a make command. The compilation takes 55 minutes on a Raspberry Pi 4.
# create a build directory
$ cd $BASEDIR/armnn
$ mkdir build
$ cd build
# run cmake
$ cmake -DCMAKE_LINKER=/usr/bin/arm-linux-gnueabihf-ld \
# compile the cmake script
$ make -j4
After installation, you can check the ARMnn library by running the UnitTests program ($ ./UnitTests). It is currently conducting 2672 tests. In our case, 31 fealures were detected. We have not investigated them further. Most failures are related to temporarily unavailable resources.