ubuntu18.04+cuda9.0+cudnn7.1.4+caffe-ssd+anaconda2安装
Posted la-terre
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu18.04+cuda9.0+cudnn7.1.4+caffe-ssd+anaconda2安装相关的知识,希望对你有一定的参考价值。
几乎是小白一枚,折腾多天才搞定,参考了很多博客和社区问答,把安装经验记下来。
1.检测显卡,安装驱动
1 ~$ ubuntu-drivers devices 2 == /sys/devices/pci0000:00/0000:00:1c.4/0000:09:00.0 == 3 modalias : pci:v000010DEd00001292sv00001028sd00000616bc03sc02i00 4 vendor : NVIDIA Corporation 5 model : GK208M [GeForce GT 740M] 6 driver : nvidia-340 - distro non-free 7 driver : nvidia-driver-390 - distro non-free recommended 8 driver : xserver-xorg-video-nouveau - distro free builtin
选择后面带recommended的驱动
1 ~$ sudo apt install nvidia-driver-390
重启,在系统设置-详细信息的图形一栏查看gpu
2.安装CUDA和cuDNN
这方面攻略很多,我就不一一写了,CUDA9.0,共5个文件。cuDNN7.1.4:
cuDNN v7.1.4 Runtime Library for Ubuntu16.04 (Deb)
cuDNN v7.1.4 Developer Library for Ubuntu16.04 (Deb)
cuDNN v7.1.4 Code Samples and User Guide for Ubuntu16.04 (Deb)
gcc、g++降级
1 sudo apt-get install gcc-4.8 2 sudo apt-get install g++-4.8
在usrin下
sudo rm g++ ##删除原链接
sudo rm gcc
sudo ln -s gcc-4.8 gcc ##重建链接
sudo ln -s g++-4.8 g++
下载好对应版本CUDA和cuDNN安装包(此处省略)
安装CUDA
由于前面已安装好驱动,在装CUDA时不用按ctrl+alt+f3切换到命令行界面,不用输入关闭一系列xxx的命令,只需在图形界面下运行.run文件
sudo sh cuda_9.0.176_384.81_linux.run
同意协议后,问是否安装显卡驱动,填no,剩下部分填yes或回车,剩下就没什么问题了。我安装后提示缺少几个文件,有点忘了,这个百度一下就能解决。之后再安装CUDA的几个补丁。
sudo sh cuda_9.0.176.1_linux.run ##共有四个补丁,这里只列一个
最后记得在~/.bashrc里加上这两条:
export PATH="/usr/local/cuda-9.0/bin:$PATH" export LD_LIBRARY_PATH="/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH"
安装cuDNN(省略)
3.安装anaconda2(省略)
4.安装caffe-ssd
安装各种依赖:
在/etc/apt下打开sources.list,源代码一项打钩,然后
sudo apt build-dep caffe-cuda
再按照之前办法安装gcc5,g++5,修改链接,之后要用g++5编译caffe
git下载caffe-ssd源代码
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
cp Makefile.config.example Makefile.config
修改Makefile.config,这里贴上我的配置:
## Refer to http://caffe.berkeleyvision.org/installation.html # Contributions simplifying and improving our build system are welcome! # cuDNN acceleration switch (uncomment to build with cuDNN). USE_CUDNN := 1 # CPU-only switch (uncomment to build without GPU support). # CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers # USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) # You should not set this flag if you will be reading LMDBs with any # possibility of simultaneous read and write ALLOW_LMDB_NOLOCK := 1 # Uncomment if you‘re using OpenCV 3 OPENCV_VERSION := 3 # To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ # CUSTOM_CXX := g++ # CUDA directory contains bin/ and lib/ directories that we need. CUDA_DIR := /usr/local/cuda # On Ubuntu 14.04, if cuda tools are installed via # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: # CUDA_DIR := /usr # CUDA architecture setting: going with all of them. # For CUDA < 6.0, comment the lines after *_35 for compatibility. # -gencode arch=compute_20,code=sm_20 # -gencode arch=compute_20,code=sm_21 CUDA_ARCH := -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 # -gencode arch=compute_50,code=sm_50 # -gencode arch=compute_52,code=sm_52 # -gencode arch=compute_61,code=sm_61 # BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := atlas #BLAS := open # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! # BLAS_INCLUDE := /path/to/your/blas # BLAS_LIB := /path/to/your/blas # Homebrew puts openblas in a directory that is not on the standard search path # BLAS_INCLUDE := $(shell brew --prefix openblas)/include # BLAS_LIB := $(shell brew --prefix openblas)/lib # This is required only if you will compile the matlab interface. # MATLAB directory should contain the mex binary in /bin. # MATLAB_DIR := /usr/local # MATLAB_DIR := /Applications/MATLAB_R2012b.app # NOTE: this is required only if you will compile the python interface. # We need to be able to find Python.h and numpy/arrayobject.h. #PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include # Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it‘s in root. ANACONDA_HOME := $(HOME)/anaconda2 PYTHON_INCLUDE := $(ANACONDA_HOME)/include $(ANACONDA_HOME)/include/python2.7 $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) # PYTHON_LIBRARIES := boost_python3 python3.6m # PYTHON_INCLUDE := /usr/include/python3.6 # /usr/lib/python3/dist-packages/numpy/core/include # We need to be able to find libpythonX.X.so or .dylib. # PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/lib # Homebrew installs numpy in a non standard path (keg only) # PYTHON_INCLUDE += $(dir $(shell python -c ‘import numpy.core; print(numpy.core.__file__)‘))/include # PYTHON_LIB += $(shell brew --prefix numpy)/lib # Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. 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/hdf5/serial # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies # INCLUDE_DIRS += $(shell brew --prefix)/include # LIBRARY_DIRS += $(shell brew --prefix)/lib # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that ‘make runtest‘ will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @
安装caffe
在caffe根目录下
make -j8 make py make test -j8 make runtest -j8
make runtest -j8如果出现这种error:
.build_release/tools/caffe: error while loading shared libraries: libhdf5_hl.so.100: cannot open shared object file: No such file or directory
,就在.bashrc中加上:
export LD_LIBRARY_PATH="/home/.../anaconda2/lib:$LD_LIBRARY_PATH"
在caffe根目录下的:
source ~/.bashrc
make runtest -j8
应该能通过,那么caffe-ssd就安装好了。
之后如果import caffe时报错:No module named caffe,就在.bashrc文件加上:
export PYTHONPATH="/home/.../caffe/python"
如果跑代码时出现 no module named google.protobuf,用:
conda install protobuf
到这里安装就完成了。
其实我本来配置的是caffe+anaconda3,但ssd貌似是用python2写的,python3跑不起来?所以就换了anaconda2。这里也顺便说下caffe+anaconda3配置:
装好anaconda3后,caffe下Makefile.config为:
## Refer to http://caffe.berkeleyvision.org/installation.html # Contributions simplifying and improving our build system are welcome! # cuDNN acceleration switch (uncomment to build with cuDNN). USE_CUDNN := 1 # CPU-only switch (uncomment to build without GPU support). # CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers # USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) # You should not set this flag if you will be reading LMDBs with any # possibility of simultaneous read and write ALLOW_LMDB_NOLOCK := 1 # Uncomment if you‘re using OpenCV 3 OPENCV_VERSION := 3 # To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ # CUSTOM_CXX := g++ # CUDA directory contains bin/ and lib/ directories that we need. CUDA_DIR := /usr/local/cuda # On Ubuntu 14.04, if cuda tools are installed via # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: # CUDA_DIR := /usr # CUDA architecture setting: going with all of them. # For CUDA < 6.0, comment the lines after *_35 for compatibility. # -gencode arch=compute_20,code=sm_20 # -gencode arch=compute_20,code=sm_21 CUDA_ARCH := -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 # -gencode arch=compute_50,code=sm_50 # -gencode arch=compute_52,code=sm_52 # -gencode arch=compute_61,code=sm_61 # BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := atlas #BLAS := open # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! # BLAS_INCLUDE := /path/to/your/blas # BLAS_LIB := /path/to/your/blas # Homebrew puts openblas in a directory that is not on the standard search path # BLAS_INCLUDE := $(shell brew --prefix openblas)/include # BLAS_LIB := $(shell brew --prefix openblas)/lib # This is required only if you will compile the matlab interface. # MATLAB directory should contain the mex binary in /bin. # MATLAB_DIR := /usr/local # MATLAB_DIR := /Applications/MATLAB_R2012b.app # NOTE: this is required only if you will compile the python interface. # We need to be able to find Python.h and numpy/arrayobject.h. #PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include # Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it‘s in root. ANACONDA_HOME := $(HOME)/anaconda3 PYTHON_INCLUDE := $(ANACONDA_HOME)/include $(ANACONDA_HOME)/include/python3.6m $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) PYTHON_LIBRARIES := boost_python3 python3.6m # PYTHON_INCLUDE := /usr/include/python3.6 # /usr/lib/python3/dist-packages/numpy/core/include # We need to be able to find libpythonX.X.so or .dylib. # PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/lib # Homebrew installs numpy in a non standard path (keg only) # PYTHON_INCLUDE += $(dir $(shell python -c ‘import numpy.core; print(numpy.core.__file__)‘))/include # PYTHON_LIB += $(shell brew --prefix numpy)/lib # Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. 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/hdf5/serial # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies # INCLUDE_DIRS += $(shell brew --prefix)/include # LIBRARY_DIRS += $(shell brew --prefix)/lib # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that ‘make runtest‘ will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @
还是用g++5,make前,打开.bashrc,要把/home/.../anaconda/lib从LD_LIBRARY_PATH中删去。在make过程中如果有boost::xxx 未定义,去boost官网下载源码,找找相关教程编译安装好,然后在caffe根目录下:
make clean
make -j8
应该就可以了,后续如果有bug可参考前面。
以上是关于ubuntu18.04+cuda9.0+cudnn7.1.4+caffe-ssd+anaconda2安装的主要内容,如果未能解决你的问题,请参考以下文章
Ubuntu18.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook深度学习环境配置
ubuntu18.04+cuda9.0+cudnn7.1.4+caffe-ssd+anaconda2安装
ubuntu18.04+CUDA9.0+cuDNN7.1.4+tensorflow1.9 环境搭建
Ubuntu 18.04上CUDA 9.0cuDNN7.0及Tensorflow 1.8的安装
TensorFlow安装(Ubuntu18.04+Anaconda3+CUDA9.0+cuDNN7.1+TensorFlow1.8.0+Pycharm)