|
20 | 20 | parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') # height, width
|
21 | 21 | parser.add_argument('--batch-size', type=int, default=1, help='batch size')
|
22 | 22 | parser.add_argument('--dynamic', action='store_true', help='dynamic ONNX axes')
|
| 23 | + parser.add_argument('--dynamic-batch', action='store_true', help='dynamic batch onnx for tensorrt and onnx-runtime') |
23 | 24 | parser.add_argument('--grid', action='store_true', help='export Detect() layer grid')
|
24 | 25 | parser.add_argument('--end2end', action='store_true', help='export end2end onnx')
|
25 | 26 | parser.add_argument('--max-wh', type=int, default=None, help='None for tensorrt nms, int value for onnx-runtime nms')
|
|
31 | 32 | parser.add_argument('--include-nms', action='store_true', help='export end2end onnx')
|
32 | 33 | opt = parser.parse_args()
|
33 | 34 | opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
|
| 35 | + opt.dynamic = opt.dynamic and not opt.end2end |
| 36 | + opt.dynamic = False if opt.dynamic_batch else opt.dynamic |
34 | 37 | print(opt)
|
35 | 38 | set_logging()
|
36 | 39 | t = time.time()
|
|
80 | 83 | f = opt.weights.replace('.pt', '.onnx') # filename
|
81 | 84 | model.eval()
|
82 | 85 | output_names = ['classes', 'boxes'] if y is None else ['output']
|
| 86 | + dynamic_axes = None |
| 87 | + if opt.dynamic: |
| 88 | + dynamic_axes = {'images': {0: 'batch', 2: 'height', 3: 'width'}, # size(1,3,640,640) |
| 89 | + 'output': {0: 'batch', 2: 'y', 3: 'x'}} |
| 90 | + if opt.dynamic_batch: |
| 91 | + opt.batch_size = 'batch' |
| 92 | + dynamic_axes = { |
| 93 | + 'images': { |
| 94 | + 0: 'batch', |
| 95 | + }, } |
| 96 | + if opt.end2end and opt.max_wh is None: |
| 97 | + output_axes = { |
| 98 | + 'num_dets': {0: 'batch'}, |
| 99 | + 'det_boxes': {0: 'batch'}, |
| 100 | + 'det_scores': {0: 'batch'}, |
| 101 | + 'det_classes': {0: 'batch'}, |
| 102 | + } |
| 103 | + else: |
| 104 | + output_axes = { |
| 105 | + 'output': {0: 'batch'}, |
| 106 | + } |
| 107 | + dynamic_axes.update(output_axes) |
83 | 108 | if opt.grid and opt.end2end:
|
84 | 109 | print('\nStarting export end2end onnx model for %s...' % 'TensorRT' if opt.max_wh is None else 'onnxruntime')
|
85 | 110 | model = End2End(model,opt.topk_all,opt.iou_thres,opt.conf_thres,opt.max_wh,device)
|
|
92 | 117 |
|
93 | 118 | torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
|
94 | 119 | output_names=output_names,
|
95 |
| - dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'}, # size(1,3,640,640) |
96 |
| - 'output': {0: 'batch', 2: 'y', 3: 'x'}} if opt.dynamic and not opt.end2end else None) |
| 120 | + dynamic_axes=dynamic_axes) |
97 | 121 |
|
98 | 122 | # Checks
|
99 | 123 | onnx_model = onnx.load(f) # load onnx model
|
|
0 commit comments