更新时间2017年10月31日
书接上文,安装好各种依赖库和显卡后,终于可以进入正题了,本节主要讲caffe-ssd的安装与配置。本帖参考了GitHub上SSD作者的说明和上一篇帖子中的参考贴http://blog.csdn.net/xuzhongxiong/article/details/52717285 的caffe安装部分。以下操作默认在root权限下执行。
第一步:在/home/yakerang/目录下,执行以下命令(yakerang为我自己的文件夹,根据自己的电脑自选)
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
第二步:创建并修改Makefile.config
首先cd进入caffe文件夹,然后执行命令
cp Makefile.config.example Makefile.config gedit Makefile.config此时将打开Makefile.config文件,根据个人情况对其内容进行修改。
a.若使用cudnn,则
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
b.若使用的opencv版本是3的,则
将
#OPENCV_VERSION := 3
修改为:
OPENCV_VERSION := 3
c.若要使用python来编写layer,则
将
#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1
d. 重要的一项 :
将# Whatever else you find you need goes here.下面的
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
这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径.
e. 在Makefile文件的第173行,把 hdf5_hl 和hdf5修改为hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改为第二行代码。
将:
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
在Makefile文件中第260行左右添加boost_regex,即
LIBRARIES += boost_thread stdc++后,如下
LIBRARIES += boost_thread stdc++ boost_regex
继续在Makefile文件中查找
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
第三步:编辑/usr/local/cuda/include/host_config.h 将其中的第115行注释掉:
将
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
第四步:将一些文件复制到/usr/local/lib文件夹下,解决"libcudart.so.8.0 cannot open shared object file: Nosuch fileor directory"
:
#注意自己CUDA的版本号!
cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/lib/libcudart.so.8.0 && sudo ldconfig
cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/lib/libcublas.so.8.0 && sudo ldconfig
cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/lib/libcurand.so.8.0 && sudo ldconfig
第五步:编译
cd进入caffe目录,依次执行以下命令:
make -j8 # Make sure to include $CAFFE_ROOT/python to your PYTHONPATH. make py make test -j8 # (Optional) make runtest -j8可能会出现no module named caffe 错误,原因是没有把caffe中的python导入到解释器,解决方法:
vim ~/.bashrc打开~/.bashrc文件,将以下内容写入文件最下方
export PYTHONPATH=~/caffe/python:$PYTHONPATH
关闭文件,执行命令:
source ~/.bashrc重新执行make命令,应该可以编译通过了。可能在 make runtest 时会有错误,遇到了百度一下即可,即使make runtest失败,也不会影响后边到使用。
第六步:下载预训练模型,下载VOC2007 和 VOC2012数据
按照作者主页中的步骤,一步步执行即可。
# Download the data. cd $HOME/data wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar # Extract the data. tar -xvf VOCtrainval_11-May-2012.tar tar -xvf VOCtrainval_06-Nov-2007.tar tar -xvf VOCtest_06-Nov-2007.tar
第七步:创建LMDB数据
cd $CAFFE_ROOT # Create the trainval.txt, test.txt, and test_name_size.txt in data/VOC0712/ ./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/ ./data/VOC0712/create_data.sh这一步一定要修改create_list.sh和create_data.sh两个文件中的路径部分。需要根据自己的目录结构进行更改,并不难。
第八步:训练与测试
训练可以利用下载到预训练模型,进行微调。执行以下命令。
# 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.pyssd_pascal.py中的路径 用到gpu标号,都需要改。如果没时间训练,可以下载作者训练好的模型,直接用。以下命令可以测试模型对数据集的准确率。
# If you would like to test a model you trained, you can do:
python examples/ssd/score_ssd_pascal.py
但是大家可能更关心对单张图片或视频的测试结果。我找到了一个测试单张图像的源文件,修改了里边的路径。将其放在caffe/examples/ssd/ 文件夹下,执行以下命令即可
python examples/ssd/ssd_detect_singleImg.py
代码如下:
# coding: utf-8
# Note: this file is expected to be in {caffe_root}/examples
# ### 1. Setup
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import pylab
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
caffe_root = '/home/yakerang/caffe'
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, '/home/yakerang/caffe/python')
import caffe
from google.protobuf import text_format
from caffe.proto import caffe_pb2
caffe.set_device(0)
caffe.set_mode_gpu()
labelmap_file = '/home/yakerang/caffe/data/VOC0712/labelmap_voc.prototxt'
file = open(labelmap_file, 'r')
labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), labelmap)
def get_labelname(labelmap, labels):
num_labels = len(labelmap.item)
labelnames = []
if type(labels) is not list:
labels = [labels]
for label in labels:
found = False
for i in xrange(0, num_labels):
if label == labelmap.item[i].label:
found = True
labelnames.append(labelmap.item[i].display_name)
break
assert found == True
return labelnames
model_def = '/home/yakerang/caffe/models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
model_weights = '/home/yakerang/caffe/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712Plus_SSD_300x300_iter_240000.caffemodel'
net = caffe.Net(model_def, model_weights, caffe.TEST)
# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array([104, 117, 123])) # mean pixel
transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2, 1, 0)) # the reference model has channels in BGR order instead of RGB
# ### 2. SSD detection
# Load an image.
image_resize = 300
net.blobs['data'].reshape(1, 3, image_resize, image_resize)
image = caffe.io.load_image('/home/yakerang/caffe/examples/images/fish-bike.jpg')
plt.imshow(image)
# Run the net and examine the top_k results
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
# Forward pass.
detections = net.forward()['detection_out']
# Parse the outputs.
det_label = detections[0, 0, :, 1]
det_conf = detections[0, 0, :, 2]
det_xmin = detections[0, 0, :, 3]
det_ymin = detections[0, 0, :, 4]
det_xmax = detections[0, 0, :, 5]
det_ymax = detections[0, 0, :, 6]
# Get detections with confidence higher than 0.6.
top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]
top_conf = det_conf[top_indices]
top_label_indices = det_label[top_indices].tolist()
top_labels = get_labelname(labelmap, top_label_indices)
top_xmin = det_xmin[top_indices]
top_ymin = det_ymin[top_indices]
top_xmax = det_xmax[top_indices]
top_ymax = det_ymax[top_indices]
# Plot the boxes
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
currentAxis = plt.gca()
for i in xrange(top_conf.shape[0]):
# bbox value
xmin = int(round(top_xmin[i] * image.shape[1]))
ymin = int(round(top_ymin[i] * image.shape[0]))
xmax = int(round(top_xmax[i] * image.shape[1]))
ymax = int(round(top_ymax[i] * image.shape[0]))
# score
score = top_conf[i]
# label
label = int(top_label_indices[i])
label_name = top_labels[i]
# display info: label score xmin ymin xmax ymax
display_txt = '%s: %.2f %d %d %d %d' % (label_name, score,xmin, ymin, xmax, ymax)
# display_bbox_value = '%d %d %d %d' % (xmin, ymin, xmax, ymax)
coords = (xmin, ymin), xmax - xmin + 1, ymax - ymin + 1
color = colors[label]
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor': color, 'alpha': 0.5})
# currentAxis.text((xmin+xmax)/2, (ymin+ymax)/2, display_bbox_value, bbox={'facecolor': color, 'alpha': 0.5})
plt.imshow(image)
pylab.show()