Install Caffe on Ubuntu 20.04 with OpenCV 4.4 - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
Caffe on Ubuntu 20.04

Install OpenCV 4.4.0 and Caffe

on Ubuntu 20.04 for Python 3
Ubuntu 18.04
Caffe on Raspberry Pi

Introduction.

This article helps you install OpenCV 4.4.0 and Caffe on Ubuntu 20.04 for Python 3.
As the support of Python 2 ended, many software packages aren't updated for Python 3 yet. And a framework such as Caffe is highly dependent on these packages. For example, the necessary library of scikit image is now no longer available for a Python 2 Caffe build.
Another issue is OpenCV. When Caffe was developed in 2013, version 2.4.13 of OpenCV was used everywhere. Now we have OpenCV 4.4.0. with a whole lot of differences. Not only the used C ++ compiler version, but also the Linux installation folders. Time to install Caffe and OpenCV 4.4.0 for Python3.
OpenCV Dependencies.
OpenCV uses intensively third-party software libraries. These must be installed on Ubuntu before OpenCV can be set up. When they are already installed, no harm is done. This way, you are always sure you have the latest version.
# a fresh start in the morning
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libavresample-dev libvorbis-dev libxine2-dev
$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev libeigen3-dev gfortran
$ sudo apt-get install libhdf5-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
# a symlink to videodev.h
$ cd /usr/include/linux
$ sudo ln -s -f ../libv4l1-videodev.h videodev.h
$ cd ~

Download OpenCV.

When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. Check before downloading the latest version at https://opencv.org/releases/. If necessary, change the names of the zip files according to the latest version. After downloading, you can unzip the files.
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.4.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.4.0.zip

$ unzip opencv.zip
$ unzip opencv_contrib.zip
The next step is some administration. First, rename your directories with more convenient names like opencv and opencv_contrib. This makes live later on easier. Next, make a directory where all the build files are located.
$ mv opencv-4.4.0 opencv
$ mv opencv_contrib-4.4.0 opencv_contrib

$ cd opencv
$ mkdir build
$ cd build

CUDA.

As known, the CUDA toolkit significantly improve the performance of deep learning software. If you have an NVIDIA graphics card, you probably already have CUDA installed in the past. If not, NVIDIA has an excellent website that guides you through the installation process. You need to determine your architecture number of the graphics card. Use this command to get all info about your card.
$
nvidia-smi
With the given information, it is easy to find the architecture version of your GPU card on this page https://developer.nvidia.com/cuda-gpus. Keep in mind, Caffe recommended a CUDA capability greater or equal to 3.0. Below that figure, the software must be rewritten to reduce the number of threads and batch sizes. An adventure that is not worth the effort. We use the found architecture number when building OpenCV and later with the Caffe build.

Build Make.

Here you tell CMake what, where and how to make OpenCV on your Ubuntu system. There are many flags involved. The most you will recognize. We save memory by excluding any (Python) examples or tests.
Note, there are only bare spaces before the -D flags, not tabs. The two last dots are not a typo it tells CMake where the CMakeLists.txt file is located; one directory up. Set the OPENCV_GENERATE_PKGCONFIG flag because we are going to use the package config when we install Caffe.

Two different sets of cmake options are provided, one with CUDA support and one without. If you enable CUDA, check your architecture number earlier found. We have CUDA_ARCH_BIN=7.0, but modify the number if it is different from yours. Also check the path to the cuDNN directories, if you enabled the WITH_CUDNN=ON as we did. Change if necessary.

with CUDA
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D BUILD_TIFF=ON \
        -D WITH_FFMPEG=ON \
        -D WITH_GSTREAMER=ON \
        -D WITH_TBB=ON \
        -D BUILD_TBB=ON \
        -D WITH_EIGEN=ON \
        -D WITH_V4L=ON \
        -D WITH_LIBV4L=ON \
        -D WITH_VTK=OFF \
        -D WITH_QT=OFF \
        -D WITH_OPENGL=ON \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_PYTHON_EXAMPLES=OFF \
        -D BUILD_NEW_PYTHON_SUPPORT=ON \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D BUILD_TESTS=OFF \
        -D OPENCV_DNN_CUDA=ON \
        -D ENABLE_FAST_MATH=ON \
        -D CUDA_FAST_MATH=ON \
        -D CUDA_ARCH_BIN=7.0 \
        -D WITH_CUBLAS=ON \
        -D WITH_CUDNN=ON \
        -D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.7.6.5 \
        -D CUDNN_INCLUDE_DIR=/usr/local/cuda/include  \ 
        -D BUILD_EXAMPLES=OFF ..

without CUDA
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D BUILD_TIFF=ON \
        -D WITH_FFMPEG=ON \
        -D WITH_GSTREAMER=ON \
        -D WITH_TBB=ON \
        -D BUILD_TBB=ON \
        -D WITH_EIGEN=OFF \
        -D WITH_V4L=ON \
        -D WITH_LIBV4L=ON \
        -D WITH_VTK=OFF \
        -D WITH_QT=OFF \
        -D WITH_OPENGL=ON \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_PYTHON_EXAMPLES=OFF \
        -D BUILD_NEW_PYTHON_SUPPORT=ON \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D BUILD_TESTS=OFF \
        -D BUILD_EXAMPLES=OFF ..
If everything went well, CMake comes with a report that looks something like the screenshot below.
Report after cmake

Make OpenCV.

Now everything is ready for the build. This takes some time. You can speed things up by using all your cores in your machine working simultaneously. So take coffee and start building with the next command.
$ make -j$(nproc)
Hopefully, your build was successful. Now to complete, install all the generated packages to the database of your system with the next commands.
$ sudo make install
$ sudo ldconfig
$ sudo apt-get update
Now it is time to check your installation in Python 3. It all speaks for itself.
Checking

Caffe dependencies.

With OpenCV on place, it is time to start installing Caffe. Just like OpenCV, Caffe is dependent on other libraries. Some of these libraries were downloaded during the installation of OpenCV, some are new. For the sake of completeness, all required libraries are listed below.
$ 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

Download Caffe.

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

Build 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
# If you use CUDA, copy the cp38_x86_64-linux-gnu_CUDA_example
$ cp Makefile.config.cp38_x86_64-linux-gnu_CUDA_example Makefile.config
# Or without CUDA, copy the cp38_x86_64-linux-gnu_example
$ cp Makefile.config.cp38_x86_64-linux-gnu_example Makefile.config

# After you select one of the above configurations, the build can start
$ make clean
$ make all -j$(nproc)
$ make test -j$(nproc)
$ make runtest -j$(nproc)
Hopefully, you will see the screen below after make runtest, indicating that everything went well.

Run test ok

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
Run test Python succes

Time for the last instructions and your Caffe installation is ready to run on Ubuntu 20.04 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
export PYTHONPATH="${PYTHONPATH}:$HOME/caffe/python"
# save and close with
Ctrl+X, Y, Enter
.bashrc

Caffe ssd version

If you run into any problems building Caffe on Ubuntu 20.04, please refer to the configuration page.
Caffe configuration
Raspberry and alt
Install 32 OS
Raspberry Pi 4
Install 64 OS
Back to content