电脑配置
系统:Ubuntu16.04
GPU:NVIDIA GTX1080
安装依赖
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
# (Python general)
sudo apt-get install -y python-pip
# (Python 2.7 development files)
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy
安装Caffe
(1) 从github上获取caffe (get git: sudo apt-get install git):
git clone https://github.com/BVLC/caffe.git
在caffe-master/python文件夹内,使用root执行依赖项的检查与安装:
sudo su
cd caffe-master/python
for req in $(cat requirements.txt); do pip install $req; done
以上过程报错,需要解决后再进行,可通过升级pip解决部分问题:
pip --version
pip install -u pip
(2) cp Makefile.config:
sudo cp Makefile.config.example Makefile.config
(3) 打开并修改配置文件:
sudo gedit Makefile.config #打开Makefile.config文件
3.1 使用CUDNN:
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
3.2 使用OpenCV
USE_OPENCV := 1 (可选)
OPENCV_VERSION := 3 (如果打开了USE_OPENCV,而且opencv版本为3时)
3.3 Python来编写layer
将
#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1
3.4 路径变化
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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
修改makefile文件
打开makefile文件,做如下修改:
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
编译
make all -j8 -数字6为并行编译进程数目,一般设置为CPU物理核心数,可以使用 $(nproc) 来替换或echo $(nproc) 来查看CPU物理核心数
make test -j8
make runtest -j8
make pycaffe -此时应该已经编译完成,再次执行以确认
忽视以下错误即可:
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
测试
MNIST数据集测试
配置caffe完成后,我们可以利用MNIST数据集对caffe进行测试,过程如下:
1.将终端定位到Caffe根目录
cd ~/caffe
2.下载MNIST数据库并解压缩
./data/mnist/get_mnist.sh
3.将其转换成Lmdb数据库格式
./examples/mnist/create_mnist.sh
4.训练网络
./examples/mnist/train_lenet.sh
训练开始:
I0922 13:14:47.332710 1418 caffe.cpp:218] Using GPUs 0
I0922 13:14:47.390920 1418 caffe.cpp:223] GPU 0: TITAN X (Pascal)
I0922 13:14:47.593374 1418 solver.cpp:44] Initializing solver from parameters:
test_iter: 100
test_interval: 500
base_lr: 0.01
display: 100
max_iter: 10000
lr_policy: "inv"
gamma: 0.0001
power: 0.75
momentum: 0.9
weight_decay: 0.0005
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
solver_mode: GPU
device_id: 0
...
I0922 13:15:00.678990 1418 solver.cpp:447] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I0922 13:15:00.682633 1418 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I0922 13:15:00.684578 1418 solver.cpp:310] Iteration 10000, loss = 0.00334544
I0922 13:15:00.684592 1418 solver.cpp:330] Iteration 10000, Testing net (#0)
I0922 13:15:00.732586 1429 data_layer.cpp:73] Restarting data prefetching from start.
I0922 13:15:00.733906 1418 solver.cpp:397] Test net output #0: accuracy = 0.9908
I0922 13:15:00.733917 1418 solver.cpp:397] Test net output #1: loss = 0.0276493 (* 1 = 0.0276493 loss)
I0922 13:15:00.733922 1418 solver.cpp:315] Optimization Done.
I0922 13:15:00.733924 1418 caffe.cpp:259] Optimization Done.
参考:
http://blog.csdn.net/aifei7320/article/details/73252635(此文记录了部分错误记录)
http://www.jianshu.com/p/7df78120803a
网友评论