-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
242 lines (212 loc) · 7.17 KB
/
main.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import argparse
import json
import time
import warnings
from pathlib import Path
from typing import List, Tuple
import torch
from torch.utils.mobile_optimizer import optimize_for_mobile
from utils import LOGGER, colorstr, export_formats, file_size, try_export
@try_export
def export_torchscript(
model: torch.ModuleDict,
img: torch.Tensor,
file: Path,
optimize: bool,
prefix=colorstr("TorchScript:"),
):
LOGGER.info(f"\n{prefix} starting export with torch {torch.__version__}...")
f = file.with_suffix(".torchscript")
ts = torch.jit.trace(model, img, strict=False)
extra_files = {"config.txt": json.dumps({"shape": img.shape})}
if optimize:
optimize_for_mobile(ts)._save_for_lite_interpreter(
str(f), _extra_files=extra_files
)
else:
ts.save(str(f), _extra_files=extra_files)
return f, None
@try_export
def export_onnx(
model: torch.ModuleDict,
im: torch.Tensor,
file: Path,
opset: int,
train: bool,
dynamic: bool,
prefix=colorstr("ONNX:"),
):
import onnx
LOGGER.info(f"\n{prefix} starting export with onnx {onnx.__version__}...")
f = file.with_suffix(".onnx")
torch.onnx.export(
model.cpu() if dynamic else model, # --dynamic only compatible with cpu
im.cpu() if dynamic else im,
f,
verbose=False,
opset_version=opset,
training=torch.onnx.TrainingMode.TRAINING
if train
else torch.onnx.TrainingMode.EVAL,
do_constant_folding=not train,
input_names=["images"],
output_names=["output"],
dynamic_axes={
"images": {0: "batch"},
"output": {0: "batch"},
}
if dynamic
else None,
)
# Checks
model_onnx = onnx.load(f) # load onnx model
onnx.checker.check_model(model_onnx) # check onnx model
onnx.save(model_onnx, f)
return f, model_onnx
@try_export
def export_engine(
model: torch.ModuleDict,
file: Path,
im: torch.Tensor,
dynamic: bool,
verbose: bool = False,
workspace: int = 4,
prefix=colorstr("TensorRT:"),
):
assert im.device.type != "cpu", "export running on CPU but must be on GPU"
import tensorrt as trt
export_onnx(model, im, file, 13, False, dynamic)
onnx = file.with_suffix(".onnx")
LOGGER.info(f"\n{prefix} starting export with TensorRT {trt.__version__}...")
assert onnx.exists(), f"failed to export ONNX file: {onnx}"
f = file.with_suffix(".engine") # TensorRT engine file
logger = trt.Logger(trt.Logger.INFO)
if verbose:
logger.min_severity = trt.Logger.Severity.VERBOSE
builder = trt.Builder(logger)
config = builder.create_builder_config()
config.max_workspace_size = workspace * 1 << 30
flag = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
network = builder.create_network(flag)
parser = trt.OnnxParser(network, logger)
if not parser.parse_from_file(str(onnx)):
raise RuntimeError(f"failed to load ONNX file: {onnx}")
inputs = [network.get_input(i) for i in range(network.num_inputs)]
outputs = [network.get_output(i) for i in range(network.num_outputs)]
LOGGER.info(f"{prefix} Network Description:")
for inp in inputs:
LOGGER.info(
f'{prefix}\tinput "{inp.name}" with shape {inp.shape} and dtype {inp.dtype}'
)
for out in outputs:
LOGGER.info(
f'{prefix}\toutput "{out.name}" with shape {out.shape} and dtype {out.dtype}'
)
if dynamic:
if im.shape[0] <= 1:
LOGGER.warning(
f"{prefix}WARNING: --dynamic model requires maximum --batch-size argument"
)
profile = builder.create_optimization_profile()
for inp in inputs:
profile.set_shape(
inp.name,
(1, *im.shape[1:]),
(max(1, im.shape[0] // 2), *im.shape[1:]),
im.shape,
)
config.add_optimization_profile(profile)
LOGGER.info(f"{prefix} building FP 16 engine in {f}")
if builder.platform_has_fast_fp16:
config.set_flag(trt.BuilderFlag.FP16)
with builder.build_engine(network, config) as engine, open(f, "wb") as t:
t.write(engine.serialize())
return f, None
def run(
file_checkpoint: Path,
batch_size: int,
imgsz: Tuple[int],
device: torch.device,
include: List[str],
optimize: bool = True,
opset: int = 12,
train: bool = False,
dynamic: bool = False,
):
t = time.time()
include = [x.lower() for x in include]
fmts = tuple(export_formats()["Argument"][1:])
flags = [x in include for x in fmts]
assert sum(flags) == len(
include
), f"ERROR: Invalid --include {include}, valid --include arguments are {fmts}"
(jit, onnx, engine) = flags
model = torch.load(file_checkpoint, map_location=device)
im = torch.zeros(batch_size, 3, *imgsz).to(device)
for _ in range(2):
y = model(im.float())
shape = tuple((y[0] if isinstance(y, tuple) else y).shape) # model output shape
LOGGER.info(
f"\n{colorstr('PyTorch:')} starting from {file_checkpoint} with output shape {shape} ({file_size(file_checkpoint):.1f} MB)"
)
f = [""] * len(fmts)
warnings.filterwarnings(action="ignore", category=torch.jit.TracerWarning)
if jit:
f[0], _ = export_torchscript(model, im, file_checkpoint, optimize)
if onnx:
f[1], _ = export_onnx(model, im, file_checkpoint, opset, train, dynamic)
if engine:
f[2], _ = export_engine(
model, file_checkpoint, im, dynamic, verbose=False, workspace=4
)
f = [str(x) for x in f if x]
if any(f):
LOGGER.info(
f"\nExport complete ({time.time() - t:.1f}s)"
f"\nResults saved to {colorstr('bold', Path(file_checkpoint).parent.resolve())}"
f"\nVisualize: https://netron.app"
)
return f
def main(opt):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
run(
file_checkpoint=Path(opt.weights),
batch_size=opt.batch_size,
imgsz=opt.imgsz,
device=device,
include=opt.include,
optimize=opt.optimize,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--weights",
type=str,
default="densenet121.pt",
help="model.pt path(s)",
)
parser.add_argument("--batch_size", type=int, default=1, help="Batch size")
parser.add_argument(
"--imgsz",
nargs="+",
type=int,
default=[224, 224],
help="image (h, w)",
)
parser.add_argument(
"--dynamic", action="store_true", help="ONNX/TensorRT: dynamic axes"
)
parser.add_argument(
"--include",
nargs="+",
default=["torchscript", "onnx", "engine"],
help="torchscript, onnx, engine",
)
parser.add_argument("--train", action="store_true", help="model.train() mode")
parser.add_argument("--opset", type=int, default=12, help="ONNX: opset version")
parser.add_argument(
"--optimize", action="store_true", help="TorchScript: optimize for mobile"
)
opt = parser.parse_args()
print(opt)
main(opt)