Install MXNet deep learning framework on a Raspberry Pi 4.
This page guides you through the installation of Apaches MXNet framework on a Raspberry Pi 4. With the installation of MXNet, the high-level Gluon toolkit will be installed at the same time. 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 about the MXNet framework, see https://mxnet.apache.org/. Because the installation on a 32-bits operating system is identical to the one on a 64-bits OS, there is no need for sperate instructions.
First of all, OpenCV is mandatory. Best, you install it according to our guide.
Besides OpenCV, MXNet needs some other software packages. Install them as follows.
# get a fresh start (if using the 64-bit OS, it is still under development!)
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies (if not already onboard)
$ sudo apt-get install git unzip cmake build-essential
$ sudo apt-get install liblapack* libopenblas* libblas*
# this will take a while
$ pip3 install scipy
# we need numpy >= v1.17.0
$ pip3 install --upgrade numpy
$ pip3 install Cython
Swap space (32-bit).
Before we can start the actual build, enlarge the memory swap space on a 32-bit OS. The 64-bit version has already enough swap space if you installed it according our guide.
$ sudo nano /etc/dphys-swapfile
This command loads the system file dphys-swapfile in Nano, a very lightweight text editor. Move the cursor with the arrow keys to the CONF_SWAPSIZE line where the new value 2048 can be entered. Next, close the session with the <Ctrl+X> key combination. With <Y> and <Enter> changes are being saved in the same file.
Two additional commands are required before the new enlarge swap space is active.
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
Install Clang on Raspberry Pi 4.
The build of MXNet is going to be memory hungry. Even with 8 GByte of RAM, you can get into troubles. Especially with regular the 32-bit Raspbian operating system as you can see below. The main reason for this error is the very long compilation chain of templated source code.
That's why we use the Clang compiler instead of the default GNU version. Many sites are comparing the two, and each has a different favourite. Bottom line, they do not differ much from each other. There are some small differences at the detail level. However, the Clang compiler uses far less memory during compilation compared to the GNU; it can be more than a factor five less. Reason to use the Clang compiler for the build of MXNet temporarily. After the export commands, cmake uses the Clang compiler. You lost these preferences as soon as you close the terminal window. Any new window will use the standard GNU compiler again.
$ sudo apt-get install clang
# set default compilers to clang (temporarily)
$ export CC=clang
$ export CXX=clang++
Once the Clang compiler is installed and set active, you can download the MXNet package.
$ git clone https://github.com/apache/incubator-mxnet.git --recursive
$ mv incubator-mxnet mxnet
The next step is generating the script for CMake. Please check if the Clang compilers are selected as shown below.
$ cd mxnet
$ mkdir build
$ cd build
$ cmake -DUSE_SSE=OFF \
The next step is actual compilation of the MXNet framework and the Gluon interface. You can run make with one (-j1) or two jobs (-j2). More will certainly crash the compilation. Please note also the suffix USE_CPP_PACKAGE=1 which will generate the C++ API. The whole build will take about 3½ to 6 hours, depending on the number of jobs.
$ make -j2 USE_CPP_PACKAGE=1
$ sudo make install
The last step is building the Python wheel and subsequent installation. It's done with the commands below.
# generate the wheel
$ cd ~/mxnet
$ make cython PYTHON=python3
# install the wheel
$ cd ~/mxnet/python
$ pip install --upgrade pip
$ pip3 install -e .
When the installation was successful, you may check your MXNet version.
And on your Raspberry Pi you will find the next folders.