-
Notifications
You must be signed in to change notification settings - Fork 118
/
trt_backend.py
152 lines (123 loc) · 5.46 KB
/
trt_backend.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
import os
import cv2
import numpy as np
import time
import logging
from .trt_loader import TrtModel
class Arcface:
def __init__(self, rec_name: str = '/models/trt-engines/arcface_r100_v1/arcface_r100_v1.plan',**kwargs):
self.rec_model = TrtModel(rec_name)
self.input_shape = None
self.max_batch_size = 1
# warmup
def prepare(self, **kwargs):
logging.info("Warming up ArcFace TensorRT engine...")
self.rec_model.build()
self.input_shape = self.rec_model.input_shapes[0]
self.max_batch_size = self.rec_model.max_batch_size
if self.input_shape[0] == -1:
self.input_shape = (1,) + self.input_shape[1:]
self.rec_model.run(np.zeros(self.input_shape, np.float32))
logging.info(
f"Engine warmup complete! Expecting input shape: {self.input_shape}. Max batch size: {self.max_batch_size}")
def get_embedding(self, face_img):
if not isinstance(face_img, list):
face_img = [face_img]
if not face_img[0].shape == (3, 112, 112):
for i, img in enumerate(face_img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1))
face_img[i] = img
face_img = np.stack(face_img)
embeddings = self.rec_model.run(face_img, deflatten=True)[0]
return embeddings
class Cosface:
def __init__(self, rec_name='/models/onnx/glintr100/glintr100.onnx',**kwargs):
self.rec_model = TrtModel(rec_name)
self.input_shape = None
self.max_batch_size = 1
self.input_mean = 127.5
self.input_std = 127.5
# warmup
def prepare(self, **kwargs):
logging.info("Warming up CosFace TensorRT engine...")
self.rec_model.build()
self.input_shape = self.rec_model.input_shapes[0]
self.max_batch_size = self.rec_model.max_batch_size
if self.input_shape[0] == -1:
self.input_shape = (1,) + self.input_shape[1:]
self.rec_model.run(np.zeros(self.input_shape, np.float32))
logging.info(
f"Engine warmup complete! Expecting input shape: {self.input_shape}. Max batch size: {self.max_batch_size}")
def get_embedding(self, face_img):
if not isinstance(face_img, list):
face_img = [face_img]
for i, img in enumerate(face_img):
input_size = tuple(img.shape[0:2][::-1])
blob = cv2.dnn.blobFromImage(img, 1.0 / self.input_std, input_size,
(self.input_mean, self.input_mean, self.input_mean), swapRB=True)[0]
face_img[i] = blob
face_img = np.stack(face_img)
embeddings = self.rec_model.run(face_img, deflatten=True)[0]
return embeddings
class FaceGenderage:
def __init__(self, rec_name: str = '/models/trt-engines/genderage_v1/genderage_v1.plan',**kwargs):
self.rec_model = TrtModel(rec_name)
self.input_shape = None
# warmup
def prepare(self, **kwargs):
logging.info("Warming up GenderAge TensorRT engine...")
self.rec_model.build()
self.input_shape = self.rec_model.input_shapes[0]
self.max_batch_size = self.rec_model.max_batch_size
if self.input_shape[0] == -1:
self.input_shape = (1,) + self.input_shape[1:]
self.rec_model.run(np.zeros(self.input_shape, np.float32))
logging.info(
f"Engine warmup complete! Expecting input shape: {self.input_shape}. Max batch size: {self.max_batch_size}")
def get(self, face_img):
if not isinstance(face_img, list):
face_img = [face_img]
if not face_img[0].shape == (3, 112, 112):
for i, img in enumerate(face_img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1))
face_img[i] = img
face_img = np.stack(face_img)
_ga = []
ret = self.rec_model.run(face_img, deflatten=True)[0]
for e in ret:
e = np.expand_dims(e, axis=0)
g = e[:, 0:2].flatten()
gender = np.argmax(g)
a = e[:, 2:202].reshape((100, 2))
a = np.argmax(a, axis=1)
age = int(sum(a))
_ga.append((gender, age))
return _ga
class DetectorInfer:
def __init__(self, model='/models/trt-engines/centerface/centerface.plan',
output_order=None,**kwargs):
self.rec_model = TrtModel(model)
self.model_name = os.path.basename(model)
self.input_shape = None
self.output_order = output_order
# warmup
def prepare(self, **kwargs):
logging.info(f"Warming up face detector TensorRT engine...")
self.rec_model.build()
self.input_shape = self.rec_model.input_shapes[0]
self.out_shapes = self.rec_model.out_shapes
self.input_dtype = np.uint8
self.max_batch_size = self.rec_model.max_batch_size
if self.input_shape[0] == -1:
self.input_shape = (1,) + self.input_shape[1:]
logging.info(self.input_shape)
if not self.output_order:
self.output_order = self.rec_model.out_names
self.rec_model.run(np.zeros(self.input_shape, np.float32))
logging.info(f"Engine warmup complete! Expecting input shape: {self.input_shape}")
def run(self, input):
net_out = self.rec_model.run(input, deflatten=True, as_dict=True)
net_out = [net_out[e] for e in self.output_order]
return net_out