-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
141 lines (113 loc) · 3.98 KB
/
Copy pathexport.py
File metadata and controls
141 lines (113 loc) · 3.98 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# ------------------------------------------------------------------------
# LW-DETR
# Copyright (c) 2024 Baidu. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------
"""
export ONNX model and TensorRT engine for deployment
"""
import os
import ast
import random
import argparse
import subprocess
from pathlib import Path
import onnx
import torch
import onnxsim
import numpy as np
from PIL import Image
from modules.utils.fetch import fetch_model_module
from utils.timers import CudaTimer as CudaTimer
from omegaconf import OmegaConf
import torch
from utils.optimizer import OnnxOptimizer
def run_command_shell(command, dry_run:bool = False) -> int:
if dry_run:
print("")
print(f"CUDA_VISIBLE_DEVICES={os.environ['CUDA_VISIBLE_DEVICES']} {command}")
print("")
status = 0
else:
status = subprocess.call(command, shell=True)
return status
def make_infer_event(device="cuda"):
dummy_img = torch.randint(0, 10, (20, 384, 640), dtype=torch.float32)
inps = dummy_img.to(device)
inps = torch.stack([inps for _ in range(1)])
return inps
def make_infer_image(device="cuda"):
dummy_img = torch.randint(0, 10, (3, 384, 640), dtype=torch.float32)
inps = dummy_img.to(device)
inps = torch.stack([inps for _ in range(1)])
return inps
def export_onnx(model, input_names, input_tensors, output_names, dynamic_axes):
output_file = '/home/sheusinger/sast.onnx'
torch.onnx.export(
model,
input_tensors,
output_file,
input_names=input_names,
output_names=output_names,
export_params=True,
keep_initializers_as_inputs=True,
do_constant_folding=True,
verbose=True,
opset_version=17,
dynamic_axes=dynamic_axes
)
print(f'Successfully exported ONNX model: {output_file}')
return output_file
def onnx_simplify(onnx_dir:str, input_names, input_tensors):
sim_onnx_dir = onnx_dir.replace(".onnx", ".sim.onnx")
if isinstance(input_tensors, torch.Tensor):
input_tensors = [input_tensors]
print(f'start simplify ONNX model: {onnx_dir}')
opt = OnnxOptimizer(onnx_dir)
opt.info('Model: original')
opt.common_opt()
opt.info('Model: optimized')
opt.save_onnx(sim_onnx_dir)
return sim_onnx_dir
def trtexec(onnx_dir:str) -> None:
engine_dir = onnx_dir.replace(".onnx", f".engine")
addition = "--useCudaGraph --useSpinWait --warmUp=500 --avgRuns=1000"
verbose = ""
command = " ".join([
"trtexec",
f"--onnx={onnx_dir}",
f"--saveEngine={engine_dir}",
f"--fp16", # --workspace=4096
f"{addition}",
f"{verbose}"])
status = run_command_shell(command)
assert status == 0, f"error({status}) in infer command: {command}"
print(f'Successfully serialized TensorRT engine: {engine_dir}')
return engine_dir
def main():
# Load the configuration file
config = OmegaConf.load('config/detect_lwdetr.yaml')
# device for export onnx
device = torch.device("cpu")
# fix the seed for reproducibility
seed = 42
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# Load the model from the checkpoint
module = fetch_model_module(config=config)
# model = module.load_from_checkpoint(config.checkpoint, **{'full_config': config})
model = module
# Make sure the model is in evaluation mode
model.eval()
input_tensors1 = make_infer_event(device)
input_tensors2 = make_infer_image(device)
input_tensors = (input_tensors1, input_tensors2)
input_names = ['input']
output_names = ['dets']
dynamic_axes = None
output_file = export_onnx(model, input_names, input_tensors, output_names, dynamic_axes)
output_file = onnx_simplify(output_file, input_names, input_tensors)
output_file = trtexec(output_file)
if __name__ == '__main__':
main()