Build OpenCV 2.4.9 & Caffe with CUDA 9.0 on Ubuntu 16.04

There are quite a lot of things you could feel annoying about the research. One of the most annoying I found recently, is trying to spending a couple of days only to build an environment for experiment while have not started any experiment yet. Here I record my procedure of building Caffe and OpenCV, with CUDA on my Ubuntu system.

# An Important Thing

The most important thing that I learned in my path of building Caffe is — Never think of restarting from scratch is a waste of time. I finally succeeded by re-installing the OS (Ubuntu) and then install all of the things one by one.

# Prerequisite

  • Ubuntu 16.04
  • Python 2.7
  • Cuda 9.0 (9.1 should also work), ensure your CUDA is compatible with your GPU
  • cudnn 7.1 (7.0 should also work)

# Installing OpenCV

Firstly, install some depedencies as following: (you can put them into a shell file and then execute it.)

Get opencv-2.4.9 using wget:

[Important] Configure the file for cuda-9.0. There are some issues for cuda≥9 to use OpenCV 2.X version so we need to configure it. According to [4, 6] and other resources, there are four files we need to modify:

  1. opencv-2.4.9/cmake/FindCUDA.cmake

(a) find this line and replace it

with

(b) find the line

and replace with

(c) find the line

and replace with

(d) find the line

and replace with

2. opencv-2.4.9/cmake/OpenCVDetectCUDA.cmake

(a) find and replace the line with this:

(b) find the code segment and replace with:

3. configure modules/gpu/src/graphcuts.cpp:

find the line:

and replace it with

4. Download the file NCVPixelOperations.hpp from here:

and use it to replace the original NCVPixelOperations.hpp:

Finally, cmake the files at opencv-2.4.9/build , build and install:

Once the installation is complete, do these steps to get OpenCV configured.

Then you can now test your OpenCV installation using:

Congrats! You finished installing the OpenCV 2.4.9!

# Install Caffe

In general, follow this official guide and take note of some points:

  • If you hope to reproduce people’s work in GitHub, be sure to build the same version of caffe as they did. For example, in this GitHub repo I needed to set the following flags:
  • In the official guide it’s saying adding this line:

However, the last two are not needed in OpenCV 2, so remove them:

Then, follow the commands in the official guide. Hopefully you successfully build and install the Caffe, you may then check the installation:

Congrats! You can now enjoy your freshly brewed Caffe :)

References

  1. Official Guide, https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide
  2. Installation note of OpenCV 2.4.9 and Caffe, https://gist.github.com/arundasan91/b432cb011d1c45b65222d0fac5f9232c#2-caffe-without-installing-anaconda
  3. Installation note of OpenCV 2.4.9 (in Chinese), https://blog.csdn.net/Solomon1558/article/details/51967280
  4. A patch for OpenCV 2.4.X for CUDA 9, https://davidstutz.de/compiling-opencv-2-4-x-with-cuda-9/
  5. Some error message solutions when building OpenCV (in Chinese), https://blog.csdn.net/hehehetanchaow/article/details/77530510
  6. Another article stating OpenCV 2.4.X for CUDA 9, https://stackoverflow.com/questions/46584000/cmake-error-variables-are-set-to-notfound

--

--

Computer Vision Applied Scientist @ Amazon. https://coldmanck.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store