Subprocess improvements (#10973)

* Use list-form arguments for subprocess.run calls where possible

Augments #10944

* Deduplicate curl code

* Avoid eval() to parse integer

---------

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Aarni KoskelaandGlenn Jocher authored and GitHub committed 2023-02-13 18:00:31 +04:00
1 parent 1a2eb532ce
commit a2de5c5bf6
8 files changed
+68 -24

No files matched your search

+26 -9
View File
@@ -194,8 +194,15 @@ def export_openvino(file, metadata, half, prefix=colorstr('OpenVINO:')):
LOGGER.info(f'\n{prefix} starting export with openvino {ie.__version__}...')
f = str(file).replace('.pt', f'_openvino_model{os.sep}')
cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} --data_type {'FP16' if half else 'FP32'}"
subprocess.run(cmd.split(), check=True, env=os.environ) # export
args = [
"mo",
"--input_model",
str(file.with_suffix('.onnx')),
"--output_dir",
f,
"--data_type",
("FP16" if half else "FP32"),]
subprocess.run(args, check=True, env=os.environ) # export
yaml_save(Path(f) / file.with_suffix('.yaml').name, metadata) # add metadata.yaml
return f, None
@@ -420,8 +427,15 @@ def export_edgetpu(file, prefix=colorstr('Edge TPU:')):
f = str(file).replace('.pt', '-int8_edgetpu.tflite') # Edge TPU model
f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model
cmd = f"edgetpu_compiler -s -d -k 10 --out_dir {file.parent} {f_tfl}"
subprocess.run(cmd.split(), check=True)
subprocess.run([
'edgetpu_compiler',
'-s',
'-d',
'-k',
'10',
'--out_dir',
str(file.parent),
f_tfl,], check=True)
return f, None
@@ -436,11 +450,14 @@ def export_tfjs(file, int8, prefix=colorstr('TensorFlow.js:')):
f_pb = file.with_suffix('.pb') # *.pb path
f_json = f'{f}/model.json' # *.json path
int8_export = ' --quantize_uint8 ' if int8 else ''
cmd = f'tensorflowjs_converter --input_format=tf_frozen_model {int8_export}' \
f'--output_node_names=Identity,Identity_1,Identity_2,Identity_3 {f_pb} {f}'
subprocess.run(cmd.split())
args = [
'tensorflowjs_converter',
'--input_format=tf_frozen_model',
'--quantize_uint8' if int8 else '',
'--output_node_names=Identity,Identity_1,Identity_2,Identity_3',
str(f_pb),
str(f),]
subprocess.run([arg for arg in args if arg], check=True)
json = Path(f_json).read_text()
with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order