-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_onnx_model.py
36 lines (28 loc) · 1.13 KB
/
convert_onnx_model.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
import torch
from models import SketchColorizationModel
def convert_onnx_model(path: str, save_dir: str):
model_ts = torch.jit.load(path)
model = SketchColorizationModel()
model.load_state_dict(model_ts.state_dict())
sample_data_line = torch.randn(1, 1, 512, 512)
sample_data_line_draft = torch.randn(1, 1, 128, 128)
sample_data_hint = torch.randn(1, 4, 128, 128)
model.eval()
model.forward(sample_data_line,
sample_data_line_draft,
sample_data_hint)
torch.onnx.export(model,
(sample_data_line,
sample_data_line_draft,
sample_data_hint),
save_dir,
opset_version=11,
export_params=True,
input_names=['line',
'line_draft',
'hint'],
output_names=['colored'])
if __name__ == "__main__":
model_path = './SketchColorizationModel.zip'
save_dir = './SketchColorizationModel.onnx'
convert_onnx_model(model_path, save_dir)