convert_model


Load necessary libs and set up caffe and caffe_root


In [1]:
from __future__ import print_function
import numpy as np
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:
import os
os.chdir('..')
caffe_root = './'
import sys
sys.path.insert(0, caffe_root + 'python')

import caffe
caffe.set_device(0)
caffe.set_mode_gpu()

coco_net = caffe.Net(caffe_root + 'models/VGGNet/coco/SSD_512x512/deploy.prototxt',
                     caffe_root + 'models/VGGNet/coco/SSD_512x512/VGG_coco_SSD_512x512_iter_240000.caffemodel',
                     caffe.TEST)

voc_net = caffe.Net(caffe_root + 'models/VGGNet/VOC0712/SSD_512x512/deploy.prototxt',
                    caffe_root + 'models/VGGNet/VOC0712/SSD_512x512/VGG_VOC0712_SSD_512x512_iter_60000.caffemodel',
                    caffe.TEST)

Set Caffe to CPU mode, load the net in the test phase for inference, and configure input preprocessing.


In [2]:
from google.protobuf import text_format
from caffe.proto import caffe_pb2

# load MS COCO model specs
file = open(caffe_root + 'models/VGGNet/coco/SSD_512x512/deploy.prototxt', 'r')
coco_netspec = caffe_pb2.NetParameter()
text_format.Merge(str(file.read()), coco_netspec)

# load MS COCO labels
coco_labelmap_file = caffe_root + 'data/coco/labelmap_coco.prototxt'
file = open(coco_labelmap_file, 'r')
coco_labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), coco_labelmap)

# load PASCAL VOC model specs
file = open(caffe_root + 'models/VGGNet/VOC0712/SSD_512x512/deploy.prototxt', 'r')
voc_netspec = caffe_pb2.NetParameter()
text_format.Merge(str(file.read()), voc_netspec)

# load PASCAL VOC labels
voc_labelmap_file = caffe_root + 'data/VOC0712/labelmap_voc.prototxt'
file = open(voc_labelmap_file, 'r')
voc_labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), voc_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

In [3]:
# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': coco_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

Load an image.


In [4]:
image = caffe.io.load_image(caffe_root + 'examples/images/fish-bike.jpg')
transformed_image = transformer.preprocess('data', image)

# set net to batch size of 1
coco_net.blobs['data'].reshape(1,3,512,512)
voc_net.blobs['data'].reshape(1,3,512,512)

# resizes the image to the right size, applies transformation etc. 
coco_net.blobs['data'].data[...] = transformed_image
voc_net.blobs['data'].data[...] = transformed_image

orig_image = transformer.deprocess('data', coco_net.blobs['data'].data)

Top5 detections using coco model.


In [5]:
detections = coco_net.forward()['detection_out']

# parse the output
det_label = detections[0,0,:,1]
det_conf = detections[0,0,:,2]
#print det_conf
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 topN detections
top_k = 5
topk_indexes = det_conf.argsort()[::-1][:top_k]

top_conf = det_conf[topk_indexes]
top_label_indexes = det_label[topk_indexes]
top_labels = get_labelname(coco_labelmap, top_label_indexes.tolist())

top_xmin = det_xmin[topk_indexes]
top_ymin = det_ymin[topk_indexes]
top_xmax = det_xmax[topk_indexes]
top_ymax = det_ymax[topk_indexes]

plot_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

plt.imshow(orig_image)
currentAxis = plt.gca()

for i in xrange(top_conf.shape[0]):
    xmin = int(round(top_xmin[i] * 512.0))
    ymin = int(round(top_ymin[i] * 512.0))
    xmax = int(round(top_xmax[i] * 512.0))
    ymax = int(round(top_ymax[i] * 512.0))
    score = top_conf[i]
    label = top_labels[i]
    name = '%s: %.2f'%(label, score)
    coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
    currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=plot_colors[i % len(plot_colors)] , linewidth=2))
    currentAxis.text(xmin, ymin, name, bbox={'facecolor':'white', 'alpha':0.5})


Top5 detections using voc model.


In [6]:
detections = voc_net.forward()['detection_out']

# parse the output
det_label = detections[0,0,:,1]
det_conf = detections[0,0,:,2]
#print det_conf
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 topN detections
top_k = 5
topk_indexes = det_conf.argsort()[::-1][:top_k]

top_conf = det_conf[topk_indexes]
top_label_indexes = det_label[topk_indexes]
top_labels = get_labelname(voc_labelmap, top_label_indexes.tolist())

top_xmin = det_xmin[topk_indexes]
top_ymin = det_ymin[topk_indexes]
top_xmax = det_xmax[topk_indexes]
top_ymax = det_ymax[topk_indexes]

plot_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

plt.imshow(orig_image)
currentAxis = plt.gca()

for i in xrange(top_conf.shape[0]):
    xmin = int(round(top_xmin[i] * 512.0))
    ymin = int(round(top_ymin[i] * 512.0))
    xmax = int(round(top_xmax[i] * 512.0))
    ymax = int(round(top_ymax[i] * 512.0))
    score = top_conf[i]
    label = top_labels[i]
    name = '%s: %.2f'%(label, score)
    coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
    currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=plot_colors[i % len(plot_colors)] , linewidth=2))
    currentAxis.text(xmin, ymin, name, bbox={'facecolor':'white', 'alpha':0.5})