ubuntu+GTX1060等+cuda+cudnn+caffe+tensorflow-gpu

安装版本:

ubuntu18.04以下,GTX10系列(即非RTX20),cuda8-9均可,安装并配置caffe,tenforflow-gpu,pycharm,matlab,QT

1.安装NVIDIA驱动
安装相关依赖:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler 
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
安装之前先卸载已经存在的驱动版本:
sudo apt-get remove --purge nvidia*
sudo apt-get update
sudo service lightdm stop
下载网址:http://www.nvidia.com/Download/index.aspx?lang=en-us
(TITAN X没有直接对应显卡驱动,只能尝试找了不要低于360)
sudo apt-get install nvidia-375 #注意在这里指定自己的驱动版本!
或者:
chmod +x  NVIDIA-Linux-x86_64-375.66.run
sudo bash ./NVIDIA-Linux-x86_64-375.66.run
或者直接进入系统软件更新,选择附加驱动安装384(TITAN X会显示两个NVIDIA驱动,340和384,选择384,340后期出毛病!)

借鉴:
https://blog.csdn.net/jzrita/article/details/72887677
https://blog.csdn.net/zoro_lov3/article/details/60581174
http://www.cnblogs.com/longmao-yiran/p/6832374.html
https://www.cnblogs.com/go-better/p/7161006.html
安装完成之后输入以下指令进行验证:
sudo nvidia-smi

