[Caffe]史上最全的caffe安裝過程


Linux下的GPU版Caffe安裝方法

系統環境:Ubuntu 14.04LTS + NV TitanX

1.1 (可選)顯卡驅動的安裝(有風險)

如果需要重裝,需要先卸載已有版本

sudo apt-get remove - -purge nvidia-*
sudo apt-get remove - -purge cuda-*

再重裝一個比較穩定的版本

sudo apt-get install -y nvidia-352    // nvidia-361也比較穩定

或者安裝其它最新版本的驅動,但一定注意,通過apt-get或者yum安裝,不要用官方的.run文件安裝驅動,否則死機必須重裝系統解決

1.2 安裝cuda

需要注意的是,cuda安裝過程會自己安裝顯卡驅動,所以第一步也可以不做。使用以下命令檢查在安裝cuda前后驗證是否有顯卡驅動附帶安裝了。

dpkg –l | grep nvidia
When installing CUDA on Ubuntu, you can choose between the Runfile Installer and the Debian Installer. The Runfile Installer is only available as a Local Installer. The Debian Installer is available as both a Local Installer and a Network Installer. The Network Installer allows you to download only the files you need. The Local Installer is a standalone installer with a large initial download. In the case of the Debian installers, the instructions for the Local and Network variants are the same. For more details, refer to the Linux Installation Guide.

以上是官方的指引,即我們可以使用本地安裝(需要下載1G多的安裝包),也可以只下載一個種子文件,安裝過程需要其它配套程序會自動下載。安裝命令如下:

sudo dpkg --install cuda-repo-<distro>-<version>.<architecture>.deb
sudo apt-get update
sudo apt-get install cuda
reboot

增加環境變量

vim ~/.bashrc
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH
source ~/.bashrc

安裝測試樣例

cuda-install-samples-7.5.sh ~
cd ~/NVIDIA_CUDA-Samples_7.5/5_Simulations/nbody
make
./nbody

檢查是否安裝完畢

Dpkg –l |grep cuda
Dpkg –l |grep nvidia
Cd /usr/local/cuda/samples/1_Utilities/deviceQuery
./deviceQuery

cuda安裝參考

1.3 安裝cudnn

Step 0: Install cuda from the standard repositories.
Step 1: Register an nvidia developer account and download cudnn here (about 80 MB)
Step 2: Check where your cuda installation is. For the installation from the repository it is /usr/lib/... and /usr/include. Otherwise, it will be /urs/local/cuda/.
You can check it with:

which nvcc 

or

ldconfig -p | grep cuda

Step 3: Copy the files:

cd folder/extracted/contents
sudo cp -P include/cudnn.h /usr/include
sudo cp -P lib64/libcudnn* /usr/lib/x86_64-linux-gnu/
sudo chmod a+r /usr/lib/x86_64-linux-gnu/libcudnn*	

cudnn安裝參考

1.4 安裝anaconda-python

Download the software in following address

sudo chmod 777 Anaconda2-4.1.1-Linux-x86_64.sh
./Anaconda2-4.1.1-Linux-x86_64.sh

然后按照提示安裝即可,安裝路徑選擇默認的路徑即可,安裝結束后會自動添加環境變量,退出終端之后再進入后生效。
note: anadconda的包管理機制雖然很方便,但有時會和系統版本產生各種不兼容反應
如果想使用系統自帶python的話,最好使用python本地虛擬環境,這樣更加便於管理。安裝Python本地虛擬環境參考

2.1 編譯Caffe源碼

git clone https://github.com/BVLC/caffe && cd caffe
cp Makefile.config.example Makefile.config 
vim Makefile.config 

uncomment

USED_CUDNN = 1
USE_CUDNN := 1
OPENCV_VERSION := 3
# Modify PYTHONPATH of include and lib	
ANACONDA_HOME := $(HOME)/pyenv/pycaffe
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
                 $(ANACONDA_HOME)/include/python2.7 \
                 $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
USE_PKG_CONFIG := 1
WITH_PYTHON_LAYER := 1

編譯

make all -j16
make pycaffe

這兩步不出問題一步就OK了,但是為了讓python能夠找到編譯好的caffe位置,需要:

cd ~/caffe/python    
# 這里面是了些python常用包,需要裝不上的直接刪去
pip install –r requirements.txt 
pwd > ~/pyenv/pycaffe/lib/python2.7/site-packages/caffe.pth    
# 設置python中caffe路徑,其中~/pyenv/pycaffe是我用的python的位置

在任意路徑下,用caffe.pth中的python,import caffe不錯誤即成功了。

2.2 常見編譯或使用中遇到的問題

(1) error: while loading shared libraries: libcudart.so.7.5: cannot open shared object file: No such file or directory
解決:

sudo ldconfig  /usr/local/cuda/lib64
step 4:sudo make pycaffe –j16  

(2) error: Python.h no such file or directory
解決:

ANACONDA_HOME := /home/wangyuanjiang/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		 $(ANACONDA_HOME)/include/python2.7 \
		 $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

solution2 Reference

(3) error: import caffe 后提示Error: No module named google.protobuf.internal
solution: pip install protobuf
solution3 referrence

(4) error: caffe with anaconda2python RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
solution 4:
download numpy-1.11.tar.gz

pip install numpy-1.11.tar.gz

(5) error: ImportError: No module named skimage.io
Solution 5:

sudo apt-get install python-skimage

reference5

(6) error6: python import caffe only in root+caffe/python directory

export PYTHONPATH=/usr/local/src/caffe/python:$PYTHONPATH

(7) error: python –version differs with sudo python –version
It’s a painful trouble!
You an add solute path of python when you want to use sudo python
You can change the permission of you directory by type

Chmod –R 777 destdir

(8) error: cublas_v2.h: No such file or directory:
./include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory
Solution:
本來以為是自己的openblas沒有安裝好,后來發現是因為在Makefile.config文件中寫錯了cuda的路徑。本來把cuda的路徑uncomment成為/usr/local/cuda就行了,但是我卻把它uncomment成為/usr了。

(9) error: when make runtest –j16, there is no share lib libhdf5_h1.so.10 and libhd5.so.10
Solution:

cd /usr/lib/x86_64-linux-gnu/
sudo cp libhdf5_hl.so.7 libhdf5_hl.so.10
sudo cp libhdf5.so.7 libhdf5.so.10

(10) error: 發現訓練一次mnist非常慢,花了大概30min但是make runtest 沒有運行完,把它運行完發現訓練變快了。還沒解決。
(11) error: caffe/proto/caffe.pb.h: No such file or directory
解決:

cp –rf build/src/caffe/proto include/caffe/

參考
official
recommend
關於pycaffe、matcaffe的使用
博客1
博客2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM