glenn-jocher pre-commit-ci[bot] commited on
Commit
079b36d
1 Parent(s): 9bc72a3

Edge TPU `tf.lite.experimental.load_delegate` fix (#6536)

Browse files

* Edge TPU `tf.lite.experimental.load_delegate` fix

Fix attempt for #6535

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

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

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Files changed (1) hide show
  1. models/common.py +6 -5
models/common.py CHANGED
@@ -374,19 +374,20 @@ class DetectMultiBackend(nn.Module):
374
  graph_def.ParseFromString(open(w, 'rb').read())
375
  frozen_func = wrap_frozen_graph(gd=graph_def, inputs="x:0", outputs="Identity:0")
376
  elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
377
- try:
378
- import tflite_runtime.interpreter as tfl # prefer tflite_runtime if installed
379
  except ImportError:
380
- import tensorflow.lite as tfl
 
381
  if 'edgetpu' in w.lower(): # Edge TPU https://coral.ai/software/#edgetpu-runtime
382
  LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
383
  delegate = {'Linux': 'libedgetpu.so.1',
384
  'Darwin': 'libedgetpu.1.dylib',
385
  'Windows': 'edgetpu.dll'}[platform.system()]
386
- interpreter = tfl.Interpreter(model_path=w, experimental_delegates=[tfl.load_delegate(delegate)])
387
  else: # Lite
388
  LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')
389
- interpreter = tfl.Interpreter(model_path=w) # load TFLite model
390
  interpreter.allocate_tensors() # allocate
391
  input_details = interpreter.get_input_details() # inputs
392
  output_details = interpreter.get_output_details() # outputs
 
374
  graph_def.ParseFromString(open(w, 'rb').read())
375
  frozen_func = wrap_frozen_graph(gd=graph_def, inputs="x:0", outputs="Identity:0")
376
  elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
377
+ try: # prefer tflite_runtime if installed
378
+ from tflite_runtime.interpreter import Interpreter, load_delegate
379
  except ImportError:
380
+ import tensorflow.lite.experimental.load_delegate as load_delegate
381
+ import tensorflow.lite.Interpreter as Interpreter
382
  if 'edgetpu' in w.lower(): # Edge TPU https://coral.ai/software/#edgetpu-runtime
383
  LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
384
  delegate = {'Linux': 'libedgetpu.so.1',
385
  'Darwin': 'libedgetpu.1.dylib',
386
  'Windows': 'edgetpu.dll'}[platform.system()]
387
+ interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)])
388
  else: # Lite
389
  LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')
390
+ interpreter = Interpreter(model_path=w) # load TFLite model
391
  interpreter.allocate_tensors() # allocate
392
  input_details = interpreter.get_input_details() # inputs
393
  output_details = interpreter.get_output_details() # outputs