-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_preprocessing_onnx.py
80 lines (66 loc) · 2.08 KB
/
make_preprocessing_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
from argparse import ArgumentParser
from typing import List
import numpy as np
import onnx
import torch
import torch.nn as nn
from onnxsim import simplify
class Preprocess(nn.Module):
def __init__(self, input_shape: List[int]):
super(Preprocess, self).__init__()
self.input_shape = tuple(input_shape)
self.mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
self.std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
# self.register_buffer(
# "mean", torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
# )
# self.register_buffer(
# "std", torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
# )
def forward(self, x: torch.Tensor):
x = torch.nn.functional.interpolate(
input=x,
size=self.input_shape[2:],
)
# x = x * (1.0 / 255.0)
x = x / 255.0
x = (x - self.mean) / self.std
return x
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-o", "--opset", type=int, default=20, help="onnx opset")
parser.add_argument(
"-s",
"--input_shape",
type=int,
nargs=4,
default=[1, 3, 448, 448],
help="input shape",
)
args = parser.parse_args()
MODEL = f"01_prep"
OPSET = args.opset
INPUT_SHAPE: List[int] = args.input_shape
model = Preprocess(input_shape=INPUT_SHAPE)
onnx_file = f"{MODEL}_{'_'.join(map(str, INPUT_SHAPE))}.onnx"
x = torch.randn(INPUT_SHAPE)
torch.onnx.export(
model,
args=(x),
f=onnx_file,
opset_version=OPSET,
input_names=["input_rgb"],
output_names=["output_prep"],
dynamic_axes={
"input_rgb": {
2: "H",
3: "W",
},
},
)
model_onnx1 = onnx.load(onnx_file)
model_onnx1 = onnx.shape_inference.infer_shapes(model_onnx1)
onnx.save(model_onnx1, onnx_file)
model_onnx2 = onnx.load(onnx_file)
model_simp, check = simplify(model_onnx2)
onnx.save(model_simp, onnx_file)