forked from kentaroy47/pytorch-onnx-tensorrt-CIFAR10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onnx_export.py
57 lines (42 loc) · 1.59 KB
/
onnx_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
import torch
from PIL import Image
from torchvision.transforms import ToTensor
import numpy as np
import numpy as np
# exporter settings
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='res18', help="set model checkpoint path")
parser.add_argument('--model_out', type=str, default='resnet18.onnx')
#parser.add_argument('--image', type=str, required=True, help='input image to use')
args = parser.parse_args()
print(args)
# set the device
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('running on device ' + str(device))
# load the image
#img = Image.open(opt.image)
#img_to_tensor = ToTensor()
#input = img_to_tensor(img).view(1, -1, img.size[1], img.size[0]).to(device)
pixels = 32
img = np.random.rand(pixels,pixels,3)
input = torch.from_numpy(img).view(1,3,pixels,pixels).float().to(device)
#print('input image size {:d}x{:d}'.format(img.size[0], img.size[1]))
print("input size is..", input.shape)
# load the model
from models import *
model = ResNet18().to(device)
# this may not coexist with onnx.
#from fp16util import network_to_half
#model = network_to_half(model)
# DataParallel does not coexist with onnx.
print(model)
checkpoint = torch.load(args.model)
model.load_state_dict(checkpoint['net'])
print("model set!")
# export the model
input_names = [ "input_0" ]
output_names = [ "output_0" ]
print('exporting model to ONNX...')
torch.onnx.export(model, input, args.model_out, verbose=True, input_names=input_names, output_names=output_names, opset_version=9)
print('model exported to {:s}'.format(args.model_out))