CentOS安装caffe
1. 下载源码
su guxiaotu # 切换到用户guxiaotu
# 切换到当前用户主目录下
cd $HOME
sudo yum install -y git # 安装git
git clone https://github.com/weiliu89/caffe.git caffe-ssd #下载代码并且重命名为caff-ssd
# 进入caffe-ssd源代码目录
cd caffe-ssd
# checkout出ssd算法源码
git checkout ssd
2. 设置环境变量
# 设置环境变量
echo 'export CAFFE_ROOT=$HOME/caffe-ssd' >> ~/.bashrc # 配置$CAFFE_ROOT
# 将/usr/lib/python2.7/dist-packages和$CAFFE_ROOT/python追加到$PYTHONPATH.
echo 'export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:$CAFFE_ROOT/python'>>~/.bashrc
# 将$CAFFE_ROOT/build/tool命令工具追加到$PATH中
echo 'export PATH=$PATH:$CAFFE_ROOT/build/tool' >> ~/.bashrc
# 使环境变量生效
source ~/.bashrc
3. 安装第三方库
sudo yum install -y epel-release \
wget \
zip \
gcc-c++ \
cmake \
protobuf-devel \
leveldb-devel \
snappy-devel \
boost-devel \
hdf5-devel \
gflags-devel \
glog-devel \
lmdb-devel \
openblas-devel \
python-devel \
liblas-devel \
atlas-devel \
libopenblas-dev \
python-matplotlib \
numpy
# 清除缓存包
sudo yum clean all
sudo rm -rf /var/cache/yum
centos中opencv-devel默认为2.4.5,会提示"warning: GStreamer: unable to query position of stream (/builddir/build/BUILD/opencv-2.4.5/modules/highgui/src/cap_gstreamer.cpp:660)",国外论坛2013年讨论过,源代码有问题。所以选择手动安装opencv
sudo wget -O /opt/opencv2.4.13.6.zip https://github.com/opencv/opencv/archive/2.4.13.6.zip
sudo unzip /opt/opencv2.4.13.6.zip -d /opt
cd /opt/opencv-2.4.13.6/ && sudo mkdir release/ && cd release
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make && sudo make install
# glog
sudo wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/google-glog/glog-0.3.3.tar.gz -P /opt
tar zxvf /opt/glog-0.3.3.tar.gz -C /opt
cd /opt/glog-0.3.3
sudo ./configure
sudo make && sudo make install
# gflags
sudo wget https://codeload.github.com/gflags/gflags/zip/v2.0 -O /opt/gflags-2.0.zip
sudo unzip /opt/gflags-2.0.zip -d /opt
cd /opt/gflags-2.0
sudo ./configure
sudo make && sudo make install
# lmdb
git clone https://github.com/LMDB/lmdb /opt/lmdb
cd /opt/lmdb/libraries/liblmdb
sudo make && sudo make instal
提醒:
每次重新make编译源代码前,需要进入之前源代码包make clean清除下编译
Ø 出现问题:如果gflags高于2.0版本会出现以下问题 /usr/bin/ld: /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/: could not read symbols: Bad value collect2: ld returned 1 exit status make: [libglog.la] Error 1
Ø 分析原因: Glog Need to be compiled into shared library.
4. 安装Python以及第三方库
sudo yum install -y python-pip
sudo pip install --upgrade pip # 升级pip到10.0.1版本
# 临时设置阿里云的pip源加快Python库的下载速度
sudo pip install -i https://mirrors.aliyun.com/pypi/simple ansible
# 安装Python第三方库
pip install Cython \
numpy \
scipy \
scikit-image \
matplotlib==1.5.3 \
ipython \
h5py \
leveldb \
networkx \
nose \
pandas \
python-dateutil \
protobuf \
python-gflags \
pyyaml \
Pillow \
mkl \
pyldap \
six --user
# 也可以按照$CAFFE_ROOT/python/requirements.txt中指定具体版本安装
pip install Cython==0.19.2 \
numpy==1.7.1 \
scipy==0.13.2 \
scikit-image==0.9.3 \
matplotlib==1.3.1 \
ipython==3.0.0 \
h5py==2.2.0 \
leveldb==0.191 \
networkx==1.8.1 \
nose==1.3.0 \
pandas==0.12.0 \
python-dateutil==2.6.0 \
protobuf==2.5.0 \
python-gflags==2.0 \
pyyaml==3.10 \
Pillow==2.3.0 \
six==1.1.0 --user
注意 matplotlib==1.5.3,1.5.3是当前1.0版本中最高版本,超过版本2.0.0之后,会提示“ImportError: cannot import name cbook”
5. 编译caffe环境
# 设置行号
echo 'set number' >> /etc/vimrc
# 进入caffe-ssd目录
cd $CAFFE_ROOT
cp Makefile.config.example Makefile.config
sudo vim Makefile.config
# 修改以下内容
8 CPU_ONLY := 1 # 第8行,将前面#取消,启用只使用CPU模式
89 WITH_PYTHON_LAYER := 1 # 第89行,取消注释表示使用Python编写layer
# 第91~92行后面追加配置hdf5路径
91 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
92 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib/usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
# 编译caffe
make all
# 编译pycaffe,前提确保$CAFFE_ROOT/python添加到环境变量PYTHONPATH中(详细请看2. 设置环境变量)
make pycaffe
make test
# 可选
make runtest -j8
Ø 出现问题: ./include/caffe/util/cudnn.hpp:8:34:致命错误:caffe/proto/caffe.pb.h:没有那个文件或目录
Ø 分析原因: 应该是版本比较低。 pip install protobuf --upgrade -i http://pypi.douban.com/simple --trusted-host pypi.douban.com --user pip install pillow --upgrade -i http://pypi.douban.com/simple --trusted-host pypi.douban.com --user
6. 准备模型以及数据集
6.1下载作者训练的模型数据,存放在$CAFFE_ROOT/models/VGGNet/
# 如果使用作者已经训练好的模型数据,请下载到$CAFFE_ROOT/model
sudo wget -P $CAFFE_ROOT/model http://www.cs.unc.edu/%7Ewliu/projects/SSD/models_VGGNet_VOC0712_SSD_300x300.tar.gz
# 解压到制定目录
tar -zxvf $CAFFE_ROOT/model/models_VGGNet_VOC0712_SSD_300x300.tar.gz -C $CAFFE_ROOT/model
6.2 下载VOC2007 and VOC2012数据集, 存放在默认目录$HOME/data/
中
# 用户主目录下创建data目录后进入
mkdir $HOME/data
# 下载数据集
sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
sudo wget -P $HOME/data http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
# 解压到指定目录(必须按照以下顺序解压,不能颠倒)
tar -xvf $HOME/data/VOCtrainval_11-May-2012.tar -C $HOME/data
tar -xvf $HOME/data/VOCtrainval_06-Nov-2007.tar -C $HOME/data
tar -xvf $HOME/data/VOCtest_06-Nov-2007.tar -C $HOME/data
注意:三个压缩文件解压顺序一定不能打乱
6.3 创建LMDB文件
cd $CAFFE_ROOT # 必须保证在$CAFFE_ROOT中执行
sudo vim /etc/ld.so.conf.d/usr-libs.conf
# 添加以下内容
/usr/local/lib
# 在data/VOC0712/中创建trainval.txt, test.txt, and test_name_size.txt
./data/VOC0712/create_list.sh
# You can modify the parameters in create_data.sh if needed.
# It will create lmdb files for trainval and test with encoded original image:
# - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb
# - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb
# and make soft links at examples/VOC0712/
sudo vim data/VOC0712/create_data.sh
# 修改一下以下值
root_dir=$CAFFE_ROOT
# 执行sh脚本生成lmdb文件
./data/VOC0712/create_data.sh
注意:如果提示缺少某个model,说明缺少对应Python第三方库或者版本过低,使用sudo pip install --upgrade 具体包名安装,也可以制定具体版本安装 提示缺少sci无法使用pip install -U命令安装scikit-image,提示“Cannot uninstall 'pyparsing'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.”
# 由于其他库以来pyparsing,所以选择忽略它 pip install scikit-image --ignore-installed pyparsing --user
Ø 出现问题: error while loading shared libraries: libgflags.so.2: cannot open shared object file: No such file or directory Ø 分析原因: 原因是程序没有找到相应的依赖库,解决方法:
- 将所有的用户需要用到的库放到/usr/loca/lib;
- 在/etc/ld.so.conf.d/目录下新建文件usr-libs.conf,内容是:/usr/local/lib
- sudo ldconfig
7. 训练/计算
7.1 训练模型
# It will create model definition files and save snapshot models in:
# - $CAFFE_ROOT/models/VGGNet/VOC0712/SSD_300x300/
# and job file, log file, and the python script in:
# - $CAFFE_ROOT/jobs/VGGNet/VOC0712/SSD_300x300/
# and save temporary evaluation results in:
# - $HOME/data/VOCdevkit/results/VOC2007/SSD_300x300/
# It should reach 77.* mAP at 120k iterations.
python examples/ssd/ssd_pascal.py
7.2 图片数据集上测试
# If you would like to test a model you trained, you can do:
python examples/ssd/score_ssd_pascal.py
7.3 视频数据测试$CAFFE_ROOT/examples/videos
cd $CAFFE_ROOT
# 测试示例视频
sudo vim $CAFFE_ROOT/examples/ssd/ssd_pascal_video.py
# 第99~100行修改模式为CPU,P.Solver.GPU修改为P.Solver.CPU
99 # Use GPU or CPU
100 solver_mode = P.Solver.CPU
# 第77~76行修改视频文件路径$CAFFE_ROOT/examples/videos
75 # The video file path
76 video_file = "examples/videos/ILSVRC2015_train_00755001.mp4"
7.4 摄像头测试
# 摄像头测试
sudo vim $CAFFE_ROOT/examples/ssd/ssd_pascal_webcam.py
# 第100~101行修改模式为CPU,P.Solver.GPU修改为P.Solver.CPU
99 # Use GPU or CPU
102 solver_mode = P.Solver.CPU
# If you would like to attach a webcam to a model you trained, you can do:
python examples/ssd/ssd_pascal_webcam.py