Skip to content

Commit 7b3aa2b

Browse files
committed
omz_converter mo to ovc
1 parent d457f81 commit 7b3aa2b

File tree

1 file changed

+66
-62
lines changed

1 file changed

+66
-62
lines changed

tools/model_tools/src/omz_tools/omz_converter.py

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def convert_to_onnx(reporter, model, output_dir, args, template_variables):
7070

7171
return success
7272

73-
def convert(reporter, model, output_dir, args, mo_props, requested_precisions):
73+
def convert(reporter, model, output_dir, args, ovc_props, requested_precisions):
7474
telemetry = _common.Telemetry()
75-
if model.mo_args is None:
75+
if model.ovc_args is None:
7676
reporter.print_section_heading('Skipping {} (no conversions defined)', model.name)
7777
reporter.print()
7878
return True
@@ -92,16 +92,16 @@ def convert(reporter, model, output_dir, args, mo_props, requested_precisions):
9292
return False
9393

9494
model_format = model.framework
95-
mo_extension_dir = mo_props.base_dir / 'extensions'
96-
if not mo_extension_dir.exists():
97-
mo_extension_dir = mo_props.base_dir
95+
ovc_extension_dir = ovc_props.base_dir / 'extensions'
96+
if not ovc_extension_dir.exists():
97+
ovc_extension_dir = ovc_props.base_dir
9898

9999
template_variables = {
100100
'config_dir': _common.MODEL_ROOT / model.subdirectory,
101101
'conv_dir': output_dir / model.subdirectory,
102102
'dl_dir': args.download_dir / model.subdirectory,
103-
'mo_dir': mo_props.base_dir,
104-
'mo_ext_dir': mo_extension_dir,
103+
'ovc_dir': ovc_props.base_dir,
104+
'ovc_ext_dir': ovc_extension_dir,
105105
}
106106

107107
if model.conversion_to_onnx_args:
@@ -112,52 +112,54 @@ def convert(reporter, model, output_dir, args, mo_props, requested_precisions):
112112
return False
113113
model_format = 'onnx'
114114

115-
expanded_mo_args = [
115+
custom_ovc_args = [
116116
string.Template(arg).substitute(template_variables)
117-
for arg in model.mo_args]
117+
for arg in model.ovc_args]
118+
119+
expanded_ovc_args = []
120+
input_model = ''
121+
for arg in custom_ovc_args:
122+
if 'input_model' in arg:
123+
input_model = arg.split('=')[-1]
124+
reporter.print(f'Model {input_model}')
125+
# if 'output' in arg:
126+
# expanded_ovc_args.append(arg)
118127

119128
for model_precision in sorted(model_precisions):
120129
data_type = model_precision.split('-')[0]
121-
layout_string = ','.join(
122-
'{}({})'.format(input.name, input.layout) for input in model.input_info if input.layout
123-
)
124-
shape_string = ','.join(str(input.shape) for input in model.input_info if input.shape)
130+
shape_string = ','.join(str(input.shape).replace(" ", "") for input in model.input_info if input.shape)
125131

126-
if layout_string:
127-
expanded_mo_args.append('--layout={}'.format(layout_string))
128132
if shape_string:
129-
expanded_mo_args.append('--input_shape={}'.format(shape_string))
133+
expanded_ovc_args.append('--input={}'.format(shape_string))
130134
if data_type == "FP16":
131-
expanded_mo_args.append("--compress_to_fp16=True")
135+
expanded_ovc_args.append("--compress_to_fp16=True")
132136
else:
133-
expanded_mo_args.append("--compress_to_fp16=False")
137+
expanded_ovc_args.append("--compress_to_fp16=False")
134138

