Commit
•
a297efc
1
Parent(s):
0365379
Edge TPU inference fix (#6686)
Browse files* refactor: use edgetpu flag
* fix: remove bitwise and assignation to tflite
* Cleanup and fix tflite
* Cleanup
Co-authored-by: Glenn Jocher <[email protected]>
- models/common.py +21 -19
models/common.py
CHANGED
@@ -279,17 +279,17 @@ class DetectMultiBackend(nn.Module):
|
|
279 |
# YOLOv5 MultiBackend class for python inference on various backends
|
280 |
def __init__(self, weights='yolov5s.pt', device=None, dnn=False, data=None):
|
281 |
# Usage:
|
282 |
-
# PyTorch:
|
283 |
-
# TorchScript:
|
284 |
-
#
|
285 |
-
#
|
286 |
-
#
|
287 |
-
#
|
288 |
-
#
|
289 |
-
# TensorFlow
|
290 |
-
#
|
291 |
-
#
|
292 |
-
#
|
293 |
from models.experimental import attempt_download, attempt_load # scoped to avoid circular import
|
294 |
|
295 |
super().__init__()
|
@@ -367,19 +367,19 @@ class DetectMultiBackend(nn.Module):
|
|
367 |
|
368 |
def wrap_frozen_graph(gd, inputs, outputs):
|
369 |
x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped
|
370 |
-
|
371 |
-
|
372 |
|
373 |
-
|
374 |
-
|
375 |
-
frozen_func = wrap_frozen_graph(gd
|
376 |
-
elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
|
377 |
try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
|
378 |
from tflite_runtime.interpreter import Interpreter, load_delegate
|
379 |
except ImportError:
|
380 |
import tensorflow as tf
|
381 |
Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate,
|
382 |
-
if
|
383 |
LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
|
384 |
delegate = {'Linux': 'libedgetpu.so.1',
|
385 |
'Darwin': 'libedgetpu.1.dylib',
|
@@ -391,6 +391,8 @@ class DetectMultiBackend(nn.Module):
|
|
391 |
interpreter.allocate_tensors() # allocate
|
392 |
input_details = interpreter.get_input_details() # inputs
|
393 |
output_details = interpreter.get_output_details() # outputs
|
|
|
|
|
394 |
self.__dict__.update(locals()) # assign all variables to self
|
395 |
|
396 |
def forward(self, im, augment=False, visualize=False, val=False):
|
@@ -436,7 +438,7 @@ class DetectMultiBackend(nn.Module):
|
|
436 |
y = (self.model(im, training=False) if self.keras else self.model(im)[0]).numpy()
|
437 |
elif self.pb: # GraphDef
|
438 |
y = self.frozen_func(x=self.tf.constant(im)).numpy()
|
439 |
-
|
440 |
input, output = self.input_details[0], self.output_details[0]
|
441 |
int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model
|
442 |
if int8:
|
|
|
279 |
# YOLOv5 MultiBackend class for python inference on various backends
|
280 |
def __init__(self, weights='yolov5s.pt', device=None, dnn=False, data=None):
|
281 |
# Usage:
|
282 |
+
# PyTorch: weights = *.pt
|
283 |
+
# TorchScript: *.torchscript
|
284 |
+
# ONNX Runtime: *.onnx
|
285 |
+
# ONNX OpenCV DNN: *.onnx with --dnn
|
286 |
+
# OpenVINO: *.xml
|
287 |
+
# CoreML: *.mlmodel
|
288 |
+
# TensorRT: *.engine
|
289 |
+
# TensorFlow SavedModel: *_saved_model
|
290 |
+
# TensorFlow GraphDef: *.pb
|
291 |
+
# TensorFlow Lite: *.tflite
|
292 |
+
# TensorFlow Edge TPU: *_edgetpu.tflite
|
293 |
from models.experimental import attempt_download, attempt_load # scoped to avoid circular import
|
294 |
|
295 |
super().__init__()
|
|
|
367 |
|
368 |
def wrap_frozen_graph(gd, inputs, outputs):
|
369 |
x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped
|
370 |
+
ge = x.graph.as_graph_element
|
371 |
+
return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))
|
372 |
|
373 |
+
gd = tf.Graph().as_graph_def() # graph_def
|
374 |
+
gd.ParseFromString(open(w, 'rb').read())
|
375 |
+
frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0")
|
376 |
+
elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
|
377 |
try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
|
378 |
from tflite_runtime.interpreter import Interpreter, load_delegate
|
379 |
except ImportError:
|
380 |
import tensorflow as tf
|
381 |
Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate,
|
382 |
+
if edgetpu: # 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',
|
|
|
391 |
interpreter.allocate_tensors() # allocate
|
392 |
input_details = interpreter.get_input_details() # inputs
|
393 |
output_details = interpreter.get_output_details() # outputs
|
394 |
+
elif tfjs:
|
395 |
+
raise Exception('ERROR: YOLOv5 TF.js inference is not supported')
|
396 |
self.__dict__.update(locals()) # assign all variables to self
|
397 |
|
398 |
def forward(self, im, augment=False, visualize=False, val=False):
|
|
|
438 |
y = (self.model(im, training=False) if self.keras else self.model(im)[0]).numpy()
|
439 |
elif self.pb: # GraphDef
|
440 |
y = self.frozen_func(x=self.tf.constant(im)).numpy()
|
441 |
+
else: # Lite or Edge TPU
|
442 |
input, output = self.input_details[0], self.output_details[0]
|
443 |
int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model
|
444 |
if int8:
|