配置過程必須記錄下來,否則換臺電腦繼續踩坑懵逼。
文章目錄
- 電腦配置和已有環境配置
- 安裝顯卡驅動
- 安裝CUDA
- 安裝cuDNN
- 安裝caffe
- caffe依賴:
- 下載caffe:
- 複製配置模板,生成配置文件:
- 修改Makefile.config和Makefile並編譯:
- 1.若出現hdf5相關問題
- 2.若出現cv:imdecode...未定義引用問題
- 3.若出現nvcc fatal : Unsupported gpu architecture 'compute_20'
- 4.若出現error This file requires compiler and library support for the ISO C++ 2011 standard
- 5.若出現/usr/bin/ld: 找不到 -lcblas和/usr/bin/ld: 找不到 -latlas
- caffe測試樣例mnist
- caffe的python接口
電腦配置和已有環境配置
- 系統:Ubuntu16.04
- GPU:NVIDIA GeForce 940MX
- Python版本:3.5.2(系統自帶,未使用anaconda)
- opencv版本:3.4.1
- protobuf版本:3.6.0 -> 3.3.0(後降版本)
通過pkg-config --modversion opencv
來查看opencv版本。
安裝顯卡驅動
可以通過lspci | grep -i nvidia
來查看顯卡型號。根據顯卡型號,我們在NVIDIA官網查看驅動版本。
使用PPA安裝驅動:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-410(我使用的940mx對應驅動版本410)
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
用nvidia-smi
查看顯卡驅動情況,安裝了cuda後也可以看到cuda版本。
安裝CUDA
欲用GPU,必先。。安CUDA,它是用於NIVDIA的GPU的並行計算框架。
NIVDIA官網CUDA下載
cuda其他版本
cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
按照官網上的提示進行安裝:
sudo dpkg -i cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda
對./bashrc進行修改:
sudo gedit ~/.bashrc
在末尾加上
export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
更新bashrc:
source ~/.bashrc
查看CUDA版本:
cat /usr/local/cuda/version.txt
測試CUDA樣例:
cd /usr/local/cuda-10.0/samples/5_Simulations/nbody/
make
sudo ./nbody
安裝cuDNN
cuDNN是NVIDIA針對深度神經網絡DNN做的加速庫。
下載:
cudnn下載地址
注:需要註冊/登錄賬號後下載
由於CUDA版本爲10.0,因此選擇cuDNN v7.3.1 for CUDA10.0,下載cuDNN v7.3.1 Library for Linux。
解壓:
sudo tar -zxvf ./cudnn-10.0-linux-x64-v7.3.1.20.solitairetheme8
複製頭文件:
cd cuda/include
sudo cp cudnn.h /usr/local/cuda/include
複製動態鏈接庫,刪除原有動態文件,並生成新的軟鏈接,使其生效:
cd ../lib64
sudo cp lib* /usr/local/cuda/lib64
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7
sudo ln -s libcudnn.so.7.3.1 libcudnn.so.7
sudo ln -s libcudnn.so.7 libcudnn.so
sudo ldconfig -v
查看cuDNN版本:
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
顯示了#define CUDNN_MAJOR 7
安裝caffe
caffe是一種常用於視頻、圖像處理的深度學習框架。
caffe依賴:
sudo apt install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev
sudo apt install libopenblas-dev #或sudo apt install libatlas-base-dev
sudo apt install protobuf-c-compiler protobuf-compiler
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt install --no-install-recommends libboost-all-dev
下載caffe:
git clone https://github.com/BVLC/caffe.git
複製配置模板,生成配置文件:
sudo cp Makefile.config.example Makefile.config
sudo gedit Makefile.config
修改Makefile.config和Makefile並編譯:
sudo make clean
sudo make all -j4
sudo make test
sudo make runtest
打開~/.bashrc:sudo gedit ~/.bashrc
末尾添加:
export PYTHON=/home/hanamaru/software/caffe-master/python:$PYTHON
使~/.bashrc生效:source ~/.bashrc
1.若出現hdf5相關問題
fatal error: hdf5.h: 沒有那個文件或目錄
和
/usr/bin/ld: 找不到 -lhdf5_hl
/usr/bin/ld: 找不到 -lhdf5
1.將Makefile中
##############################
# Derive include and lib directories
##############################
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
修改爲:
##############################
# Derive include and lib directories
##############################
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
2.將hdf5路徑添加到Makefile.config中
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
改爲
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
2.若出現cv:imdecode…未定義引用問題
將Makefile修改爲:
##############################
# Derive include and lib directories
##############################
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio
3.若出現nvcc fatal : Unsupported gpu architecture ‘compute_20’
在Makefile.config中根據CUDA版本設置
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 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_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
由於我的CUDA版本爲10.0,所以修改爲
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
4.若出現error This file requires compiler and library support for the ISO C++ 2011 standard
若在CMakeLists.txt中添加”set(CXX_STANDARD 11)”並沒有用,則可以嘗試將protobuf版本降低。
wget https://github.com/google/protobuf/archive/v3.3.0.zip
unzip v3.3.0.zip
cd protobuf-3.3.0/
./autogen.sh
./configure --prefix=/usr/local/protobuf
sudo make
sudo make check
sudo make install
在./autogen.sh時可能出現一些小問題,主要是依賴的工具未安裝。
sudo apt install curl autoconf libtool libsysfs-dev
添加路徑
sudo gedit ~/.bashrc
將以下加至末尾
export PATH=$PATH:/usr/local/protobuf/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
source ~/.bashrc
5.若出現/usr/bin/ld: 找不到 -lcblas和/usr/bin/ld: 找不到 -latlas
參考
修改Makefile.config(我使用的是openblas)
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /usr/include/openblas
BLAS_LIB := /usr/lib/openblas-base
caffe測試樣例mnist
cd caffe/data/mnist
./get_mnist.sh
cd ../../
./examples/mnist/create_mnist.sh
./examples/mnist/train_lenet.sh
上面的cd ../../
表示回到caffe根目錄下,因爲新版caffe都需要從根目錄上執行。否則會報錯:./create_mnist.sh: 17: ./create_mnist.sh: build/examples/mnist/convert_mnist
訓練過程:
ok,caffe配置完成
caffe的python接口
注意Makefile.config裏的python路徑的配置
使用ubuntu自帶python
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3/dist-packages/numpy/core/include \
/usr/include
使用anaconda3
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/software/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
#########記得要把Linux系統本身的Python 2.7的PYTHON_INCLUD註釋掉
# 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
#################還有boost
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
#################還有PYTHON_LIB
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
在caffe根目錄下
sudo make clean
sudo make pycaffe
若出現cannot find -lboost_python3
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libboost_python-py35.so libboost_python3.so
將caffe路徑添加到pycharm中
File->Settings->Project interpreter->Show All->Interpreter Paths-> +(添加caffe的python路徑 yourcaffepath/caffe/python)