-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_mobilenet_ssd_300.py
318 lines (248 loc) · 11.9 KB
/
test_mobilenet_ssd_300.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import mxnet as mx
import nnvm
import tvm
import numpy as np
import cv2
from PIL import Image
from tvm.contrib import graph_runtime
import nnvm.testing
import nnvm.compiler
import topi
import math
import sys, os, time
import xml_parser
import argparse
from timeit import default_timer as timer
ssd_model = 'PycharmProjects/ssd/ssd_models/mobilenet/ssd_mxnet/deploy_ssd_mobilenet_300_fromcaffe_no_detection' # input ssd model here
shape = 300
checkpoint = 0
target = 'opencl'
ctx = tvm.context(target, 0)
dshape = (1, 3, shape, shape)
dtype = 'float32'
threshold = 0.01
num_anchor=1917 #8766 #8732
class_names = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"]
input_folder = '/home/prdcv/Desktop/darknet/test_voc_2007/VOCdevkit/VOC2007/JPEGImages'
DEBUG_MODE = False
ROUND_DECIMAL = 3
if DEBUG_MODE is True:
debug_file = open("Detail_detection_time_" + str(time.time()) + ".csv", "w")
header = "total_detection_time, set_data_input_t, get_output_time, multibox_time, mulibox_detection_time\n"
debug_file.write(header)
# (SIZE, RATIOS, STEP)
default_shape = ((1, 512, 38, 38), (1, 1024, 19, 19), (1, 512, 10, 10),
(1, 256, 5, 5), (1, 256, 3, 3), (1, 256, 1, 1))
# end params
# @tvm.register_func
# def tvm_callback_cuda_compile(code):
# ptx = nvcc.compile_cuda(code, target="ptx")
# return ptx
def transform_image(image):
image = np.array(image) - np.array([127.5, 127.5, 127.5])
image = image.transpose((2, 0, 1))
image = image[np.newaxis, :]
return image
def transform_image_300(image):
img = np.array(image) - np.array([123., 117., 104.])
img = img * 0.007843
img = img.astype(np.float32)
img = img.transpose((2, 0, 1))
img = img[np.newaxis, :]
return img
def display_plt(img, out, thresh=0.5):
import random
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = (10, 10)
pens = dict()
plt.clf()
plt.imshow(img)
for det in out:
cid = int(det[0])
if cid < 0:
continue
score = det[1]
if score < thresh:
continue
if cid not in pens:
pens[cid] = (random.random(), random.random(), random.random())
scales = [img.shape[1], img.shape[0]] * 2
xmin, ymin, xmax, ymax = [int(p * s) for p, s in zip(det[2:6].tolist(), scales)]
rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False,
edgecolor=pens[cid], linewidth=3)
plt.gca().add_patch(rect)
text = class_names[cid]
plt.gca().text(xmin, ymin - 2, '{:s} {:.3f}'.format(text, score),
bbox=dict(facecolor=pens[cid], alpha=0.5),
fontsize=12, color='white')
plt.show(block=False)
plt.pause(0.0001)
def get_multibox_detection_output(np_cls_prob, np_loc_preds, np_anchors, batch_size, num_anchors, num_classes):
import nnvm.symbol as sym
cls_prob = sym.Variable("cls_prob")
loc_preds = sym.Variable("loc_preds")
anchors = sym.Variable("anchors")
transform_loc_data, valid_count = sym.multibox_transform_loc(cls_prob=cls_prob, loc_pred=loc_preds,
anchor=anchors)
out = sym.nms(data=transform_loc_data, valid_count=valid_count)
target = "llvm"
dtype = "float32"
ctx = tvm.cpu()
graph, lib, _ = nnvm.compiler.build(out, target, {"cls_prob": (batch_size, num_classes, num_anchors),
"loc_preds": (batch_size, num_anchors * 4),
"anchors": (1, num_anchors, 4)})
m = graph_runtime.create(graph, lib, ctx)
m.set_input(**{"cls_prob": np_cls_prob, "loc_preds": np_loc_preds, "anchors": np_anchors})
m.run()
_, out_shape = nnvm.compiler.graph_util.infer_shape(graph, shape={"data": dshape})
out = m.get_output(0,tvm.nd.empty(tuple(out_shape[0]), dtype)) # output of "mbox_conf_softmax", shape: (1, 21, 8732)
return out
def get_multibox_detection_output_tvm(np_cls_prob, np_loc_preds, np_anchors, batch_size, num_anchors, num_classes):
target_cpu = 'llvm'
ctx = tvm.cpu()
cls_prob = tvm.placeholder((1, 21, num_anchors), name="cls_prob")
loc_preds = tvm.placeholder((1, num_anchors * 4), name="loc_preds")
anchors = tvm.placeholder((1, num_anchors, 4), name="anchors")
tvm_cls_prob = tvm.nd.array(np_cls_prob.asnumpy().astype(cls_prob.dtype), ctx)
tvm_loc_preds = tvm.nd.array(np_loc_preds.asnumpy().astype(loc_preds.dtype), ctx)
tvm_anchors = tvm.nd.array(np_anchors.asnumpy().astype(anchors.dtype), ctx)
with tvm.target.create(target_cpu):
out = topi.vision.ssd.multibox_detection(cls_prob, loc_preds, anchors, clip=False, threshold=0.01,
nms_threshold=0.45,
force_suppress=False, variances=(0.1, 0.1, 0.2, 0.2), nms_topk=400)
s = topi.generic.schedule_multibox_detection(out)
tvm_out = tvm.nd.array(np.zeros((1, num_anchors, 6)).astype(out.dtype), ctx)
f = tvm.build(s, [cls_prob, loc_preds, anchors, out], 'llvm')
f(tvm_cls_prob, tvm_loc_preds, tvm_anchors, tvm_out)
return tvm_out
def get_prior_output(input_data, f, oshape, ctx_multi):
tvm_input_data = tvm.nd.array(input_data, ctx_multi)
tvm_out = tvm.nd.array(np.zeros(oshape, dtype=dtype), ctx_multi)
f(tvm_input_data, tvm_out)
return tvm_out
def get_argument():
parser = argparse.ArgumentParser()
parser.add_argument('-imp', '--image_path', help="Image path")
parser.add_argument('-op', '--out_path', help="XML output path")
parser.add_argument('-oi', '--out_image', help="Detected images result folder")
args = parser.parse_args()
xml_result_folder = ""
image_output_folder = ""
OUT_IMAGE = False
image_path = '/home/prdcv/Desktop/darknet/test_voc_2007/VOCdevkit/VOC2007/JPEGImages'
if args.out_path is None:
xml_folder_name = ssd_model.split("/")[-1] + "_xml"
xml_result_folder = ssd_model.replace(ssd_model.split("/")[-1], xml_folder_name)
else:
xml_result_folder = args.out_path
if os.path.exists(xml_result_folder) is False:
os.mkdir(xml_result_folder)
if os.path.exists(image_path) is False:
print "Please check images path! It was not existed"
sys.exit()
if args.out_image is None:
OUT_IMAGE = False
else:
OUT_IMAGE = True
image_output_folder = args.out_image
if os.path.exists(image_output_folder) is False:
os.mkdir(image_output_folder)
return xml_result_folder, image_path, image_output_folder, OUT_IMAGE
def load_image_data(image_name):
# using opencv for the convinence
img = cv2.imread(image_name)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_transform = transform_image_300(cv2.resize(img, (shape, shape)))
# shape_dict = {'data': img_transform.shape}
return img, img_transform
def detect_object(nnvm_sym, nnvm_params, img_transform, m, tvm_output):
set_data_input_t = time.time()
m.set_input('data', tvm.nd.array(img_transform.astype(dtype)))
m.run()
set_data_input_time = round(time.time() - set_data_input_t, ROUND_DECIMAL)
#print('set_data_input_time: ' + str(set_data_input_time))
getout_t = time.time()
tvm_output_0 = m.get_output(0, tvm.nd.empty(tuple(tvm_output[0]),'float32')) # output of "mbox_conf_softmax", shape: (1, 21, 8732)
tvm_output_1 = m.get_output(1, tvm.nd.empty(tuple(tvm_output[1]),'float32')) # output of "mbox_loc" layer, shape: (1, 34928)
tvm_output_2 = m.get_output(2, tvm.nd.empty(tuple(tvm_output[2]),'float32')) # output of "broadcast_mul0" layer, shape: (1, 512, 38, 38)
getout_time = round(time.time() - getout_t, 6)
#print('getout_time: ' + str(getout_time))
get_mul_detection_t = time.time()
final_output = get_multibox_detection_output_tvm(tvm_output_0, tvm_output_1, tvm_output_2, 1, num_anchor, 21)
get_mul_detection_time = round(time.time() - get_mul_detection_t, ROUND_DECIMAL)
#print('get_mul_detection_time: ' + str(get_mul_detection_time))
detect_time = round(set_data_input_time + getout_time + get_mul_detection_time, ROUND_DECIMAL)
print("run time: "+str(round(set_data_input_time,5))+", getout_time: "+str(round(getout_time,5))+", detection_time: "+ str(round(get_mul_detection_time,5))+", total time: "+str(round(detect_time,5)))
#print('detect_time: ' + str(detect_time))
if DEBUG_MODE is True:
text = str(detect_time) + "," + str(set_data_input_time) + "," + \
str(getout_time) + "," + str(get_mul_detection_time) + "\n"
debug_file.write(text)
return detect_time, final_output
def isImage(filename):
isImg = filename.endswith('.png') or filename.endswith('.jpg')
return isImg
def get_det_value_output(img, raw_det_result, thresh):
res = []
for det in raw_det_result:
cid = int(det[0])
if cid < 0:
continue
score = det[1]
if score < thresh:
continue
scales = [img.shape[1], img.shape[0]] * 2
xmin, ymin, xmax, ymax = [int(p * s) for p, s in zip(det[2:6].tolist(), scales)]
text = class_names[cid]
res.append((cid, score, (xmin, ymin, xmax, ymax)))
res = sorted(res, key=lambda x: -x[1])
return res
if __name__ == "__main__":
xml_result_folder, image_path, image_output_folder, OUT_IMAGE = get_argument()
mx_sym, args, auxs = mx.model.load_checkpoint(ssd_model, checkpoint)
nnvm_sym, nnvm_params = nnvm.frontend.from_mxnet(mx_sym, args, auxs)
print('model compiled.')
#mx.contrib.quantization.quantize_model()
ctx = tvm.context(target, 0)
shape_dict = {'data': dshape}
with nnvm.compiler.build_config(opt_level=1):
graph, lib, params = nnvm.compiler.build(nnvm_sym, target, shape_dict, params=nnvm_params)
m_graph = graph_runtime.create(graph, lib, ctx)
m_graph.set_input(**params)
_, outshape = nnvm.compiler.graph_util.infer_shape(graph, shape={"data": dshape})
tvm_ouput_init = []
for i in range(0, len(outshape)):
tvm_ouput_init.append(tvm.nd.empty(tuple(outshape[i]), dtype))
voc_name = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable',
'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
class_file = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
for f in os.listdir(image_path):
image_name = os.path.join(image_path, f)
#print f
if os.path.isfile(image_name) and isImage(image_name):
im_load_start_t = time.time()
image, img_transform = load_image_data(image_name)
if image is None or img_transform is None:
break
load_img_time = time.time() - im_load_start_t
detect_time, final_output = detect_object(nnvm_sym, nnvm_params, img_transform, m_graph, outshape)
res = get_det_value_output(image, final_output.asnumpy()[0], thresh=threshold)
width=image.shape[1]
height = image.shape[0]
for resu in res:
prob=round(resu[1],4)
left = max(0, resu[2][0])
top = max(0, resu[2][1])
right = min(width, resu[2][2])
bot = min(height, resu[2][3])
line = f.replace('.jpg', '') + " " + str(prob) + " " + str(left) + " " + str(top) + " " + str(right) + " " + str(bot) + '\n'
class_file[resu[0]] = class_file[resu[0]] + line
for i in range(20):
file = open(output_folder + 'comp4_det_test_' + voc_name[i]+'.txt', 'w')
file.write(class_file[i])
file.close()
if DEBUG_MODE is True:
debug_file.close()