Add dynamic batch-size (ONNX) + Fixes
This commit is contained in:
@@ -40,9 +40,21 @@ def yolov7_u6_export(weights, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOv7_u6 model\n')
|
||||
|
||||
device = select_device('cpu')
|
||||
model = yolov7_u6_export(args.weights, device)
|
||||
|
||||
if len(model.names.keys()) > 0:
|
||||
print('\nCreating labels.txt file')
|
||||
f = open('labels.txt', 'w')
|
||||
for name in model.names.values():
|
||||
f.write(name + '\n')
|
||||
f.close()
|
||||
|
||||
model = nn.Sequential(model, DeepStreamOutput())
|
||||
|
||||
img_size = args.size * 2 if len(args.size) == 1 else args.size
|
||||
@@ -50,15 +62,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 YOLOv7-u6 conversion')
|
||||
@@ -66,6 +92,7 @@ def parse_args():
|
||||
parser.add_argument('-s', '--size', nargs='+', type=int, default=[640], help='Inference size [H,W] (default [640])')
|
||||
parser.add_argument('--opset', type=int, default=12, 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