内容目录:
- 预安装库
- 驱动禁用
- 驱动禁用
- 安装CUDA
- 编译cuda samples
- CUDNN5安装
- Opencv安装
- Anaconda安装
- 编译Caffe
- 编译Caffe
- 错误总结
-
预安装库
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran ffmpeg
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
# 安装 BLAS QT pip pip3 Hdf5 Boost Vim import...
sudo apt-get install libatlas-base-dev qt5-default libhdf5-serial-dev libhdf5-dev libboost-all-dev vim wget libatlas-base-dev imagemagick python-pip libhdf5-serial-dev libhdf5-dev libboost-all-dev vim wget libatlas-base-dev imagemagick
# 安装pycaffe接口所需要的依赖项
sudo apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags cython ipython python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
-
驱动禁用
可能出现 Ctrl+alt+F1黑屏的情况,先试一下Ctrl+alt+F1能不能进入命令行模式。
按Ctrl+Alt+F1进入命令提示符,新建一个黑名单文件
sudo vim /etc/modprobe.d/blacklist-nouveau.conf
写入下面两行:
blacklist nouveau
options nouveau modset=0
保存退出(:wq!) ,然后执行 :
sudo update-initramfs -u
再执行
lspci | grep nouveau
查看是否有内容 如果没有内容 ,说明禁用成功,如果有内容,就重启一下再查看
-
驱动安装
sudo service lightdm stop # 必须要执行,不然会安装失败
sudo chmod 755 NVIDIA-Linux-x86_64-367.44.run # 获取执行权限
sudo ./NVIDIA-Linux-x86_64-367.44.run # 安装驱动
Accept ... Continue installation... 安装完成之后
sudo service lightdm start
- 安装CUDA
sudo cuda_<version>_linux.run # version表示下载的版本
中间有一步:
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?
(y)es/(n)o/(q)uit: n //是否覆盖驱动?这是关键,要填n 因为前面已经手动安装过驱动了
-
编译cuda samples
cd /usr/local/cuda/samples
sudo make -j16
# 编译完之后运行
cd ./bin/x86_64/linux/release
sudo ./deviceQuery
-
CUDNN5安装
# 下载cudnn并解压
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/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
-
Opencv安装
查看已安装的opencv版本:
pkg-config --modversion opencv
opencv安装请移步:Building OpenCV for Tegra with CUDA
-
Anaconda安装
bash Anaconda3-<version>-<platform>.sh
# 有个地方问你是否将anaconda安装路径加入到环境变量(.bashrc)中,
这个一定要输入yes,否则自己要往里写变量。
在 /etc/ld.so.conf 写入下面路径:
/home/yourname/anaconda3/lib
在~/.bashrc最后添加你的类似下面的路径:
export LD_LIBRARY_PATH="/home/wanzew/anaconda3/lib:$LD_LIBRARY_PATH"
export PATH="/home/wanzew/anaconda3/bin:$PATH"
-
编译Caffe
# 进入下载的caffe目录,执行:
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
"CuDNN是NVIDIA专门针对Deep Learning框架设计的一套GPU计算加速库,用于实现高性能的并行计算,在有GPU并且安装CuDNN的情况下可以打开即将注释去掉。"
# CPU-only switch (uncomment to build without GPU support).
#CPU_ONLY := 1
"表示是否用GPU,如果只有CPU这里要打开"
# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 1
"因为要用到OpenCV库所以要打开,下面这两个选项表示是选择Caffe的数据管理第三方库,两者都不打开 Caffe默认用的是LMDB,这两者均是嵌入式数据库管理系统编程库。"
USE_LEVELDB := 1
USE_LMDB := 1
# 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
"当需要读取LMDB文件时可以取消注释,默认不打开。"
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
"用pkg-config --modversion opencv命令查看opencv版本"
# 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++
"linux系统默认使用g++编译器,OSX则是clang++。"
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
"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 *_50 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50
"这些参数需要根据GPU的计算能力 (http://blog.csdn.net/jiajunlee/article/details/52067962)来进行设置,6.0以下的版本不支持×_50的计算能力。"
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
"如果用的是ATLAS计算库则赋值atlas,MKL计算库则用mkl赋值,OpenBlas则赋值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 := /usr/local/OpenBlas/include
BLAS_LIB := /usr/local/OpenBlas/lib
"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
"matlab安装库的目录"
# 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
"python安装目录"
# 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.5m # PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib <font color="green">python库位置</font>
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 /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
"所用的GPU的ID编号"
# enable pretty build (comment to see full commands)
-
编译Caffe
make all -j8
make test -j8
make runtest -j8
继续安装pyhon接口依赖库
进入caffe/python目录:
执行:
for req in $(cat requirements.txt); do pip install $req; done
//如果安装了anaconda,这一步就不需要了。
然后:
cd ../
make pycaffe
最终查看python接口是否编译成功:
进入python环境,进行import操作
python >>> import caffe
如果没有提示错误,则编译成功
这是将caffe的路径保存到环境变量中。
解决办法:添加环境变量
# 根据你的安装路径来,别照抄
export CAFFE_ROOT=/home/wanzew/caffe/
export PYTHONPATH=/home/wanzew/caffe/python:$PYTHONPATH
-
错误总结