Ubuntu16.04,CUDA8.0,Caffe-ssd,OpenCV3.1配置指南(二)

更新時間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.py
ssd_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()  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章