-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathonnx_helper.py
281 lines (264 loc) · 10.9 KB
/
onnx_helper.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
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# code modified from: https://github.com/deepinsight/insightface/blob/master/recognition/arcface_torch/onnx_helper.py
from __future__ import division
import datetime
import os
import os.path as osp
import glob
import numpy as np
import cv2
import sys
import onnxruntime
import onnx
import argparse
from onnx import numpy_helper
class ArcFaceORT:
def __init__(self, model_path, cpu=False):
self.model_path = model_path
self.model_dir = os.path.dirname(model_path)
# providers = None will use available provider, for onnxruntime-gpu it will be "CUDAExecutionProvider"
self.providers = ['CPUExecutionProvider'
] if cpu else ['CUDAExecutionProvider']
#input_size is (w,h), return error message, return None if success
def check(self, track='cfat', test_img=None):
#default is cfat
max_model_size_mb = 1024
max_feat_dim = 512
max_time_cost = 15
if track.startswith('ms1m'):
max_model_size_mb = 1024
max_feat_dim = 512
max_time_cost = 10
elif track.startswith('glint'):
max_model_size_mb = 1024
max_feat_dim = 1024
max_time_cost = 20
elif track.startswith('cfat'):
max_model_size_mb = 1024
max_feat_dim = 512
max_time_cost = 15
elif track.startswith('unconstrained'):
max_model_size_mb = 1024
max_feat_dim = 1024
max_time_cost = 30
else:
return "track not found"
if not os.path.exists(self.model_path):
return f"{self.model_path} not exists"
if not os.path.isdir(self.model_dir):
return f"{self.model_dir} should be directory"
print('use onnx-model:', self.model_path)
try:
session = onnxruntime.InferenceSession(
self.model_path, providers=self.providers)
except Exception as e:
return "load onnx failed"
input_cfg = session.get_inputs()[0]
input_shape = input_cfg.shape
print('input-shape:', input_shape)
if len(input_shape) != 4:
return "length of input_shape should be 4"
if not isinstance(input_shape[0], str):
#return "input_shape[0] should be str to support batch-inference"
print('reset input-shape[0] to None')
model = onnx.load(self.model_path)
model.graph.input[0].type.tensor_type.shape.dim[
0].dim_param = 'None'
new_model_path = osp.join(self.model_dir, 'zzzzrefined.onnx')
onnx.save(model, new_model_path)
self.model_path = new_model_path
print('use new onnx-model:', self.model_path)
try:
session = onnxruntime.InferenceSession(
self.model_path, providers=self.providers)
except:
return "load onnx failed"
input_cfg = session.get_inputs()[0]
input_shape = input_cfg.shape
print('new-input-shape:', input_shape)
self.image_size = tuple(input_shape[2:4][::-1])
#print('image_size:', self.image_size)
input_name = input_cfg.name
outputs = session.get_outputs()
output_names = []
for o in outputs:
output_names.append(o.name)
#print(o.name, o.shape)
if len(output_names) != 1:
return "number of output nodes should be 1"
self.session = session
self.input_name = input_name
self.output_names = output_names
#print(self.output_names)
model = onnx.load(self.model_path)
graph = model.graph
if len(graph.node) < 8:
return "too small onnx graph"
input_size = (112, 112)
self.crop = None
if track == 'cfat':
crop_file = osp.join(self.model_dir, 'crop.txt')
if osp.exists(crop_file):
lines = open(crop_file, 'r').readlines()
if len(lines) != 6:
return "crop.txt should contain 6 lines"
lines = [int(x) for x in lines]
self.crop = lines[:4]
input_size = tuple(lines[4:6])
if input_size != self.image_size:
return "input-size is inconsistant with onnx model input, %s vs %s" % (
input_size, self.image_size)
self.model_size_mb = os.path.getsize(self.model_path) / float(1024 *
1024)
if self.model_size_mb > max_model_size_mb:
return "max model size exceed, given %.3f-MB" % self.model_size_mb
input_mean = None
input_std = None
if track == 'cfat':
pn_file = osp.join(self.model_dir, 'pixel_norm.txt')
if osp.exists(pn_file):
lines = open(pn_file, 'r').readlines()
if len(lines) != 2:
return "pixel_norm.txt should contain 2 lines"
input_mean = float(lines[0])
input_std = float(lines[1])
if input_mean is not None or input_std is not None:
if input_mean is None or input_std is None:
return "please set input_mean and input_std simultaneously"
else:
find_sub = False
find_mul = False
for nid, node in enumerate(graph.node[:8]):
print(nid, node.name)
if node.name.startswith('Sub') or node.name.startswith(
'_minus'):
find_sub = True
if node.name.startswith('Mul') or node.name.startswith(
'_mul') or node.name.startswith('Div'):
find_mul = True
if find_sub and find_mul:
print("find sub and mul")
#mxnet arcface model
input_mean = 0.0
input_std = 1.0
else:
input_mean = 127.5
input_std = 127.5
self.input_mean = input_mean
self.input_std = input_std
for initn in graph.initializer:
weight_array = numpy_helper.to_array(initn)
dt = weight_array.dtype
if dt.itemsize < 4:
return 'invalid weight type - (%s:%s)' % (initn.name, dt.name)
assert test_img is not None
test_img = cv2.resize(test_img, self.image_size)
feat, cost = self.benchmark(test_img)
batch_result = self.check_batch(test_img)
batch_result_sum = float(np.sum(batch_result))
if batch_result_sum in [float('inf'), -float('inf')
] or batch_result_sum != batch_result_sum:
print(batch_result)
print(batch_result_sum)
return "batch result output contains NaN!"
if len(feat.shape) < 2:
return "the shape of the feature must be two, but get {}".format(
str(feat.shape))
if feat.shape[1] > max_feat_dim:
return "max feat dim exceed, given %d" % feat.shape[1]
self.feat_dim = feat.shape[1]
cost_ms = cost * 1000
if cost_ms > max_time_cost:
return "max time cost exceed, given %.4f" % cost_ms
self.cost_ms = cost_ms
print(
'check stat:, model-size-mb: %.4f, feat-dim: %d, time-cost-ms: %.4f, input-mean: %.3f, input-std: %.3f'
% (self.model_size_mb, self.feat_dim, self.cost_ms,
self.input_mean, self.input_std))
return None
def check_batch(self, img):
if not isinstance(img, list):
imgs = [img, ] * 32
if self.crop is not None:
nimgs = []
for img in imgs:
nimg = img[self.crop[1]:self.crop[3], self.crop[0]:self.crop[
2], :]
if nimg.shape[0] != self.image_size[1] or nimg.shape[
1] != self.image_size[0]:
nimg = cv2.resize(nimg, self.image_size)
nimgs.append(nimg)
imgs = nimgs
blob = cv2.dnn.blobFromImages(
images=imgs,
scalefactor=1.0 / self.input_std,
size=self.image_size,
mean=(self.input_mean, self.input_mean, self.input_mean),
swapRB=True)
net_out = self.session.run(self.output_names,
{self.input_name: blob})[0]
return net_out
def meta_info(self):
return {
'model-size-mb': self.model_size_mb,
'feature-dim': self.feat_dim,
'infer': self.cost_ms
}
def forward(self, imgs):
if not isinstance(imgs, list):
imgs = [imgs]
input_size = self.image_size
if self.crop is not None:
nimgs = []
for img in imgs:
nimg = img[self.crop[1]:self.crop[3], self.crop[0]:self.crop[
2], :]
if nimg.shape[0] != input_size[1] or nimg.shape[
1] != input_size[0]:
nimg = cv2.resize(nimg, input_size)
nimgs.append(nimg)
imgs = nimgs
blob = cv2.dnn.blobFromImages(
imgs,
1.0 / self.input_std,
input_size, (self.input_mean, self.input_mean, self.input_mean),
swapRB=True)
net_out = self.session.run(self.output_names,
{self.input_name: blob})[0]
return net_out
def benchmark(self, img):
input_size = self.image_size
if self.crop is not None:
nimg = img[self.crop[1]:self.crop[3], self.crop[0]:self.crop[2], :]
if nimg.shape[0] != input_size[1] or nimg.shape[1] != input_size[
0]:
nimg = cv2.resize(nimg, input_size)
img = nimg
blob = cv2.dnn.blobFromImage(
img,
1.0 / self.input_std,
input_size, (self.input_mean, self.input_mean, self.input_mean),
swapRB=True)
costs = []
for _ in range(50):
ta = datetime.datetime.now()
net_out = self.session.run(self.output_names,
{self.input_name: blob})[0]
tb = datetime.datetime.now()
cost = (tb - ta).total_seconds()
costs.append(cost)
costs = sorted(costs)
cost = costs[5]
return net_out, cost