Ubuntu16.04 configures opencv2, python2, cuda8.0, cudnn and caffe

opencv installation

(1) Install compilation tools and dependency packages

Install the build tools:

sudo apt-get install build-essential  
Install dependencies:
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
Install optional packages:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev 
(2) Download opencv-2.4.13, compile and install

Just go to the official website to download, download address: https://opencv.org/releases.html , then unzip it, and enter the folder opencv-2.4.13 under the terminal (you can also right-click "Open Terminal" in the specified file directory Open the terminal directly):

cd opencv-2.4.13

 Create a new folder to store temporary files:

mkdir release

Switch to that temporary folder: 

cd release

Start compiling:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..  
make -j4  
sudo make install

(3) Environment configuration

The configuration environment adds the opencv library to the path so that the system can find it :

sudo gedit /etc/ld.so.conf.d/opencv.conf  

Inside the opened file add: /usr/local/lib

Then update:

sudo ldconfig  

Continue editing:

sudo gedit /etc/bash.bashrc   

At the end of the opened file add:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  

Save and exit to make environment changes take effect immediately:

source /etc/bash.bashrc  

If you want to call opencv in python, you also need to install opencv-python, and execute the following command in the terminal:

sudo apt-get install python-opencv  

To test, enter python in the terminal, and then enter import cv2. If no error is reported, it means that it is normal.

cuda, caffe installation

(1) Install dependencies

1. General dependencies

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler  
sudo apt-get install --no-install-recommends libboost-all-dev  

2. BLAS dependencies

sudo apt-get install libatlas-base-dev

or install:

sudo apt-get install libopenblas-dev

3. python dependencies

Install python and its header files (ubuntu16 comes with python2 and python3)

sudo apt-get install python  
sudo apt-get install python-dev  

Install python dependencies

sudo apt-get install python-numpy
sudo apt-get install ipython
sudo apt-get install ipython-notebook
sudo apt-get install python-sklearn
sudo apt-get install python-skimage
sudo apt-get install python-protobuf

4. Install glog and gflags and lmdb dependencies

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev  

(2) Install cuda and graphics card driver

Download cuda8.0, download address: https://developer.nvidia.com/cuda-80-ga2-download-archive , pay attention to your system version, what I downloaded here is cuda-repo-ubuntu1604-8-0-local -ga2_8.0.61-1_amd64.deb.

Enter your cuda download path and enter in the terminal (note that the cuda file here needs to be changed to the name of the file you downloaded):

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb  
sudo apt-get update  
sudo apt-get install cuda  

1) Set environment variables after installation is complete

sudo gedit /etc/bash.bashrc

Add at the end of the opened file:

export PATH=/usr/local/cuda-8.0/bin:$PATH  
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH  

Then make the changed environment take effect immediately:

source /etc/bash.bashrc

2) After the installation is complete, verify whether the graphics card driver is installed (if it cannot be detected or the file cannot be found, you can restart the system, because the graphics card driver has just been installed)

cat /proc/driver/nvidia/version

3) Install git, and download the caffe code, which is stored in your /home path by default

sudo apt-get install git  
git clone https://github.com/BVLC/caffe.git  

Go to your caffe source directory

cd caffe  

4) Install cudnn (select install)

CuDNN is a set of GPU computing acceleration solutions specially designed for the Deep Learning framework. Currently supported DL libraries include Caffe, ConvNet, Torch7, etc. The installation is very simple. In fact, it is to perform some copy operations and put the header files and library files under the system path.

First of all, you need to download the cudnn file. I downloaded version 5.1: cudnn-8.0-linux-x64-v5.1.tgz (you need to register for an Nvidia official website account to download, of course, Baidu can download many resources), and then unzip it, it will generate a Folder named cuda, in this directory, execute the following command:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include   
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64   
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*  

5) Compile Caffe

If you need to use cuDNN, please remove the # (comment) before the line USE_CUDNN := 1 in Makefile.config. If the installed opencv is version 3, you should also make corresponding changes (if you need other configurations, please carefully read Makefile.config)

cp ./Makefile.config.example ./Makefile.config  

If your computer does not have a GPU, and you only need to use the CPU, uncomment the CPU_ONLY.

# CPU-only switch (uncomment to build without GPU support).  
# CPU_ONLY := 1  

If you want to use GPU, make no changes here. What needs to be changed at this time is to remove the comment before WITH_PYTHON_LAYER := 1, so that it is convenient to use Python to call.

# Uncomment to support layers written in Python (will link against Python libs)  
WITH_PYTHON_LAYER := 1  
At the same time, in order to match the computing power of cuda8.0, please remove the first two lines in CUDA_ARCH in Makefile.config (reserved is fine, a warning will pop up when compiling)
-gencode arch=compute_20, code=sm_20  
-gencode arch=compute_20, code=sm_21  

In the Makefile.config file, add /usr/include/hdf5/serial/ to INCLUDE_DIRS, that is, change the first line of code below to the second line of code.

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include  
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/  

In the Makefile, change hdf5_hl and hdf5 to hdf5_serial_hl and hdf5_serial, that is, change the first line of code below to the second line of code.

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5  
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial  

Then compile to complete the caffe installation

First execute: ( because my computer CPU is 4 cores, so the parameter 4 is used )

make pycaffe -j4
Then enter:
make all -j4  

Then enter the command:

make test -j4

Then enter:

make runtest -j4

If the final display passes, it means that the caffe compilation is successful. Of course, if you encounter a problem, you can usually solve the problem by sending the error information directly to Baidu.


references:

https://www.cnblogs.com/wm123/p/5385940.html

https://blog.csdn.net/woainishifu/article/details/73995489

https://blog.csdn.net/u011452807/article/details/72637421

https://www.cnblogs.com/ygh1229/p/6528134.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326219194&siteId=291194637