-
Notifications
You must be signed in to change notification settings - Fork 2
/
infer.py
265 lines (220 loc) · 8.96 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
# 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.
import os
from os import path as osp
import argparse
import numpy as np
import random
from PIL import Image
import paddle
from paddle.vision import transforms as T
from paddle import inference
from paddle.inference import Config, create_predictor
from model import PatchCore, postporcess_score_map, get_model
from utils import plot_fig
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_args():
# general params
parser = argparse.ArgumentParser("PaddleVideo Inference model script")
parser.add_argument('-c',
'--config',
type=str,
default='configs/example.yaml',
help='config file path')
parser.add_argument("-i", "--input_file", type=str, help="input file path")
parser.add_argument("--model_file", type=str)
parser.add_argument("--params_file", type=str)
# params for predict
parser.add_argument("-b", "--batch_size", type=int, default=1)
parser.add_argument("--use_gpu", type=str2bool, default=True)
parser.add_argument("--precision", type=str, default="fp32")
parser.add_argument("--ir_optim", type=str2bool, default=True)
parser.add_argument("--use_tensorrt", type=str2bool, default=False)
parser.add_argument("--gpu_mem", type=int, default=4000)
parser.add_argument("--enable_benchmark", type=str2bool, default=False)
parser.add_argument("--enable_mkldnn", type=str2bool, default=False)
parser.add_argument("--cpu_threads", type=int, default=None)
parser.add_argument("--save_path", type=str, default='./test_tipc/output/')
parser.add_argument("--category", type=str, default='capsule')
parser.add_argument("--stats", type=str, default='./test_tipc/output/stats')
parser.add_argument("--seed", type=int, default=521)
# params for process control
parser.add_argument("--enable_post_process", action='store_true', default=False)
return parser.parse_args()
def create_paddle_predictor(args):
config = Config(args.model_file, args.params_file)
if args.use_gpu:
config.enable_use_gpu(args.gpu_mem, 0)
else:
config.disable_gpu()
if args.cpu_threads:
config.set_cpu_math_library_num_threads(args.cpu_threads)
if args.enable_mkldnn:
# cache 10 different shapes for mkldnn to avoid memory leak
config.set_mkldnn_cache_capacity(10)
config.enable_mkldnn()
if args.precision == "fp16":
config.enable_mkldnn_bfloat16()
# config.disable_glog_info()
config.switch_ir_optim(args.ir_optim) # default true
if args.use_tensorrt:
# choose precision
if args.precision == "fp16":
precision = inference.PrecisionType.Half
elif args.precision == "int8":
precision = inference.PrecisionType.Int8
else:
precision = inference.PrecisionType.Float32
# calculate real max batch size during inference when tenrotRT enabled
num_seg = 1
num_views = 1
max_batch_size = args.batch_size * num_views * num_seg
config.enable_tensorrt_engine(precision_mode=precision,
max_batch_size=max_batch_size)
config.enable_memory_optim()
# use zero copy
config.switch_use_feed_fetch_ops(False)
predictor = create_predictor(config)
return config, predictor
def preprocess(img):
transform_x = T.Compose([T.Resize(256),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
x = Image.open(img).convert('RGB')
x = transform_x(x).unsqueeze(0)
return x.numpy()
def parse_file_paths(input_path: str) -> list:
if osp.isfile(input_path):
files = [
input_path,
]
else:
files = os.listdir(input_path)
files = [
file for file in files
if (file.endswith(".png"))
]
files = [osp.join(input_path, file) for file in files]
return files
def postprocess(args, test_imgs, class_name, outputs, stats):
outputs = [paddle.to_tensor(i) for i in outputs]
model = get_model('coreset' if 'memory_bank' in stats.keys() else 'padim+')(None)
model.load(stats)
outputs = [model.project(i) for i in outputs]
outputs = paddle.concat(outputs, axis=0)
score_map, image_score = model.generate_scores_map(outputs, (256, 256))
print(f'image_score:{image_score}')
# Normalization
max_score = score_map.max()
min_score = score_map.min()
scores = (score_map - min_score) / (max_score - min_score)
save_name = args.save_path
plot_fig(test_imgs, scores, None, 0.5, save_name, class_name, True, 'infer')
print('saved')
def main():
args = parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
paddle.seed(args.seed)
model_name = args.model_name
print(f"Inference model({model_name})...")
# InferenceHelper = build_inference_helper(cfg.INFERENCE)
print('load train set feature from: %s' % args.stats)
stats = paddle.load(args.stats)
inference_config, predictor = create_paddle_predictor(args)
# get input_tensor and output_tensor
input_names = predictor.get_input_names()
output_names = predictor.get_output_names()
input_tensor_list = []
output_tensor_list = []
for item in input_names:
input_tensor_list.append(predictor.get_input_handle(item))
for item in output_names:
output_tensor_list.append(predictor.get_output_handle(item))
# get the absolute file path(s) to be processed
files = parse_file_paths(args.input_file)
if args.enable_benchmark:
num_warmup = 0
# instantiate auto log
import auto_log
pid = os.getpid()
autolog = auto_log.AutoLogger(
model_name=model_name,
model_precision=args.precision,
batch_size=args.batch_size,
data_shape="dynamic",
save_path="./output/auto_log.lpg",
inference_config=inference_config,
pids=pid,
process_name=None,
gpu_ids=0 if args.use_gpu else None,
time_keys=['preprocess_time', 'inference_time', 'postprocess_time'],
warmup=num_warmup)
# Inferencing process
batch_num = args.batch_size
for st_idx in range(0, len(files), batch_num):
ed_idx = min(st_idx + batch_num, len(files))
# auto log start
if args.enable_benchmark:
autolog.times.start()
# Pre process batched input
batched_inputs = [files[st_idx:ed_idx]]
imgs = []
test_imgs = []
for inp in batched_inputs[0]:
img = preprocess(inp)
imgs.append(img)
test_imgs.extend(img)
imgs = np.concatenate(imgs)
batched_inputs = [imgs]
# get pre process time cost
if args.enable_benchmark:
autolog.times.stamp()
# run inference
input_names = predictor.get_input_names()
for i, name in enumerate(input_names):
input_tensor = predictor.get_input_handle(name)
input_tensor.reshape(batched_inputs[i].shape)
input_tensor.copy_from_cpu(batched_inputs[i].copy())
# do the inference
predictor.run()
# get inference process time cost
if args.enable_benchmark:
autolog.times.stamp()
# get out data from output tensor
results = []
# get out data from output tensor
output_names = predictor.get_output_names()
for i, name in enumerate(output_names):
output_tensor = predictor.get_output_handle(name)
output_data = output_tensor.copy_to_cpu()
results.append(output_data)
#
if args.enable_post_process:
postprocess(args, test_imgs, args.category, results, stats)
# get post process time cost
if args.enable_benchmark:
autolog.times.end(stamp=True)
# time.sleep(0.01) # sleep for T4 GPU
# report benchmark log if enabled
if args.enable_benchmark:
autolog.report()
if __name__ == "__main__":
main()