-
Notifications
You must be signed in to change notification settings - Fork 392
/
refinedet_demo.py
120 lines (102 loc) · 4.29 KB
/
refinedet_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
In this example, we will load a RefineDet model and use it to detect objects.
'''
import argparse
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import skimage.io as io
# Make sure that caffe is on the python path:
caffe_root = './'
os.chdir(caffe_root)
sys.path.insert(0, os.path.join(caffe_root, 'python'))
import caffe
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 ShowResults(img, image_file, results, labelmap, threshold=0.6, save_fig=False):
plt.clf()
plt.imshow(img)
plt.axis('off')
ax = plt.gca()
num_classes = len(labelmap.item) - 1
colors = plt.cm.hsv(np.linspace(0, 1, num_classes)).tolist()
for i in range(0, results.shape[0]):
score = results[i, -2]
if threshold and score < threshold:
continue
label = int(results[i, -1])
name = get_labelname(labelmap, label)[0]
color = colors[label % num_classes]
xmin = int(round(results[i, 0]))
ymin = int(round(results[i, 1]))
xmax = int(round(results[i, 2]))
ymax = int(round(results[i, 3]))
coords = (xmin, ymin), xmax - xmin, ymax - ymin
ax.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=3))
display_text = '%s: %.2f' % (name, score)
ax.text(xmin, ymin, display_text, bbox={'facecolor':color, 'alpha':0.5})
if save_fig:
plt.savefig(image_file[:-4] + '_dets.jpg', bbox_inches="tight")
print('Saved: ' + image_file[:-4] + '_dets.jpg')
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu_id', type=int, default=0)
parser.add_argument('--save_fig', action='store_true')
args = parser.parse_args()
# gpu preparation
if args.gpu_id >= 0:
caffe.set_device(args.gpu_id)
caffe.set_mode_gpu()
# load labelmap
labelmap_file = 'data/VOC0712/labelmap_voc.prototxt'
file = open(labelmap_file, 'r')
labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), labelmap)
# load model
model_def = 'models/VGGNet/VOC0712/refinedet_vgg16_320x320/deploy.prototxt'
model_weights = 'models/VGGNet/VOC0712/refinedet_vgg16_320x320/VOC0712_refinedet_vgg16_320x320_final.caffemodel'
net = caffe.Net(model_def, model_weights, caffe.TEST)
# image preprocessing
if '320' in model_def:
img_resize = 320
else:
img_resize = 512
net.blobs['data'].reshape(1, 3, img_resize, img_resize)
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
# im_names = os.listdir('examples/images')
im_names = ['000456.jpg', '000542.jpg', '001150.jpg', '001763.jpg', '004545.jpg']
for im_name in im_names:
image_file = 'examples/images/' + im_name
image = caffe.io.load_image(image_file)
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
detections = net.forward()['detection_out']
det_label = detections[0, 0, :, 1]
det_conf = detections[0, 0, :, 2]
det_xmin = detections[0, 0, :, 3] * image.shape[1]
det_ymin = detections[0, 0, :, 4] * image.shape[0]
det_xmax = detections[0, 0, :, 5] * image.shape[1]
det_ymax = detections[0, 0, :, 6] * image.shape[0]
result = np.column_stack([det_xmin, det_ymin, det_xmax, det_ymax, det_conf, det_label])
# show result
ShowResults(image, image_file, result, labelmap, 0.6, save_fig=args.save_fig)