-
Notifications
You must be signed in to change notification settings - Fork 193
/
pytorch2onnx.py
36 lines (27 loc) · 1.55 KB
/
pytorch2onnx.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
import argparse
import torch
import model.detector
import utils.utils
if __name__ == '__main__':
#指定训练配置文件
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default='',
help='Specify training profile *.data')
parser.add_argument('--weights', type=str, default='',
help='The path of the .pth model to be transformed')
parser.add_argument('--output', type=str, default='./model.onnx',
help='The path where the onnx model is saved')
opt = parser.parse_args()
cfg = utils.utils.load_datafile(opt.data)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.detector.Detector(cfg["classes"], cfg["anchor_num"], True, True).to(device)
model.load_state_dict(torch.load(opt.weights, map_location=device))
#sets the module in eval node
model.eval()
test_data = torch.rand(1, 3, cfg["height"], cfg["width"]).to(device)
torch.onnx.export(model, #model being run
test_data, # model input (or a tuple for multiple inputs)
opt.output, # 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