系統出問題不得不重裝系統,所有caffe相關也都需重裝,在此做個記錄。
環境:Ubuntu14.04/桌上型電腦內建顯示卡+獨立顯示卡gtx960
準備好的檔案:
1.安裝cuda7.5
a)禁用nouveau
$ cd /etc/modprobe.d/
$ sudo geidit blacklist.conf
在/etc/modprobe.d/blacklist.conf最後一行加上:
blacklist nouveau
options nouveau modeset=0
b)關閉GUI:
$ sudo service lightdm stop
c) Alt + ctrl +F1進入指令行
d) 更改CUDA Toolkit執行權限:
切換到cuda安裝檔案的路徑
$ sudo chmod +x cuda_7._linux.run
e) 執行CUDA安裝檔案
f) 切換回GUI:
$ sudo service lightdm start
g) 成功切回GUI後,添加環境變量PATH:
$ sudo gedit /etc/profile
h) 在/etc/profile檔案最後添加
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH
i) 然後source一下,使之生效
$ source /etc/profile
j) 檢查上述的環境變量是否設定成功。
終端中輸入 :
$ env
在輸出的環境變量中檢查有無上述 h) 中設定的變量,如果有則代表設定成功。
k) 再然後就是添加共享庫變量:
$ cd /etc/ld.so.conf.d/
$ touch cuda.conf
$ sudo gedit cuda.conf
在 /etc/ld.so.conf.d/加入檔案 cuda.conf, 内容如下
/usr/local/cuda-/lib64
執行下列指令使之立刻生效
$ sudo ldconfig
2.檢驗cuda是否安裝成功
a) 檢查 NVIDIA Driver是否安裝成功
終端輸入 :
$ cat /proc/driver/nvidia/version
會輸出NVIDIA Driver的版本号
b) 檢查 CUDA Toolkit是否安裝成功
終端輸入 :
會輸出CUDA的版本資訊
c) 嘗試編譯cuda提供的例子
切換到例子存放的路徑,預設路徑是
~/NVIDIA_CUDA-7.5_Samples
(即
/home/XX/ NVIDIA_CUDA-7.5_Samples
)
然後終端輸入:
$ make
如果出現錯誤的話,則會立即報錯停止,否則會開始進入編譯階段。
我的第一次運作時出現了報錯,提示的錯誤資訊是系統中沒有g++
然後在終端運作 :
$ sudo apt-get install g++
安裝完g++後,再make就正常了 。整個編譯的時間持續比較長,耐心等待。
d) 運作編譯生成的二進制檔案。
編譯後的二進制檔案 預設存放在
~/NVIDIA_CUDA-7.5_Samples/bin/x86_64/linux/release
中。
切換路徑 :
終端輸入 :
$ ./deviceQuery
若正确顯示安裝的cuda7.5即GPU資訊,則表示安裝成功。
3.安裝依賴庫
包含了boost opencv leveldb protobuf 等:
BLAS安裝:
4.Caffe安裝
a) 安裝git
$ sudo add-apt-repository ppa:git-core/ppa
中間暫停時,按Enter鍵Enter繼續安裝。
$ sudo apt-get update
$ sudo apt-get install git
安裝下載下傳完成後,可以使用下面的指令行,确認git的版本:
b) 下載下傳caffe(注意下載下傳的位置,下載下傳到
/home/just
):
$ git clone https://github.com/BVLC/caffe.git
Cd進home/just/caffe,然後編譯caffe:
$ cd ~/caffe
$ sudo cp Makefile.config.example Makefile.config
$ sudo make all
$ sudo make test
$ sudo make runtest
5.安裝OpenCV
a) 下載下傳OpenCV
b) 解壓到任意目錄
$ unzip opencv-..zip
c) 進入源碼目錄,建立release目錄
$ cd opencv-.
$ mkdir release
d) 可以看到在OpenCV目錄下,有個CMakeLists.txt檔案,需要事先安裝一些軟體
e) 進入release目錄,安裝OpenCV是所有的檔案都會被放到這個release目錄下
$ cd release
f) cmake編譯OpenCV源碼,安裝所有的lib檔案都會被安裝到/usr/local目錄下
注:這裡-D的作用是設定編譯的選項,如選擇release模式, 輸出的目錄,不編譯cuda子產品,編譯openMP與QT子產品,這裡的QT模式帶有很強的可視化效果,推薦。但需要在第一步中加上 libqt4-dev 安裝QT環境
g) 安裝
$ sudo make install
6.測試 OpenCV
(1) Create a directory DisplayImagefor test project:
$ mkdir DisplayImage
$ cd DisplayImage
(2) Create DisplayImage.cpp and edit it: gedit DisplayImage.cpp
Then edit the DisplayImage.cpp:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -;
}
Mat image;
image = imread( argv[], );
if ( !image.data )
{
printf("No image data \n");
return -;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey();
return ;
}
(3) Create a CMake file:
$ gedit CMakeLists.txt
Then edit the CMakeLists.txt:
cmake_minimum_required(VERSION )
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
(4) Generate the executable file:
$ cmake .
$ make
(5) Execute it:
$ ./DisplayImage lena.jpg
成功則顯示圖像。
7.cudnn
6.cudnn
1.進官網下載下傳檔案(cudnn-7.5-linux-x64-v5.0-ga.tgz)
在終端中切換到檔案所在檔案夾,輸入下面指令:
$ sudo tar xvf cudnn--linux-x64-v5.-ga.tgz
$ cd cuda/include
$ sudo cp *.h /usr/local/include/
$ cd ../lib64
$ sudo cp lib* /usr/local/lib/
$ cd /usr/local/lib
$ sudo chmod +r libcudnn.so..
$ /usr/local/lib$ sudo ln -sf libcudnn.so.. libcudnn.so.
$ sudo ln -sf libcudnn.so. libcudnn.so^C
$ sudo ldconfig
然後切換到caffe根目錄下,将Makefile.config中的USE_CUDNN行前的#去掉:
$ cd ~/caffe
$ sudo gedit Makefile.config
将Makefile.config中的USE_CUDNN行前的#去掉。
儲存後重新編譯:
$ cd ~/caffe
$ sudo make clean
$ sudo make all
$ sudo make test
$ sudo make runtest
遇到過error:
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.7.0: cannot open shared object file: No such file or directory
解決辦法
sudo ldconfig /usr/local/cuda-/lib64
8.配置pycaffe
-
安裝anaconda2
去官網下載下傳,下載下傳2.7版本(Anaconda2-4.2.0-Linux-x86_64.sh)。
bash Anaconda2--Linux-x86_64.sh
安裝的時候,最後一定要選yes。
安裝完畢打開
home/just/.bashrc
發現,它自動在最後添加了:
#Add by anaconda2 4.2.0 installer
Export PATH=”/home/just/anaconda2/bin:$PATH”
修改Makefile.config:
cd ~/caffe
sudo gedit 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 :=
# 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_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/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 \
$(ANACONDA_HOME)/lib/python2/site-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 :=
# 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
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 :=
# enable pretty build (comment to see full commands)
Q ?= @
開始編譯:
$ cd ~/caffe/
$ sudo make clean
$ sudo make all -j8
$ sudo make test -j8
$ sudo make runtest -j8
$ make pycaffe -j8
編譯runtest的時候,遇到這樣的錯誤:
.build_release/test/test_all.testbin: error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory
這是因為 libhdf5.so的版本問題,你可以進入
/usr/lib/x86_64-linux-gnu
看一下,你的libhdf5.so.x中的那個x是多少,比如我的是libhdf5.so.7
是以可以執行下面幾行代碼解決:
# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libhdf5.so.7 libhdf5.so.10
# sudo ln -s libhdf5_hl.so.7 libhdf5_hl.so.10
# sudo ldconfig
最終檢視python接口是否編譯成功:
進入python環境,進行import操作
$ python
>>> import caffe
如果沒有提示錯誤,則編譯成功。
報錯:no module named caffe
解決方法:将anacanda與caffe頭檔案進行連結。
打開.bashrc:
$ cd ~
$ sudo gedit .bashrc
添加:
export PYTHONPATH=”/home/just/caffe/python:$PYTHONPATH”
在進入python環境,import caffe,
報錯:ImportError:No module named google.protobuf.internal
解決方法:
$ pip install protobuf
$ /home/just/anaconda2/bin/pip install protobuf
盡情的:
$ python
>>Import caffe
$ Ipython
>>Import caffe
由于安裝了anaconda,jupyter notebook就已經自動安裝好,可以:
$ jupyter notebook
就會在浏覽器中打開notebook。