如何仅在 CPU 上 运行 tflite
How to run tflite on CPU only
我有一个 tflite 模型,它在珊瑚 USB 中 运行,但我也在 CPU 中将其设置为 运行(作为在珊瑚 USB 物理上不可用时通过一些测试的替代方法可用)。
我找到了 但给出的答案没有用。
我的代码如下所示:
class CoralObjectDetector(object):
def __init__(self, model_path: str, label_path: str):
"""
CoralObjectDetector, this object allows to pre-process images and perform object detection.
:param model_path: path to the .tflite file with the model
:param label_path: path to the file with labels
"""
self.label_path = label_path
self.model_path = model_path
self.labels = dict() # type: Dict[int, str]
self.load_labels()
self.interpreter = tflite.Interpreter(model_path),
experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
# more code and operations
模型和标签是从 here 下载的。
我想加载相同模型的替代版本,让我在没有 珊瑚 USB 加速器的情况下执行 (即仅在 CPU 中)。我的目标如下:
class CoralObjectDetector(object):
def __init__(self, model_path: str, label_path: str, run_in_coral: bool):
"""
CoralObjectDetector, this object allows to pre-process images and perform object detection.
:param model_path: path to the .tflite file with the model
:param label_path: path to the file with labels
:param run_in_coral: whether or not to run it on coral (use CPU otherwise)
"""
self.label_path = label_path
self.model_path = model_path
self.labels = dict() # type: Dict[int, str]
self.load_labels()
if run_in_coral:
self.interpreter = tflite.Interpreter(model_path),
experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
else:
# I expect somethig like this
self.interpreter = tflite.CPUInterpreter(model_path)
# more code and operations
我不确定 inference/prediction 方法中是否只需要这个或其他东西。
当您编译 Coral 模型时,它会将它可以执行的所有操作映射到单个 TPU 自定义 OP - 例如:
.
这意味着该模型只能在 TPU 上运行。话虽如此,您的 TFLite 解释器也可以 运行 CPU 建模(我们所做的只是添加实验委托来处理 edgetpu-custom-op)。要 运行 CPU 版本,只需传递模型的 CPU 版本(在编译之前)。
对于您的对象检测,如果您使用我们在 test_data, you'll see we provide the CPU and TPU version (for example for MNv1 SSD we have CPU and TPU 版本中提供的模型之一)。如果您将这些插入到我们的任何代码中,您会看到两者都有效。
在选择您使用的型号时,我会简单地检查一下是否连接了 Coral TPU。
我有一个 tflite 模型,它在珊瑚 USB 中 运行,但我也在 CPU 中将其设置为 运行(作为在珊瑚 USB 物理上不可用时通过一些测试的替代方法可用)。
我找到了
我的代码如下所示:
class CoralObjectDetector(object):
def __init__(self, model_path: str, label_path: str):
"""
CoralObjectDetector, this object allows to pre-process images and perform object detection.
:param model_path: path to the .tflite file with the model
:param label_path: path to the file with labels
"""
self.label_path = label_path
self.model_path = model_path
self.labels = dict() # type: Dict[int, str]
self.load_labels()
self.interpreter = tflite.Interpreter(model_path),
experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
# more code and operations
模型和标签是从 here 下载的。
我想加载相同模型的替代版本,让我在没有 珊瑚 USB 加速器的情况下执行 (即仅在 CPU 中)。我的目标如下:
class CoralObjectDetector(object):
def __init__(self, model_path: str, label_path: str, run_in_coral: bool):
"""
CoralObjectDetector, this object allows to pre-process images and perform object detection.
:param model_path: path to the .tflite file with the model
:param label_path: path to the file with labels
:param run_in_coral: whether or not to run it on coral (use CPU otherwise)
"""
self.label_path = label_path
self.model_path = model_path
self.labels = dict() # type: Dict[int, str]
self.load_labels()
if run_in_coral:
self.interpreter = tflite.Interpreter(model_path),
experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
else:
# I expect somethig like this
self.interpreter = tflite.CPUInterpreter(model_path)
# more code and operations
我不确定 inference/prediction 方法中是否只需要这个或其他东西。
当您编译 Coral 模型时,它会将它可以执行的所有操作映射到单个 TPU 自定义 OP - 例如:
这意味着该模型只能在 TPU 上运行。话虽如此,您的 TFLite 解释器也可以 运行 CPU 建模(我们所做的只是添加实验委托来处理 edgetpu-custom-op)。要 运行 CPU 版本,只需传递模型的 CPU 版本(在编译之前)。
对于您的对象检测,如果您使用我们在 test_data, you'll see we provide the CPU and TPU version (for example for MNv1 SSD we have CPU and TPU 版本中提供的模型之一)。如果您将这些插入到我们的任何代码中,您会看到两者都有效。
在选择您使用的型号时,我会简单地检查一下是否连接了 Coral TPU。