继第一章,本章是caffe环境搭建
环境:
系统:Ubuntu16.04
显卡:NVIDIA GTX2070
搭建步骤:
1、安装caffe 的一些依赖项
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 libatlas-base-dev libboost-all-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
2、安装python相关依赖包
sudo apt-get install python-pip
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
3、安装Caffe
(1)下载caffe
cd ~
mkdir git //在home下新建一个git文件夹
git clone https://github.com/BVLC/caffe.git //从github上git caffe
(2)打开到刚刚git下来的caffe,将Makefile.config.example的内容复制到Makefile.config
cd caffe
cp Makefile.config.example Makefile.config
(3)打开Makefile.config文件
gedit Makefile.config
修改Makefile.config如下:
#USE_CUDNN := 1 修改为 USE_CUDNN := 1
#OPENCV_VERSION := 3 修改为 OPENCV_VERSION := 3
#WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1
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
《补充》如果是TX2平台,LIBRARY_DIRS不一样
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/aarch64-linux-gnu /usr/lib/aarch64-linux-gnu/hdf5/serial
(4)为hdf5创建链接
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10 libhdf5_hl.so
(5)make
cd .. \\此时位置应该处于caffe文件夹下
make all -j12 //j12代表计算机cpu有12个核,因此可以多线程一起make,这样make的速度会快很多。
make test -j12
make runtest -j12
make pycaffe //如果以后用python来开发的话必须执行这一句,一般不管你是否用python,都会执行这一句
make distribute
《补充》 若想直观查看cpu个数以及使用情况安装htop
sudo apt-get install htop
htop
下图为 make runtest -j6之后:
到这里,caffe环境就算安装完成了。下面是我遇到常见的错误汇总。
常见错误汇总:
一般在执行 make all -j12 会出现意想不到的错误
1、错误1
错误:
recipe for target '.build_release/cuda/src/caffe/solvers/nesterov_solver.o' failed
make: *** [.build_release/cuda/src/caffe/solvers/nesterov_solver.o] Error 1
make: *** Waiting for unfinished jobs....
解决方式:
sudo gedit Makefile.config
注释掉#-gencode arch=compute_20,code=sm_20 \
#-gencode arch=compute_20,code=sm_21 \
2、错误2
错误:
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
/usr/bin/ld: cannot find -lhdf5
/usr/bin/ld: cannot find -lhdf5_hl
collect2: error: ld returned 1 exit status
Makefile:567: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error
解决方式:
Lhdf5出问题有很多原因,需要逐一排查:
(1)认真核对Makefile.config里面的内容,尽量不要手敲,直接复制
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
(2)修改Makefile里面的内容
在Makefile文件中找到:
LIBRARIES +=glog gflags protobuf boost_system boost_filesystem m hdf5 hl hdf5
把它改成:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
(3)软连接创建出错
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10 libhdf5.so //注意你的libhdf5_serial.so.10和我的可能不一样
sudo ln -s libhdf5_serial_hl.so.10 libhdf5_hl.so
(4)libhdf5.so可能没有需安装,执行
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
测试mnist数据集
1、下载mnist数据集
cd ~/caffe
./data/mnist/get_mnist.sh
如果出现权限不够的情况,就加个chmod +X赋予权限
2、转换格式
下载的数据集为二进制格式,必须转换成caffe能识别的lmdb格式
./examples/mnist/create_mnist.sh
浏览/example/mnist下出现mnist_train_lmdb和mnist_test_lmdb就对了
3、训练数据集
./example/mnist/train_lenet.sh
下图为训练好后的截图,分类准确率99.06%,达到训练效果。