机器软硬件环境:
- GPU:GTX 1060 6G(对应的驱动版本是384)
- 系统:ubuntu1604
- gcc: 5.4
- opencv: 3.2
- CUDA: 8.0
- Cudnn: 5.0
安装GPU驱动
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install -y nvidia-384
sudo apt-get install -y mesa-common-dev
sudo apt-get install -y freeglut3-dev
重启之后,输入命令:
nvidia-smi
能看到GPU显卡的输出信息则安装成功
安装cuda
去nvidia官网下载相应的驱动,安装命令:
sudo sh cuda_***.run
注意:里面有个选择是否安装驱动,一定记得取消,因为上一步我们自己安装了显卡驱动。
打开~/.bashrc文件:
sudo gedit ~/.bashrc
添加环境变量
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
使环境变量生效
source ~/.bashrc
创建链接文件,打开文件(此时没有该文件会被新建):
sudo gedit /etc/ld.so.conf.d/cuda.conf
在文件中写入:/usr/local/cuda/lib64
保存后退出,使该文件生效:
sudo ldconfig
验证方式1:
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
如果能看到相关的硬件信息,则安装成功。
验证方式2:
nvcc -V
若是能看到cuda的版本等信息,说明也是安装成功的。
安装Cudnn
cuDNN是GPU加速计算深层神经网络的库,同样可以去官网下载然后解压缩:
tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz
复制文件到相关的文件夹:
sudo cp ~/cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp ~/cuda/lib64/lib* /usr/local/cuda/lib64/
更新软连接:
sudo rm -rf /usr/local/cuda/lib64/libcudnn.so /usr/local/cuda/lib64/libcudnn.so.5
sudo ln -s /usr/local/cuda/lib64/libcudnn.so.5.1.5 /usr/local/cuda/lib64/libcudnn.so.5
sudo ln -s /usr/local/cuda/lib64/libcudnn.so.5 /usr/local/cuda/lib64/libcudnn.so
安装OpenCV
安装依赖项:
sudo apt-get -y install build-essential
sudo apt-get -y install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get -y install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get -y install libxvidcore-dev libx264-dev # 处理视频所需的包
sudo apt-get -y install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get -y install ffmpeg
配置编译OpenCV:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j8
sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
编译Caffe
安装依赖项:
sudo apt-get -y install build-essential cmake git pkg-config
sudo apt-get -y install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get -y install libatlas-base-dev
sudo apt-get -y install libboost-all-dev
sudo apt-get -y install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get -y install python-pip
sudo apt-get -y install python-dev
sudo apt-get -y install python-skimage
sudo apt-get -y install python-numpy python-scipy
sudo apt-get -y install --no-install-recommends libboost-all-dev
拉取源码:
git clone https://github.com/BVLC/caffe.git #git caffe from github
源码、配置文件等修改:
-
若使用cudnn,则将# USE_CUDNN := 1 取消注释;
-
若使用的opencv版本是3的则将# OPENCV_VERSION := 3 取消注释;
-
若要使用python来编写layer,则需要将# WITH_PYTHON_LAYER := 1 取消注释;
-
重要的一项 将# Whatever else you find you need goes here.下面的代码
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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
这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径
- makefile文件
打开makefile文件,将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
- 编辑host_config.h文件
编辑/usr/local/cuda/include/host_config.h,将其中的第115行注释掉:
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
注释掉
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
依赖库
for req in $(cat requirements.txt); do sudo -H pip install $req --upgrade; done
编译
make all -j8
make test -j8
make runtest -j8
make pycaffe -j8
写入环境变量
sudo gedit ~/.bashrc
export PYTHONPATH=~/caffe/python
source ~/.bashrc
编译py-faster-rcnn
下载源码:
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
再次按照上面编译caffe的方式再次编译py-faster-rcnn,没出现报错则编译完成,如有报错请参见博文:
https://blog.csdn.net/sophia_xw/article/details/80662992
下一篇文章会写faster-rcnn的训练和验证,未完待续…