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

Install OpenCV 4.1.2 and Caffe

on Ubuntu 18.04 for Python 3

Introduction.

This article helps you install OpenCV 4.1.2 and Caffe on Ubuntu 18.04 for Python 3.
As the support of Python 2 is coming to it end this year, many software packages aren't updated for Python 2 either. 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.1.1. 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.1.2 for Python3.
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. It is not necessary to describe it again here. Keep in mind, Caffe recommended a CUDA capability greater or equal to 3.0. Below that number, the software must be rewritten to reduce the number of threads and batch sizes. An adventure that is not worth the effort. Consult this Wikipedia page for the CUDA capabilities of your graphics card.
OpenCV Dependencies.
Let's start with installing OpenCV 4.1.2 on Ubuntu 18.04. 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. Install line for line the next packages after the common system refresh update/upgrade.
$ sudo apt-get update
$ sudo apt-get upgrade

$ 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 libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
$ sudo apt-get install libavresample-dev libvorbis-dev
$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install x264 libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install python3-testresources
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ cd /usr/include/linux
$ sudo ln -s -f ../libv4l1-videodev.h videodev.h
$ cd ~
$ sudo apt-get install libxine2-dev
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
$ sudo apt-get update
$ sudo apt-get install libjasper-dev
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev gfortran
$ sudo apt-get install libhdf5-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev

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.1.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.2.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.1.2 opencv
$ mv opencv_contrib-4.1.2 opencv_contrib

$ cd opencv
$ mkdir build
$ cd build

Build Make.

This is a important step. 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. Keep also a single space between the -D and the argument. Otherwise, CMake will misinterpreter the command. The two last dots are not a typo, it tells CMake where the CMakeLists.txt file is located; one directory up. The OPENCV_GENERATE_PKGCONFIG flag must be set because we are going to use the package config when we install Caffe. The CUDA_NVCC_FLAGS flag is only used when the old CUDA version 9 is installed on Ubuntu.
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_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 \
        -D WITH_CUDA=ON \
        -D ENABLE_FAST_MATH=ON \
        -D CUDA_FAST_MATH=ON \
        -D CUDA_NVCC_FLAGS=”-D_FORCE_INLINES –expt-relaxed-constexpr” \
        -D WITH_CUBLAS=ON ..
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_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. The command nproc gives you the number of cores availible. In my machine eight, hence make -j8. So take coffee and start building with the next command.
$ make -j8
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 libopencv-dev
$ sudo apt-get install libhdf5-serial-dev
$ sudo apt-get install 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
$ sudo apt-get install 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 a Caffe fork special adapted for OpenCV 4.1. The fork is identical to the original software. Only a few lines are altered in the Makefile and the Makefile.config.example. Also, are some cpp files adapted to the latest OpenCV declarations. The same applies to the WeiLiu fork. You can choose which you want to install, the adapted Caffe or the more extensive WeiLiu fork. Or both of course.
$ cd ~
$ wget -O caffe.zip https://github.com/Qengineering/caffe/archive/master.zip
$ unzip caffe.zip
$ cd caffe-master
The next step is some administration to get the building right. First, we need to know which Python3 version Ubuntu is currently using. Probably 3.6 if you using Ubuntu 18.04, but better be sure.
$ python3 --version
If your version differs from the Python 3.6 version, then a single line in the Makefile will have to be adjusted. Open the Makefile with gedit and change line 218 with the version number you just found. Save the modified file next and close gedit. For clarity's sake, it is not necessary to change Makefile if your Python version is 3.6.
# assuming you are still in the folder ~/caffe-master
$ gedit Makefile
Modify Makefile
The next step is to get the current status of OpenCV. If you have just installed OpenCV according to the instructions above, you can skip this step. Everything will be fine.
Otherwise, you must determine your OpenCV version for Python 3 and the existence of a package configuration file. This can be done by importing cv2 in python3 and the following line. See the screen dump below for more information.
$ python3
>>> import cv2
>>> cv.__version__
-----
>>> exit()

