-
Notifications
You must be signed in to change notification settings - Fork 32
/
edgetpumodel.py
279 lines (214 loc) · 9.49 KB
/
edgetpumodel.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
import time
import os
import sys
import logging
import yaml
import numpy as np
import pycoral.utils.edgetpu as etpu
from pycoral.adapters import common
from nms import non_max_suppression, non_max_suppresion_v8
import cv2
import json
from utils import plot_one_box, Colors, get_image_tensor
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("EdgeTPUModel")
class EdgeTPUModel:
def __init__(self, model_file, names_file, conf_thresh=0.25, iou_thresh=0.45, filter_classes=None, agnostic_nms=False, max_det=1000, v8=False):
"""
Creates an object for running a Yolov5 model on an EdgeTPU
Inputs:
- model_file: path to edgetpu-compiled tflite file
- names_file: yaml names file (yolov5 format)
- conf_thresh: detection threshold
- iou_thresh: NMS threshold
- filter_classes: only output certain classes
- agnostic_nms: use class-agnostic NMS
- max_det: max number of detections
"""
model_file = os.path.abspath(model_file)
if not model_file.endswith('tflite'):
model_file += ".tflite"
self.model_file = model_file
self.conf_thresh = conf_thresh
self.iou_thresh = iou_thresh
self.filter_classes = filter_classes
self.agnostic_nms = agnostic_nms
self.max_det = max_det
self.v8 = v8
logger.info("Confidence threshold: {}".format(conf_thresh))
logger.info("IOU threshold: {}".format(iou_thresh))
self.inference_time = None
self.nms_time = None
self.interpreter = None
self.colors = Colors() # create instance for 'from utils.plots import colors'
self.get_names(names_file)
self.make_interpreter()
self.get_image_size()
def get_names(self, path):
"""
Load a names file
Inputs:
- path: path to names file in yaml format
"""
with open(path, 'r') as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
names = cfg['names']
logger.info("Loaded {} classes".format(len(names)))
self.names = names
def make_interpreter(self):
"""
Internal function that loads the tflite file and creates
the interpreter that deals with the EdgetPU hardware.
"""
# Load the model and allocate
self.interpreter = etpu.make_interpreter(self.model_file)
self.interpreter.allocate_tensors()
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
logger.debug(self.input_details)
logger.debug(self.output_details)
self.input_zero = self.input_details[0]['quantization'][1]
self.input_scale = self.input_details[0]['quantization'][0]
self.output_zero = self.output_details[0]['quantization'][1]
self.output_scale = self.output_details[0]['quantization'][0]
# If the model isn't quantized then these should be zero
# Check against small epsilon to avoid comparing float/int
if self.input_scale < 1e-9:
self.input_scale = 1.0
if self.output_scale < 1e-9:
self.output_scale = 1.0
logger.debug("Input scale: {}".format(self.input_scale))
logger.debug("Input zero: {}".format(self.input_zero))
logger.debug("Output scale: {}".format(self.output_scale))
logger.debug("Output zero: {}".format(self.output_zero))
logger.info("Successfully loaded {}".format(self.model_file))
def get_image_size(self):
"""
Returns the expected size of the input image tensor
"""
if self.interpreter is not None:
self.input_size = common.input_size(self.interpreter)
logger.debug("Expecting input shape: {}".format(self.input_size))
return self.input_size
else:
logger.warning("Interpreter is not yet loaded")
def predict(self, image_path, save_img=True, save_txt=True):
logger.info("Attempting to load {}".format(image_path))
full_image, net_image, pad = get_image_tensor(image_path, self.input_size[0])
pred = self.forward(net_image)
base, ext = os.path.splitext(image_path)
output_path = base + "_detect" + ext
det = self.process_predictions(pred[0], full_image, pad, output_path, save_img=save_img, save_txt=save_txt)
return det
def forward(self, x:np.ndarray, with_nms=True) -> np.ndarray:
"""
Predict function using the EdgeTPU
Inputs:
x: (C, H, W) image tensor
with_nms: apply NMS on output
Returns:
prediction array (with or without NMS applied)
"""
tstart = time.time()
# Transpose if C, H, W
if x.shape[0] == 3:
x = x.transpose((1, 2, 0))
x = x.astype('float32')
# Scale input, conversion is: real = (int_8 - zero)*scale
x = (x/self.input_scale) + self.input_zero
if self.v8:
x = x[np.newaxis].astype(np.int8)
else:
x = x[np.newaxis].astype(np.uint8)
self.interpreter.set_tensor(self.input_details[0]['index'], x)
self.interpreter.invoke()
# Scale output
result = (common.output_tensor(self.interpreter, 0).astype('float32') - self.output_zero) * self.output_scale
if self.v8:
result = np.transpose(result, [0, 2, 1]) # tranpose for yolov8 models
self.inference_time = time.time() - tstart
if with_nms:
tstart = time.time()
if self.v8:
nms_result = non_max_suppresion_v8(result, self.conf_thresh, self.iou_thresh, self.filter_classes,
self.agnostic_nms, max_det=self.max_det)
else:
nms_result = non_max_suppression(result, self.conf_thresh, self.iou_thresh, self.filter_classes,
self.agnostic_nms, max_det=self.max_det)
self.nms_time = time.time() - tstart
return nms_result
else:
return result
def get_last_inference_time(self, with_nms=True):
"""
Returns a tuple containing most recent inference and NMS time
"""
res = [self.inference_time]
if with_nms:
res.append(self.nms_time)
return res
def get_scaled_coords(self, xyxy, output_image, pad):
"""
Converts raw prediction bounding box to orginal
image coordinates.
Args:
xyxy: array of boxes
output_image: np array
pad: padding due to image resizing (pad_w, pad_h)
"""
pad_w, pad_h = pad
in_h, in_w = self.input_size
out_h, out_w, _ = output_image.shape
ratio_w = out_w/(in_w - pad_w)
ratio_h = out_h/(in_h - pad_h)
out = []
for coord in xyxy:
x1, y1, x2, y2 = coord
x1 *= in_w*ratio_w
x2 *= in_w*ratio_w
y1 *= in_h*ratio_h
y2 *= in_h*ratio_h
x1 = max(0, x1)
x2 = min(out_w, x2)
y1 = max(0, y1)
y2 = min(out_h, y2)
out.append((x1, y1, x2, y2))
return np.array(out).astype(int)
def process_predictions(self, det, output_image, pad, output_path="detection.jpg", save_img=True, save_txt=True, hide_labels=False, hide_conf=False):
"""
Process predictions and optionally output an image with annotations
"""
if len(det):
# Rescale boxes from img_size to im0 size
# x1, y1, x2, y2=
det[:, :4] = self.get_scaled_coords(det[:, :4], output_image, pad)
output = {}
base, ext = os.path.splitext(output_path)
s = ""
# Print results
for c in np.unique(det[:, -1]):
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
if s != "":
s = s.strip()
s = s[:-1]
logger.info("Detected: {}".format(s))
# Write results
for *xyxy, conf, cls in reversed(det):
if save_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (self.names[c] if hide_conf else f'{self.names[c]} {conf:.2f}')
output_image = plot_one_box(xyxy, output_image, label=label, color=self.colors(c, True))
if save_txt:
output[base] = {}
output[base]['box'] = xyxy
output[base]['conf'] = conf
output[base]['cls'] = cls
output[base]['cls_name'] = self.names[c]
if save_txt:
output_txt = base+".txt"
with open(output_txt, 'w') as f:
json.dump(output, f, indent=1)
if save_img:
cv2.imwrite(output_path, output_image)
return det