Add dynamic batch-size (ONNX) + Fixes
This commit is contained in:
@@ -51,6 +51,11 @@ def yolov6_export(weights, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOv6 model\n')
|
||||
|
||||
device = torch.device('cpu')
|
||||
model = yolov6_export(args.weights, device)
|
||||
|
||||
@@ -64,15 +69,29 @@ def main(args):
|
||||
onnx_input_im = torch.zeros(1, 3, *img_size).to(device)
|
||||
onnx_output_file = os.path.basename(args.weights).split('.pt')[0] + '.onnx'
|
||||
|
||||
dynamic_axes = {
|
||||
'input': {
|
||||
0: 'batch'
|
||||
},
|
||||
'output': {
|
||||
0: 'batch'
|
||||
}
|
||||
}
|
||||
|
||||
print('\nExporting the model to ONNX')
|
||||
torch.onnx.export(model, onnx_input_im, onnx_output_file, verbose=False, opset_version=args.opset,
|
||||
do_constant_folding=True, input_names=['input'], output_names=['output'], dynamic_axes=None)
|
||||
do_constant_folding=True, input_names=['input'], output_names=['output'],
|
||||
dynamic_axes=dynamic_axes if args.dynamic else None)
|
||||
|
||||
if args.simplify:
|
||||
print('Simplifying the ONNX model')
|
||||
import onnxsim
|
||||
model_onnx = onnx.load(onnx_output_file)
|
||||
model_onnx, _ = onnxsim.simplify(model_onnx)
|
||||
onnx.save(model_onnx, onnx_output_file)
|
||||
|
||||
print('Done: %s\n' % onnx_output_file)
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='DeepStream YOLOv6 conversion')
|
||||
@@ -81,6 +100,7 @@ def parse_args():
|
||||
parser.add_argument('--p6', action='store_true', help='P6 model')
|
||||
parser.add_argument('--opset', type=int, default=13, help='ONNX opset version')
|
||||
parser.add_argument('--simplify', action='store_true', help='ONNX simplify model')
|
||||
parser.add_argument('--dynamic', action='store_true', help='Dynamic batch-size')
|
||||
args = parser.parse_args()
|
||||
if not os.path.isfile(args.weights):
|
||||
raise SystemExit('Invalid weights file')
|
||||
|
||||
Reference in New Issue
Block a user