-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexport.py
More file actions
33 lines (29 loc) · 1.45 KB
/
export.py
File metadata and controls
33 lines (29 loc) · 1.45 KB
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
import os
import argparse
import torch
from pathlib import Path
from utils import netio, device
from model import Model
parser = argparse.ArgumentParser()
parser.add_argument('--batch-size', type=eval, required=True,
help='The batch size of exported model. A python expression that result in integer is acceptable')
parser.add_argument('--outdir', type=str, default='exported',
help='The output directory under the directory of model, defaults to "exported"')
parser.add_argument('--trt', action="store_true",
help='If specified, convert exported ONNX model to TensorRT workspace using trtexec')
parser.add_argument('model', type=Path,
help='Model to export')
args = parser.parse_args()
with torch.inference_mode():
ckpt_path = netio.find_checkpoint(args.model)
if ckpt_path is None:
raise FileNotFoundError(f"{ckpt_path} does not contain checkpoints")
out_dir: Path = ckpt_path.parent / args.outdir
out_dir.mkdir(exist_ok=True)
onnx_paths = Model.load(ckpt_path, device.default(), True).export(out_dir, ckpt_path.stem,
args.batch_size)
if args.trt:
for onnx_path in onnx_paths:
os.system(f"trtexec --onnx={onnx_path} --fp16 --saveEngine={onnx_path.with_suffix('.trt')} "
"--workspace=8192 --noDataTransfers")
print(f'Model exported to {out_dir}')