-
Notifications
You must be signed in to change notification settings - Fork 15
/
torch_to_onnx.py
41 lines (32 loc) · 1.08 KB
/
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
import os
import argparse
import onnx
import torch
from siamese import SiameseNetwork
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-c',
'--checkpoint',
type=str,
help="Path of model checkpoint to be used for inference.",
required=True
)
parser.add_argument(
'-o',
'--out_path',
type=str,
help="Path for saving tensorrt model.",
required=True
)
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
checkpoint = torch.load(args.checkpoint)
model = SiameseNetwork(backbone=checkpoint['backbone'])
model.to(device)
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
torch.onnx.export(model, (torch.rand(1, 3, 224, 224).to(device), torch.rand(1, 3, 224, 224).to(device)), args.out_path, input_names=['input'],
output_names=['output'], export_params=True)
onnx_model = onnx.load(args.out_path)
onnx.checker.check_model(onnx_model)