当前位置: 首页 > 知识库问答 >
问题:

Coral Dev Board上的TensorFlow Lite模型未在TPU上运行

江飞白
2023-03-14

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

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

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

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

开发板运行的是孟德尔版本2.0、Python版本3.5.3、tflite运行时版本2.1.0.post1(我知道我应该更新孟德尔版本,但我目前使用的是Windows PC,访问Linux机器或尝试使用Putty、VirtualBox或WSL从Windows更新开发板将是一件痛苦的事。如果只有Coral支持的Windows,就像Raspberry Pi一样…)。

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

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

共有2个答案

杭镜
2023-03-14

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

石正卿
2023-03-14

看起来你已经在我们的github页面上问了这个问题,并在这里得到了回答。只是想分享供他人参考

 类似资料:
  • 2018-05-09 11:51:38.783009:I tensorflow/contrib/lite/toco/import_tensorflow.cc:1057]转换不支持的操作:SquaredDifference 2018-05-09 11:51:38.783211:I tensorflow/contrib/lite/toco/import_tensorflow.cc:1057]转换不支持

  • 我正试图在PySpark上运行NuPIC,但我遇到了麻烦。有没有人知道我该怎么修好它? 当我不使用PySpark时,代码运行良好,但我现在尝试从Spark数据集运行它。 我试图使用目录中的源代码来运行它,因为通过安装Nupic包来运行它会导致一些其他错误。 谢谢你的帮助!! 我正在尝试运行这个函数 然而,我得到了这个错误,并不理解。 我想NuPIC无法访问frameworks/opf/htm_pr

  • 问题内容: 我正在运行Keras模型,提交截止日期为36小时,如果我在cpu上训练我的模型大约需要50个小时,是否可以在gpu上运行Keras? 我正在使用Tensorflow后端,并在未安装anaconda的Jupyter笔记本上运行它。 问题答案: 是的,您可以在GPU上运行keras模型。几件事您将必须首先检查。 您的系统具有GPU(Nvidia。因为AMD尚未运行) 您已经安装了Tenso

  • 我正在运行一个Keras模型,提交截止日期为36小时,如果我在cpu上训练我的模型,大约需要50小时,有没有办法在gpu上运行Keras? 我正在使用Tensorflow后端并在我的Jupyter笔记本上运行它,而没有安装anaconda。

  • 我有一个在keras中训练的模型,这是一个在MNIST数据集上训练的简单模型。 我试图做的是重写这个模型并在FPGA设备上运行。为了做到这一点,我想充分了解量化模型是如何工作的。 首先,我用训练后的量化将这个模型转换为。tflite格式和UINT8精度(https://www.tensorflow.org/lite/performance/post_training_quantization).

  • 我正在尝试在智能手机上的应用程序上运行张量流精简模型。首先,我使用 LSTM 用数值数据训练模型,并使用张量流.Keras 构建模型层。我使用了 TensorFlow V2.x 并将训练的模型保存在服务器上。之后,该模型由应用程序下载到智能手机的内部存储器,并使用“映射字节缓冲区”加载到解释器。直到这里一切正常。 问题在于解释器无法读取和运行模型。我还在 build.gradle 上添加了所需的依