-
Notifications
You must be signed in to change notification settings - Fork 4
/
yolov5.py
executable file
·146 lines (132 loc) · 5.16 KB
/
yolov5.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
import cv2
import sys
import tensorrt as trt
import pycuda.autoinit
import pycuda.driver as cuda
import numpy as np
import time
from utils import sigmoid_v, make_grid, nms
INPUT_W = 640
INPUT_H = 640
class Yolov5():
def __init__(self, model):
# load tensorrt engine
TRT_LOGGER = trt.Logger(trt.Logger.INFO)
with open(model, 'rb') as f, trt.Runtime(TRT_LOGGER) as runtime:
engine = runtime.deserialize_cuda_engine(f.read())
self.context = engine.create_execution_context()
stream = cuda.Stream()
# allocate memory
inputs, outputs, bindings = [], [], []
for binding in engine:
size = trt.volume(engine.get_binding_shape(binding))
dtype = trt.nptype(engine.get_binding_dtype(binding))
host_mem = cuda.pagelocked_empty(size, dtype)
device_mem = cuda.mem_alloc(host_mem.nbytes)
bindings.append(int(device_mem))
if engine.binding_is_input(binding):
inputs.append({'host': host_mem, 'device': device_mem })
else:
outputs.append({'host': host_mem, 'device': device_mem })
# save to class
self.inputs = inputs
self.outputs = outputs
self.bindings = bindings
self.stream = stream
# post processing config
self.cls_nums = 80
self.filters = (self.cls_nums + 5) * 3
self.output_filter = self.cls_nums + 5
self.output_shapes = [
(1, 3, 80, 80, self.output_filter),
(1, 3, 40, 40, self.output_filter),
(1, 3, 20, 20, self.output_filter)
]
self.strides = np.array([8., 16., 32.])
anchors = np.array([
[[10, 13], [16, 30], [33, 23]],
[[30, 61], [62, 45], [59, 119]],
[[116, 90], [156, 198], [373, 326]],
])
self.nl = len(anchors)
self.nc = 80 # classes
self.no = self.nc + 5 # outputs per anchor
self.na = len(anchors[0])
a = anchors.copy().astype(np.float32)
a = a.reshape(self.nl, -1, 2)
self.anchors = a.copy()
self.anchor_grid = a.copy().reshape(self.nl, 1, -1, 1, 1, 2)
def detect(self, img):
resized, = self.preprocess_image(img)
outputs = self.inference(resized)
# reshape from flat to (1, 3, x, y, 85)
reshaped = []
for output, shape in zip(outputs, self.output_shapes):
reshaped.append(output.reshape(shape))
return reshaped
def preprocess_image(self, image_raw):
h, w, c = image_raw.shape
image = cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB)
r_w = INPUT_W / w
r_h = INPUT_H / h
if r_h > r_w:
tw = INPUT_W
th = int(r_w * h)
tx1 = tx2 = 0
ty1 = int((INPUT_H - th) / 2)
ty2 = INPUT_H - th - ty1
else:
tw = int(r_h * w)
th = INPUT_H
tx1 = int((INPUT_W - tw) / 2)
tx2 = INPUT_W - tw - tx1
ty1 = ty2 = 0
image = cv2.resize(image, (tw, th))
image = cv2.copyMakeBorder(
image, ty1, ty2, tx1, tx2, cv2.BORDER_CONSTANT, (128, 128, 128)
)
image = image.astype(np.float32)
image /= 255.0
image = np.transpose(image, [2, 0, 1])
image = np.expand_dims(image, axis=0)
image = np.ascontiguousarray(image)
return image
def inference(self, img):
# copy img to input memory
self.inputs[0]['host'] = np.ravel(img)
# transfer data to the gpu
for inp in self.inputs:
cuda.memcpy_htod_async(inp['device'], inp['host'], self.stream)
# run inference
start = time.time()
self.context.execute_async_v2(
bindings=self.bindings,
stream_handle=self.stream.handle)
# fetch outputs from gpu
for out in self.outputs:
cuda.memcpy_dtoh_async(out['host'], out['device'], self.stream)
# synchronize stream
self.stream.synchronize()
end = time.time()
print('execution time:', end-start)
return [out['host'] for out in self.outputs]
def post_process(self, outputs, conf_thres=0.5, iou_thres=0.6, origin_w=0, origin_h=0):
scaled = []
grids = []
for out in outputs:
out = sigmoid_v(out)
_, _, width, height, _ = out.shape
grid = make_grid(width, height)
grids.append(grid)
scaled.append(out)
z = []
for out, grid, stride, anchor in zip(scaled, grids, self.strides, self.anchor_grid):
_, _, width, height, _ = out.shape
out[..., 0:2] = (out[..., 0:2] * 2. - 0.5 + grid) * stride
out[..., 2:4] = (out[..., 2:4] * 2) ** 2 * anchor
out = out.reshape((1, 3 * width * height, self.output_filter))
z.append(out)
pred = np.concatenate(z, 1)
xc = pred[..., 4] > conf_thres
pred = pred[xc]
return nms(pred, iou_thres, origin_w, origin_h)