-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexport.py
204 lines (167 loc) · 6.55 KB
/
export.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
import argparse
from pathlib import Path
import torch
import torch.nn as nn
from backends.decode import decode_detection
import yaml
from importlib import import_module
try:
import onnx
import onnx.utils
from onnxsim import simplify
has_simplify = True
except BaseException:
has_simplify = False
pass
class CenterNet(nn.Module):
def __init__(self, backend, max_detections, is_rotated=False, nms=3):
super().__init__()
self.backend = backend
self.max_detections = max_detections
self.is_rotated = is_rotated
self.nms = nms
def forward(self, x):
out = self.backend(x)
has_kps = "kps" in out
dets = decode_detection(
torch.clamp(out["hm"].sigmoid_(), min=1e-4, max=1 - 1e-4),
out["wh"],
out["reg"],
kps=out["kps"] if has_kps else None,
K=self.max_detections,
rotated=self.is_rotated,
nms_size=self.nms)
if has_kps:
dets, kps = dets
kps[..., 0:2] *= self.backend.down_ratio
dets[:, :, :4] *= self.backend.down_ratio
# boxes, scores, classes
if self.is_rotated:
if has_kps:
return dets[:, :, :5], dets[:, :, 5], dets[:, :, 6], kps
return dets[:, :, :5], dets[:, :, 5], dets[:, :, 6]
if has_kps:
return dets[:, :, :4], dets[:, :, 4], dets[:, :, 5], kps
return dets[:, :, :4], dets[:, :, 4], dets[:, :, 5]
def build_model(experiment, model_spec, without_decode_detections,
max_detections, nms=3, use_last=True):
module = import_module(f"backends.{model_spec['name']}")
backend = getattr(module, 'build')(**model_spec["params"])
ckpt = experiment / ('model_last.pth' if use_last else 'model_best.pth')
if ckpt.exists():
checkpoint = torch.load(ckpt)
backend.load_state_dict(checkpoint['state_dict'])
print(f"Restore weights {ckpt} successful!")
else:
print(f"No weights were found in folder {experiment}")
if model_spec['name'] == 'efficientnet':
backend.base.set_swish(memory_efficient=False)
if not without_decode_detections:
model = CenterNet(backend, max_detections,
model_spec["params"]["rotated_boxes"], nms)
else:
model = backend
model.eval()
return model
def export_model(experiment, model, model_name,
input_shape, without_decode_detections, simplify_model):
shape = [1, ] + input_shape
x = torch.randn(*shape, requires_grad=True)
torch_out = model(x)
possible_outputs = ['boxes', 'scores', 'classes', 'kps']
if without_decode_detections:
outputs = list(torch_out.keys())
else:
outputs = [possible_outputs[i] for i in range(len(torch_out))]
suffix = '_wd' if without_decode_detections else ''
output_path = experiment / \
f"centernet_{model_name}_{shape[2]}x{shape[3]}{suffix}.onnx"
torch.onnx.export(model, # model being run
x,
output_path,
export_params=True, # store the trained parameter weights inside the model file
opset_version=11, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=['input'], # the model's input names
output_names=outputs)
# dynamic_axes={'input': {0: 'batch_size'}})
print(f"Export model to {output_path} successful!")
if simplify_model:
print("Simplify model")
if not has_simplify:
print("Can't simplify model because ONNX Simplifier is not install")
print("Please install it: pip install onnx-simplifier")
return
onnx_model = onnx.load(output_path)
simplyfied_model, check = simplify(
onnx_model, input_shapes={"input": shape})
simplyfied_model = onnx.shape_inference.infer_shapes(simplyfied_model)
onnx_model = onnx.utils.polish_model(simplyfied_model)
onnx.checker.check_model(simplyfied_model)
onnx.checker.check_model(onnx_model)
output_path = experiment / \
f"centernet_{model_name}_{shape[2]}x{shape[3]}{suffix}_smpl.onnx"
onnx.save(simplyfied_model, output_path)
print(f"Export simplified model to {output_path} successful!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Export an experiment to onnx model')
parser.add_argument(
"-e", "--experiment", required=True,
help="Path to the experiment that should be exported.")
parser.add_argument(
"-i",
"--input_shape",
type=int,
nargs='+',
default=[800, 800],
help="Input size for onnx model")
parser.add_argument(
"-l", "--use_last", action="store_true",
help="If given, not the best but the last checkpoint will be restored.")
parser.add_argument(
"-wd", "--without_decode_detections", action="store_true",
help="If given, CenterNet output will be the heads instead of boxes. \
The reason for this is that boxes leads to problems with TensorRT.")
parser.add_argument(
"-s", "--simplify", action="store_true",
help="If given it will be tried to simplify the model using onnx-simplifier."
)
parser.add_argument(
"--nms", type=int, default=3,
help="Kernel size for max pooling or CenterNet NMS."
)
args = parser.parse_args()
experiment = Path(args.experiment)
if not experiment.exists():
print("Experiment does not exists!")
exit(-1)
shape_len = len(args.input_shape)
if shape_len == 1:
input_shape = [3, args.input_shape[0], args.input_shape[1]]
elif shape_len == 2:
input_shape = [3, ] + args.input_shape
else:
print("Invlaid input shape ")
exit(-2)
g = list(experiment.glob('*/config.yaml'))
if len(g) == 0:
print("No config.yaml file where found in experiment folder!")
exit(-1)
with open(g[0]) as f:
cfg = yaml.load(f)
model_specs = cfg["model"]["backend"]
model = build_model(
experiment,
model_specs,
args.without_decode_detections,
cfg["max_detections"],
args.nms,
args.use_last)
export_model(
experiment,
model,
model_specs["name"],
input_shape,
args.without_decode_detections,
args.simplify)