國內(nèi)的thinkpad t系列一般會配一塊N卡田轧,想著雖然卡不咋地,但是做一些簡單的實驗還是可以的,于是決定在這臺本子上面裝CUDA和caffe來練習一下。下面是步驟伊履,前方有坑,要仔細讀:
安裝NVIDIA的驅(qū)動款违,使用ubuntu自己的driver安裝:
settings -> software & updates -> Additional Drivers -> 選擇安裝NVIDIA驅(qū)動
雖然有手動安裝的方法唐瀑,但是我覺得這樣最保險,一定不會裝錯插爹。去NVIDIA官網(wǎng)下載CUDA8.0哄辣,注意用.run文件,而不是.deb赠尾,據(jù)說這樣坑比較少一些力穗。。气嫁。
log out出去当窗,然后ctrl + alt + f1進入命令行模式:
首先,禁止X server桌面寸宵,不然無法繼續(xù)安裝下去崖面。
sudo service lightdm stop
然后,按照英偉達官網(wǎng)的指令:
sudo sh cuda.***.run (看你下的哪個版本)
我第一次裝的時候沒有指明tmpdir也裝成了梯影,但是因為下面有一步出錯了卸載了重新裝巫员,沒有加tmpdir的話會提示 disk space check has failed. Installation cannot continue. 這種情況就要自己指定一個解壓的地方來存放臨時安裝文件了。后面可以寫 --tmpdir=...(根據(jù)自己的情況)不過我后來很囧的發(fā)現(xiàn)我出現(xiàn)這個問題是因為后面自己在/目錄下面拷了一個有點大的數(shù)據(jù)集甲棍。简识。。rm之后就好了,不用指明tmpdir七扰,就直接在/下就可以奢赂。<br />
接下來,安裝的時候很重要的一點是別盲目選y!!!第一個選項問你要不要裝一個Driver戳寸,要選no呈驶,要選no,要選no疫鹊,重要的話說三遍袖瞻。因為這個就是第一步干的事,不能用它這里的driver拆吆,不然很可能不匹配聋迎,reboot之后就會出現(xiàn)很蛋疼的問題,什么low graphics blabla的...桌面都出不來了枣耀,只能卸掉nvidia的驅(qū)動重新來霉晕。
- 安裝完成后reboot,檢驗一下是不是裝好了捞奕。
lspci | grep -i nvidia
如果能檢測到的話說明驅(qū)動沒問題牺堰。再看一下現(xiàn)在的主顯卡:
prime-select query
test CUDA:
nvidia-smi
如果可以顯示GPU當前信息的話,那么CUDA就裝好了颅围。
還可以把sample都編譯一下伟葫,進入/home/username/NVIDIA...
sudo make
一堆warning...哎...<br >這里要設置一下環(huán)境變量,
sudo gedit /etc/profile
文件末尾添加PATH=/usr/local/cuda/bin:$PATHexport PATH <br >保存完成后院促,執(zhí)行如下命令使環(huán)境變量立即生效:
source /etc/profile
然后還需要添加lib的路徑:
sudo gedit /etc/ld.so.conf.d/cuda.conf
在文件中寫入如下內(nèi)容然后保存:
/usr/local/cuda/lib64<br >之后執(zhí)行如下命令使之生效:
sudo ldconfig
ldconfig命令的用途筏养,主要是在默認搜尋目錄(/lib和/usr/lib)以及動態(tài)庫配置文件/etc/ld.so.conf內(nèi)所列的目錄下,搜索出可共享的動態(tài)鏈接庫(格式如前介紹,lib.so)常拓,進而創(chuàng)建出動態(tài)裝入程序(ld.so)所需的連接和緩存文件渐溶。緩存文件默認為/etc/ld.so.cache,此文件保存已排好序的動態(tài)鏈接庫名字列表弄抬。我們之前修改過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裝了:
依然取去官網(wǎng)下荔茬,我下的是CuDNN Library for Linux v5.1,下載需要注冊一下...CuDNN的作用是對CUDA進行一些修改和優(yōu)化竹海,使得更適合于對神經(jīng)網(wǎng)絡進行計算。
<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的路徑修改等机错。我根據(jù)自己的安裝修改后的內(nèi)容如下:
## 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。弱匪。青瀑。。因為我發(fā)現(xiàn)我即使把那些依賴都裝好了萧诫,還是.7斥难,如果有更好的解決方案請告訴我,謝謝~<br />根據(jù)需求編譯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