Skip to content

Commit

Permalink
Removed shell=True from subprocess commands that require user inputs (u…
Browse files Browse the repository at this point in the history
…ltralytics#7875)

* Removed shell=True from subprocess commands that require user inputs. Also removed unused arguments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added check=True

* Revert line add

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
3 people authored and Clay Januhowski committed Sep 8, 2022
1 parent 4982a79 commit b53f398
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def export_onnx(model, im, file, opset, train, dynamic, simplify, prefix=colorst
LOGGER.info(f'{prefix} export failure: {e}')


def export_openvino(model, im, file, half, prefix=colorstr('OpenVINO:')):
def export_openvino(file, half, prefix=colorstr('OpenVINO:')):
# YOLOv5 OpenVINO export
try:
check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
Expand All @@ -178,7 +178,7 @@ def export_openvino(model, im, file, half, prefix=colorstr('OpenVINO:')):
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.check_output(cmd, shell=True)
subprocess.check_output(cmd.split())

LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
return f
Expand Down Expand Up @@ -443,7 +443,7 @@ def export_saved_model(model,
return None, None


def export_pb(keras_model, im, file, prefix=colorstr('TensorFlow GraphDef:')):
def export_pb(keras_model, file, prefix=colorstr('TensorFlow GraphDef:')):
# YOLOv5 TensorFlow GraphDef *.pb export https://github.com/leimao/Frozen_Graph_TensorFlow
try:
import tensorflow as tf
Expand Down Expand Up @@ -498,7 +498,7 @@ def export_tflite(keras_model, im, file, int8, data, nms, agnostic_nms, prefix=c
LOGGER.info(f'\n{prefix} export failure: {e}')


def export_edgetpu(keras_model, im, file, prefix=colorstr('Edge TPU:')):
def export_edgetpu(file, prefix=colorstr('Edge TPU:')):
# YOLOv5 Edge TPU export https://coral.ai/docs/edgetpu/models-intro/
try:
cmd = 'edgetpu_compiler --version'
Expand All @@ -519,15 +519,15 @@ def export_edgetpu(keras_model, im, file, prefix=colorstr('Edge TPU:')):
f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model

cmd = f"edgetpu_compiler -s -o {file.parent} {f_tfl}"
subprocess.run(cmd, shell=True, check=True)
subprocess.run(cmd.split(), check=True)

LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
return f
except Exception as e:
LOGGER.info(f'\n{prefix} export failure: {e}')


def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')):
def export_tfjs(file, prefix=colorstr('TensorFlow.js:')):
# YOLOv5 TensorFlow.js export
try:
check_requirements(('tensorflowjs',))
Expand All @@ -541,8 +541,8 @@ def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')):
f_json = f'{f}/model.json' # *.json path

cmd = f'tensorflowjs_converter --input_format=tf_frozen_model ' \
f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}'
subprocess.run(cmd, shell=True)
f'--output_node_names=Identity,Identity_1,Identity_2,Identity_3 {f_pb} {f}'
subprocess.run(cmd.split())

with open(f_json) as j:
json = j.read()
Expand Down Expand Up @@ -638,7 +638,7 @@ def run(
if onnx or xml: # OpenVINO requires ONNX
f[2] = export_onnx(model, im, file, opset, train, dynamic, simplify)
if xml: # OpenVINO
f[3] = export_openvino(model, im, file, half)
f[3] = export_openvino(file, half)
if coreml:
nb = shape[1]
_, f[4] = export_coreml(model, im, file, nb, nc, names, conf_thres, iou_thres, int8, half)
Expand All @@ -659,13 +659,13 @@ def run(
conf_thres=conf_thres,
iou_thres=iou_thres) # keras model
if pb or tfjs: # pb prerequisite to tfjs
f[6] = export_pb(model, im, file)
f[6] = export_pb(model, file)
if tflite or edgetpu:
f[7] = export_tflite(model, im, file, int8=int8 or edgetpu, data=data, nms=nms, agnostic_nms=agnostic_nms)
if edgetpu:
f[8] = export_edgetpu(model, im, file)
f[8] = export_edgetpu(file)
if tfjs:
f[9] = export_tfjs(model, im, file)
f[9] = export_tfjs(file)

# Finish
f = [str(x) for x in f if x] # filter out '' and None
Expand Down

0 comments on commit b53f398

Please sign in to comment.