$ pkg-config --libs opencv4
Pkg config
As can be seen, Ubuntu runs version 4.1.1 of OpenCV in Python 3.
An opencv4 pkg-config is also available for OpenCV. If a pkg-config could not be found, the line after the command would be empty.

The last thing before all the preparations for the build has been made is the location of the hfd5 libraries. This folder can be found with the following command. Keep a note of the directory names.
$ dpkg -L libhdf5-dev
Hdf5 location

Makefile.config.

Before the build of Caffe can begin, the previously found parameters need to be set in the Makefile.config. The first step is coping the Makefile from the example file. Next is the editing of some lines in the file. Because nano highlight the syntax much better than gedit, we use nano this time. All the commands are shown below.
$ cd ~/caffe-master
$ cp Makefile.config.example Makefile.config
$ nano Makefile.config
Slide 1.
The first change is the USE_CUDNN line without comment. If you have installed CUDA and the cuDNN toolkit on your Ubuntu system, of course. If you only have CUDA but no cuDNN, you may beter install cuDNN before continuing. cuDNN speeds up your tensor calculations with about 36%. If you have no GPU acceleration and therefore no CUDA on board, leave USE_CUDNN commented, and remove the hash before CPU_ONLY. This will activate the CPU_ONLY directive telling Caffe not to use CUDA at all.

Leave USE_OPENCV commented if you have OpenCV on UBUNTU installed (keep the hash as the first character of the line). Without OpenCV Caffe still works perfectly. Only some image algorithms are replaced by Caffe's own code. Caffe is first and for all a deep learning framework, not some sophisticated image processing toolkit.

Slide 2.
If your OpenCV version is 3, change the 4 to a 3 on the line. If you have an even older version (1 or 2), consider upgrading to the 4.1.1 version. Caffe will eventually work with OpenCV 2.4.13, but you are on your own in this adventure. In case you stick to a version lower than 3, comment out both OPENCV_VERSION and USE_PKG_CONFIG lines by placing a hash at the front.

Only if you have an OpenCV version 4 and the previously performed pkg-config test shows the existence of the configuration package opencv4, leave the line USE_PKG_CONFIG uncommented. In all other case disable the line with a hash.

Slide 3.
Select your favorite Basic Linear Algebra Subprogram here. OpenBLAS is slightly faster, which is why we chose this library.

Slide 4.
As you can see, everything related to Python 2 has been disabled,  Python 3 is now activated. Check your Python 3 version and change it if necessary.
Please check also the existence of the given Numpy location.

Slide 5.
Check if the hfd5 folders are set according to the locations earlier found with the dpkg test. Change if needed.
Note also the line WITH_PYTHON_LAYER being active. Later on, the pytest depends on active layers. So don't disable this line.

Save and close the Makefile.config with Crtl+X, Y and Enter.

Make Caffe.

The next commands will make the Caffe installation complete. Again, by using multiple cores, the build goes faster (the -j8 switch). By the way, it's a good practice to clean up any previous build before you start a new one.
# assuming you are still in the folder ~/caffe-master
$ make clean
$ make all -j8
$ make test -j8
$ make runtest -j8
Hopefully, you will see the screen below after make runtest, indicating that everything went well.
RunTest
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. 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 are still in the folder ~/caffe-master
$ make pycaffe -j8
$ make pytest -j8
Pytest
Time for the last instructions and your Caffe installation is ready to run on Ubuntu 18.04 with OpenCV 4.1.1 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}:/my/path_to/caffe-master/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 "success" screenshot.
$ cd ~
$ sudo nano ~/.bashrc
# at the end of the file add the next line
export PYTHONPATH="${PYTHONPATH}:/my/path_to/caffe-master/python"
# save and close with
Ctrl+X, Y, Enter
Succes
Raspberry and alt
Info
Raspberry Pi 4
Back to content