【发布时间】:2020-03-20 14:04:03
【问题描述】:
我有一个 TensorFlow Lite 模型和一个 Coral 开发板,我想在开发板的 TPU 上执行推理。
在我的 Python 推理脚本中初始化 TensorFlow Lite 解释器时,我按照 the Google Coral TFLite Python example 中的示例(链接到 getting started guide for the Coral Dev Board)中的示例添加“libedgetpu.so.1”作为实验委托,但推理是完全正确的与我未指定 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 documentation 中看不到任何关于此的内容。
开发板正在运行 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 浮点数据,如 this 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-learning tensorflow2.0 tensorflow-lite tpu google-coral