-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathexport.py
More file actions
69 lines (61 loc) · 2.25 KB
/
Copy pathexport.py
File metadata and controls
69 lines (61 loc) · 2.25 KB
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
import argparse
import torch
from train import AnimeSegmentation, net_names
def export_onnx(model, img_size, path):
import onnx
from onnxsim import simplify
torch.onnx.export(
model, # model being run
torch.randn(
1, 3, img_size, img_size
), # model input (or a tuple for multiple inputs)
path, # where to save the model (can be a file or file-like object)
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=["img"], # the model's input names
output_names=["mask"], # the model's output names
dynamic_axes={
"img": {0: "batch_size"}, # variable length axes
"mask": {0: "batch_size"},
},
verbose=True,
)
onnx_model = onnx.load(path)
model_simp, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.save(model_simp, path)
print("finished exporting onnx")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# model args
parser.add_argument(
"--net", type=str, default="isnet_is", choices=net_names, help="net name"
)
parser.add_argument(
"--ckpt",
type=str,
default="saved_models/isnetis.ckpt",
help="model checkpoint path",
)
parser.add_argument(
"--out", type=str, default="saved_models/isnetis.onnx", help="output path"
)
parser.add_argument(
"--to",
type=str,
default="onnx",
choices=["only_state_dict", "only_net_state_dict", "onnx"],
help="export to ()",
)
parser.add_argument("--img-size", type=int, default=1024, help="input image size")
opt = parser.parse_args()
print(opt)
model = AnimeSegmentation.try_load(opt.net, opt.ckpt, "cpu", img_size=opt.img_size)
model.eval()
if opt.to == "only_state_dict":
torch.save(model.state_dict(), opt.out)
elif opt.to == "only_net_state_dict":
torch.save(model.net.state_dict(), opt.out)
elif opt.to == "onnx":
export_onnx(model, opt.img_size, opt.out)