Ubuntu 16.04 OpenCV 3.1.0 + cuda8.0 +cudnn 5.1.5

Source: Internet
Author: User
Tags git clone

Installation Order:

OpenCV 3.1.0

Cuda 8.0

CUDNN 5.1.5

Caffe 1. Installation of OpenCV 3.1.0 1.1 installation relies on necessary installation

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev Libswscale-dev
Optional Installation
sudo apt-get install checkinstall yasm libtiff5-dev libjpeg-dev libjasper-dev libdc1394-22-dev Libxine2-dev Libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev Libv4l-dev Python-dev python-numpy Libtbb-dev Libgtk2.0-dev libfaac-dev Libmp3lame-dev Libopencore-amrnb-dev libopencore-amrwb-dev Libtheora-dev Libxvidcore-dev x264 v4l-utils Libeigen3-dev
1.2 Download OpenCVTo the official website http://opencv.org/downloads.html download OpenCV 3.1.0 download finished after decompression.
1.3 Installation OpenCV
CD opencv-3.1.0
mkdir build
CD build
cmake-d cmake_build_type=release-d cmake_install_prefix=/usr/local ..

In this process, there is a good chance of an error: Icv:downloading ippicv_linux_20151201.tgz timeout.
Solution: Go to the internet for downloading. Direct search file name, with download address: here
Replace the file with the same name under the opencv-3.1.0/3rdparty/ippicv/downloads/linux-* directory after downloading, and cmake again.
Optional (show some of the compiled content specified):
cmake-d cmake_build_type=release-d cmake_install_prefix=/usr/local with_tbb=on-d build_new_python_support=on-d with _v4l=on-d install_c_examples=on-d install_python_examples=on-d build_examples=on-d with_qt=on-d with_opengl=on-d ble_fast_math=1-d with_cuda=on-d cuda_fast_math=1-d with_cublas=1-d cuda_generation=auto-d WITH_GSTREAMER_0_10=OFF.

And then under the build directory:

Make-j4
-J4 represents the four-core operation, which can be selected according to the computer configuration.

sudo make install

There will be problems encountered in the Make-j4 g++ version too high, I installed many times, encountered once.

Modify the CMakeList.txt file in the opencv-3.1.0 directory, add the set (cmake_cxx_flags "${cmake_cxx_flags}-d_force_inlines") at the beginning and then CMAKE again, and make -J4, make install again.

If you encounter ' nppigraphcutstate ' has not been declared, it is OPENCV incompatible with cuda8.0. There are problems with previous versions of OPENCV, opencv2.4.13 no problem, this is the only version released in 2016. Workaround:

In the Path/to/opencv/modules/cudalegacy/src/graphcuts.cpp, put

#if!defined (Have_cuda) | | Defined (Cuda_disabler)

To

#if!defined (Have_cuda) | | Defined (Cuda_disabler) | | (cudart_version>=8000)

Reference: openCV3.1 Installation

set up a shared LibraryIn addition, in later use (for example, when I compile Caffe), the library may not be found, because the library is not placed in a location that can be found
This section refers to: OpenCV program compile Error "Libopencv_core.so.2.4:cannot open shared object file:no such file or directory" in U Buntu 12.04
In the/usr/local directory to find the location of the OpenCV library, I see in the answer that someone is under/USR/LOCAL/OPENCV and/usr/local/opencv2, but I found here under the/usr/local/lib. If it cannot be found, try:
sudo updatedb && Locate libopencv_core.so.3.1
3.1 is the version number.

New file:
sudo vim/etc/ld.so.conf.d/opencv.conf
Write address/usr/local/lib and save it.
Update:
sudo ldconfig-v
1.4 Test OpenCV 1) Create working directory
mkdir ~/opencv-lena
cd ~/opencv-lena
gedit DisplayImage.cpp
2) Edit the following code
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace CV;
int main (int argc, char** argv)
{
	if (argc!= 2)
	{
		printf ("Usage:DisplayImage.out <image_path>\n ");
		return-1;
	}
	Mat image;
	Image = Imread (argv[1], 1);
	if (!image.data)
	{
		printf ("No image data \ n");
		return-1;
	}
	Namedwindow ("Display Image", window_autosize);
	Imshow ("Display image", image);
	Waitkey (0);
	return 0;
}
3) Create CMake compile file
Gedit CMakeLists.txt
Write to the following content
Cmake_minimum_required (VERSION 2.8)
Project (displayimage)
find_package (OpenCV required)
add_ Executable (displayimage DisplayImage.cpp)
target_link_libraries (DisplayImage ${opencv_libs})
4) Compiling
CD ~/opencv-lena
cmake.
Make
5) ImplementationAt this time the Opencv-lena folder has produced an executable file displayimage, download lena.jpg placed under Opencv-lena, run
./displayimage lena.jpg
6) Results

2. Install cuda8.0I am using offline. Deb installation method, according to their own computer and software situation, go to the official website download DBE package, about 1.8G. Then open the terminal in the directory where it resides, and perform the following command installation:

sudo dpkg-i cuda-repo-<distro>_<version>_<architecture>.deb   # own. Deb name
sudo apt-get Update  
Remember to reboot the computer after the installation: sudo reboot for the video card configuration to take effect.
3. Install cudnn5.1.5Download Cudnn v5.1 livrary for Linux
The above mentioned CUDNN installation package download good decompression, into the extracted files, in the terminal to perform the following instructions installation:
sudo tar xvf cudnn-8.0-linux-x64-v5.1.tgz
cd cuda/include
sudo cp *.h/usr/local/include/
CD. /lib64
sudo cp lib*/usr/local/lib/
cd/usr/local/lib
sudo chmod +r libcudnn.so.5.1.5
sudo ln-sf libcudnn.so.5.1.5 libcudnn.so.5
sudo ln-sf libcudnn.so.5 libcudnn.so
sudo ldconfig


4. Install Caffe 1. Install all kinds of update packages first
sudo apt-get update

sudo apt-get upgrade

sudo apt-get install-y build-essential git cmake

sudo apt -get install-y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler

sudo apt-get Install-y libatlas-base-dev

sudo apt-get install-y--no-install-recommends libboost-all-dev

sudo apt-get Install-y libgflags-dev libgoogle-glog-dev liblmdb-dev

sudo apt-get install-y python-pip

sudo apt-get install- Y python-dev
sudo apt-get install-y python-numpy python-scipy

sudo apt-get install-y libopencv-dev

2, download Caffe
CD ~
mkdir git  //Create a new Git folder under home to store those github git from the Zong
git clone https://github.com/BVLC/ Caffe.git    //git from GitHub Caffe
3. Start Installation
CD Caffe    //open to just git down the Caffe
CP Makefile.config.example Makefile.config// Copy the contents of the Makefile.config.example to Makefile.config
//Because make instructions can only be   Makefile.config file, while Makefile.config.example is the Makefile example given by Caffe
Gedit      // Open Makefile.config File
Carefully read the comments in the makefile in fact know how to operate, in order to facilitate understanding, the author or a description of the various configuration.
In the open Makefile.config modify the following:
If you do not use the GPU, then the
# cpu_only: = 1
modified to:
cpu_only: = 1

//If the use of CUDNN, then
# use_cudnn: = 1 modified to
:
USE_CUDNN: = 1

///If the OPENCV version used is 3, then the
# opencv_version: = 3 is
modified to:
opencv_version: = 3

// To write LAYER using PYTHON, you need to
Modify # With_python_layer: = 1
to
with_python_layer: = 1

//Important one
will # Whatever else you find your need goes here. The following
include_dirs: = $ (python_include)/usr/local/include
Library_ Dirs: = $ (python_lib)/usr/local/lib/usr/lib
modified to:
include_dirs: =  $ (python_include)/usr/local/ Include/usr/include/hdf5/serial
Library_dirs: = $ (python_lib)/usr/local/lib/usr/lib/usr/lib/x86_64- Linux-gnu/usr/lib/x86_64-linux-gnu/hdf5/serial
//This is because the ubuntu16.04 file contains a change in location, especially the HDF5 location that needs to be used, so you need to change this path
4. Make various documents
CD..   \ \ This position should be under the Caffe folder make

All-j4  //j4 on behalf of the computer CPU has 4 cores, so can be multithreaded together makes, so make the speed will be much faster. Also commonly used are J8 make

Test-j4 made

runtest-j4 make

Pycaffe   ///If you want to use Python later to develop this sentence, generally, whether you use Python or not, Will perform this sentence make

distribute
5.caffe Test

Mnist is a handwritten digital library, maintained by DL Daniel Yan LeCun. Mnist, originally used for handwritten digit recognition on checks, is now the starting practice library for DL. The special model for mnist recognition is lenet, the earliest CNN model.

The Mnist Data training sample is 60000, the test sample is 10000, each sample is the 28*28 size black and white picture, the handwritten number is 0-9, therefore divides into the 10 class download hypothesis already in Caffe's root directory, now starts to download:

sudo sh data/mnist/get_mnist.sh

After successful operation, there are four files in the data/mnist/directory:
Train-images-idx3-ubyte: Training Set Sample (9912422 bytes)
Train-labels-idx1-ubyte: Training set corresponding annotation (28881 bytes)
T10k-images-idx3-ubyte: Test Set Picture (1648877 bytes)
T10k-labels-idx1-ubyte: Test set corresponding annotation (4542 bytes)

This data cannot be used directly in Caffe and needs to be converted into Lmdb data

sudo sh examples/mnist/create_mnist.sh

If the conversion fails please check the filename of data/mnist/t10k-images-idx3-ubyte (sometimes the file name T10k-images.idx3-ubyte is extracted is not correct, modified)

examples/mnist/folder is running Lmdb data

After the conversion is successful, it will generate two folders in the examples/mnist/directory, Mnist_train_lmdb and Mnist_test_lmdb, and the Data.mdb and Lock.mdb stored in it, which is the running data we need.

Next is to modify the configuration file, if you have a GPU and have been fully installed, this step can be omitted, if not, you need to modify the Solver configuration file.

There are two configuration files required, one is lenet_solver.prototxt, and the other is train_lenet.prototxt.

First Open Lenet_solver_prototxt

sudo gedit examples/mnist/lenet_solver.prototxt

Set the maximum number of iterations at Max_iter, as needed, and decide if the last line of Solver_mode should be converted to a CPU and then save exit.
Run This example now to start running:


CPU running about 13 minutes, the GPU running time of about 4 minutes, Gpu+cudnn run about 40 seconds, precision is about 99%.

If you have questions qq:873040807 reference: http://blog.csdn.net/jhszh418762259/article/details/52957495 reference: http://www.tuicool.com/ Articles/nyjryra Reference: http://blog.csdn.net/dcxhun3/article/details/51859740

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.