135-
mo_output_dir = output_dir / model.subdirectory / model_precision
136-
mo_cmd = [*mo_props.cmd_prefix,
137-
'--framework={}'.format(model_format),
138-
f'--output_dir={mo_output_dir}',
139-
'--model_name={}'.format(model.name),
140-
'--input={}'.format(','.join(input.name for input in model.input_info)),
141-
*expanded_mo_args, *mo_props.extra_args]
139+
ovc_output_dir = output_dir / model.subdirectory / model_precision
140+
ovc_cmd = [*ovc_props.cmd_prefix,
141+
input_model,
142+
f'--output_model={str(Path(ovc_output_dir, model.name + ".xml"))}',
143+
*expanded_ovc_args]
142144

143145
reporter.print_section_heading('{}Converting {} to IR ({})',
144146
'(DRY RUN) ' if args.dry_run else '', model.name, model_precision)
145147

146-
reporter.print('Conversion command: {}', _common.command_string(mo_cmd))
148+
reporter.print('Conversion command: {}', _common.command_string(ovc_cmd))
147149

148150
if not args.dry_run:
149151
reporter.print(flush=True)
150152

151-
if not reporter.job_context.subprocess(mo_cmd):
153+
if not reporter.job_context.subprocess(ovc_cmd):
152154
telemetry.send_event('md', 'converter_failed_models', model.name)
153155
telemetry.send_event('md', 'converter_error',
154-
json.dumps({'error': 'mo-failed', 'model': model.name, 'precision': model_precision}))
156+
json.dumps({'error': 'ovc-failed', 'model': model.name, 'precision': model_precision}))
155157
return False
156158

157159
reporter.print()
158160
core = Core()
159161
core.set_property({"ENABLE_MMAP": False})
160-
rt_model = core.read_model(str(mo_output_dir / model.name) + '.xml')
162+
rt_model = core.read_model(str(ovc_output_dir / model.name) + '.xml')
161163
try:
162164
val = validation.validate_string('model_type', model.model_info['model_type'])
163165
rt_model.set_rt_info(val, ['model_info', 'model_type'])
@@ -193,7 +195,7 @@ def convert(reporter, model, output_dir, args, mo_props, requested_precisions):
193195
rt_model.set_rt_info(val, ['model_info', 'labels'])
194196
except KeyError:
195197
pass
196-
save_model(rt_model, str(mo_output_dir / model.name) + '.xml', "FP16" == data_type)
198+
save_model(rt_model, str(ovc_output_dir / model.name) + '.xml', "FP16" == data_type)
197199
return True
198200

199201
def num_jobs_arg(value_str):
@@ -223,18 +225,20 @@ def converter(argv):
223225
parser.add_argument('--precisions', metavar='PREC[,PREC...]',
224226
help='run only conversions that produce models with the specified precisions')
225227
parser.add_argument('-p', '--python', type=Path, metavar='PYTHON', default=sys.executable,
226-
help='Python executable to run Model Optimizer with')
227-
parser.add_argument('--mo', type=Path, metavar='MO.PY',
228-
help='Model Optimizer entry point script')
229-
parser.add_argument('--add_mo_arg', dest='extra_mo_args', metavar='ARG', action='append',
230-
help='Extra argument to pass to Model Optimizer')
228+
help='Python executable to run OVC Converter with')
229+
parser.add_argument('--add_ovc_arg', dest='extra_ovc_args', metavar='ARG', action='append',
230+
help='Extra argument to pass to OVC Converter')
231231
parser.add_argument('--dry_run', action='store_true',
232232
help='Print the conversion commands without running them')
233233
parser.add_argument('-j', '--jobs', type=num_jobs_arg, default=1,
234234
help='number of conversions to run concurrently')
235235

236236
# aliases for backwards compatibility
237-
parser.add_argument('--add-mo-arg', dest='extra_mo_args', action='append', help=argparse.SUPPRESS)
237+
parser.add_argument('--mo', type=Path, metavar='MO.PY',
238+
help='OVC Converter entry point script')
239+
parser.add_argument('--add_mo_arg', dest='extra_ovc_args', metavar='ARG', action='append',
240+
help='Extra argument to pass to OVC Converter')
241+
parser.add_argument('--add-mo-arg', dest='extra_ovc_args', action='append', help=argparse.SUPPRESS)
238242
parser.add_argument('--dry-run', action='store_true', help=argparse.SUPPRESS)
239243

