Add dynamic batch-size (ONNX) + Fixes
This commit is contained in:
@@ -46,9 +46,21 @@ def damoyolo_export(weights, config_file, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening DAMO-YOLO model')
|
||||
|
||||
device = torch.device('cpu')
|
||||
cfg, model = damoyolo_export(args.weights, args.config, device)
|
||||
|
||||
if len(cfg.dataset['class_names']) > 0:
|
||||
print('Creating labels.txt file')
|
||||
f = open('labels.txt', 'w')
|
||||
for name in cfg.dataset['class_names']:
|
||||
f.write(name + '\n')
|
||||
f.close()
|
||||
|
||||
model = nn.Sequential(model, DeepStreamOutput())
|
||||
|
||||
img_size = args.size * 2 if len(args.size) == 1 else args.size
|
||||
@@ -56,15 +68,29 @@ def main(args):
|
||||
onnx_input_im = torch.zeros(1, 3, *img_size).to(device)
|
||||
onnx_output_file = cfg.miscs['exp_name'] + '.onnx'
|
||||
|
||||
dynamic_axes = {
|
||||
'input': {
|
||||
0: 'batch'
|
||||
},
|
||||
'output': {
|
||||
0: 'batch'
|
||||
}
|
||||
}
|
||||
|
||||
print('Exporting 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 DAMO-YOLO conversion')
|
||||
@@ -73,6 +99,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=11, 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')
|
||||
|
||||
@@ -8,6 +8,7 @@ from ppdet.utils.check import check_version, check_config
|
||||
from ppdet.utils.cli import ArgsParser
|
||||
from ppdet.engine import Trainer
|
||||
from ppdet.slim import build_slim_model
|
||||
from ppdet.data.source.category import get_categories
|
||||
|
||||
|
||||
class DeepStreamOutput(nn.Layer):
|
||||
@@ -39,13 +40,26 @@ def ppyoloe_export(FLAGS):
|
||||
os.makedirs('.tmp')
|
||||
static_model, _ = trainer._get_infer_cfg_and_input_spec('.tmp')
|
||||
os.system('rm -r .tmp')
|
||||
return cfg, static_model
|
||||
return trainer.cfg, static_model
|
||||
|
||||
|
||||
def main(FLAGS):
|
||||
print('\nStarting: %s' % FLAGS.weights)
|
||||
|
||||
print('\nOpening PPYOLOE model\n')
|
||||
|
||||
paddle.set_device('cpu')
|
||||
cfg, model = ppyoloe_export(FLAGS)
|
||||
|
||||
anno_file = cfg['TestDataset'].get_anno()
|
||||
if os.path.isfile(anno_file):
|
||||
_, catid2name = get_categories(cfg['metric'], anno_file, 'detection_arch')
|
||||
print('\nCreating labels.txt file')
|
||||
f = open('labels.txt', 'w')
|
||||
for name in catid2name.values():
|
||||
f.write(str(name) + '\n')
|
||||
f.close()
|
||||
|
||||
model = nn.Sequential(model, DeepStreamOutput())
|
||||
|
||||
img_size = [cfg.eval_height, cfg.eval_width]
|
||||
@@ -55,14 +69,18 @@ def main(FLAGS):
|
||||
onnx_input_im['scale_factor'] = paddle.static.InputSpec(shape=[None, 2], dtype='float32', name='scale_factor')
|
||||
onnx_output_file = cfg.filename + '.onnx'
|
||||
|
||||
print('\nExporting the model to ONNX\n')
|
||||
paddle.onnx.export(model, cfg.filename, input_spec=[onnx_input_im], opset_version=FLAGS.opset)
|
||||
|
||||
if FLAGS.simplify:
|
||||
print('\nSimplifying 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('\nDone: %s\n' % onnx_output_file)
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = ArgsParser()
|
||||
|
||||
@@ -41,9 +41,21 @@ def yolov5_export(weights, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOv5 model\n')
|
||||
|
||||
device = select_device('cpu')
|
||||
model = yolov5_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
|
||||
@@ -54,15 +66,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 YOLOv5 conversion')
|
||||
@@ -71,6 +97,7 @@ def parse_args():
|
||||
parser.add_argument('--p6', action='store_true', help='P6 model')
|
||||
parser.add_argument('--opset', type=int, default=17, 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')
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -45,9 +45,21 @@ def yolov7_export(weights, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOv7 model\n')
|
||||
|
||||
device = select_device('cpu')
|
||||
model = yolov7_export(args.weights, device)
|
||||
|
||||
if len(model.names) > 0:
|
||||
print('\nCreating labels.txt file')
|
||||
f = open('labels.txt', 'w')
|
||||
for name in model.names:
|
||||
f.write(name + '\n')
|
||||
f.close()
|
||||
|
||||
model = nn.Sequential(model, DeepStreamOutput())
|
||||
|
||||
img_size = args.size * 2 if len(args.size) == 1 else args.size
|
||||
@@ -58,15 +70,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 conversion')
|
||||
@@ -75,6 +101,7 @@ def parse_args():
|
||||
parser.add_argument('--p6', action='store_true', help='P6 model')
|
||||
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')
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -48,9 +48,21 @@ def yolov8_export(weights, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOv8 model\n')
|
||||
|
||||
device = select_device('cpu')
|
||||
model = yolov8_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
|
||||
@@ -58,15 +70,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 YOLOv8 conversion')
|
||||
@@ -74,6 +100,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=16, 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')
|
||||
|
||||
@@ -34,6 +34,11 @@ def yolonas_export(model_name, weights, num_classes, size):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLO-NAS model\n')
|
||||
|
||||
device = torch.device('cpu')
|
||||
model = yolonas_export(args.model, args.weights, args.classes, args.size)
|
||||
|
||||
@@ -44,15 +49,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 YOLO-NAS conversion')
|
||||
@@ -62,6 +81,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=14, 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 args.model == '':
|
||||
raise SystemExit('Invalid model name')
|
||||
|
||||
@@ -57,9 +57,21 @@ def yolor_export(weights, cfg, size, device):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOR model\n')
|
||||
|
||||
device = torch.device('cpu')
|
||||
model = yolor_export(args.weights, args.cfg, args.size, device)
|
||||
|
||||
if hasattr(model, 'names') and len(model.names) > 0:
|
||||
print('\nCreating labels.txt file')
|
||||
f = open('labels.txt', 'w')
|
||||
for name in model.names:
|
||||
f.write(name + '\n')
|
||||
f.close()
|
||||
|
||||
model = nn.Sequential(model, DeepStreamOutput())
|
||||
|
||||
img_size = args.size * 2 if len(args.size) == 1 else args.size
|
||||
@@ -70,15 +82,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 YOLOR conversion')
|
||||
@@ -88,6 +114,7 @@ def parse_args():
|
||||
parser.add_argument('--p6', action='store_true', help='P6 model')
|
||||
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')
|
||||
|
||||
@@ -42,6 +42,11 @@ def yolox_export(weights, exp_file):
|
||||
|
||||
def main(args):
|
||||
suppress_warnings()
|
||||
|
||||
print('\nStarting: %s' % args.weights)
|
||||
|
||||
print('Opening YOLOX model')
|
||||
|
||||
device = torch.device('cpu')
|
||||
model, exp = yolox_export(args.weights, args.exp)
|
||||
|
||||
@@ -52,15 +57,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('Exporting 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 YOLOX conversion')
|
||||
@@ -68,6 +87,7 @@ def parse_args():
|
||||
parser.add_argument('-c', '--exp', required=True, help='Input exp (.py) file path (required)')
|
||||
parser.add_argument('--opset', type=int, default=11, 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