Coral Dev Board 上的 TensorFlow Lite 模型不是 TPU 上的 运行

TensorFlow Lite model on Coral Dev Board not running on TPU

我有一个 TensorFlow Lite 模型和一个 Coral Dev Board,我想在 Dev Board 的 TPU 上进行推理。

在我的 Python 推理脚本中初始化 TensorFlow Lite 解释器时,我按照 the Google Coral TFLite Python example (linked to in the getting started guide for the Coral Dev Board 中的示例添加 "libedgetpu.so.1" 作为实验委托,但是推理完全相同速度与我没有指定 TPU 实验委托时一样,所以我假设推理仍在 运行 开发板的 CPU 上。开发板上的推理时间(有和没有实验委托)为 32 秒;在我的台式机上,如果我 运行 CPU 上的 TFLite 模型,则相同测试集的推理时间为 10 秒,如果我 运行 在 Keras 中转换为相同模型,则推理时间为 1.3 秒TFLite(我认为这比 TFLite 更快,因为它使用多个内核)。

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

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

中看不到任何相关信息

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

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

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()

您似乎已经在我们的 github 页面上问过这个问题并且是 answered here。只是想分享给其他人参考

所以我从你的post了解到你是运行主机平台上的推理脚本。 根据 https://coral.ai/docs/edgetpu/tflite-python/#load-tensorflow-lite-and-run-an-inference 中的文档 您必须能够 load_delegate 文件,使用 Windows 中的 edgetpu.dll 而不是您使用的 libedgetpu.so.1。 希望对您有所帮助!