https://github.com/chuanqi305/MobileNet-SSD
https://github.com/GuoJaw/caffe-ssd-mobilenet
环境搭建
【1】去windows下载链接的代码,train和deploy模型
train和deploy模型放在MobileNet-SSD目录下
【2】将下载好的MobileNet-SSD目录放到ubuntu下的~/caffe-ssd/examples目录下
【3】测试./demo.py
训练自己的数据
(1)制作VOC格式的数据集,并转换成LMDB格式:Face_test_lmdb Face_trainval_lmdb
(2)进入~/caffe-ssd/examples/MobileNet-SSD目录下
(2-1)软链接
ln -s PATH_TO_YOUR_TRAIN_LMDB trainval_lmdb
ln -s PATH_TO_YOUR_TEST_LMDB test_lmdb
ln -s /home/gjw/caffe-ssd/examples/Face/Face_test_lmdb test_lmdb
ln -s /home/gjw/caffe-ssd/examples/Face/Face_trainval_lmdb trainval_lmdb
(2-2)将~/caffe-ssd/data/Face下的labelmap_voc.prototxt test.txt trainval.txt test_name_size.txt复制到MobileNet-SSD目录下
(2-3)gen_model.sh 5 #类别+1
注解:
在example目录下,生成文件MobileNetSSD_deploy.prototxt MobileNetSSD_test.prototxt MobileNetSSD_train.prototxt。
【修改参数】
MobileNetSSD_train.prototxt中的batch_size=24改成1
MobileNetSSD_test.prototxt中的batch_size=8改成1
(3)开始训练
sh train.sh
注解:生成的caffemodel结果保存在snapshot中。
将生成的caffemodel模型使用merge_bn.py 进行转换
修改python merge_bn.py 文件的路径
import numpy as np
import sys,os
caffe_root = '/home/gjw/caffe-ssd/'
sys.path.insert(0, caffe_root + 'python')
import caffe
train_proto = 'example/MobileNetSSD_train.prototxt'
train_model = 'snapshot/mobilenet_iter_1200.caffemodel' #should be your snapshot caffemodel
deploy_proto = 'example/MobileNetSSD_deploy.prototxt'
save_model = 'example/MobileNetSSD_deploy.caffemodel'
结果:执行python merge_bn.py ,在example下生成MobileNetSSD_deploy.caffemodel
测试训练结果
1.测试精度
修改MobileNet-SSD-master/solver_test.prototxt文件内容见下:
train_net: "example/MobileNetSSD_train.prototxt"
test_net: "example/MobileNetSSD_test.prototxt"
执行:sh test.sh
执行结果:
I1201 16:41:51.978435 32000 solver.cpp:332] Iteration 0, loss = 4.1392
I1201 16:41:51.978489 32000 solver.cpp:433] Iteration 0, Testing net (#0)
I1201 16:41:51.985108 32000 net.cpp:693] Ignoring source layer mbox_loss
I1201 16:42:12.844115 32000 blocking_queue.cpp:50] Data layer prefetch queue empty
I1201 16:44:03.012107 32000 solver.cpp:546] Test net output #0: detection_eval = 0.211737
I1201 16:44:03.014663 32000 solver.cpp:337] Optimization Done.
I1201 16:44:03.014703 32000 caffe.cpp:254] Optimization Done.
2.测试文件夹中的图片python demo.py
caffe_root = '/home/gjw/caffe-ssd/'
net_file= 'example/MobileNetSSD_deploy.prototxt' #必须用相对路径,不能使用绝对路径
caffe_model='example/MobileNetSSD_deploy.caffemodel' #必须用相对路径,不能使用绝对路径
CLASSES = ('background',
'open', 'close', 'yawn', 'smoke')
3.测试视频 python demo_webcam.py
import numpy as np
import sys,os
import cv2
caffe_root = '/home/gjw/caffe-ssd/'
sys.path.insert(0, caffe_root + 'python')
import caffe
net_file= 'example/MobileNetSSD_deploy.prototxt'
caffe_model='example/MobileNetSSD_deploy.caffemodel'
test_dir = "images"
if not os.path.exists(caffe_model):
print("MobileNetSSD_deploy.caffemodel does not exist,")
print("use merge_bn.py to generate it.")
exit()
net = caffe.Net(net_file,caffe_model,caffe.TEST)
CLASSES = ('background',
'open', 'close', 'yawn', 'smoke')
def preprocess(src):
img = cv2.resize(src, (300,300))
img = img - 127.5
img = img * 0.007843
return img
def postprocess(img, out):
h = img.shape[0]
w = img.shape[1]
box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])
cls = out['detection_out'][0,0,:,1]
conf = out['detection_out'][0,0,:,2]
return (box.astype(np.int32), conf, cls)
def detect(origimg):
img = preprocess(origimg)
img = img.astype(np.float32)
img = img.transpose((2, 0, 1))
net.blobs['data'].data[...] = img
out = net.forward()
box, conf, cls = postprocess(origimg, out)
for i in range(len(box)):
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(origimg, p1, p2, (0,255,0))
p3 = (max(p1[0], 15), max(p1[1], 15))
title = "%s:%.2f" % (CLASSES[int(cls[i])], conf[i])
cv2.putText(origimg, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 1)
cv2.imshow("SSD", origimg)
k = cv2.waitKey(1) & 0xff
#Exit if ESC pressed
if k == 27 : return False
return True
##############################################
cap=cv2.VideoCapture(0)
while(1): # get a frame
ret, frame = cap.read() # show a frame
detect(frame)
cap.release()
cv2.destroyAllWindows()
如果数据集里面不完全是RGB三通道的,还要修改train.prototxt和test.prototxt文件:
transform_param {
scale: 0.007843
force_color: true ###添加
mean_value: 127.5
mean_value: 127.5
mean_value: 127.5
}