1. 程式人生 > >ubuntu14.04安裝caffe+python介面

ubuntu14.04安裝caffe+python介面

1 ubuntu系統安裝

2 caffe環境安裝

General dependencies

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
BLAS: sudo apt-get install libatlas-base-dev

Python: sudo apt-get install python-dev python-pip

ubuntu 14.04 :sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

CUDA: 使用的cuda7.5版本

sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
sudo apt-get update
sudo apt-get install -y cuda

設定cuda環境:

在/etc/profile檔案最後新增CUDA環境變數:

sudo gedit /etc/profile

PATH=/usr/local/cuda/bin:$PATH

export PATH

   

儲存後, 執行下列命令, 使環境變數立即生效

source /etc/profile

同時需要新增lib庫路徑: 在 /etc/ld.so.conf.d/加入檔案 cuda.conf

sudo gedit /etc/ld.so.conf.d/cuda.conf

內容如下:

/usr/local/cuda/lib64 

儲存後,執行下列命令使之立刻生效

sudo ldconfig 

CUDNN:使用4.0版本

tar -zxvf cudnn-7.0-linux-x64-v4.0-prod.tgz  
cd cuda  
sudo cp lib64/lib* /usr/local/cuda/lib64/  
sudo cp include/cudnn.h /usr/local/cuda/include/   

cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.4.0.7
sudo ln -sf libcudnn.so.4.0.7 libcudnn.so.4
sudo ln -sf libcudnn.so.4 libcudnn.so
sudo ldconfig

CUDNN:使用5.0版本

tar -zxvf cudnn-7.5-linux-x64-v5.0-ga.tgz  
cd cuda  
sudo cp lib64/lib* /usr/local/cuda/lib64/  
sudo cp include/cudnn.h /usr/local/cuda/include/   

cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.0.5
sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig

3 python 介面編譯

安裝git和下載caffe原始碼:

sudo apt-get install build-essential cmake git pkg-config
git clone https://github.com/BVLC/caffe.git

下載python依賴環境 

sudo su 
sudo apt-get install gfortran
pip install --upgrade pip
for req in $(cat requirements.txt); do pip install $req; done 
// 推薦使用國內映象安裝,你懂的,速度快
for req in $(cat requirements.txt); do pip install $req -i http://pypi.douban.com/simple; done 
for req in $(cat requirements.txt); do pip install $req -i https://pypi.tuna.tsinghua.edu.cn/simple; done 

編譯:先拷貝一份Makefile.config檔案,取消一下注釋

cp Makefile.config.example  Makefile.config

# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

使用如下命令編譯caffe和python介面

make -j
make py

編譯 pycaffe時報錯:fatal error: numpy/arrayobject.h沒有那個檔案或目錄

其實numpy已經是安裝的,anaconda2裡面有,Python中import numpy也沒有問題,但就是在此處報錯,解決方法:
sudo apt-get install python-numpy

4 使用spyder呼叫python介面和kdevelop呼叫c++

安裝

sudo pip install spyder
sudo apt-get install kdevelop