From d16c89b33a416bd15fe5db157f298043c9ba919c Mon Sep 17 00:00:00 2001 From: Phil2020 <35833843+phodgers@users.noreply.github.com> Date: Thu, 25 Nov 2021 16:28:55 +0000 Subject: [PATCH 1/4] tensorflow or tflite exclusively as interpreter As per bug report https://github.com/ultralytics/yolov5/issues/5709 I think there should be only one attempt to assign interpreter, and it appears tflite is only ever needed for the case of edgetpu model. --- models/common.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/models/common.py b/models/common.py index 8836a655986a..9fb14041faec 100644 --- a/models/common.py +++ b/models/common.py @@ -337,7 +337,10 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=True): context = model.create_execution_context() batch_size = bindings['images'].shape[0] else: # TensorFlow model (TFLite, pb, saved_model) - import tensorflow as tf + if 'edgetpu' in w.lower(): + import tflite_runtime.interpreter as tfli + else: + import tensorflow as tf if pb: # https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped @@ -354,7 +357,6 @@ def wrap_frozen_graph(gd, inputs, outputs): elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python if 'edgetpu' in w.lower(): LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...') - import tflite_runtime.interpreter as tfli delegate = {'Linux': 'libedgetpu.so.1', # install https://coral.ai/software/#edgetpu-runtime 'Darwin': 'libedgetpu.1.dylib', 'Windows': 'edgetpu.dll'}[platform.system()] From 5d85639080d9c1fec3cc151ec90690ff55eb26e0 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 25 Nov 2021 17:47:17 +0100 Subject: [PATCH 2/4] Scope imports --- models/common.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/models/common.py b/models/common.py index 9fb14041faec..57230db060fa 100644 --- a/models/common.py +++ b/models/common.py @@ -337,32 +337,32 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=True): context = model.create_execution_context() batch_size = bindings['images'].shape[0] else: # TensorFlow model (TFLite, pb, saved_model) - if 'edgetpu' in w.lower(): - import tflite_runtime.interpreter as tfli - else: - import tensorflow as tf if pb: # https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt + LOGGER.info(f'Loading {w} for TensorFlow *.pb inference...') + import tensorflow as tf def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped return x.prune(tf.nest.map_structure(x.graph.as_graph_element, inputs), tf.nest.map_structure(x.graph.as_graph_element, outputs)) - LOGGER.info(f'Loading {w} for TensorFlow *.pb inference...') graph_def = tf.Graph().as_graph_def() graph_def.ParseFromString(open(w, 'rb').read()) frozen_func = wrap_frozen_graph(gd=graph_def, inputs="x:0", outputs="Identity:0") elif saved_model: LOGGER.info(f'Loading {w} for TensorFlow saved_model inference...') + import tensorflow as tf model = tf.keras.models.load_model(w) elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python if 'edgetpu' in w.lower(): LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...') + import tflite_runtime.interpreter as tfli delegate = {'Linux': 'libedgetpu.so.1', # install https://coral.ai/software/#edgetpu-runtime 'Darwin': 'libedgetpu.1.dylib', 'Windows': 'edgetpu.dll'}[platform.system()] interpreter = tfli.Interpreter(model_path=w, experimental_delegates=[tfli.load_delegate(delegate)]) else: LOGGER.info(f'Loading {w} for TensorFlow Lite inference...') + import tensorflow as tf interpreter = tf.lite.Interpreter(model_path=w) # load TFLite model interpreter.allocate_tensors() # allocate input_details = interpreter.get_input_details() # inputs From 276f64b7a93c02b592a9a1bceefaaee89c9b8d4f Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 25 Nov 2021 17:49:14 +0100 Subject: [PATCH 3/4] Nested definition line fix --- models/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/models/common.py b/models/common.py index 57230db060fa..4a64b072f344 100644 --- a/models/common.py +++ b/models/common.py @@ -340,6 +340,7 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=True): if pb: # https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt LOGGER.info(f'Loading {w} for TensorFlow *.pb inference...') import tensorflow as tf + def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped return x.prune(tf.nest.map_structure(x.graph.as_graph_element, inputs), From 994c22aaf7bac7b3469157b0fa3f18b97360b7dd Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 25 Nov 2021 17:52:59 +0100 Subject: [PATCH 4/4] Update common.py --- models/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/common.py b/models/common.py index 4a64b072f344..284f03e6de20 100644 --- a/models/common.py +++ b/models/common.py @@ -340,7 +340,7 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=True): if pb: # https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt LOGGER.info(f'Loading {w} for TensorFlow *.pb inference...') import tensorflow as tf - + def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped return x.prune(tf.nest.map_structure(x.graph.as_graph_element, inputs),