Compile OpenCV 3.1 And Python 2.7 With CUDA 7.5 On Ubuntu 14.04

COMPILE OPENCV 3.1 AND PYTHON 2.7 WITH CUDA
ref:http://rolflussi.blogspot.co.id/2015/09/opencv-with-cuda-on-ubuntu-1404.html
1. Create a new virtualenv with python parameter point to python2.7.

2. Check python version:

3. Check pip version:

4. Install numpy:

CHECK numpy VERSION:

IMPORTANT: I FOUND AN ERROR ‘The data should normally be NULL! in function allocate’ WHEN TRY TO TEST SIFT Holography Feature Matching (flann.knnMatch). PLS FIX THIS FIRST (SEE SOLUTION IN /media/data/MASTER/opencv/github_list_fish_rec.txt) BEFORE BUILD OPENCV AT STEP #5 BELOW! — OKAY. THIS IS ALREADY FIXED ON THE LAST COMPILE (OPENCV 3.1 WITH PYTHON 2.7)

5. We already cloned opencv 3.1.0 and opencv_contrib 3.1.0. so now, go to the build WITH CUDA and do setup:
<OLD>
THIS IS THE INITIAL CMAKE COMMAND: (DONT USE IT!)

</OLD>

<OLD>
THIS IS THE CMAKE COMMAND WITH CUDA I GOT FROM http://rolflussi.blogspot.co.id/2015/09/opencv-with-cuda-on-ubuntu-1404.html: (DONT USE IT! ‘WITH_QT’ COMMAND GAVE ME AN ERROR! MAY BE I DONT HAVE QT INSTALLED)

THIS IS THE ERROR:

</OLD>

<NEW>
THIS IS THE CMAKE COMMAND WITH CUDA AND REMOVE ‘WITH_QT’ COMMAND. THIS IS WORK!!! BUT MAKE SURE ‘build’ directory EMPTY FIRST!
NOTE: I USE ‘INSTALL_C_EXAMPLES=ON’ INSTEAD OF ‘OFF’ LIKE PREVIOUS BUILD. LET ME KNOW IF EVERYTHING WORKS (ITS CONFIRMED!!!)

</NEW>

NOTE:
PLS SEE THE NVIDIA CUDA PART ON THE ABOVE RESULT IS INCLUDED! BUILD WITH CUDA IS FAST (A FEW MINUTES)!

6. Compile opencv:
NOTE: BEFORE EXECUTING THIS, CONSIDER TO EDIT THIS FILE:

SEE BELOW HOW TO MODIFY IT!!!

NOTE:
AFTER WAITING FOR MORE THAN 1 hour AND 25 MINUTES (91% COMPLETED), THE COMPILATION IS STOPPED WITH THESE ERRORS STACK:

SOLUTION:
ref:https://github.com/Itseez/opencv/issues/5859
I USED A TEMPORARY SOLUTION: EDIT /home/teddy/opencv/build/samples/gpu/CMakeFiles/example_gpu_opengl.dir/link.txt. THEN ADD THIS: -lGL -lGLU AT THE END OF THE LINE (IN SINGLE LINE! DONT ADD AT THE NEW LINE!). SO THE FILE WOULD BE LIKE THIS:

SAVE THE FILE THEN RE-MAKE (BUT I SUGGEST TO REMOVE ‘build’ DIRECTORY, CREATE IT AGAIN LIKE STEP #5. BUT BEFORE EXECUTING STEP #6, EDIT link.txt FILE LIKE THE ABOVE! ). THIS SOLUTION IS WORKING!!!
ACTUALLY THERE IS A PERMANENT SOLUTION IS SUGGESTED IN THE SAME URL, BUT I DONT KNOW HOW TO DO IT!!! LEARN IT

OK. HERE IS THE SUCCESFULL COMPILE (MAKE)

NOTE:
SEE THE MORE COMPLETE COMPILATION (MAKE) PROCESS IN THIS FILE: /media/data/MASTER/opencv/make_opencv_cuda.txt
ITS NOT FULL COMPLETED BECAUSE I MISSED SOME DATA BUT IT WORTHS TO SEE (THERE ARE SOME WARNING!!!)
THIS COMPILATION TOOK MORE THAN 1 hour AND 30 MINUTES. COMPILATION WITH CUDA (MAY BE ALSO WITH OPENGL) MAKE IT SLOWER!!!

WARNINGS:
1.

2.

7. Assuming OpenCV 3.1 compiled without error, you can now install it on your system:
NOTE: BEFORE EXECUTING THIS COMMAND, MODIFY AGAIN THE ABOVE FILE: /home/teddy/opencv/build/samples/gpu/CMakeFiles/example_gpu_opengl.dir/link.txt OR WE’D GET THE SAME ERROR (PLS SEE THE ERROR HERE: /media/data/MASTER/opencv/opencv_cuda_make_install_error.txt ):

THIS TIME WE MUST USE SUDO BECAUSE THE FILE IS LOCKED:

THEN ADD THIS AT THE END OF THE SINGLE LINE LIKE ABOVE: -lGL -lGLU
THEN SAVE AND CLOSE
INSTALL AGAIN:

NOTE:
FOR COMPLETE PROCESS RESULT, SEE THIS FILE: /media/data/MASTER/opencv/opencv_cuda_make_install_success.txt

8.

9. If you’ve reached this step without an error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages
However, our ‘opencv_p27_cuda’ virtual environment is located in our home directory — thus to use OpenCV within our cv environment, we first need to sym-link OpenCV into the site-packages directory of the ‘opencv_p27_cuda’ virtual environment:

10. Check the installation opencv version:

11. EXECUTE SOME OPENCV EXAMPLES in /home/teddy/opencv/build/bin/ DIRECTORY:
a. Run this example (image grain):

b. Test Cuda Codec:

 

Installation of CUDA Toolkit 7.5 On Ubuntu 14.04

CONSIDER USING CUDA???
FROM THIS: https://en.wikipedia.org/wiki/CUDA, MY GPU Nvidia GeForce GT 520M IS SUPPORTED!!! Micro Architecture Fermi (https://en.wikipedia.org/wiki/Fermi_(microarchitecture)) version 2.1
CHECK CUDA FROM TERMINAL:
ref:http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/#axzz44a7lZNcT

INSTALLING CUDA TOOLKIT 7.5 ON UBUNTU 14.04
ref:http://www.r-tutor.com/gpu-computing/cuda-installation/cuda7.5-ubuntu
https://www.quantstart.com/articles/Installing-Nvidia-cuda-on-Ubuntu-14-04-for-Linux-GPU-Computing
https://developer.nvidia.com/cuda-downloads -> Linux -> x86_64 -> Ubuntu -> 14.04 -> deb(local)
http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
(1.9 GB)
1. The first task is to make sure that you have the GNU compiler collection (GCC) tools installed. This is carried out by installing the build-essential package:

2. Download the latest Cuda toolkit:
Manually FROM:
http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
OR:

3. PREPARE THE PACKAGE:

OK

4. Update

5. INSTALL CUDA:

SOLUTION:
ref:http://askubuntu.com/questions/672047/anyone-has-successfully-installed-cuda-7-5-on-ubuntu-14-04-3-lts-x86-64

RUN AGAIN:

6. REBOOT

7. MODIFY ENVIRONMENT VARIABLES:
As part of the CUDA environment, you should add the following in the .bashrc file of your home folder.

ADD THESE LINES AT THE BOTTOM:

THEN SAVE AND CLOSE!

8. Remember to make sure that the terminal has access to these variables:

9. Before proceeding to test the GPU cards we will ensure that the drivers are correctly installed. The following line will provide us with the driver version:

10. Check the version of the Nvidia CUDA compiler:

TRY THE CUDA 7.5 SAMPLES:
1. Copy the samples into /home/teddy/Documents/

2. GO to the samples directory:

3. Go to the deviceQuery directory THEN make:

4. run the deviceQuery script to test if we can communicate with the GPU:

 

5. Go to the bandwidthTest directory THEN make:

 

6. run bandwidthTest:

 

Upgrade CUDA Toolkit 7.5 to 8.0 & cuDNN v5.1 On Ubuntu 14.04

Reference: http://dhaneshr.net/2016/11/09/upgrading-to-cuda-8-0-on-ubuntu-16-04/
https://developer.nvidia.com/cuda-downloads
https://developer.nvidia.com/rdp/cudnn-download
http://www.pyimagesearch.com/2016/07/04/how-to-install-cuda-toolkit-and-cudnn-for-deep-learning/

The NVIDIA CUDA Toolkit: A development environment for building GPU-accelerated applications. This toolkit includes a compiler specifically designed for NVIDIA GPUs and associated math libraries + optimization routines.
The cuDNN library: A GPU-accelerated library of primitives for deep neural networks. Using the cuDNN package, you can increase training speeds by upwards of 44%, with over 6x speedups in Torch and Caffe.

Currently My system Ubuntu 14.04 installed CUDA toolkit 7.5

Now I want to upgrade it to CUDA toolkit 8.0:

  1. Download the .deb file of CUDA Toolkit 8 (I prefer local here to make the installation smooth) from https://developer.nvidia.com/cuda-downloads.
    I need to select the correct file according to my systemThe file (https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64-deb) is BIG (1.9 GB)!!! IT TOOK ALMOST ONE HOUR TO COMPLETE THE DOWNLOADED FILE
  2. Download cuDNN (NVIDIA CUDA Deep Neural Network library) from https://developer.nvidia.com/rdp/cudnn-download (You need to be a member first)Download the ‘cuDNN v5.1 Library for Linux’ (https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v5.1/prod_20161129/8.0/cudnn-8.0-linux-x64-v5.1-tgz). File size is just 98MB.
  3. Remove CUDA 7.5 package first (NO???)
    This is not working ‘sudo apt-get remove nvidia-cuda-toolkit’

    This is also no make sense ‘sudo apt-get purge cuda-7.5’

    HOW THIS ‘purge’ ONLY REMOVE 16,4kB DATA???
  4. Install the .deb file (THIS INSTALLATION WOULD NOT REMOVE THE OLDER 7.5 VERSION. IT’D BE INSTALLED SIDE BY SIDE)
    Then ‘sudo apt-get update’
    Then

    OK. THEN RESTART YOUR COMPUTER TO SEE ANY WRONG HAPPENED!
  5. The CUDA toolkit 8 is installed in /usr/local/cuda-8.0 (next to ‘cuda-7.5’ and the symlink ‘cuda’)Then we need to modify the environment variables:

    Modify this line

    to

    Save and Close. Then make sure the system use the new changes

    Read also: https://devtalk.nvidia.com/default/topic/971478/installing-cuda-7-5-next-to-cuda-8-0-on-ubuntu-14-04-so-i-can-keep-using-zed-driver-v1-1-0-/
  6. Check CUDA version and the driver


    Now I already use CUDA Toolkit 8.0. I CAN CHANGE ‘.bashrc’ TO BACK TO THE 7.5 VERSION IF IT’S NEEDED!

TRY THE CUDA 8.0 SAMPLES:

  1. Copy the samples into /home/teddy/Documents/

    the samples directory in /home/teddy/Documents/NVIDIA_CUDA-8.0_Samples/
  2. Go into the samples directory
  3. Go into the deviceQuery directory THEN make:


     
  4. run the deviceQuery script to test if we can communicate with the GPU:

     
  5. Go into the bandwidthTest (/NVIDIA_CUDA-8.0_Samples/1_Utilities/bandwidthTest) directory THEN make:

     
  6. run bandwidthTest:

     

INSTALL cuDNN:
Installing cuDNN is quite simple — all we need to do is extract the .tgz file then copy the files in the lib64  and include directories to their appropriate locations on our machine:

Check cuDNN version (http://stackoverflow.com/questions/31326015/how-to-verify-cudnn-installation):