Aditya Lohia Shivam Swanrkar glenn-jocher commited on
Commit
95aefea
1 Parent(s): e27ca0d

Dynamic ONNX engine generation (#2208)

Browse files

* add: dynamic onnx export

* delete: test onnx inference

* fix dynamic output axis

* Code reduction

* fix: dynamic output axes, dynamic input naming

* Remove fixed axes

Co-authored-by: Shivam Swanrkar <[email protected]>
Co-authored-by: Glenn Jocher <[email protected]>

Files changed (1) hide show
  1. models/export.py +4 -1
models/export.py CHANGED
@@ -22,6 +22,7 @@ if __name__ == '__main__':
22
  parser = argparse.ArgumentParser()
23
  parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path') # from yolov5/models/
24
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') # height, width
 
25
  parser.add_argument('--batch-size', type=int, default=1, help='batch size')
26
  opt = parser.parse_args()
27
  opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
@@ -70,7 +71,9 @@ if __name__ == '__main__':
70
  print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
71
  f = opt.weights.replace('.pt', '.onnx') # filename
72
  torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
73
- output_names=['classes', 'boxes'] if y is None else ['output'])
 
 
74
 
75
  # Checks
76
  onnx_model = onnx.load(f) # load onnx model
 
22
  parser = argparse.ArgumentParser()
23
  parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path') # from yolov5/models/
24
  parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') # height, width
25
+ parser.add_argument('--dynamic', action='store_true', help='dynamic ONNX axes')
26
  parser.add_argument('--batch-size', type=int, default=1, help='batch size')
27
  opt = parser.parse_args()
28
  opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
 
71
  print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
72
  f = opt.weights.replace('.pt', '.onnx') # filename
73
  torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
74
+ output_names=['classes', 'boxes'] if y is None else ['output'],
75
+ dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'}, # size(1,3,640,640)
76
+ 'output': {0: 'batch', 2: 'y', 3: 'x'}} if opt.dynamic else None)
77
 
78
  # Checks
79
  onnx_model = onnx.load(f) # load onnx model