[2] SSD配置+訓練VOC0712+訓練自己的資料集
阿新 • • 發佈:2018-10-31
GitHub https://github.com/weiliu89/caffe/tree/ssd
http://blog.csdn.net/u010733679/article/details/52125597
一、安裝配置
sudo apt-get install -y liblapack-dev liblapack3 libopenblas-base libopenblas-dev
-------------------------------------------------------------------------------
1.
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
2.Makefile.config
caffe --> SSD/caffe
3.
make -j8
make py
make test -j8
make runtest -j8
4.寫入環境變數
sudo gedit /etc/profile
export PYTHONPATH=/home/gjw/SSD/caffe/python
登出
===================================================
二、測試
[注]下載訓練好的模型進行下面的測試
(1)訓練好的模型名稱:models_VGGNet_VOC0712_SSD_300x300.tar .gz
(2)連結
https://drive.google.com/file/d/0BzKzrI_SkD1_WVVTSmQxU0dVRzA/view
(3)解壓,/models/VGGNet--->~/caffe/model
測試一:視訊、攝像頭
[測試1]
演示網路攝像頭識別效果,終端輸入:
python examples/ssd/ssd_pascal_webcam.py
[測試2]
python examples/ssd/ssd_pascal_video.py
測試二: 訓練VOC資料集
首先我們不妨先跑一下專案的demo, 需要下載資料集,提前訓練好的資料集等。
下載預訓練的模型,連結:https://gist.github .com/weiliu89/2ed6e13bfd5b57cf81d6,
下載完成後儲存在:
caffe/models/VGGNet/
1.
下載VOC2007和VOC2012資料集, 放在/data目錄下:
cd 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
tar -xvf VOCtrainval_11-May-2012.tar
tar -xvf VOCtrainval_06-Nov-2007.tar
tar -xvf VOCtest_06-Nov-2007.tar
2.
建立lmdb格式的資料:
cd caffe
./data/VOC0712/create_list.sh
./data/VOC0712/create_data.sh
3.
(1)
gpu-->"0,1,2,3"
(2)
batch_size = 8 #32
accum_batch_size = 8 #32
(3)訓練VOC資料集
python examples/ssd/ssd_pascal.py
************************************************************
************************************************************
三、訓練自己的資料集
1.製作VOC2007資料集:labelImg工具
/data/VOCdevkit/driver
/data/VOCdevkit/driver/Annotations
/data/VOCdevkit/driver/ImageSets
/data/VOCdevkit/driver/JPEGImages
2.VOC資料轉換成LMDB資料
SSD提供了VOC資料到LMDB資料的轉換指令碼 data/VOC0712/create_list.sh 和
./data/VOC0712/create_data.sh,這兩個指令碼是完全針對VOC0712目錄下的資料進行的轉換。
實現中為了不破壞VOC0712目錄下的資料內容,針對我們自己的資料集,修改了上面這兩個指令碼,
將指令碼中涉及到VOC0712的資訊替換成我們自己的目錄資訊。
在處理我們的資料集時,將VOC0712替換成driver。
-------------------------------------------------------------------------------------
(1)mkdir /home/gjw/SSD/caffe/data/driver
(2)將data/VOC0712下的create_list.sh,create_data.sh,labelmap_voc.prototxt
這三個檔案copy到driver目錄下
(3)
修改後的這兩個檔案分別為:
[create_list.sh]
for name in VOC2007 VOC2012 --> for name in driver
[create_data.sh]
dataset_name="VOC0712" --> dataset_name="driver"
[labelmap_voc.prototxt]
將該檔案中的類別修改成和自己的資料集相匹配
(4)
$ ./data/driver/create_list.sh
在/home/gjw/SSD/caffe/data/driver目錄下test.txt,test_name_size.txt,trainval.txt
$ ./data/driver/create_data.sh
在/home/gjw/data/VOCdevkit/driver/lmdb目錄下檢視轉換完成的LMDB資料資料
3. 使用SSD進行自己資料集的訓練
VGG_ILSVRC_16_layers_fc_reduced.caffemodel:https://gist.github.com/weiliu89/2ed6e13bfd5
b57cf81d6
http://pan.baidu.com/s/1o8hpU7g 72fm
訓練時使用ssd demo中提供的預訓練好的VGGnet model :
VGG_ILSVRC_16_layers_fc_reduced.caffemodel
將該模型儲存到$CAFFE_ROOT/models/VGGNet下。
將$CAFFE_ROOT/examples/ssd/ssd_pascal.py copy一份 ssd_pascal_driver.py檔案, 根據自己的資料集
修改ssd_pascal_driver.py
主要修改點:
(1)train_data和test_data修改成指向自己的資料集LMDB
train_data = "examples/driver/driver_trainval_lmdb"
test_data = "examples/driver/driver_test_lmdb"
save_dir = "models/VGGNet/driver/{}".format(job_name)
snapshot_dir = "models/VGGNet/driver/{}".format(job_name)
job_dir = "jobs/VGGNet/person/{}".format(job_name)
output_result_dir = "{}/data/VOCdevkit/driver/VOC2007/{}/Main".format(os.environ ['HOME'], job_name)
name_size_file = "data/driver/test_name_size.txt"
label_map_file = "data/driver/labelmap_voc.prototxt"
(2) num_test_image該變數修改成自己資料集中測試資料的數量
(3) num_classes 該變數修改成自己資料集中 標籤類別數量數 + 1
(4)
batch_size = 1 # 32
accum_batch_size = 1
test_batch_size = 1
base_lr = 0.000004
'max_iter': 120000
'test_interval': 100000
-----------------------------------------------------------------
examples/ssd/ssd_pascal.py
gpu = '0,1,2,3' --> gpu = '0'
訓練命令:
python examples/ssd/ssd_pascal_driver.py
VGG_VOC0712_SSD_300x300_iter_????.caffemodel存放在目錄
$CAFFE_ROOT/SSD/caffe/models/VGGNet/person/SSD_300x300中
1、 訓練
sudo gedit ~/SSD/caffe/examples/ssd/ssd_pascal.py
(1)gpus=’0,1,2,3’
(2) 如果出現問題cudasuccess(2vs0)則說明您的顯示卡計算量有限,再次
batch_size =32 //32 16 8 4
(3)
cd ~/SSD/caffe
python examples/ssd/ssd_pascal.py
2、 精度測試
終端輸入:
python examples\ssd\score_ssd_pascal.py
3、視訊、攝像頭測試
[準備工作]
修改E:\caffe\caffe-ssd-microsoft\models\VGGNet\VOC0712
\SSD_300x300下的配置檔案deploy.prototxt中的下面兩個變數為全路徑:
label_map_file: "E:/caffe/caffe-ssd-microsoft/data/VOC0712/labelmap_voc.prototxt"
name_size_file: "E:/caffe/caffe-ssd-microsoft/data/VOC0712/test_name_size.txt"
(2)用生成的模型測試本地攝像頭:ssd_pascal_webcam.py
修改ssd_pascal_webcam.py的pretrain_model變數為自己剛訓練好的模型:
pretrain_model = "E:/caffe/caffe-ssd-microsoft/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_4000.caffemodel"
python examples\ssd\ssd_pascal_webcam.py
4、 單張影象測試
(1)jupyter notebook
(2)
python ssd_detect.py
# coding: utf-8
# # Detection with SSD
#
# In this example, we will load a SSD model and use it to detect objects.
# ### 1. Setup
#
# * First, Load necessary libs and set up caffe and caffe_root
# In[1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Make sure that caffe is on the python path:
caffe_root = '/home/gjw/SSD/caffe/' # this file is expected to be in {caffe_root}/examples
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, 'python')
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
# * Load LabelMap.
# In[2]:
from google.protobuf import text_format
from caffe.proto import caffe_pb2
# load PASCAL VOC labels
labelmap_file = '/home/gjw/SSD/caffe/data/car/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
# * Load the net in the test phase for inference, and configure input preprocessing.
# In[3]:
model_def ='/home/gjw/SSD/caffe/models/VGGNet/car/SSD_300x300/deploy.prototxt' ###
model_weights = '/home/gjw/SSD/caffe/models/VGGNet/car/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_15000.caffemodel' ####
net = caffe.Net(model_def, # defines the structure of the model
model_weights, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
# 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.
# In[4]:
# set net to batch size of 1
image_resize = 300
net.blobs['data'].reshape(1,3,image_resize,image_resize)
image = caffe.io.load_image('/home/gjw/test/000030.jpg') ##新建test
#plt.imshow(image)
# * Run the net and examine the top_k results
# In[5]:
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.25]
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()
plt.imshow(image)
currentAxis = plt.gca()
for i in xrange(top_conf.shape[0]):
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 = top_conf[i]
label = int(top_label_indices[i])
label_name = top_labels[i]
display_txt = '%s: %.2f'%(label_name, score)
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})
plt.show()