主机硬件配置清单:华硕Z370主板+Intel i7 + NVIDIA 1080Ti
第一步:下载文件
下载好这些文件,放到/home/文件夹(博主所放)中,方便安装,分别为:
1. NVIDIA显卡驱动下载,如图1所示:
图1
2.CUDA下载(选择.run安装包),如图2所示:
图2 CUDA版本
3.cuDNN下载
这个需要账号,申请一个就行了,很简单,可以下载所需版本。
5.opencv下载,如图3所示:
图3 opencv3.1.0
第二步:处理黑屏问题(因为这个重装多次系统)
可能是主板的问题,安装完驱动之后就黑屏,解决方法是关掉Secure Boot,具体操作是:
->“不断地”点击“F2”(不同主板进入BIOS不一样),进入BIOS -> 找到Secure Boot选项 -> 将“Enabled”选项改为“Disabled”选项,意思是关闭SecureBoot ->
第三步:安装caffe教程
step1 安装依赖包
step2 禁用nouveau
step3 安装NVIDIA显卡驱动
step4 配置环境变量
step5 安装CUDA 8.0
step6 验证CUDA8.0是否安装成功
step7 安装cuDNN
step8 安装opencv
step9 安装pycaffe
step1 安装依赖包
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
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 install git cmake build-essential
句就可以了。
step2 禁用nouveau
nouveau(英语:/nuːˈvoʊ/) 是一个自由开放源代码CPU驱动程序,是为AMD的CPU所编写。只有禁用nouveau后才可以安装NVIDIA显卡驱动,首先确定是否禁用,命令行输入:
lsmod |grep nouveau
如果有输出,则需要禁止该NVIDIA第三方驱动,若无,则忽略这个步骤。
禁用方法是在命令行输入:
sudo vim /etc/modprobe.d/blacklist-nouveau.conf
打开文件之后在里面添加语句,首先按‘i’,进入输入模式,然后输入:
blacklist nouveau option nouveau modeset=0
最后按下“:”“w”“q”保存退出,中间会出现一些提示,无视就可以了。为了让命令生效,还要在命令行再次输入:
sudo update-initramfs -u
step3 安装NVIDIA显卡驱动
先按Ctrl+Alt+F1到控制台,关闭当前图形环境,会黑屏,不用怕!分别输入:
sudo init 3
sudo rm -r /tmp/.X*
$sudo service lightdm stop
再安装驱动程序:
cd /home/ #进入home,安装下载好的驱动(博主下载到home文件夹中)
sudo sh NVIDIA-Linux-x86_64-xxx.run #安装
一路按照提示选择安装(全yes),主要有接受协议,在系统内核注册,用新路径注册,最后启动图形环境
sudo service lightdm start
重启电脑
sudo reboot
step4 配置环境变量
修改环境变量有两个地方,一个是在/home/.bashrc这个隐藏文件中,更改这个只会临时生效,关闭命令行窗口之后就无效了,还有永久生效方法是修改/etc/profile这个文件,因为这个是安装NVIDIA显卡驱动,仅仅需要更改/home/.bashrc这个文件就行了,命令行输入:
sudo vim ~/.bashrc 或者 sudo vim /home/.bashrc
在文件的最后面加入两行代码,还是先按“i”进入输入模式,再输入:
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
依次按“:”“w”“q”保存退出。
step5 安装CUDA 8.0
现在出到CUDA9.1了,建议安装CUDA8.0版本,版本太高容易出问题,进入xxx.run文件所在目录,也就是依次执行下列命令:
cd /home/ #本文教程所有预先下载文件都放到home文件中
sudo chmod +x xxx.run #给权限
sudo ./xxx.run #安装
显卡驱动一定选择n,不装,其他都默认yes
最后配置环境变量,命令行输入:
sudo vim /ect/profile
按“i”进入输入模式,在文件最后加入两行代码:
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
依次按“:”,“w”,“q”保存退出,并输入命令使配置生效:
sudo ldconfig
验证CUDA8.0是否安装成功,执行命令:
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
出现下列信息,代表安装成功,重点看黄色字体!
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GT 1080Ti"
CUDA Driver Version / Runtime Version 8.0 / 8.0
CUDA Capability Major/Minor version number: 3.5
Total amount of global memory: 2004 MBytes (2100953088 bytes)
( 2) Multiprocessors, (192) CUDA Cores/MP: 384 CUDA Cores
GPU Max Clock rate: 1032 MHz (1.03 GHz)
Memory Clock rate: 800 Mhz
Memory Bus Width: 64-bit
L2 Cache Size: 524288 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 1 copy engine(s)
Run time limit on kernels: No
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GT 1080Ti
Result = PASS
step6 安装cuDNN
下载完成后解压,得到一个 cudn 文件夹,该文件夹下include 和 lib64 两个文件夹,命令行进入 cudn/include 路径下,然后进行以下操作:
sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件
然后命令行进入 cudn/lib64 路径下,运行以下命令:
sudo cp lib* /usr/local/cuda/lib64/ #复制所有动态链接库
cd /usr/local/cuda/lib64/sudo rm -rf libcudnn.so libcudnn.so.5 #删除原有的动态文件
sudo ln -s libcudnn.so.5.1.10 libcudnn.so.5 #生成软衔接
sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接
注意上面黄色链接,版本不同,链接不一样,查看方式:
locate libcudnn.so
执行完会显示:
/home/xxx/.local/share/Trash/files/libcudnn.so
/home/xxx/.local/share/Trash/files/libcudnn.so.5
/home/xxx/.local/share/Trash/files/libcudnn.so.5.1.10
/home/xxx/.local/share/Trash/files/cuda/lib64/libcudnn.so
/home/xxx/.local/share/Trash/files/cuda/lib64/libcudnn.so.5
/home/xxx/.local/share/Trash/files/cuda/lib64/libcudnn.so.5.1.10
/home/xxx/.local/share/Trash/info/libcudnn.so.5.1.10.trashinfo
/home/xxx/.local/share/Trash/info/libcudnn.so.5.trashinfo
/home/xxx/.local/share/Trash/info/libcudnn.so.trashinfo
/home/xxx/cuda/lib64/libcudnn.so
/home/xxx/cuda/lib64/libcudnn.so.5
/home/xxx/cuda/lib64/libcudnn.so.5.1.10
/usr/local/lib/libcudnn.so
/usr/local/lib/libcudnn.so.5
安装完成后可用在命令行输入: nvcc -V 命令验证是否安装成功,若出现以下信息则表示安装成功:
Cuda compilation tools, release 8.0, V8.0.61
step7 安装opencv
首先修改 /opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp 文件内容,如图:
在执行 make -j12 命令编译到 92% 时可能会出现错误,这是由于opecv3.1与cuda8.0不兼容所导致。
然后再下载的文件解压到你要安装的位置,命令行进入已解压的文件夹 opencv-3.1.0 目录下,执行:
mkdir build # 创建编译的文件夹
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j12 #编译
编译成功后安装:
sudo make install #安装
查看是否安装成功:
pkg-config --modversion opencv
step8 安装caffe
进入你想要的安装目录,执行:
git clone https://github.com/BVLC/caffe.git
进入 caffe 文件夹,将 Makefile.config.example 文件复制一份,并更名为 Makefile.config ,命令为:
sudo cp Makefile.config.example Makefile.config
然后修改 Makefile.config 文件,在 caffe 目录下打开该文件:
sudo vim Makefile.config
修改 Makefile.config 文件内容:修改处已经标红:
# 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库所以要打开"
# 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
"用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++
# 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的计算能力
()来进行设置,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)/anaconda
# 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
# 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 /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)
Q ?= @
然后修改 caffe 目录下的 Makefile 文件:
sudo vim Makefile
将
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将:
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
然后修改 /usr/local/cuda/include/host_config.h 文件 :输入sudo vim host_config.h
将
#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!
上面都弄好之后就可以编译了,输入:
make all -j12
然后进行测试,输入:
make runtest -j12
最后出现下图,表示编译通过:
出现这个界面就表示caffe已经安装成功,要是想用python调用caffe,还要安装pycaffe。
step9 安装pycaffe
首先编译 pycaffe :
cd caffe
sudo make pycaffe -j12
编译成功之后会显示:
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
touch python/caffe/proto/__init__.py
PROTOC(python) src/caffe/proto/caffe.proto
编译pycaffe成功之后,验证一下是否可以在python中导入caffe包,在命令号输入:
python
然后输入:
>>>import caffe
我遇到的错误是:
No module named caffe
解决方法:打开终端,输入python,然后输入:
import sys
sys.path.append("/你的caffe位置/caffe/python")
sys.path.append("/你的caffe位置/caffe/python/caffe")
一劳永逸的方法是,更改环境变量:命令行输入:
sudo vim /etc/profile
最后添加上:
export PYTHONPATH=/你的caffe位置/caffe/python:$PYTHONPATH
保存并退出,然后命令行输入:
source /etc/profile
关闭终端,再打开输入:
python
再输入:
import caffe
就成功了!