-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
260 lines (228 loc) · 9.02 KB
/
predict.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
# -*- coding: utf-8 -*-
"""
.. codeauthor:: Daniel Seichter <daniel.seichter@tu-ilmenau.de>
"""
import argparse as ap
import os
import cv2
from nicr_scene_analysis_datasets import Hypersim
from nicr_scene_analysis_datasets.utils.img import save_indexed_png
import numpy as np
import onnx
import onnxruntime as ort
from tqdm import tqdm
from utils import DEFAULT_DATASET_PATH
from utils import DEFAULT_ONNX_FILEPATH
from utils import DEFAULT_PREDICTIONS_PATH
def _get_ort_session(onnx_filepath, img_hw, topk=3):
model = onnx.load(onnx_filepath)
# get network output shape (same as input shape)
# note: our optimizations to the resize operations seem to break onnx's
# shape inference with OpSet >= 13
model_output_img_shape = (
model.graph.input[0].type.tensor_type.shape.dim[2].dim_value,
model.graph.input[0].type.tensor_type.shape.dim[3].dim_value
)
# add missing nodes: final upsampling, softmax, and topk
# see: https://github.com/onnx/onnx/blob/main/docs/Operators.md
# -> final upsampling
final_upsampling_node = onnx.helper.make_node(
'Resize',
# inputs=['output', 'roi', 'scales'],
inputs=['output', '', 'scales'], # '' for 'roi' requires OpSet >= 13
outputs=['final_upsampling_output'],
coordinate_transformation_mode='pytorch_half_pixel',
cubic_coeff_a=-0.75,
mode='linear',
nearest_mode='floor',
)
# roi = onnx.helper.make_tensor('roi', onnx.TensorProto.FLOAT, [0], [])
scale_h = img_hw[0] / model_output_img_shape[0]
scale_w = img_hw[1] / model_output_img_shape[1]
scales = onnx.helper.make_tensor('scales',
onnx.TensorProto.FLOAT, [4],
[1, 1, scale_h, scale_w])
# -> softmax (note that softmax op with 4D inputs requires OpSet >= 13)
softmax_node = onnx.helper.make_node(
'Softmax',
inputs=['final_upsampling_output'],
outputs=['prediction'],
axis=1
)
# topk
topk_node = onnx.helper.make_node(
'TopK',
inputs=['prediction', 'k'],
outputs=['scores', 'classes'],
axis=1,
largest=1,
sorted=1
)
k = onnx.helper.make_tensor('k', onnx.TensorProto.INT64, [1], [int(topk)])
# add new nodes and initializers to graph
# model.graph.initializer.append(roi)
model.graph.initializer.append(scales)
model.graph.node.append(final_upsampling_node)
model.graph.node.append(softmax_node)
model.graph.initializer.append(k)
model.graph.node.append(topk_node)
# replace output information
if model.graph.input[0].type.tensor_type.shape.dim[0].dim_param:
# dynamic batch axis
b = model.graph.input[0].type.tensor_type.shape.dim[0].dim_param
else:
# fixed batch axis
b = model.graph.input[0].type.tensor_type.shape.dim[0].dim_value
scores_info = onnx.helper.make_tensor_value_info('scores',
onnx.TensorProto.FLOAT,
shape=[b, topk, *img_hw])
classes_info = onnx.helper.make_tensor_value_info('classes',
onnx.TensorProto.INT64,
shape=[b, topk, *img_hw])
model.graph.output.pop(0)
model.graph.output.append(scores_info)
model.graph.output.append(classes_info)
# perform final check
onnx.checker.check_model(model)
# onnx.save(model, './model.onnx')
# create onnxruntime seesion
ort_session = ort.InferenceSession(
model.SerializeToString(),
providers=[
# 'TensorrtExecutionProvider',
'CUDAExecutionProvider',
'CPUExecutionProvider'
]
)
return ort_session
def _parse_args():
parser = ap.ArgumentParser(formatter_class=ap.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--onnx-filepath',
type=str,
default=DEFAULT_ONNX_FILEPATH,
help="Path to ONNX model to use."
)
parser.add_argument(
'--dataset-path',
type=str,
default=DEFAULT_DATASET_PATH,
help="Path to the dataset."
)
parser.add_argument(
'--dataset-split',
type=str,
default='test',
help="Dataset split to use."
)
parser.add_argument(
'--output-path',
type=str,
default=DEFAULT_PREDICTIONS_PATH,
help="Path where to store predicted semantic segmentation."
)
parser.add_argument(
'--topk',
type=int,
default=3,
help="TopK classes to consider."
)
return parser.parse_args()
def main():
# args
args = _parse_args()
# load data
dataset = Hypersim(
dataset_path=args.dataset_path,
split=args.dataset_split,
subsample=None,
sample_keys=('identifier', 'rgb', 'depth'),
depth_mode='raw'
)
RGB_MEAN = np.array((0.485, 0.456, 0.406), dtype='float32') * 255
RGB_STD = np.array((0.229, 0.224, 0.225), dtype='float32') * 255
# ensure that the used depth stats are valid for this model (there was a
# copy and paste issue that we fixed in future versions of
# nicr_scene_analysis_datasets)
assert dataset.depth_mean == 6249.621001070915
assert dataset.depth_std == 6249.621001070915 # <- c&p ^^
# process files (for simplification with batch size 1)
ort_session = None
for sample in tqdm(dataset, desc='Processing files'):
# load model lazily (we need a sample to get the spatial dimensions)
if ort_session is None:
ort_session = _get_ort_session(
onnx_filepath=args.onnx_filepath,
img_hw=sample['rgb'].shape[:2],
topk=args.topk
)
# get network input shape (from rgb input)
h, w = ort_session.get_inputs()[0].shape[-2:]
# rgb preprocessing
# -> resize
rgb = cv2.resize(sample['rgb'], (w, h),
interpolation=cv2.INTER_LINEAR)
# -> normalize
rgb = rgb.astype('float32')
rgb -= RGB_MEAN[None, None, ...]
rgb /= RGB_STD[None, None, ...]
# -> create tensor (add batch axis, channels first)
rgb = rgb.transpose(2, 0, 1)[None, ...]
# depth preprocessing
# -> resize
depth = cv2.resize(sample['depth'], (w, h),
interpolation=cv2.INTER_NEAREST)
# -> normalize
mask_invalid = depth == 0 # mask for invalid depth values
depth = depth.astype('float32')
depth -= dataset.depth_mean
depth /= dataset.depth_std
# reset invalid values (the network should not be able to learn from
# these pixels)
depth[mask_invalid] = 0
# -> create tensor (add batch and channel axes)
depth = depth[None, None, ...]
# apply model
scores, classes = ort_session.run(None, {'rgb': rgb, 'depth': depth})
# remove batch axis
scores = scores[0]
classes = classes[0]
# cast classes to uint8 (< 255 classes)
classes = classes.astype('uint8')
# create predicted segmentation
# note that we store the topk predictions as class_idx + score (to
# save some space), you may further can think about using float16
scores_clamped = np.clip(scores, a_min=0, a_max=0.9999)
classes = classes + 1 # add void class (void + 40 classes)
segmentation = scores_clamped + classes
# ensure that class is still correct (top0 only)
assert (segmentation[0].astype('uint8') == classes[0]).all()
# store predicted segmentation
# -> topk prediction (for mapping later)
fp = os.path.join(args.output_path, args.dataset_split,
f'{Hypersim.SEMANTIC_DIR}_topk',
*sample['identifier'])
os.makedirs(os.path.dirname(fp), exist_ok=True)
np.save(f'{fp}.npy', segmentation)
# -> predicted classes
for i in range(args.topk):
dirname = Hypersim.SEMANTIC_DIR
if i > 0:
dirname += f'_topk_{i}'
fp = os.path.join(args.output_path, args.dataset_split,
dirname, *sample['identifier'])
os.makedirs(os.path.dirname(fp), exist_ok=True)
cv2.imwrite(f'{fp}.png', segmentation[i].astype('uint8'))
# -> predicted classes as colored images (with color palette, do not
# load these images later on with OpenCV, PIL is fine)
for i in range(args.topk):
dirname = Hypersim.SEMANTIC_COLORED_DIR
if i > 0:
dirname += f'_topk_{i}'
fp = os.path.join(args.output_path, args.dataset_split,
dirname, *sample['identifier'])
os.makedirs(os.path.dirname(fp), exist_ok=True)
save_indexed_png(f'{fp}.png', segmentation[i].astype('uint8'),
colormap=dataset.semantic_class_colors)
if __name__ == '__main__':
main()