forked from PaddlePaddle/PaddleSeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer_dataset.py
329 lines (280 loc) · 10.6 KB
/
infer_dataset.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
# 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 time
import yaml
import numpy as np
import paddle
import paddle.nn.functional as F
from paddle.inference import create_predictor
from paddle.inference import Config as PredictConfig
from paddleseg.deploy.infer import DeployConfig
from paddleseg.cvlibs import manager
from paddleseg.utils import logger, metrics, progbar
from infer import auto_tune, use_auto_tune, Predictor
def parse_args():
parser = argparse.ArgumentParser(description='Model Infer')
parser.add_argument(
"--config",
dest="cfg",
help="The config file.",
default=None,
type=str,
required=True)
parser.add_argument(
'--dataset_type',
help='The name of dataset, such as Cityscapes, PascalVOC and ADE20K.',
type=str,
default=None,
required=True)
parser.add_argument(
'--dataset_path',
help='The directory of the dataset to be predicted. If set dataset_path, '
'it use the test and label images to calculate the mIoU.',
type=str,
default=None,
required=True)
parser.add_argument(
'--dataset_mode',
help='The dataset mode, such as train, val.',
type=str,
default="val")
parser.add_argument(
'--resize_width',
help='Set the resize width to acclerate the test. In default, it is 0, '
'which means use the origin width.',
type=int,
default=0)
parser.add_argument(
'--resize_height',
help='Set the resize height to acclerate the test. In default, it is 0, '
'which means use the origin height.',
type=int,
default=0)
parser.add_argument(
'--batch_size',
help='Mini batch size of one gpu or cpu.',
type=int,
default=1)
parser.add_argument(
'--device',
choices=['cpu', 'gpu'],
default="gpu",
help="Select which device to inference, defaults to gpu.")
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(
'--min_subgraph_size',
default=3,
type=int,
help='The min subgraph size in tensorrt prediction.')
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(
'--with_argmax',
help='Perform argmax operation on the predict result.',
action='store_true')
parser.add_argument(
'--print_detail',
help='Print GLOG information of Paddle Inference.',
action='store_true')
return parser.parse_args()
def get_dataset(args):
comp = manager.DATASETS
if args.dataset_type not in comp.components_dict:
raise RuntimeError("The dataset is not supported.")
cfg = DeployConfig(args.cfg)
if args.resize_width == 0 and args.resize_height == 0:
transforms = cfg.transforms.transforms
else:
# load and add resize to transforms
assert args.resize_width > 0 and args.resize_height > 0
with codecs.open(args.cfg, 'r', 'utf-8') as file:
dic = yaml.load(file, Loader=yaml.FullLoader)
transforms_dic = dic['Deploy']['transforms']
transforms_dic.insert(0, {
"type": "Resize",
'target_size': [args.resize_width, args.resize_height]
})
transforms = DeployConfig.load_transforms(transforms_dic).transforms
kwargs = {
'transforms': transforms,
'dataset_root': args.dataset_path,
'mode': args.dataset_mode
}
dataset = comp[args.dataset_type](**kwargs)
return dataset
def check_shape(shape, args):
assert args.resize_height == 0 or shape[2] == args.resize_height
assert args.resize_width == 0 or shape[3] == args.resize_width
def auto_tune(args, dataset, 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.
dataset(dataset): an dataset.
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)
num = min(len(dataset), 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 idx, (img, _) in enumerate(dataset):
data = np.array([img])
check_shape(data.shape, args)
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
if idx + 1 >= num:
break
logger.info("Auto tune success.\n")
class DatasetPredictor(Predictor):
def __init__(self, args):
super().__init__(args)
def run_dataset(self):
"""
Read the data from dataset and calculate the accurary of the inference model.
"""
dataset = get_dataset(self.args)
input_names = self.predictor.get_input_names()
input_handle = self.predictor.get_input_handle(input_names[0])
output_names = self.predictor.get_output_names()
output_handle = self.predictor.get_output_handle(output_names[0])
intersect_area_all = 0
pred_area_all = 0
label_area_all = 0
total_time = 0
progbar_val = progbar.Progbar(target=len(dataset), verbose=1)
for idx, (img, label) in enumerate(dataset):
data = np.array([img])
check_shape(data.shape, args)
input_handle.reshape(data.shape)
input_handle.copy_from_cpu(data)
start_time = time.time()
self.predictor.run()
pred = output_handle.copy_to_cpu()
end_time = time.time()
total_time += (end_time - start_time)
pred = self._postprocess(pred)
pred = paddle.to_tensor(pred, dtype='int64')
label = paddle.to_tensor(label, dtype="int32")
if pred.shape != label.shape:
label = paddle.unsqueeze(label, 0)
label = F.interpolate(label, pred.shape[-2:])
label = paddle.squeeze(label, 0)
intersect_area, pred_area, label_area = metrics.calculate_area(
pred,
label,
dataset.num_classes,
ignore_index=dataset.ignore_index)
intersect_area_all = intersect_area_all + intersect_area
pred_area_all = pred_area_all + pred_area
label_area_all = label_area_all + label_area
progbar_val.update(idx + 1)
class_iou, miou = metrics.mean_iou(intersect_area_all, pred_area_all,
label_area_all)
class_acc, acc = metrics.accuracy(intersect_area_all, pred_area_all)
kappa = metrics.kappa(intersect_area_all, pred_area_all, label_area_all)
logger.info("input width: {}, input height: {}".format(
args.resize_width, args.resize_height))
logger.info(
"[EVAL] #Images: {} mIoU: {:.4f} Acc: {:.4f} Kappa: {:.4f} ".format(
len(dataset), miou, acc, kappa))
logger.info("[EVAL] Class IoU: \n" + str(np.round(class_iou, 4)))
logger.info("[EVAL] Class Acc: \n" + str(np.round(class_acc, 4)))
logger.info("[EVAL] Average time: %.3f ms/img" %
(total_time * 1000.0 / len(dataset)))
def main(args):
if use_auto_tune(args):
dataset = get_dataset(args)
tune_img_nums = 10
auto_tune(args, dataset, tune_img_nums)
predictor = DatasetPredictor(args)
predictor.run_dataset()
if use_auto_tune(args) and \
os.path.exists(args.auto_tuned_shape_file):
os.remove(args.auto_tuned_shape_file)
if __name__ == '__main__':
"""
Based on the infer config and dataset, this program read the test and
label images, applys the transfors, run the predictor, output the accuracy.
For example:
python deploy/python/infer_dataset.py \
--config path/to/bisenetv2/deploy.yaml \
--dataset_type Cityscapes \
--dataset_path path/to/cityscapes
python deploy/python/infer_dataset.py \
--config path/to/bisenetv2/deploy.yaml \
--dataset_type Cityscapes \
--dataset_path path/to/cityscapes \
--device gpu \
--use_trt True \
--enable_auto_tune True
"""
args = parse_args()
main(args)