-
Notifications
You must be signed in to change notification settings - Fork 1
/
export.py
37 lines (30 loc) · 1.03 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
import torch
from logger import log
from utils import resolve_device
from model import StyleNet
def export_to_onnx(ckpt_dir: str, out_path: str, imsize=1024):
log.info("PyTorch --> ONNX Exporter.")
log.info(f"Loading model with ckpt: {ckpt_dir}")
device = resolve_device()
model = StyleNet(ckpt_dir).to(device)
input_names = ["content_img", "style_img", "alpha"]
output_names = ["output_img"]
return_t = torch.Tensor([0]).to(device)
infer = torch.Tensor([1]).to(device)
alpha = torch.Tensor([1.0]).to(device)
dummy_tensor = torch.ones(1, 3, imsize, imsize).to(device)
dummy_input = (dummy_tensor, dummy_tensor, alpha, return_t, infer)
log.info("Exporting model to ONNX...")
torch.onnx.export(
model,
dummy_input,
out_path,
input_names=input_names,
output_names=output_names,
verbose=True,
opset_version=9,
)
log.info(
f"Model with input size {imsize} exported to ONNX at {out_path}."
)
log.info("Done Done London!")