-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_torch_to_onnx.py
87 lines (71 loc) · 2.65 KB
/
convert_torch_to_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
import numpy as np
import torch
from peekingduck.pipeline.nodes.model.yoloxv1.yolox_files.model import YOLOX
# technotes:
# YOLOX input shape = (1, 3, 416, 416)
# output shape = (N, 85) Qn: what is N?
# where cols[0:79] are the 80 classes,
# [80:83] is the bbox, [84] is confidence
INPUT_SIZE = (416, 416)
YOLOX_DIR = "/home/aisg/src/ongtw/PeekingDuck/peekingduck_weights/yolox"
MODEL_MAP = {
"yolox-tiny": {
"path": "/home/aisg/src/ongtw/PeekingDuck/peekingduck_weights/yolox/yolox-tiny.pth",
"size": {"depth": 0.33, "width": 0.375},
},
"yolox-s": {
"path": "/home/aisg/src/ongtw/PeekingDuck/peekingduck_weights/yolox/yolox-s.pth",
"size": {"depth": 0.33, "width": 0.5},
},
"yolox-m": {
"path": "/home/aisg/src/ongtw/PeekingDuck/peekingduck_weights/yolox/yolox-m.pth",
"size": {"depth": 0.67, "width": 0.75},
},
"yolox-l": {
"path": "/home/aisg/src/ongtw/PeekingDuck/peekingduck_weights/yolox/yolox-l.pth",
"size": {"depth": 1.0, "width": 1.0},
},
}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_type = "yolox-tiny"
def get_model_path(model_code: str) -> str:
the_path = MODEL_MAP[model_code]["path"]
return the_path
def get_model_size(model_code: str) -> str:
the_size = MODEL_MAP[model_code]["size"]
return the_size
def get_model_save_path(model_code: str) -> str:
the_path = f"{YOLOX_DIR}/{model_code}.onnx"
return the_path
def convert_model(model_code: str):
model_path = get_model_path(model_code)
model_save_path = get_model_save_path(model_code)
model_size = get_model_size(model_code)
print(f"generating {model_save_path}")
ckpt = torch.load(model_path, map_location="cpu")
model = YOLOX(80, model_size["depth"], model_size["width"])
# model.half()
model.eval()
model.load_state_dict(ckpt["model"])
# model = fuse_model(model) # what is this?
batch_size = 1
x = torch.randn(batch_size, 3, 416, 416, requires_grad=True)
torch_out = model(x)
torch.onnx.export(
model,
x,
"yolox.onnx",
verbose=True,
export_params=True,
opset_version=10,
do_constant_folding=False,
input_names=["images"],
output_names=["pred_output"],
# output_names=["classes", "bboxes"],
# dynamic_axes={"input":{0:"batch_size"},
# "output":{0:"batch_size"}}
)
if __name__ == "__main__":
for model_code in MODEL_MAP.keys():
convert_model(model_code)
break