Install Caffe on a Raspberry Pi 4.
This page will guide you through the installation of Caffe 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 about the Caffe, see: https://caffe.berkeleyvision.org/. It doesn't matter which operating system your Raspberry Pi has. During installation, you can select your operating system from a range of configuration templates.
The Caffe framework has a few dependencies to other libraries. We assume you have already installed OpenCV on your Raspberry Pi according to our guides. If not, better to do it first.
$ sudo apt-get install cmake git unzip
$ sudo apt-get install libprotobuf-dev libleveldb-dev liblmdb-dev
$ sudo apt-get install libsnappy-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev
$ sudo apt-get install the python3-dev python3-skimage
$ sudo pip3 install pydot
$ sudo apt-get install graphviz
When all required libraries are installed, Caffe can be downloaded.
On our GitHub page, you will find the famous Caffe clone from WeiLui89. Unlike the original Caffe, this version supports SSD operations. WeiLui was one of the original developers of the Single Shot MultiBox Detector. For detailed information, see his article.
We special adapted his fork for OpenCV 4.4 and Python3. Also, we added some additional Makefile scripts for Raspberry Pi and Ubuntu.
$ cd ~
$ wget -O caffe.zip https://github.com/Qengineering/caffe/archive/ssd.zip
$ unzip caffe.zip
$ mv caffe-ssd caffe
We have created several configuration files. In most cases, the configuration works immediately, without errors. Select the appropriate one and start building Caffe. If the build fails, see the Caffe configuration page.
$ cd ~/caffe
# Choose your operating system here
# Raspberry Pi with 32-bit OS (Raspbian)
$ cp Makefile.config.cp37_arm-linux-gnueabihf_example Makefile.config
# Raspberry Pi with 64-bit OS
$ cp Makefile.config.cp37_aarch64-linux-gnu_example Makefile.config
# Raspberry Pi with Ubuntu 18.04
$ cp Makefile.config.cp36_aarch64-linux-gnu_example Makefile.config
# Raspberry Pi with Ubuntu 20.04
$ cp Makefile.config.cp38_aarch64-linux-gnu_example Makefile.config
# After you select one of the above configurations, the build can start
$ make clean
$ make all -j4
$ make test -j4
$ make runtest -j4
Hopefully, you will see the screen below after make runtest, indicating that everything went well.
There is some additional software you can make. First, there is pycaffe. You will need this interface if you like to run Caffe in Python 3. Otherwise, Caffe will only run from the command line or from within a C++ program. See the original Caffe documentation for more info.
You can also run Caffe inside MATLAB. This time you need to make matcaffe. However, some lines in the Makefile.config needs to be set before you can successfully build matcaffe. Again, the original documentation is your friend here. After the pytest, your screen looks like something like the one below.
# Assuming you're still in ~/caffe
$ make pycaffe
$ make pytest
Time for the last instructions and your Caffe installation is ready to run on Raspberry Pi 4 with OpenCV 4.4.0 and Python 3.
You need to add Caffe to your PYTHONPATH. This can be done in ~/.bashrc or ~/.profile. We choose bashrc. Open the file with and add the line export PYTHONPATH="$PYTHONPATH:$HOME/caffe/python" at the very end of the file. Save and close with Crtl+X, Y and Enter as usual.
In the end, you can import Caffe now from any location as can be seen in the screenshot.
$ cd ~
$ sudo nano ~/.bashrc
# at the end of the file add the next line
# save and close with
Ctrl+X, Y, Enter
If you run into any problems building Caffe on Raspberry Pi 4, please refer to the configuration page.