硬件环境:Ubuntu14.04LTS X64, GT750M
资源获取
1.获取源代码
上github上获取整个[工程](https://github.com/weiliu89/caffe/tree/ssd),选择download zip,得到caffe-ssd.zip文件。输入命令:
unzip caffe-ssd.zip
进行解压。
2.获取模型文件
下载models标题下面的07+12: SSD300*, SSD512*中的SSD300*,得到models_VGGNet_VOC0712_SSD_300x300.tar.gz文件,使用命令:
tar -zvxf models_VGGNet_VOC0712_SSD_300x300.tar.gz
解压得到models文件夹,讲文件夹中的内容复制到caffe-ssd/models下
编译安装
1.准备环境
SSD基于caffe框架开发,所以编译安装SSD的条件与编译安装caffe相同。没有配置过得请按照博客Ubuntu14.04通过make+cmake编译安装caffe 准备各种依赖。
安装scikit-image
这应该是SSD用于处理图像的工具吧:
sudo apt-get install python-skimage
安装numpy
sudo apt-get install python-numpy
安装python-protobuf
sudo apt-get install python-protobuf
2.修改Makefile.config
cd到caffe-ssd的注目录,输入命令
cp Makefile.config.example Makefile.config
如果是按照博客Ubuntu14.04通过make+cmake编译安装caffe 准备的环境的话需要对Makefile.config更改两处:
一是BLAS库的类型需要从SSD默认的OpenBlas更改为ATLAS:
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# BLAS := open
二是显卡计算能力的调整,我的显卡是GT750M,CUDA能力为3.0,所以需要做如下更改:
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 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_50,code=compute_50
# CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53
caffe中的Makefile.config默认的为:
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_50,code=compute_50
SSD将这一段注释了,添加了下面这一段:
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53
这里我发现了一个令人尴尬的失误,我本来打算是把SSD添加的注释掉然后把caffe默认的那一段的注释取消。但是结果好像是两个都注释了,但是还是跑通了LOL。
3.修改json_parser_read.hpp
为什么要修改请看后文遇到的问题总结。这里在computer中搜索json_parser_read.hpp,具体方法是打开文件夹Document,选中computer,在搜索json_parser_read.hpp。找到该文件的路径之后用如下命令打开:
sudo gedit /usr/include/boost/property_tree/detail/json_parser_read.hpp
将257行开始的escape代码段注释掉,如下:
/*escape
= chset_p(detail::widen<Ch>("\"\\/bfnrt").c_str())
[typename Context::a_escape(self.c)]
| 'u' >> uint_parser<unsigned long, 16, 4, 4>()
[typename Context::a_unicode(self.c)]
;*/
4.编译安装
输入命令:
make -j4
make py
make test -j4
# (Optional)
make runtest -j4
其中-jx表示采用多核编译,根据自己电脑的核数选择参数。
5.运行ssd_pascal_webcam.py
首先执行如下命令(注意修改为自己的路径)
export PYTHONPATH=/home/gph/Desktop/caffe-ssd/python/
之后执行
python examples/ssd/ssd_pascal_webcam.py
即可运行。
问题总结
一 运行make之后出现如下错误:
/usr/include/boost/property_tree/detail/json_parser_read.hpp:257:264: error: ‘type name’ declared as function returning an array
escape
^
/usr/include/boost/property_tree/detail/json_parser_read.hpp:257:264: error: ‘type name’ declared as function returning an array
make: * [.build_release/cuda/src/caffe/layers/detection_output_layer.o] Error 1
make: * Waiting for unfinished jobs….
解决办法:
修改json_parser_read.hpp:打开文件夹Document,选中computer,在搜索json_parser_read.hpp,找到该文件的路径之后用如下命令打开
sudo gedit /usr/include/boost/property_tree/detail/json_parser_read.hpp
将257行开始的escape代码段注释掉即可,如下:
/*escape
= chset_p(detail::widen<Ch>("\"\\/bfnrt").c_str())
[typename Context::a_escape(self.c)]
| 'u' >> uint_parser<unsigned long, 16, 4, 4>()
[typename Context::a_unicode(self.c)]
;*/
二 make py之后出现如下错误:
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
#include <numpy/arrayobject.h>
^
compilation terminated.
make: *** [python/caffe/_caffe.so] Error 1
解决办法:
安装numpy:
sudo apt-get install python-numpy
三 make runtest -j4出现如下错误:
Check failed: error == cudaSuccess (8 vs. 0) invalid device function
解决办法:
invalid device function是和显卡计算能力有关的,这个说名显卡的计算能力设置不对,修改Makefile.config相应地方如下:
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 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_50,code=compute_50
# CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53
caffe中的Makefile.config默认的为:
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_50,code=compute_50
SSD将这一段注释了,添加了下面这一段:
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_53,code=sm_53 \
-gencode arch=compute_53,code=compute_53
这里我发现了一个令人尴尬的失误,我本来打算是把SSD添加的注释掉然后把caffe默认的那一段的注释取消。但是结果好像是两个都注释了,但是还是跑通了LOL。
四 运行python examples/ssd/ssd_pascal_webcam.py出错
错误一:
Traceback (most recent call last):
File “examples/ssd/ssd_pascal_webcam.py”, line 2, in
import caffe
ImportError: No module named caffe
解决办法,添加相应的caffe python路径:
export PYTHONPATH=/home/gph/Desktop/caffe-ssd/python/
错误二:
Cannot copy param 0 weights from layer 'conv4_3_norm_mbox_loc'; shape mismatch. Source param shape is 12 512 3 3 (55296); target param shape is 16 512 3 3 (73728). To learn this layer's parameters from scratch rather than copying from a saved net, rename the layer.
解决方法:
模型文件和py文件不匹配,下载正确的模型文件。py中生成的test.prototxt中的conv4_3_norm_mbox_loc层配置如下:
layer {
name: "conv4_3_norm_mbox_loc"
type: "Convolution"
bottom: "conv4_3_norm"
top: "conv4_3_norm_mbox_loc"
param {
lr_mult: 1.0
decay_mult: 1
}
param {
lr_mult: 2.0
decay_mult: 0
}
convolution_param {
num_output: 16 <<==================此处为16
pad: 1
kernel_size: 3
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
}
所以相应的模型文件的deploy.prototxt对应的层也应该如一样为16,而有的模型文件的prototxt文件为12,所以出错。