国内的thinkpad t系列一般会配一块N卡,想着虽然卡不咋地,但是做一些简单的实验还是可以的,于是决定在这台本子上面装CUDA和caffe来练习一下。下面是步骤,前方有坑,要仔细读:
安装NVIDIA的驱动,使用ubuntu自己的driver安装:
settings -> software & updates -> Additional Drivers -> 选择安装NVIDIA驱动
虽然有手动安装的方法,但是我觉得这样最保险,一定不会装错。去NVIDIA官网下载CUDA8.0,注意用.run文件,而不是.deb,据说这样坑比较少一些。。。
log out出去,然后ctrl + alt + f1进入命令行模式:
首先,禁止X server桌面,不然无法继续安装下去。
sudo service lightdm stop
然后,按照英伟达官网的指令:
sudo sh cuda.***.run (看你下的哪个版本)
我第一次装的时候没有指明tmpdir也装成了,但是因为下面有一步出错了卸载了重新装,没有加tmpdir的话会提示 disk space check has failed. Installation cannot continue. 这种情况就要自己指定一个解压的地方来存放临时安装文件了。后面可以写 --tmpdir=...(根据自己的情况)不过我后来很囧的发现我出现这个问题是因为后面自己在/目录下面拷了一个有点大的数据集。。。rm之后就好了,不用指明tmpdir,就直接在/下就可以。<br />
接下来,安装的时候很重要的一点是别盲目选y!!!第一个选项问你要不要装一个Driver,要选no,要选no,要选no,重要的话说三遍。因为这个就是第一步干的事,不能用它这里的driver,不然很可能不匹配,reboot之后就会出现很蛋疼的问题,什么low graphics blabla的...桌面都出不来了,只能卸掉nvidia的驱动重新来。
- 安装完成后reboot,检验一下是不是装好了。
lspci | grep -i nvidia
如果能检测到的话说明驱动没问题。再看一下现在的主显卡:
prime-select query
test CUDA:
nvidia-smi
如果可以显示GPU当前信息的话,那么CUDA就装好了。
还可以把sample都编译一下,进入/home/username/NVIDIA...
sudo make
一堆warning...哎...<br >这里要设置一下环境变量,
sudo gedit /etc/profile
文件末尾添加PATH=/usr/local/cuda/bin:$PATHexport PATH <br >保存完成后,执行如下命令使环境变量立即生效:
source /etc/profile
然后还需要添加lib的路径:
sudo gedit /etc/ld.so.conf.d/cuda.conf
在文件中写入如下内容然后保存:
/usr/local/cuda/lib64<br >之后执行如下命令使之生效:
sudo ldconfig
ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib.so),进而创建出动态装入程序(ld.so)所需的连接和缓存文件。缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表。我们之前修改过cuda.conf,为了让链接可以找到这里来,才需要这个ldconfig的命令。
或者这样也可以:
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrcsource ~/.bashrc
- 再把cuDNN装了:
依然取去官网下,我下的是CuDNN Library for Linux v5.1,下载需要注册一下...CuDNN的作用是对CUDA进行一些修改和优化,使得更适合于对神经网络进行计算。
<br />下载后解压,然后进入该目录,把所有的lib复制到/usr/local/cuda/lib64/下,把头文件 cudnn.h复制到 /usr/local/cuda/include/下。
sudo cp lib* /usr/local/cuda/lib64/
sudo cp cudnn.h /usr/local/cuda/include/
然后更新软连接:
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.5
sudo ln -s libcudnn.so.5.1.5 libcudnn.so.5
sudo ln -s libcudnn.so.5 libcudnn.so
我安装的是5.1.5的,大家要看下自己cudnn解压出来的lib下面的版本号,不可以盲目复制...
- 安装一些依赖项
sudo apt-get install freeglut3-dev libx11-dev libxmu-dev libxi-dev libglu1-mesa-dev
sudo apt-get install 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 libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
- 安装python的必备
sudo apt-get install python-dev python-pip
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done
我的pip一直出些问题,后来我就用sudo easy-install pip安装了pip。这个apt-get安装出来的很蛋疼不知道为什么...
- 安装caffe
git clone https://github.com/BVLC/caffe.git
要安装BLAS作为caffe的matrix运算支持。可以选择安装Atlas。
sudo apt-get install libatlas-base-dev
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
编译之前修改Makefile.config:
由于需要CuDNN,所以把 # USE_CUDNN := 1的注释去掉。还有Anaconda2的路径修改等。我根据自己的安装修改后的内容如下:
## 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 *_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
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# 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
# MATLAB_DIR := /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
# 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
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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
# 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目录下:
make all -j16
make test
make runtest
在进行make runtest的时候可能会报错说找不到libhdf5.so.10的错误。我解决这个的方式是去/usr/lib/下把libhdf5.so.7和libhdf5_hl.so.7该成了.10。。。。因为我发现我即使把那些依赖都装好了,还是.7,如果有更好的解决方案请告诉我,谢谢~<br />根据需求编译matcaffe和pycaffe
make matcaffe
make pycaffe
matcaffe要能找到mex文件,所以先把matlab装好然后把matlab的路径填到Makefile.config里面去。还有在make pycaffe的时候,因为我是用的anaconda2,Python.h就在anaconda2/include/python2.7/下面,所以这个路径要包含进去,在PYTHON_INCLUDE下面ANACONDA的第二行取消注释。
- 运行mnist demo
sh data/mnist/get_mnist.sh
sh examples/mnist/create_mnist.sh
sh examples/mnist/train_lenet.sh