This is a guide for installing OpenCV 3.1 on Ubuntu 16.04 with Cuda 8 support. This has been tested using a system with a GeForce GTX 1060 and on one with a GeForce GTX 1080.
Install Nvidia drivers
# Start clean
sudo apt purge nvidia-*
# Add the PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
For Cuda support we need the compatible driver version (generally NOT the newest). For Cuda 8 driver version 367.57 was used.
sudo apt-get install nvidia-367
UPDATE: now it seems that nvidia-367
actually installs nvidia-375
due to security reasons. See https://devtalk.nvidia.com/default/topic/1000667/cuda-setup-and-installation/failed-to-initialize-nvml-driver-library-version-mismatch/
Fixing Unity if it doesn't boot (from http://askubuntu.com/questions/760356/ubuntu-16-04-unity-no-desktop-just-background-wallpaper):
sudo rm -fr ~/.cache/compizconfig-1
sudo rm -fr ~/.compiz
Then try this if your session not loading :
sudo rm -fr ~/.Xauthority
sudo rm -fr ~/.config/autostart
Reinstall compiz
sudo apt-get install --reinstall ubuntu-desktop unity compizconfig-settings-manager upstart
In addition on some systems the secure boot BIOS settings have to be modified. On the Asus x99-a II motherboard the Secure Boot option is set to "Windows Only" by default. Setting it to "Other OS" allows Ubuntu to use Nvidia drivers (see http://askubuntu.com/a/761281)
Install CUDA 8 and CuDNN 5.1 from Nvidia's developer site: https://developer.nvidia.com/cuda-downloads Download cuda 8.0 and run the following:
cd ~/Downloads
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
sudo apt update
sudo apt install cuda
NOTE: techincally the following step is not required for OpenCV since this is the Cuda Deep Learning library, but it is included here for conveneince (it is required for libraries such as Caffe).
Get the "cuDNN v5.1 Library for Linux" from https://developer.nvidia.com/rdp/cudnn-download (need to register) and then run the following:
sudo tar -xvf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local
Then add the following to your ~/.bashrc
vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
export PATH=$PATH:/usr/local/cuda-8.0/bin
sudo apt-get install cmake cmake-qt-gui
Get OpenCV 3.1 from this fork for CUDA 8 support:
mkdir ~/code/opencv-3.1.0
cd ~/code/opencv-3.1.0
git clone [email protected]:daveselinger/opencv.git .
git checkout 3.1.0-with-cuda8
See the following for more details: opencv/opencv#6677
Adapted from this guide: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide Package pre-reqs
sudo apt install --assume-yes build-essential cmake git pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy
sudo apt install libhdf5-dev
sudo apt install --assume-yes libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
sudo apt install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
sudo apt install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils
Configure CMake
cd ~/code/opencv-3.1.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..
You should see the following after cmake finishes:
-- Other third-party libraries:
...
-- Use Cuda: YES (ver 8.0)
...
-- NVIDIA CUDA
-- Use CUFFT: YES
-- Use CUBLAS: YES
-- USE NVCUVID: NO
-- NVIDIA GPU arch: 20 21 30 35
-- NVIDIA PTX archs: 30
-- Use fast math: NO
Finally build OpenCV!
# Build!
make -j $(($(nproc) + 1))
sudo make install
Thank you for the tutorial, I followed it using an old laptop with ubuntu 16.04 and a GeForce GT 555M.
I add a couple of comments, in case someone needs a configuration similar to mine, that is basically using SURF, SVM and ANN in opencv.
I cloned the repository of opencv_contrib in the same parent where opencv-3.1.0 is located and I moved to the 3.1.0 version of the opencv_contrib repository
git checkout 3.1.0
.Then, I added several params to the cmake step:
Such as:
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules
for adding the modules-D BUILD_opencv_java=OFF
because I am not using java-D CUDA_GENERATION=Auto
for compiling focusing on my gpu arch, it makes the compilation way faster-D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_NVCUVID=1 -D WITH_CUFFT=ON -D WITH_EIGEN=ON -D WITH_IPP=ON
because I need to use these libraries (maybe not all, but just in case)
I built opencv and checked the installation using a gpu sample of opencv.