240244
args = parser.parse_args(argv)
@@ -268,47 +272,47 @@ def converter(argv):
268272
if unknown_precisions:
269273
sys.exit('Unknown precisions specified: {}.'.format(', '.join(sorted(unknown_precisions))))
270274

271-
mo_path = args.mo
275+
ovc_path = args.mo
272276

273-
if mo_path is None:
274-
mo_executable = shutil.which('mo')
277+
if ovc_path is None:
278+
ovc_executable = shutil.which('ovc')
275279

276-
if mo_executable:
277-
mo_path = Path(mo_executable)
280+
if ovc_executable:
281+
ovc_path = Path(ovc_executable)
278282
else:
279283
try:
280-
mo_path = Path(os.environ['INTEL_OPENVINO_DIR']) / 'tools/mo/openvino/tools/mo/mo.py'
281-
if not mo_path.exists():
282-
mo_path = Path(os.environ['INTEL_OPENVINO_DIR']) / 'tools/model_optimizer/mo.py'
284+
# ovc_path = Path(os.environ['INTEL_OPENVINO_DIR']) / 'tools/mo/openvino/tools/mo/mo.py'
285+
# if not ovc_path.exists():
286+
ovc_path = Path(os.environ['INTEL_OPENVINO_DIR']) / 'tools/ovc/ovc.py'
283287
except KeyError:
284-
sys.exit('Unable to locate Model Optimizer. '
285-
+ 'Use --mo or run setupvars.sh/setupvars.bat from the OpenVINO toolkit.')
288+
sys.exit('Unable to locate OVC Converter. '
289+
+ 'Use --ovc or run setupvars.sh/setupvars.bat from the OpenVINO toolkit.')
286290

287-
if mo_path is not None:
288-
mo_path = mo_path.resolve()
289-
mo_cmd_prefix = [str(args.python), '--', str(mo_path)]
291+
if ovc_path is not None:
292+
ovc_path = ovc_path.resolve()
293+
ovc_cmd_prefix = [str(args.python), '--', str(ovc_path)]
290294

291-
if str(mo_path).lower().endswith('.py'):
292-
mo_dir = mo_path.parent
295+
if str(ovc_path).lower().endswith('.py'):
296+
ovc_dir = ovc_path.parent
293297
else:
294-
mo_package_path, stderr = _common.get_package_path(args.python, 'openvino.tools.mo')
295-
mo_dir = mo_package_path
298+
ovc_package_path, stderr = _common.get_package_path(args.python, 'openvino.tools.ovc')
299+
ovc_dir = ovc_package_path
296300

297-
if mo_package_path is None:
298-
mo_package_path, stderr = _common.get_package_path(args.python, 'mo')
299-
if mo_package_path is None:
301+
if ovc_package_path is None:
302+
ovc_package_path, stderr = _common.get_package_path(args.python, 'ovc')
303+
if ovc_package_path is None:
300304
sys.exit('Unable to load Model Optimizer. Errors occurred: {}'.format(stderr))
301-
mo_dir = mo_package_path.parent
305+
ovc_dir = ovc_package_path.parent
302306

303307
output_dir = args.download_dir if args.output_dir is None else args.output_dir
304308

305309
reporter = _reporting.Reporter(_reporting.DirectOutputContext())
306-
mo_props = ModelOptimizerProperties(
307-
cmd_prefix=mo_cmd_prefix,
308-
extra_args=args.extra_mo_args or [],
309-
base_dir=mo_dir,
310+
ovc_props = ModelOptimizerProperties(
311+
cmd_prefix=ovc_cmd_prefix,
312+
extra_args=args.extra_ovc_args or [],
313+
base_dir=ovc_dir,
310314
)
311-
shared_convert_args = (output_dir, args, mo_props, requested_precisions)
315+
shared_convert_args = (output_dir, args, ovc_props, requested_precisions)
312316

313317
def convert_model(model, reporter):
314318
if model.model_stages:

0 commit comments

Comments
 (0)