forked from PaddlePaddle/PaddleSeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer.py
487 lines (422 loc) · 16.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Copyright (c) 2021 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.
import argparse
import codecs
import os
import sys
LOCAL_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(LOCAL_PATH, '..', '..'))
import cv2
import tqdm
import yaml
import numpy as np
import paddle
from paddle.inference import create_predictor, PrecisionType
from paddle.inference import Config as PredictConfig
from paddleseg.cvlibs import manager
from paddleseg.utils import get_sys_env, logger
from utils import get_image_list, mkdir
import transforms as T
def parse_args():
parser = argparse.ArgumentParser(description='Deploy for matting model')
parser.add_argument(
"--config",
dest="cfg",
help="The config file.",
default=None,
type=str,
required=True)
parser.add_argument(
'--image_path',
dest='image_path',
help='The directory or path or file list of the images to be predicted.',
type=str,
default=None,
required=True)
parser.add_argument(
'--trimap_path',
dest='trimap_path',
help=
'The directory or path or file list of the triamp to help predicted.',
type=str,
default=None)
parser.add_argument(
'--batch_size',
dest='batch_size',
help='Mini batch size of one gpu or cpu.',
type=int,
default=1)
parser.add_argument(
'--save_dir',
dest='save_dir',
help='The directory for saving the predict result.',
type=str,
default='./output')
parser.add_argument(
'--device',
choices=['cpu', 'gpu'],
default="gpu",
help="Select which device to inference, defaults to gpu.")
parser.add_argument(
'--cpu_threads',
default=10,
type=int,
help='Number of threads to predict when using cpu.')
parser.add_argument(
'--enable_mkldnn',
default=False,
type=eval,
choices=[True, False],
help='Enable to use mkldnn to speed up when using cpu.')
parser.add_argument(
'--use_trt',
default=False,
type=eval,
choices=[True, False],
help='Whether to use Nvidia TensorRT to accelerate prediction.')
parser.add_argument(
"--precision",
default="fp32",
type=str,
choices=["fp32", "fp16", "int8"],
help='The tensorrt precision.')
parser.add_argument(
'--enable_auto_tune',
default=False,
type=eval,
choices=[True, False],
help=
'Whether to enable tuned dynamic shape. We uses some images to collect '
'the dynamic shape for trt sub graph, which avoids setting dynamic shape manually.'
)
parser.add_argument(
'--auto_tuned_shape_file',
type=str,
default="auto_tune_tmp.pbtxt",
help='The temp file to save tuned dynamic shape.')
parser.add_argument(
"--benchmark",
type=eval,
default=False,
help=
"Whether to log some information about environment, model, configuration and performance."
)
parser.add_argument(
"--model_name",
default="",
type=str,
help='When `--benchmark` is True, the specified model name is displayed.'
)
parser.add_argument(
'--print_detail',
default=True,
type=eval,
choices=[True, False],
help='Print GLOG information of Paddle Inference.')
return parser.parse_args()
def use_auto_tune(args):
return hasattr(PredictConfig, "collect_shape_range_info") \
and hasattr(PredictConfig, "enable_tuned_tensorrt_dynamic_shape") \
and args.device == "gpu" and args.use_trt and args.enable_auto_tune
class DeployConfig:
def __init__(self, path):
with codecs.open(path, 'r', 'utf-8') as file:
self.dic = yaml.load(file, Loader=yaml.FullLoader)
self._transforms = self.load_transforms(
self.dic['Deploy']['transforms'])
self._dir = os.path.dirname(path)
@property
def transforms(self):
return self._transforms
@property
def model(self):
return os.path.join(self._dir, self.dic['Deploy']['model'])
@property
def params(self):
return os.path.join(self._dir, self.dic['Deploy']['params'])
@staticmethod
def load_transforms(t_list):
com = manager.TRANSFORMS
transforms = []
for t in t_list:
ctype = t.pop('type')
transforms.append(com[ctype](**t))
return T.Compose(transforms)
def auto_tune(args, imgs, img_nums):
"""
Use images to auto tune the dynamic shape for trt sub graph.
The tuned shape saved in args.auto_tuned_shape_file.
Args:
args(dict): input args.
imgs(str, list[str]): the path for images.
img_nums(int): the nums of images used for auto tune.
Returns:
None
"""
logger.info("Auto tune the dynamic shape for GPU TRT.")
assert use_auto_tune(args)
if not isinstance(imgs, (list, tuple)):
imgs = [imgs]
num = min(len(imgs), img_nums)
cfg = DeployConfig(args.cfg)
pred_cfg = PredictConfig(cfg.model, cfg.params)
pred_cfg.enable_use_gpu(100, 0)
if not args.print_detail:
pred_cfg.disable_glog_info()
pred_cfg.collect_shape_range_info(args.auto_tuned_shape_file)
predictor = create_predictor(pred_cfg)
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
for i in range(0, num):
data = np.array([cfg.transforms(imgs[i])[0]])
input_handle.reshape(data.shape)
input_handle.copy_from_cpu(data)
try:
predictor.run()
except:
logger.info(
"Auto tune fail. Usually, the error is out of GPU memory, "
"because the model and image is too large. \n")
del predictor
if os.path.exists(args.auto_tuned_shape_file):
os.remove(args.auto_tuned_shape_file)
return
logger.info("Auto tune success.\n")
class Predictor:
def __init__(self, args):
"""
Prepare for prediction.
The usage and docs of paddle inference, please refer to
https://paddleinference.paddlepaddle.org.cn/product_introduction/summary.html
"""
self.args = args
self.cfg = DeployConfig(args.cfg)
self._init_base_config()
if args.device == 'cpu':
self._init_cpu_config()
else:
self._init_gpu_config()
self.predictor = create_predictor(self.pred_cfg)
if hasattr(args, 'benchmark') and args.benchmark:
import auto_log
pid = os.getpid()
self.autolog = auto_log.AutoLogger(
model_name=args.model_name,
model_precision=args.precision,
batch_size=args.batch_size,
data_shape="dynamic",
save_path=None,
inference_config=self.pred_cfg,
pids=pid,
process_name=None,
gpu_ids=0,
time_keys=[
'preprocess_time', 'inference_time', 'postprocess_time'
],
warmup=0,
logger=logger)
def _init_base_config(self):
self.pred_cfg = PredictConfig(self.cfg.model, self.cfg.params)
if not self.args.print_detail:
self.pred_cfg.disable_glog_info()
self.pred_cfg.enable_memory_optim()
self.pred_cfg.switch_ir_optim(True)
def _init_cpu_config(self):
"""
Init the config for x86 cpu.
"""
logger.info("Using CPU")
self.pred_cfg.disable_gpu()
if self.args.enable_mkldnn:
logger.info("Using MKLDNN")
# cache 1- different shapes for mkldnn
self.pred_cfg.set_mkldnn_cache_capacity(10)
self.pred_cfg.enable_mkldnn()
self.pred_cfg.set_cpu_math_library_num_threads(self.args.cpu_threads)
def _init_gpu_config(self):
"""
Init the config for nvidia gpu.
"""
logger.info("using GPU")
self.pred_cfg.enable_use_gpu(100, 0)
precision_map = {
"fp16": PrecisionType.Half,
"fp32": PrecisionType.Float32,
"int8": PrecisionType.Int8
}
precision_mode = precision_map[self.args.precision]
if self.args.use_trt:
logger.info("Use TRT")
self.pred_cfg.enable_tensorrt_engine(
workspace_size=1 << 30,
max_batch_size=1,
min_subgraph_size=300,
precision_mode=precision_mode,
use_static=False,
use_calib_mode=False)
if use_auto_tune(self.args) and \
os.path.exists(self.args.auto_tuned_shape_file):
logger.info("Use auto tuned dynamic shape")
allow_build_at_runtime = True
self.pred_cfg.enable_tuned_tensorrt_dynamic_shape(
self.args.auto_tuned_shape_file, allow_build_at_runtime)
else:
logger.info("Use manual set dynamic shape")
min_input_shape = {"x": [1, 3, 100, 100]}
max_input_shape = {"x": [1, 3, 2000, 3000]}
opt_input_shape = {"x": [1, 3, 512, 1024]}
self.pred_cfg.set_trt_dynamic_shape_info(
min_input_shape, max_input_shape, opt_input_shape)
def run(self, imgs, trimaps=None, imgs_dir=None):
self.imgs_dir = imgs_dir
num = len(imgs)
input_names = self.predictor.get_input_names()
input_handle = {}
for i in range(len(input_names)):
input_handle[input_names[i]] = self.predictor.get_input_handle(
input_names[i])
output_names = self.predictor.get_output_names()
output_handle = self.predictor.get_output_handle(output_names[0])
args = self.args
for i in tqdm.tqdm(range(0, num, args.batch_size)):
# warm up
if i == 0 and args.benchmark:
for _ in range(5):
img_inputs = []
if trimaps is not None:
trimap_inputs = []
trans_info = []
for j in range(i, i + args.batch_size):
img = imgs[i]
trimap = trimaps[i] if trimaps is not None else None
data = self._preprocess(img=img, trimap=trimap)
img_inputs.append(data['img'])
if trimaps is not None:
trimap_inputs.append(
data['trimap'][np.newaxis, :, :])
trans_info.append(data['trans_info'])
img_inputs = np.array(img_inputs)
if trimaps is not None:
trimap_inputs = (
np.array(trimap_inputs)).astype('float32')
input_handle['img'].copy_from_cpu(img_inputs)
if trimaps is not None:
input_handle['trimap'].copy_from_cpu(trimap_inputs)
self.predictor.run()
results = output_handle.copy_to_cpu()
results = results.squeeze(1)
for j in range(args.batch_size):
trimap = trimap_inputs[
j] if trimaps is not None else None
result = self._postprocess(
results[j], trans_info[j], trimap=trimap)
# inference
if args.benchmark:
self.autolog.times.start()
img_inputs = []
if trimaps is not None:
trimap_inputs = []
trans_info = []
for j in range(i, i + args.batch_size):
img = imgs[i]
trimap = trimaps[i] if trimaps is not None else None
data = self._preprocess(img=img, trimap=trimap)
img_inputs.append(data['img'])
if trimaps is not None:
trimap_inputs.append(data['trimap'][np.newaxis, :, :])
trans_info.append(data['trans_info'])
img_inputs = np.array(img_inputs)
if trimaps is not None:
trimap_inputs = (np.array(trimap_inputs)).astype('float32')
input_handle['img'].copy_from_cpu(img_inputs)
if trimaps is not None:
input_handle['trimap'].copy_from_cpu(trimap_inputs)
if args.benchmark:
self.autolog.times.stamp()
self.predictor.run()
if args.benchmark:
self.autolog.times.stamp()
results = output_handle.copy_to_cpu()
results = results.squeeze(1)
for j in range(args.batch_size):
trimap = trimap_inputs[j] if trimaps is not None else None
result = self._postprocess(
results[j], trans_info[j], trimap=trimap)
self._save_imgs(result, imgs[i + j])
if args.benchmark:
self.autolog.times.end(stamp=True)
logger.info("Finish")
def _preprocess(self, img, trimap=None):
data = {}
data['img'] = img
if trimap is not None:
data['trimap'] = trimap
data['gt_fields'] = ['trimap']
data = self.cfg.transforms(data)
return data
def _postprocess(self, alpha, trans_info, trimap=None):
"""recover pred to origin shape"""
if trimap is not None:
trimap = trimap.squeeze(0)
alpha[trimap == 0] = 0
alpha[trimap == 255] = 1
for item in trans_info[::-1]:
if item[0] == 'resize':
h, w = item[1][0], item[1][1]
alpha = cv2.resize(
alpha, (w, h), interpolation=cv2.INTER_LINEAR)
elif item[0] == 'padding':
h, w = item[1][0], item[1][1]
alpha = alpha[:, :, 0:h, 0:w]
else:
raise Exception("Unexpected info '{}' in im_info".format(
item[0]))
return alpha
def _save_imgs(self, alpha, img_path):
ori_img = cv2.imread(img_path)
alpha = (alpha * 255).astype('uint8')
if self.imgs_dir is not None:
img_path = img_path.replace(self.imgs_dir, '')
name, ext = os.path.splitext(img_path)
if name[0] == '/':
name = name[1:]
alpha_save_path = os.path.join(args.save_dir, 'alpha/', name + '.png')
clip_save_path = os.path.join(args.save_dir, 'clip/', name + '.png')
# save alpha
mkdir(alpha_save_path)
cv2.imwrite(alpha_save_path, alpha)
# save clip image
mkdir(clip_save_path)
alpha = alpha[:, :, np.newaxis]
clip = np.concatenate([ori_img, alpha], axis=-1)
cv2.imwrite(clip_save_path, clip)
def main(args):
imgs_list, imgs_dir = get_image_list(args.image_path)
if args.trimap_path is None:
trimaps_list = None
else:
trimaps_list, _ = get_image_list(args.trimap_path)
if use_auto_tune(args):
tune_img_nums = 10
auto_tune(args, imgs_list, tune_img_nums)
predictor = Predictor(args)
predictor.run(imgs=imgs_list, trimaps=trimaps_list, imgs_dir=imgs_dir)
if use_auto_tune(args) and \
os.path.exists(args.auto_tuned_shape_file):
os.remove(args.auto_tuned_shape_file)
if args.benchmark:
predictor.autolog.report()
if __name__ == '__main__':
args = parse_args()
main(args)