SSD实践篇

源项目https://github.com/conner99/caffe
环境
windows7 + vs2013 + Cuda7.5
caffe: caffe-ssd-microsoft
Python: 2.7

之前的准备工作http://blog.csdn.net/run_it_faraway/article/details/76855639

下面讲讲怎么检测高分辨率图像中飞机的轮子
问题:由于SSD的性能问题,单次检测分辨率较大的图无法获取细节的特征,例如:高清图像下飞机的轮子之类的
解决方法:通过先验得知,轮子肯定在飞机上;通过SSD检测飞机,将带有飞机的局部信息图片提取出来,再通过SSD检测飞机的轮子,这就避免了提取细节的问题

具体代码:
SSD\Python_code\ssd_image_airplane.py #检测飞机
SSD\Python_code\ssd_image_wheel.py #检测飞机的轮子
SSD\Python_code\ssd_image_airplane_wheel.py #先检测飞机,再检测轮子

首先由之前叙述的方法得到单独检测飞机和轮子的caffemodel

训练检测飞机使用的数据集为1920X1080的带有标签的高分辨率图像
训练检测轮子使用的数据集为375X500的带标签的低分辨率图像

测试代码如下:
ssd_image_airplane.py #单独检测飞机

# -*- coding:utf8 -*-
import numpy as np
import pylab
import matplotlib.pyplot as plt
#%matplotlib inline

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 = 'F:/SSD/caffe-ssd-microsoft/'  # caffe根目录
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, caffe_root + 'Build/x64/Release/pycaffe')
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()

from google.protobuf import text_format
from caffe.proto import caffe_pb2

# load PASCAL VOC labels
labelmap_file = 'data/VOC0712/labelmap_voc_airplane.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 = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
#model_weights = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_60000.caffemodel'
model_weights = 'models/VGGNet/VOC0712/SSD_300x300/models/2_airplane/VGG_VOC0712_SSD_300x300_airplane_iter_48000.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

# 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('examples/images/000120.jpg')#测试图片
plt.imshow(image)


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.5]

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]


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})

pylab.show()

检测效果
这里写图片描述

另一种飞机(不过训练时只归为一类,标签都为airplane)
这里写图片描述

ssd_image_airplane.py #单独检测飞机轮子

# -*- coding:utf8 -*-
import numpy as np
import pylab
import matplotlib.pyplot as plt
#%matplotlib inline

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 = 'F:/SSD/caffe-ssd-microsoft/'  # this file is expected to be in {caffe_root}/examples
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, caffe_root + 'Build/x64/Release/pycaffe')
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()

from google.protobuf import text_format
from caffe.proto import caffe_pb2

# load PASCAL VOC labels
labelmap_file = 'data/VOC0712/labelmap_voc_wheel.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 = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
#model_weights = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_60000.caffemodel'
model_weights = 'models/VGGNet/VOC0712/SSD_300x300/models/2_wheel/VGG_VOC0712_SSD_300x300_wheel_iter_40000.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

# 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('examples/images/000015.jpg')
image = caffe.io.load_image('examples/images/crop.jpg')#测试图片

plt.imshow(image)


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.5]

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]


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})

pylab.show()

代码中测试图片examples/images/crop.jpg为ssd_image_airplane.py中检测出飞机的区域
在代码ssd_image_airplane_wheel.py中有保存
运行结果
这里写图片描述

这里写图片描述

ssd_image_airplane_wheel.py #检测高分辨率图像中飞机的轮子

# -*- coding:utf8 -*-
import numpy as np
from PIL import Image
import pylab
import matplotlib.pyplot as plt
#%matplotlib inline

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 = 'F:/SSD/caffe-ssd-microsoft/'  # this file is expected to be in {caffe_root}/examples
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, caffe_root + 'Build/x64/Release/pycaffe')
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
import cv2

from google.protobuf import text_format
from caffe.proto import caffe_pb2

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


def demo(net,image_name):
    im = caffe.io.load_image('examples/images/{}'.format(image_name))
    # 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

    # set net to batch size of 1
    image_resize = 300
    net.blobs['data'].reshape(1,3,image_resize,image_resize)

    transformed_image = transformer.preprocess('data', im)
    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.5]

    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]


    colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()

    currentAxis = plt.gca()


    for i in xrange(top_conf.shape[0]):
        xmin = int(round(top_xmin[i] * im.shape[1]))
        ymin = int(round(top_ymin[i] * im.shape[0]))
        xmax = int(round(top_xmax[i] * im.shape[1]))
        ymax = int(round(top_ymax[i] * im.shape[0]))

        if flag==0:
            image1=caffe.io.load_image('examples/images/{}'.format(im_name))
            plt.imshow(image1)
            global  wheel_region
            score = top_conf[i]
            label = int(top_label_indices[i])
            label_name = top_labels[i]
            display_txt = '%s: %.2f'%(label_name, score)
            wheel_region= (airplane_region[0]+xmin,airplane_region[1]+ymin,airplane_region[0]+xmax,airplane_region[1]+ymax)
            coords = (wheel_region[0], wheel_region[1]), wheel_region[2]-wheel_region[0]+1, wheel_region[3]-wheel_region[1]+1
            color = colors[label]
            currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))

#            currentAxis.text(wheel_region[0], wheel_region[1], display_txt, bbox={'facecolor':color, 'alpha':0.5})
        if flag==1:
            global  airplane_region
            airplane_region=(xmin,ymin,xmax,ymax)
            img=Image.open('examples/images/{}'.format(image_name))
            cropimg=img.crop(airplane_region)
            cropimg.save(r'examples/images/crop.jpg')
            global flag
            flag =0


if __name__ == '__main__':
    # load PASCAL VOC labels
    flag=1
    airplane_region=(0,0,0,0)
    wheel_region=(0,0,0,0)
    labelmap_file = 'data/VOC0712/labelmap_voc_wheel.prototxt'
#    labelmap_file2 = 'data/VOC0712/labelmap_voc_wheel.prototxt'
    file = open(labelmap_file, 'r')
#    file2 = open(labelmap_file2, 'r')
    labelmap = caffe_pb2.LabelMap()
    text_format.Merge(str(file.read()), labelmap)
#    text_format.Merge(str(file2.read()), labelmap)
    model_def = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
    #model_weights = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_60000.caffemodel'
    model_weights = 'models/VGGNet/VOC0712/SSD_300x300/models/2_airplane/VGG_VOC0712_SSD_300x300_airplane_iter_48000.caffemodel'
    model_weights2 = 'models/VGGNet/VOC0712/SSD_300x300/models/2_wheel/VGG_VOC0712_SSD_300x300_wheel_iter_46000.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)
    net2 = caffe.Net(model_def,      # defines the structure of the model
                    model_weights2,  # contains the trained weights
                    caffe.TEST)     # use test mode (e.g., don't perform dropout)



    im_names = ['000120.jpg'] #测试图片,位置caffe_root\examples\images\000120.jpg

    for im_name in im_names:
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print 'Demo for {}'.format(im_name)
        demo(net, im_name)
        im_names2 = 'crop.jpg'
        demo(net2, im_names2)


    plt.show()

运行结果
这里写图片描述

这里写图片描述
未完待续。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值