首页 > 解决方案 > Coral 开发板上的 TensorFlow Lite 模型未在 TPU 上运行

问题描述

我有一个 TensorFlow Lite 模型和一个 Coral 开发板,我想在开发板的 TPU 上执行推理。

在我的 Python 推理脚本中初始化 TensorFlow Lite 解释器时,我添加“libedgetpu.so.1”作为实验委托,遵循Google Coral TFLite Python 示例中的示例(链接到Coral 开发板的入门指南) ,但是推理与我未指定 TPU 实验委托时的速度完全相同,因此我假设推理仍在开发板的 CPU 上运行。开发板上的推理时间(有和没有实验代表)是 32 秒;在我的台式电脑上,如果我在 CPU 上运行 TFLite 模型,相同测试集的推理时间为 10 秒,如果我在转换为 TFLite 之前在 Keras 中运行相同模型,则为 1.3 秒(我假设这比 TFLite 更快,因为它利用多个核心)。

我的问题:如何让推理在开发板的 TPU 而不是 CPU 上运行?

我想知道这是否是我在转换为 TFLite 格式之前在我的 PC 上构建 Keras 模型时需要指定的内容(例如,使用with tf.device上下文管理器或使生成的 TFLite 模型使用 TPU 的东西),但我看不到任何关于这在TensorFlow Lite Converter Python API 文档中。

开发板正在运行 Mendel 版本 2.0、Python 版本 3.5.3、tflite-runtime 版本 2.1.0.post1(我知道我应该更新 Mendel 版本,但是我目前使用的是 Windows PC,这会很痛苦访问 Linux 机器,或者尝试使用 Putty、VirtualBox 或 WSL 从 Windows 更新开发板。如果只有 Coral 支持 Windows,就像 Raspberry Pi 那样......)。

下面是我的推理脚本(如果需要,我也可以上传训练脚本和模型;数据集是 MNIST,转换为 NumPy 浮点数据,如本 Gist中所述):

import numpy as np
from time import perf_counter
try:
    # Try importing the small tflite_runtime module (this runs on the Dev Board)
    print("Trying to import tensorflow lite runtime...")
    from tflite_runtime.interpreter import Interpreter, load_delegate
    experimental_delegates=[load_delegate('libedgetpu.so.1.0')]
except ModuleNotFoundError:
    # Try importing the full tensorflow module (this runs on PC)
    try:
        print("TFLite runtime not found; trying to import full tensorflow...")
        import tensorflow as tf
        Interpreter = tf.lite.Interpreter
        experimental_delegates = None
    except ModuleNotFoundError:
        # Couldn't import either module
        raise RuntimeError("Could not import Tensorflow or Tensorflow Lite")

# Load data
mnist_file = np.load("data/mnist.npz")
x_test = mnist_file["x_test"]
y_test = mnist_file["y_test"]
x_test = x_test.astype(np.float32)

# Initialise the interpreter
tfl_filename = "lstm_mnist_model_b10000.tflite"
interpreter = Interpreter(model_path=tfl_filename,
    experimental_delegates=experimental_delegates)
interpreter.allocate_tensors()

print("Starting evaluation...")
for _ in range(3):
    input_index = (interpreter.get_input_details()[0]['index'])
    output_index = (interpreter.get_output_details()[0]['index'])
    # Perform inference
    t0 = perf_counter()
    interpreter.set_tensor(input_index, x_test)
    interpreter.invoke()
    result = interpreter.get_tensor(output_index)
    t1 = perf_counter()
    # Print accuracy and speed
    num_correct = (result.argmax(axis=1) == y_test).sum()
    print("Time taken (TFLite) = {:.4f} s".format(t1 - t0))
    print('TensorFlow Lite Evaluation accuracy = {} %'.format(
        100 * num_correct / len(x_test)))
    # Reset interpreter state (I don't know why this should be necessary, but
    # accuracy suffers without it)
    interpreter.reset_all_variables()

标签: machine-learningtensorflow2.0tensorflow-litetpugoogle-coral

解决方案


看起来您已经在我们的 github 页面上提出了这个问题,并且在这里得到了回答。只是想分享给别人参考


推荐阅读