2.安装CUDA
下载CUDA
首先在官网上(https://developer.nvidia.com/cuda-downloads)下载CUDA:
下载完成后执行以下命令:
sudo sh cuda_8.0.27_linux.run
中间再安装显卡驱动时,要NO,其他都是YES或者default(不要妄再此安装invidia驱动,容易出现X server occur!!一般无法解决)
环境变量配置1
打开~/.bashrc文件:
sudo vim ~/.bashrc 写入
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
环境变量設置2:sudo gedit /etc/profile 添加:export PATH = /usr/local/cuda/bin:$PATH
动态链接库:sudo gedit /etc/ld.so.conf.d/cuda.conf  添加:/usr/local/cuda/lib64
最后sudo ldconfig
测试CUDA的sammples
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
sudo ./deviceQuery
如果现实一些关于GPU的信息,则说明安装成功。
或nvcc--version


卸载Cuda
cd /usr/local/cuda/bin
sudo ./uninstall_cuda_x.x.pl

sudo rm -rf cuda
sudo ln -s /usr/local/cuda-9.0 /usr/local/cuda

3.配置cuDNN
首先去官网(https://developer.nvidia.com/rdp/cudnn-download)下载
进行解压tar zxvf FileName.tgz
cd进入cudnn5.1解压之后的include目录(后期进行特纳是tensorflow发现必须cudnn6.0)
sudo cp cudnn.h /usr/local/cuda/include/ 
再将cd进入lib64目录下的动态文件进行复制和链接:
sudo cp lib* /usr/local/cuda/lib64/    #复制动态链接库
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.6    #删除原有动态文件
sudo ln -s libcudnn.so.6.0.21 libcudnn.so.6  #生成软衔接
sudo ln -s libcudnn.so.6 libcudnn.so     #生成软链接

4.好多人尝试在此处降低版本 g++,gcc不推荐,后期caffe make时可能会因为版本低而出现问题
出现“unsupport complier”错误:gcc版本过高!
出现google::base::CheckOpMessageBuilder::NewString()’未定义的引用 :版本过低!
以gcc4.9与g++4.9为例
安装低版本gcc与g++:
sudo apt-get install gcc-4.9 g++-4.9
之后进入/usr/bin:
cd /usr/bin
先删除和gcc5.0关联的gcc:
sudo rm gcc
sudo rm g++
再建个软连接
sudo ln -s gcc-4.9 gcc
sudo ln -s g++-4.9 g+

5.安装QT

https://blog.csdn.net/fk1174/article/details/52048191
(因为安装opencv时有一个选项需要QI,所以进行安装)
本机采取的做法是错误的,如下:
5-1.https://download.qt.io/archive/qt/4.8/4.8.5/下载安装包
5-2.解压文件,进入里面:
sudo apt-get install g++
sudo apt-get install libX11-dev libXext-dev libXtst-dev
./configure
然后make
sudo make install
最后在.bashrc里加入内容:
export PATH=$PATH:/usr/local/Trolltech/Qt-4.8.5/bin:/usr/local/Trolltech/Qt-4.8.5/include
export QTDIR=/usr/local/Trolltech/Qt-4.8.5
最后source .bashrc 
正确的做法我认为应该是:https://blog.csdn.net/hansion3333/article/details/60468590所示

6.从官网上下载opencv3.2.0 
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip  
upzip 3.2.0.zip
或者tar -zxvf xxx.tar.gz
将opencv_contrib放在opecv3.2文件里面,
首先安装需要的依赖项:
pip install opencv-python
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install python-pip
sudo apt-get install liblapacke-dev checkinstall(安装时可能遇到下面这个错误:
fata error: LAPACKE_H_PATH-NOTFOUND when building OpenCV 3.2解决方案:sudo apt-get install liblapacke-dev checkinstall)
mkdir build   #新建一个build文件夹,编译的工程都在这个文件夹里
(查看文件夹里内容:
/opencv-3.2.0$ ls
3rdparty  cmake            data     LICENSE         platforms
apps      CMakeLists.txt   doc      modules         README.md
build     CONTRIBUTING.md  include  opencv_contrib  samples

cd build/ 
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..(报错!!QT等未能链接等)
修改:
 cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
或者修改为cmake ..
 make -j4
sudo make install

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig


测试是否安装成功
pkg-config --modversion opencv  

1 root@VB:# python
2 >>>import cv2  
3 >>>cv2.__version__
4 '3.2.0'
pkg-config --modversion opencv  

问题1
安装QT:https://blog.csdn.net/fk1174/article/details/52048191
或者:-D WITH_QT=ON(QT没有链接上,删去)
问题2
fata error: LAPACKE_H_PATH-NOTFOUND when building OpenCV 3.2
解决方案:sudo apt-get install liblapacke-dev checkinstall
问题3:
ICV: Downloading ippicv_linux_20151201.tgz...(下载不下来,失败)
可以自己下载ippicv_linux_20151201.tgz然后复制替换到opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e文件下
后期发现依然无用,每次cmake,make,make install都会重新进行一遍,这里只能祈祷网好了!
opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz
失败错误内容:
CMake Error at cmake/OpenCVFindIPP.cmake:243 (include):
  include could not find load file:

    /home/ciisr/code/zhang/huanjing/opencv-3.2.0/3rdparty/ippicv/downloader.cmake
Call Stack (most recent call first):
  cmake/OpenCVFindLibsPerf.cmake:37 (include)
  CMakeLists.txt:558 (include)
问题4:
NppiGraphcutState** ppState, Npp8u* pDeviceMem);
这是因为opecv3.0与cuda8.0不兼容导致的。解决办法:
修改 ~/opencv/modules/cudalegacy/src/graphcuts.cpp文件内容,
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION>=8000)
问题5:最后依然会有失败,于是换成3.1.0(openCV的卸载:https://blog.csdn.net/qiaoji6073/article/details/76572801)
-- Installing: /usr/local/bin/opencv_traincascade
-- Set runtime path of "/usr/local/bin/opencv_traincascade" to "/usr/local/lib:/usr/local/cuda-8.0/lib64"
-- Installing: /usr/local/bin/opencv_createsamples
-- Set runtime path of "/usr/local/bin/opencv_createsamples" to "/usr/local/lib:/usr/local/cuda-8.0/lib64"
-- Installing: /usr/local/bin/opencv_annotation
-- Set runtime path of "/usr/local/bin/opencv_annotation" to "/usr/local/lib:/usr/local/cuda-8.0/lib64"


7.配置caffe
依赖包:
sudo apt-get update 
sudo apt-get install -y build-essential cmake git pkg-config 
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  

下载:git clone https://github.com/BVLC/caffe.git
sudo cp Makefile.config.example Makefile.config
修改1:Makefile.config:
#USE_CUDNN := 1(目前没有去掉)
#OPENCV_VERSION := 3 
#WITH_PYTHON_LAYER := 1 
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  

修改2:makefile
CXXFLAGS += -std=gnu++11 -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)(我的去掉了-std=gnu++11)
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
修改3:/usr/local/cuda/include/host_config.h   注释掉
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
修改4:~/.bashrc
export PYTHONPATH=~/caffe/python(然后source ~/.bashrc)

make all -j8
make runtest
make pycaffe
测试:
python
import caffe(不报错)

8.安装faster rcnn
依赖包:    
pip install cython  
pip install easydict  
apt-get install python-opencv  
下载:git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
进入py-faster-rcnn/lib
make
进入py-faster-rcnn\caffe-fast-rcnn
与配置caffe一样
sudo make -j8 && make pycaffe
成功!


9.安装tensorflow-gpu 1.4.0
不建议安装anaconda,
sudo apt-get install libcupti-dev
sudo pip install tensorflow-gpu==1.4.0
测试:
python
import tensorflow as tf
tf.__version__

运行时30vs0:
增加sudo
还做了改变2:
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
或者方法2:(我失败了)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)
或者:tf.Session(config=tf.ConfigProto(allow_growth=True))
很多人的意思是找到代码中的session,
在session定义前 增加
config = tf.ConfigProto(allow_soft_placement=True)
#最多占gpu资源的70%
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
#开始不会给tensorflow全部gpu资源 而是按需增加
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
有的时候我们需要指定GPU号,否则会侵占其他资源
执行代码前export CUDA_VISIBLE_DEVICES=n
另一种方法:
在运行的代码里增加如下
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"       # 使用第二块GPU(从0开始)
这是固定使用某块GPU,
禁用某块GPU
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

10.pycharm
安装Pycharm 详情可见:https://jingyan.baidu.com/article/60ccbceb4e3b0e64cab19733.html
关于激活的问题,选择网页激活 然后输入 http://idea.imsxm.com/

11.matlab

在网盘上下载安装包及Crack破解文件之后,解压两个压缩文件,并用Crack文件中的install替换matlab2014安装目录下/java/jar/下的install文件。然后在命令行cd进入matlab2014目录,输入:

$ sudo ./install
1、选择“不联网安装”; 
2、当出现密钥时,随意输入20个数字12345-67890-12345-67890即可; 
3、选择自己需要安装的工具; 
4、需要激活时选择不要联网激活,运用Crack目录下的“license_405329_R2014a.lic”文件作为激活文件 
安装完成之后,还要将Crack/linux目录下的libmwservices.so文件拷贝到/usr/local/MATLAB/R2014a/bin/glnxa64。在Crack/linux目录下的命令行输入:

$ sudo cp libmwservices.so /usr/local/MATLAB/R2014a/bin/glnxa64 
安装完成之后,直接在命令行输入matlab,就能过进行使用了

查看版本:
opencv:
pkg-config --modversion opencv  
Python import cv2 cv2.__version__

ubuntu:


安装配置:
sudo dpkg -i xxxx.deb
tar zxvf xxx.xx

你可能感兴趣的:(caffe)