forked from PaddlePaddle/PaddleRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer.py
304 lines (265 loc) · 11.1 KB
/
infer.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
import os
import os.path as osp
import argparse
from operator import itemgetter
import numpy as np
import paddle
from paddle.inference import Config
from paddle.inference import create_predictor
from paddle.inference import PrecisionType
from paddlers.tasks import load_model
from paddlers.utils import logging
from config_utils import parse_configs
class _bool(object):
def __new__(cls, x):
if isinstance(x, str):
if x.lower() == 'false':
return False
elif x.lower() == 'true':
return True
return bool.__new__(x)
class TIPCPredictor(object):
def __init__(self,
model_dir,
device='cpu',
gpu_id=0,
cpu_thread_num=1,
use_mkl=True,
mkl_thread_num=4,
use_trt=False,
memory_optimize=True,
trt_precision_mode='fp32',
benchmark=False,
model_name='',
batch_size=1):
self.model_dir = model_dir
self._model = load_model(model_dir, with_net=False)
if trt_precision_mode.lower() == 'fp32':
trt_precision_mode = PrecisionType.Float32
elif trt_precision_mode.lower() == 'fp16':
trt_precision_mode = PrecisionType.Float16
else:
logging.error(
"TensorRT precision mode {} is invalid. Supported modes are fp32 and fp16."
.format(trt_precision_mode),
exit=True)
self.config = self.get_config(
device=device,
gpu_id=gpu_id,
cpu_thread_num=cpu_thread_num,
use_mkl=use_mkl,
mkl_thread_num=mkl_thread_num,
use_trt=use_trt,
use_glog=False,
memory_optimize=memory_optimize,
max_trt_batch_size=1,
trt_precision_mode=trt_precision_mode)
self.predictor = create_predictor(self.config)
self.batch_size = batch_size
if benchmark:
import auto_log
pid = os.getpid()
self.autolog = auto_log.AutoLogger(
model_name=model_name,
model_precision=trt_precision_mode,
batch_size=batch_size,
data_shape='dynamic',
save_path=None,
inference_config=self.config,
pids=pid,
process_name=None,
gpu_ids=0,
time_keys=[
'preprocess_time', 'inference_time', 'postprocess_time'
],
warmup=0,
logger=logging)
self.benchmark = benchmark
def get_config(self, device, gpu_id, cpu_thread_num, use_mkl,
mkl_thread_num, use_trt, use_glog, memory_optimize,
max_trt_batch_size, trt_precision_mode):
config = Config(
osp.join(self.model_dir, 'model.pdmodel'),
osp.join(self.model_dir, 'model.pdiparams'))
if device == 'gpu':
config.enable_use_gpu(200, gpu_id)
config.switch_ir_optim(True)
if use_trt:
if self._model.model_type == 'segmenter':
logging.warning(
"Semantic segmentation models do not support TensorRT acceleration, "
"TensorRT is forcibly disabled.")
elif self._model.model_type == 'detector' and 'RCNN' in self._model.__class__.__name__:
logging.warning(
"RCNN models do not support TensorRT acceleration, "
"TensorRT is forcibly disabled.")
else:
config.enable_tensorrt_engine(
workspace_size=1 << 10,
max_batch_size=max_trt_batch_size,
min_subgraph_size=3,
precision_mode=trt_precision_mode,
use_static=False,
use_calib_mode=False)
else:
config.disable_gpu()
config.set_cpu_math_library_num_threads(cpu_thread_num)
if use_mkl:
if self._model.__class__.__name__ == 'MaskRCNN':
logging.warning(
"MaskRCNN does not support MKL-DNN, MKL-DNN is forcibly disabled"
)
else:
try:
# Cache 10 different shapes for mkldnn to avoid memory leak.
config.set_mkldnn_cache_capacity(10)
config.enable_mkldnn()
config.set_cpu_math_library_num_threads(mkl_thread_num)
except Exception as e:
logging.warning(
"The current environment does not support MKL-DNN, MKL-DNN is disabled."
)
pass
if not use_glog:
config.disable_glog_info()
if memory_optimize:
config.enable_memory_optim()
config.switch_use_feed_fetch_ops(False)
return config
def preprocess(self, images, transforms):
preprocessed_samples, batch_trans_info = self._model.preprocess(
images, transforms, to_tensor=False)
return preprocessed_samples, batch_trans_info
def postprocess(self, net_outputs, batch_restore_list, topk=1):
if self._model.model_type == 'classifier':
true_topk = min(self._model.num_classes, topk)
if self._model.postprocess is None:
self._model.build_postprocess_from_labels(topk)
# XXX: Convert ndarray to tensor as `self._model.postprocess` requires
assert len(net_outputs) == 1
net_outputs = paddle.to_tensor(net_outputs[0])
outputs = self._model.postprocess(net_outputs)
class_ids = map(itemgetter('class_ids'), outputs)
scores = map(itemgetter('scores'), outputs)
label_names = map(itemgetter('label_names'), outputs)
preds = [{
'class_ids_map': l,
'scores_map': s,
'label_names_map': n,
} for l, s, n in zip(class_ids, scores, label_names)]
elif self._model.model_type in ('segmenter', 'change_detector'):
label_map, score_map = self._model.postprocess(
net_outputs, batch_restore_list=batch_restore_list)
preds = [{
'label_map': l,
'score_map': s
} for l, s in zip(label_map, score_map)]
elif self._model.model_type == 'detector':
net_outputs = {
k: v
for k, v in zip(['bbox', 'bbox_num', 'mask'], net_outputs)
}
preds = self._model.postprocess(net_outputs)
elif self._model.model_type == 'restorer':
res_maps = self._model.postprocess(
net_outputs[0], batch_restore_list=batch_restore_list)
preds = [{'res_map': res_map} for res_map in res_maps]
else:
logging.error(
"Invalid model type {}.".format(self.model_type), exit=True)
return preds
def _run(self, images, topk=1, transforms=None, time_it=False):
if self.benchmark and time_it:
self.autolog.times.start()
preprocessed_input, batch_trans_info = self.preprocess(images,
transforms)
input_names = self.predictor.get_input_names()
for name in input_names:
input_tensor = self.predictor.get_input_handle(name)
input_tensor.copy_from_cpu(preprocessed_input[name])
if self.benchmark and time_it:
self.autolog.times.stamp()
self.predictor.run()
output_names = self.predictor.get_output_names()
net_outputs = []
for name in output_names:
output_tensor = self.predictor.get_output_handle(name)
net_outputs.append(output_tensor.copy_to_cpu())
if self.benchmark and time_it:
self.autolog.times.stamp()
res = self.postprocess(
net_outputs, batch_restore_list=batch_trans_info, topk=topk)
if self.benchmark and time_it:
self.autolog.times.end(stamp=True)
return res
def predict(self, data_dir, file_list, topk=1, warmup_iters=5):
transforms = self._model.test_transforms
# Warm up
iters = 0
while True:
for images in self._parse_lines(data_dir, file_list):
if iters >= warmup_iters:
break
self._run(
images=images,
topk=topk,
transforms=transforms,
time_it=False)
iters += 1
else:
continue
break
results = []
for images in self._parse_lines(data_dir, file_list):
res = self._run(
images=images, topk=topk, transforms=transforms, time_it=True)
results.append(res)
return results
def _parse_lines(self, data_dir, file_list):
with open(file_list, 'r') as f:
batch = []
for line in f:
items = line.strip().split()
items = [osp.join(data_dir, item) for item in items]
if self._model.model_type == 'change_detector':
batch.append((items[0], items[1]))
else:
batch.append(items[0])
if len(batch) == self.batch_size:
yield batch
batch.clear()
if 0 < len(batch) < self.batch_size:
yield batch
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str)
parser.add_argument('--inherit_off', action='store_true')
parser.add_argument('--model_dir', type=str, default='./')
parser.add_argument(
'--device', type=str, choices=['cpu', 'gpu'], default='cpu')
parser.add_argument('--enable_mkldnn', type=_bool, default=False)
parser.add_argument('--cpu_threads', type=int, default=10)
parser.add_argument('--use_trt', type=_bool, default=False)
parser.add_argument(
'--precision', type=str, choices=['fp32', 'fp16'], default='fp16')
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--benchmark', type=_bool, default=False)
parser.add_argument('--model_name', type=str, default='')
args = parser.parse_args()
cfg = parse_configs(args.config, not args.inherit_off)
eval_dataset = cfg['datasets']['eval']
data_dir = eval_dataset.args['data_dir']
file_list = eval_dataset.args['file_list']
predictor = TIPCPredictor(
args.model_dir,
device=args.device,
cpu_thread_num=args.cpu_threads,
use_mkl=args.enable_mkldnn,
mkl_thread_num=args.cpu_threads,
use_trt=args.use_trt,
trt_precision_mode=args.precision,
benchmark=args.benchmark)
predictor.predict(data_dir, file_list)
if args.benchmark:
predictor.autolog.report()