Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support static model export with End2End (EfficientNMS) #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ def export_onnx(model, im, file, opset, dynamic, simplify, prefix=colorstr('ONNX


@try_export
def export_onnx_end2end(model, im, file, simplify, topk_all, iou_thres, conf_thres, device, labels, prefix=colorstr('ONNX END2END:')):
def export_onnx_end2end(model, im, file, opset, dynamic, simplify, topk_all, iou_thres, conf_thres, device, labels, prefix=colorstr('ONNX END2END:')):
# YOLO ONNX export
check_requirements('onnx')
import onnx
LOGGER.info(f'\n{prefix} starting export with onnx {onnx.__version__}...')
f = os.path.splitext(file)[0] + "-end2end.onnx"
batch_size = 'batch'
batch_size, ch, *imgsz = list(im.shape) # BCHW
batch_size = 'batch' if dynamic else batch_size

dynamic_axes = {'images': {0 : 'batch', 2: 'height', 3:'width'}, } # variable length axes

Expand All @@ -164,16 +165,17 @@ def export_onnx_end2end(model, im, file, simplify, topk_all, iou_thres, conf_thr
shapes = [ batch_size, 1, batch_size, topk_all, 4,
batch_size, topk_all, batch_size, topk_all]

torch.onnx.export(model,
im,
f,
verbose=False,
export_params=True, # store the trained parameter weights inside the model file
opset_version=12,
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=['images'],
output_names=output_names,
dynamic_axes=dynamic_axes)
torch.onnx.export(
model.cpu() if dynamic else model, # --dynamic only compatible with cpu
im.cpu() if dynamic else im,
f,
verbose=False,
export_params=True, # store the trained parameter weights inside the model file
opset_version=opset,
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=['images'],
output_names=output_names,
dynamic_axes=dynamic_axes if dynamic else None)

# Checks
model_onnx = onnx.load(f) # load onnx model
Expand Down Expand Up @@ -581,7 +583,7 @@ def run(
if onnx_end2end:
if isinstance(model, DetectionModel):
labels = model.names
f[2], _ = export_onnx_end2end(model, im, file, simplify, topk_all, iou_thres, conf_thres, device, len(labels))
f[2], _ = export_onnx_end2end(model, im, file, opset, dynamic, simplify, topk_all, iou_thres, conf_thres, device, len(labels))
else:
raise RuntimeError("The model is not a DetectionModel.")
if xml: # OpenVINO
Expand Down Expand Up @@ -668,7 +670,6 @@ def parse_opt():

if 'onnx_end2end' in opt.include:
opt.simplify = True
opt.dynamic = True
opt.inplace = True
opt.half = False

Expand Down