利用SSD和自己训练好的模型进行目标检测

来源:互联网 发布:arctime字幕软件官方 编辑:程序博客网 时间:2024/06/05 19:45
本文翻译自:/caffe-ssd/examples/ssd_detect.ipynb
首先怎么安装jupyter以及使用jupyter
安装:sudo pip install jupyter
使用:到/caffe-ssd/examples目录下:输入:jupyter notebook
点击ssd_detect.ipynb,如下图:

利用SSD进行目标检测:1:加载必要的库以及设置caffe的路径import numpy as npimport matplotlib.pyplot as plt#设置plt的属性plt.rcParams['figure.figsize'] = (10, 10) plt.rcParams['image.interpolation'] = 'nearest'plt.rcParams['image.cmap'] = 'gray'#设置路径caffe_root = '/home/amax/XIAOVV/caffe-ssd'  #python的上一层目录import osos.chdir(caffe_root) #os.chdir() 方法用于改变当前工作目录到指定的路径。import syssys.path.insert(0, 'python')import caffecaffe.set_device(2)  #设置GPU为第3块GPUcaffe.set_mode_gpu()2.加载标签文件from google.protobuf import text_formatfrom caffe.proto import caffe_pb2labelmap_file = 'data/VOC5100/labelmap_voc.prototxt'“““我的labelmap_voc文件如下:item {  name: "none_of_the_above"  label: 0  display_name: "background"}item {  name: "aeroplane"  label: 1  display_name: "aeroplane"}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 labelnames3.加载测试网络文件和训练好的权重文件,以及对输入图像进行预处理#加载网络model_def = 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'model_weights = 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_60000.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],输入图像像素在0-255之间,调整为0-1之间transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB  修改RGB对应关系4.SSD目标检测#加载图像# set net to batch size of 1image_resize = 320net.blobs['data'].reshape(1,3,image_resize,image_resize)image = caffe.io.load_image('examples/images/1_11_15_plain.jpg')plt.imshow(image)

transformed_image = transformer.preprocess('data', image)net.blobs['data'].data[...] = transformed_image  #把输入的图像加载到网络的data层# 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.5. 找到置信度大于或等于0.5的检测结果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})plt.savefig(os.path.join('/home/amax/Desktop/Code', 'images', '11111.jpg'))plt.show()



原创粉丝点击