-
Notifications
You must be signed in to change notification settings - Fork 98
/
export_onnx.py
156 lines (125 loc) · 4.92 KB
/
export_onnx.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
"""Export a checkpoint as an ONNX model.
Applies onnx utilities to improve the exported model and
also tries to simplify the model with onnx-simplifier.
https://github.com/onnx/onnx/blob/master/docs/PythonAPIOverview.md
https://github.com/daquexian/onnx-simplifier
"""
import argparse
import shutil
import torch
import openpifpaf
try:
import onnx
import onnx.utils
except ImportError:
onnx = None
try:
import onnxsim
except ImportError:
onnxsim = None
def apply(model, outfile, verbose=True):
# dummy_input = torch.randn(1, 3, 193, 257)
dummy_input = torch.randn(1, 3, 97, 129)
# Providing input and output names sets the display names for values
# within the model's graph. Setting these does not change the semantics
# of the graph; it is only for readability.
#
# The inputs to the network consist of the flat list of inputs (i.e.
# the values you would pass to the forward() method) followed by the
# flat list of parameters. You can partially specify names, i.e. provide
# a list here shorter than the number of inputs to the model, and we will
# only set that subset of names, starting from the beginning.
input_names = ['input_batch']
# output_names = [
# 'pif_c',
# 'pif_r',
# 'pif_b',
# 'pif_s',
# 'paf_c',
# 'paf_r1',
# 'paf_r2',
# 'paf_b1',
# 'paf_b2',
# ]
output_names = ['cif', 'caf']
torch.onnx.export(
model, dummy_input, outfile, verbose=verbose,
input_names=input_names, output_names=output_names,
keep_initializers_as_inputs=True,
# opset_version=10,
do_constant_folding=True,
export_params=True,
# dynamic_axes={ # TODO: gives warnings
# 'input_batch': {0: 'batch', 2: 'height', 3: 'width'},
# 'pif_c': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# 'pif_r': {0: 'batch', 3: 'fheight', 4: 'fwidth'},
# 'pif_b': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# 'pif_s': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# 'paf_c': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# 'paf_r1': {0: 'batch', 3: 'fheight', 4: 'fwidth'},
# 'paf_b1': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# 'paf_r2': {0: 'batch', 3: 'fheight', 4: 'fwidth'},
# 'paf_b2': {0: 'batch', 2: 'fheight', 3: 'fwidth'},
# },
)
def optimize(infile, outfile=None):
if outfile is None:
assert infile.endswith('.onnx')
outfile = infile
infile = infile.replace('.onnx', '.unoptimized.onnx')
shutil.copyfile(outfile, infile)
model = onnx.load(infile)
optimized_model = onnx.optimizer.optimize(model)
onnx.save(optimized_model, outfile)
def check(modelfile):
model = onnx.load(modelfile)
onnx.checker.check_model(model)
def polish(infile, outfile=None):
if outfile is None:
assert infile.endswith('.onnx')
outfile = infile
infile = infile.replace('.onnx', '.unpolished.onnx')
shutil.copyfile(outfile, infile)
model = onnx.load(infile)
polished_model = onnx.utils.polish_model(model)
onnx.save(polished_model, outfile)
def simplify(infile, outfile=None):
if outfile is None:
assert infile.endswith('.onnx')
outfile = infile
infile = infile.replace('.onnx', '.unsimplified.onnx')
shutil.copyfile(outfile, infile)
simplified_model, check_ok = onnxsim.simplify(infile, check_n=3, perform_optimization=False)
assert check_ok
onnx.save(simplified_model, outfile)
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass
def main():
parser = argparse.ArgumentParser(
prog='python3 -m openpifpaf.export_onnx',
description=__doc__,
formatter_class=CustomFormatter,
)
parser.add_argument('--version', action='version',
version='OpenPifPaf {version}'.format(version=openpifpaf.__version__))
parser.add_argument('--checkpoint', default='resnet50')
parser.add_argument('--outfile', default='openpifpaf-resnet50.onnx')
parser.add_argument('--simplify', dest='simplify', default=False, action='store_true')
parser.add_argument('--polish', dest='polish', default=False, action='store_true',
help='runs checker, optimizer and shape inference')
parser.add_argument('--optimize', dest='optimize', default=False, action='store_true')
parser.add_argument('--check', dest='check', default=False, action='store_true')
args = parser.parse_args()
model, _ = openpifpaf.network.factory(checkpoint=args.checkpoint)
apply(model, args.outfile)
if args.simplify:
simplify(args.outfile)
if args.optimize:
optimize(args.outfile)
if args.polish:
polish(args.outfile)
if args.check:
check(args.outfile)
if __name__ == '__main__':
main()