-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_write_results_yolov_3and5.py
More file actions
427 lines (352 loc) · 18.7 KB
/
test_write_results_yolov_3and5.py
File metadata and controls
427 lines (352 loc) · 18.7 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# command to generate report with
# python3 /opt/intel/openvino_2020.4.287/deployment_tools/tools/benchmark_tool/benchmark_app.py \
# --path_to_model ~/projects/nuc_format/inference-demo/exported-models/tf2oda_efficientdetd0_512x384_pedestrian_LR02/saved_model/saved_model.xml \
# --report_type average_counters -niter 10 -d MYRIAD -nireq 1
# Data Formats
# Date Model Model_Short Framework Network Resolution Dataset Custom_Parameters Hardware Hardware_Optimization DetectionBoxes_Precision/mAP DetectionBoxes_Precision/mAP@.50IOU DetectionBoxes_Precision/mAP@.75IOU DetectionBoxes_Precision/mAP (small) DetectionBoxes_Precision/mAP (medium) DetectionBoxes_Precision/mAP (large) DetectionBoxes_Recall/AR@1 DetectionBoxes_Recall/AR@10 DetectionBoxes_Recall/AR@100 DetectionBoxes_Recall/AR@100 (small) DetectionBoxes_Recall/AR@100 (medium) DetectionBoxes_Recall/AR@100 (large)
# Date Model Model_Short Framework Network Resolution Dataset Custom_Parameters Hardware Hardware_Optimization Batch_Size Throughput Mean_Latency Latencies
# EXAMPLE USAGE - the following command extracts infos from reports and parses them into a new file
# python3 openvino_latency_parser.py g_--avrep tf_inceptionv1_224x224_imagenet_3.16G_avg_cnt_rep.csv --inf_rep tf_inceptionv1_224x224_imagenet_3.16G.csv --save_new
#EXAMPLE USAGE - the following command extracts infos from reports and appends them to a new line of the existing_file csv
# python3 openvino_latency_parser.py --avg_rep tf_inceptionv1_224x224_imagenet_3.16G_avg_cnt_rep.csv --inf_rep tf_inceptionv1_224x224_imagenet_3.16G.csv --existing_file latency_tf_inceptionv1_224x224_imagenet_3.16G.csv
License_info:
# ==============================================================================
# ISC License (ISC)
# Copyright 2020 Christian Doppler Laboratory for Embedded Machine Learning
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
"""
# TODO: update example usage
# Futures
from __future__ import print_function
# Built-in/Generic Imports
import sys, os, json, argparse
import logging
import time
# Libs
import numpy as np
import pandas as pd
from openvino.inference_engine import IECore
import cv2
# Own modules
import helpers_yolov3and5 as helper
__author__ = "Matvey Ivanov"
__copyright__ = (
"Copyright 2021, Christian Doppler Laboratory for " "Embedded Machine Learning"
)
__credits__ = ["Matvey Ivanov"]
__license__ = "ISC"
__version__ = "0.1.0"
__maintainer__ = "Matvey Ivanov"
__email__ = "matvey.ivanov@tuwien.ac.at"
__status__ = "Experimental"
logging.basicConfig(format="[ %(levelname)s ] %(message)s", level=logging.INFO, stream=sys.stdout)
log = logging.getLogger()
class YoloParams:
# ------------------------------------------- Extracting layer parameters ------------------------------------------
# Magic numbers are copied from yolo samples
def __init__(self, side):
self.num = 3 #if 'num' not in param else int(param['num'])
self.coords = 4 #if 'coords' not in param else int(param['coords'])
self.classes = 80 #if 'classes' not in param else int(param['classes'])
self.side = side
self.anchors = [10.0, 13.0, 16.0, 30.0, 33.0, 23.0, 30.0, 61.0, 62.0, 45.0, 59.0, 119.0, 116.0, 90.0, 156.0,
198.0,
373.0, 326.0] #if 'anchors' not in param else [float(a) for a in param['anchors'].split(',')]
#self.isYoloV3 = False
#if param.get('mask'):
# mask = [int(idx) for idx in param['mask'].split(',')]
# self.num = len(mask)
# maskedAnchors = []
# for idx in mask:
# maskedAnchors += [self.anchors[idx * 2], self.anchors[idx * 2 + 1]]
# self.anchors = maskedAnchors
# self.isYoloV3 = True # Weak way to determine but the only one.
def log_params(self):
params_to_print = {'classes': self.classes, 'num': self.num, 'coords': self.coords, 'anchors': self.anchors}
[log.info(" {:8}: {}".format(param_name, param)) for param_name, param in params_to_print.items()]
def letterbox(img, size=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
shape = img.shape[:2] # current shape [height, width]
w, h = size
# Scale ratio (new / old)
r = min(h / shape[0], w / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = w - new_unpad[0], h - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (w, h)
ratio = w / shape[1], h / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
top2, bottom2, left2, right2 = 0, 0, 0, 0
if img.shape[0] != h:
top2 = (h - img.shape[0])//2
bottom2 = top2
img = cv2.copyMakeBorder(img, top2, bottom2, left2, right2, cv2.BORDER_CONSTANT, value=color) # add border
elif img.shape[1] != w:
left2 = (w - img.shape[1])//2
right2 = left2
img = cv2.copyMakeBorder(img, top2, bottom2, left2, right2, cv2.BORDER_CONSTANT, value=color) # add border
return img
def scale_bbox(x, y, height, width, class_id, confidence, im_h, im_w, resized_im_h=640, resized_im_w=640):
gain = min(resized_im_w / im_w, resized_im_h / im_h) # gain = old / new
pad = (resized_im_w - im_w * gain) / 2, (resized_im_h - im_h * gain) / 2 # wh padding
x = int((x - pad[0]) / gain)
y = int((y - pad[1]) / gain)
w = int(width / gain)
h = int(height / gain)
xmin = max(0, int(x - w / 2))
ymin = max(0, int(y - h / 2))
xmax = min(im_w, int(xmin + w))
ymax = min(im_h, int(ymin + h))
# Method item() used here to convert NumPy types to native types for compatibility with functions, which don't
# support Numpy types (e.g., cv2.rectangle doesn't support int64 in color parameter)
return dict(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, class_id=class_id.item(), confidence=confidence.item())
def parse_yolo_region(blob, resized_image_shape, original_im_shape, params, threshold):
# ------------------------------------------ Validating output parameters ------------------------------------------
out_blob_n, out_blob_c, out_blob_h, out_blob_w = blob.shape
predictions = 1.0 / (1.0 + np.exp(-blob))
assert out_blob_w == out_blob_h, "Invalid size of output blob. It sould be in NCHW layout and height should " \
"be equal to width. Current height = {}, current width = {}" \
"".format(out_blob_h, out_blob_w)
# ------------------------------------------ Extracting layer parameters -------------------------------------------
orig_im_h, orig_im_w = original_im_shape
resized_image_h, resized_image_w = resized_image_shape
objects = list()
side_square = params.side * params.side
# ------------------------------------------- Parsing YOLO Region output -------------------------------------------
bbox_size = int(out_blob_c / params.num) # 4+1+num_classes
for row, col, n in np.ndindex(params.side, params.side, params.num):
bbox = predictions[0, n * bbox_size:(n + 1) * bbox_size, row, col]
x, y, width, height, object_probability = bbox[:5]
class_probabilities = bbox[5:]
if object_probability < threshold:
continue
x = (2 * x - 0.5 + col) * (resized_image_w / out_blob_w)
y = (2 * y - 0.5 + row) * (resized_image_h / out_blob_h)
if int(resized_image_w / out_blob_w) == 8 & int(resized_image_h / out_blob_h) == 8: # 80x80,
idx = 0
elif int(resized_image_w / out_blob_w) == 16 & int(resized_image_h / out_blob_h) == 16: # 40x40
idx = 1
elif int(resized_image_w / out_blob_w) == 32 & int(resized_image_h / out_blob_h) == 32: # 20x20
idx = 2
width = (2 * width) ** 2 * params.anchors[idx * 6 + 2 * n]
height = (2 * height) ** 2 * params.anchors[idx * 6 + 2 * n + 1]
class_id = np.argmax(class_probabilities)
confidence = object_probability
objects.append(scale_bbox(x=x, y=y, height=height, width=width, class_id=class_id, confidence=confidence,
im_h=orig_im_h, im_w=orig_im_w, resized_im_h=resized_image_h,
resized_im_w=resized_image_w))
return objects
def intersection_over_union(box_1, box_2):
width_of_overlap_area = min(box_1['xmax'], box_2['xmax']) - max(box_1['xmin'], box_2['xmin'])
height_of_overlap_area = min(box_1['ymax'], box_2['ymax']) - max(box_1['ymin'], box_2['ymin'])
if width_of_overlap_area < 0 or height_of_overlap_area < 0:
area_of_overlap = 0
else:
area_of_overlap = width_of_overlap_area * height_of_overlap_area
box_1_area = (box_1['ymax'] - box_1['ymin']) * (box_1['xmax'] - box_1['xmin'])
box_2_area = (box_2['ymax'] - box_2['ymin']) * (box_2['xmax'] - box_2['xmin'])
area_of_union = box_1_area + box_2_area - area_of_overlap
if area_of_union == 0:
return 0
return area_of_overlap / area_of_union
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="NCS2 settings test")
parser.add_argument("-m", "--model_path", default="./model.xml", help="model to test with", type=str,required=False,)
parser.add_argument( "-i", "--image_dir", default="./images", help="images for the inference", type=str,required=False,)
parser.add_argument("-d", "--device", default="CPU", help="target device to run the inference [CPU, MYRIAD]",
type=str, required=False,)
parser.add_argument("-out", '--detections_out', default='detections.csv', help='Output file detections', type=str, required=False)
parser.add_argument("-is", '--input_source', default='tf2oda', help='Model Source Framework [onnx, tf2oda]', type=str, required=False)
parser.add_argument("-t", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
default=0.5, type=float)
parser.add_argument("-iout", "--iou_threshold", help="Optional. Intersection over union threshold for overlapping "
"detections filtering", default=0.4, type=float)
parser.add_argument("--labels", help="Optional. Labels mapping file", default=None, type=str)
parser.add_argument('--show', dest='show', action='store_true')
parser.add_argument('--no-show', dest='show', action='store_false')
parser.set_defaults(yolo=False)
args = parser.parse_args()
print(args)
if args.labels:
with open(args.labels, 'r') as f:
labels_map = [x.strip() for x in f]
else:
labels_map = None
if not os.path.isfile(args.model_path):
sys.exit("Invalid model xml given!")
model_xml = args.model_path
model_bin = args.model_path.split(".xml")[0] + ".bin"
if not os.path.isfile(model_xml) or not os.path.isfile(model_bin):
sys.exit("Could not find IR model for: " + model_xml)
ie = IECore()
net = ie.read_network(model=model_xml, weights=model_bin)
print("Loaded model: {}, weights: {}".format(model_xml, model_bin))
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
in_blob = net.input_info[input_blob].input_data.shape
net.input_info[input_blob].precision = "U8"
net.batch_size = 1
print("Loading network and perform on:", args.device)
exec_net = ie.load_network(network=net, device_name=args.device, num_requests=1)
combined_data = []
_, _, net_h, net_w = net.input_info[input_blob].input_data.shape
for filename in os.listdir(args.image_dir):
total_latency_start_time = time.time()
original_image = cv2.imread(os.path.join(args.image_dir, filename))
image = letterbox(original_image.copy(), (net_w, net_h))
if image.shape[:-1] != (net_h, net_w):
log.info(f"Image {args.image_dir} is resized from {image.shape[:-1]} to {(net_h, net_w)}")
image = cv2.resize(image, (net_w, net_h))
image = image.transpose((2, 0, 1))
image = np.expand_dims(image, axis=0)
print("\n\nStarting inference for picture:" + filename)
output = exec_net.infer(inputs={input_blob: image})
h, w, _ = original_image.shape
objects = list()
for layer_name, out_blob in exec_net.requests[0].output_blobs.items():
layer_params = YoloParams(side=out_blob.buffer.shape[2])
print("Layer {} parameters: ".format(layer_name))
layer_params.log_params()
objects += parse_yolo_region(out_blob.buffer, image.shape[2:],
# in_image.shape[2:], layer_params,
original_image.shape[:-1], layer_params,
args.prob_threshold)
# Filtering overlapping boxes with respect to the --iou_threshold CLI parameter
objects = sorted(objects, key=lambda obj: obj['confidence'], reverse=True)
for i in range(len(objects)):
if objects[i]['confidence'] == 0:
continue
for j in range(i + 1, len(objects)):
if intersection_over_union(objects[i], objects[j]) > args.iou_threshold:
objects[j]['confidence'] = 0
# Drawing objects with respect to the --prob_threshold CLI parameter
objects = [obj for obj in objects if obj['confidence'] >= args.prob_threshold]
print("\n********************THESE ARE THE RECOGNIZED OBJECTS********************\n")
for o in objects:
print(o)
# Save to detections file.
# Format:
# {'xmin': 442, 'xmax': 621, 'ymin': 91, 'ymax': 273, 'class_id': 0, 'confidence': 0.9272235631942749}
helper.save_detections_to_csv(o, filename, w, h, combined_data)
if args.show:
# show frame with bounding boxes
origin_im_size = original_image.shape[:-1]
for obj in objects:
# Validation bbox of detected object
if obj['xmax'] > origin_im_size[1] or obj['ymax'] > origin_im_size[0] or obj['xmin'] < 0 or obj[
'ymin'] < 0:
continue
color = (int(min(obj['class_id'] * 12.5, 255)),
min(obj['class_id'] * 7, 255), min(obj['class_id'] * 5, 255))
det_label = labels_map[obj['class_id']] if labels_map and len(labels_map) >= obj['class_id'] else \
str(obj['class_id'])
cv2.rectangle(original_image, (obj['xmin'], obj['ymin']), (obj['xmax'], obj['ymax']), color, 2)
cv2.putText(original_image,
"#" + det_label + ' ' + str(round(obj['confidence'] * 100, 1)) + ' %',
(obj['xmin'], obj['ymin'] - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
# uncomment the next line for better overview on a 1080p display
#cv2.imshow("DetectionResults", cv2.resize(original_image, dsize=(1500,1000)))
cv2.imshow("DetectionResults", original_image)
key = cv2.waitKey(0)
# ESC key
if key == 27:
break
# Create detections
dataframe = pd.DataFrame(
combined_data,
columns=[
"filename",
"width",
"height",
"class",
"xmin",
"ymin",
"xmax",
"ymax",
"score",
],
)
# Create output directories
os.makedirs(os.path.dirname(args.detections_out), exist_ok=True)
dataframe.to_csv(args.detections_out, index=False, sep=";") #
print("Written detections to ", args.detections_out)
"""
for i, detection in enumerate(detections):
combination_str = ""
if len(net.outputs) == 1:
_, class_id, confidence, xmin, ymin, xmax, ymax = detection
else:
class_id = labels[i]
xmin, ymin, xmax, ymax, confidence = detection
if confidence > 0.5:
label = int(class_id)
xmin = float(xmin)
ymin = float(ymin)
xmax = float(xmax)
ymax = float(ymax)
combined_data.append(
[
filename,
str(w),
str(h),
str(label),
str(xmin),
str(ymin),
str(xmax),
str(ymax),
str(confidence),
]
)
total_latency_stop_time = time.time()
total_latency = total_latency_stop_time - total_latency_start_time
print("Total latency for {} : {}s".format(filename, total_latency))
break
dataframe = pd.DataFrame(
combined_data,
columns=[
"filename",
"width",
"height",
"class",
"xmin",
"ymin",
"xmax",
"ymax",
"score",
],
)
# Warn if no detections
print(detections)
# Create output directories
if not os.path.isdir(os.path.dirname(args.detections_out)):
os.makedirs(os.path.dirname(args.detections_out))
print("Created ", os.path.dirname(args.detections_out))
dataframe.to_csv(args.detections_out, index=False, sep=";")
print("Written detections to ", args.detections_out)"""