-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.py
202 lines (174 loc) · 5.99 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
import argparse
import os.path
from typing import List
import torch
from module_onnx.xfeat import XFeat
# from lightglue_onnx.end2end import normalize_keypoints
from utils import load_image
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--xfeat_path",
type=str,
default="weights/xfeat.pt",
required=False,
help="Path to load the feature extractor PT model.",
)
parser.add_argument(
"--output_path",
type=str,
default=None,
required=False,
help="Path to save the feature extractor ONNX model.",
)
parser.add_argument(
"--dynamic", action="store_true", help="Whether to allow dynamic image sizes."
)
parser.add_argument(
"--dense",
action="store_true",
help="Whether to export a dense keypoints extractor instead of simple extractor.",
)
parser.add_argument(
"--end2end",
action="store_true",
help="Whether to export an end-to-end pipeline instead of individual models.",
)
# Extractor-specific args:
parser.add_argument(
"--top_k",
type=int,
default=None,
required=False,
help="Maximum number of keypoints outputted by the extractor.",
)
return parser.parse_args()
def export_onnx(
xfeat_path=None,
output_path=None,
img0_path="assets/ref.png",
img1_path="assets/tgt.png",
dynamic=False,
dense=False,
end2end=False,
top_k=None,
):
# Sample images for tracing
image0, _ = load_image(img0_path)
image1, _ = load_image(img1_path)
# Models
xfeat = XFeat(weights=xfeat_path, top_k=top_k).eval()
# ONNX Export
if end2end:
# ------------------------------
# Export Extractor and Matching
# ------------------------------
output_path = xfeat_path.replace(".pt", "_e2e.onnx")
xfeat.forward = xfeat.match_xfeat
if dense:
xfeat.forward = xfeat.match_xfeat_star
output_path = xfeat_path.replace(".pt", "_dense_e2e.onnx")
dynamic_axes = {
"mkpts0": {0: "num_keypoints"},
"mkpts1": {0: "num_keypoints"},
}
if dynamic:
dynamic_axes.update({"image0": {2: "height", 3: "width"},
"image1": {2: "height", 3: "width"}})
else:
print(
f"WARNING: Exporting without --dynamic implies that the extractor's input image size will be locked to {image0.shape[-2:]}"
)
output_path = output_path.replace(
".onnx", f"_{image0.shape[-2]}x{image0.shape[-1]}.onnx"
)
torch.onnx.export(
xfeat,
(image0, image1),
output_path,
verbose=False,
do_constant_folding=True,
input_names=["image0", "image1"],
output_names=["mkpts0", "mkpts1"],
opset_version=17,
dynamic_axes=dynamic_axes,
)
else:
# -----------------
# Export Extractor
# -----------------
dynamic_axes = {
"keypoints": {0: "num_keypoints"},
"descriptors": {0: "num_keypoints"}
}
if dense:
output_path = xfeat_path.replace(".pt", "_dense.onnx")
xfeat.forward = xfeat.detectAndComputeDense
dynamic_axes.update({"scales": {0: "num_keypoints"}})
output_names = ["keypoints", "descriptors", "scales"]
else:
output_path = xfeat_path.replace(".pt", ".onnx")
xfeat.forward = xfeat.detectAndCompute
dynamic_axes.update({"scores": {0: "num_keypoints"}})
output_names = ["keypoints", "descriptors", "scores"]
# Add dynamic input
if dynamic:
dynamic_axes.update({"images": {2: "height", 3: "width"}})
else:
print(
f"WARNING: Exporting without --dynamic implies that the extractor's input image size will be locked to {image0.shape[-2:]}"
)
output_path = output_path.replace(
".onnx", f"_{image0.shape[-2]}x{image0.shape[-1]}.onnx"
)
# Export model
torch.onnx.export(
xfeat,
image0,
output_path,
verbose=False,
do_constant_folding=True,
input_names=["images"],
output_names=output_names,
opset_version=17,
dynamic_axes=dynamic_axes,
)
# -----------------
# Export Matching
# -----------------
# Simulate keypoints, features
kpts = torch.rand(top_k, 2, dtype=torch.float32)
descr = torch.rand(top_k, 64, dtype=torch.float32)
scales = torch.rand(top_k, dtype=torch.float32)
# Dynamic input
dynamic_axes = {
"kpts0": {0: "num_kpts0"},
"feats0": {0: "num_kpts0"},
"kpts1": {0: "num_kpts1"},
"feats1": {0: "num_kpts1"},
}
input_names = ["kpts0", "feats0", "kpts1", "feats1"]
input_values = [kpts, descr, kpts, descr]
if dense:
output_matching_path = os.path.join(os.path.dirname(output_path), "matching_dense.onnx")
xfeat.forward = xfeat.match_star_onnx
dynamic_axes.update({"scales0": {0: "num_kpts0"},})
input_names.append("scales0")
input_values.append(scales)
else:
output_matching_path = os.path.join(os.path.dirname(output_path), "matching.onnx")
xfeat.forward = xfeat.match_onnx
torch.onnx.export(
xfeat,
tuple(input_values),
output_matching_path,
verbose=False,
do_constant_folding=False,
input_names=input_names,
output_names=["mkpts0", "mkpts1"],
opset_version=17,
dynamic_axes=dynamic_axes,
)
if __name__ == "__main__":
args = parse_args()
export_onnx(**vars(args))