首页 > 解决方案 > 如何在脚本中加载 tflite 模型?

问题描述

我已经使用bazel.pb将文件转换为文件。现在我想在我的python脚本中加载这个模型只是为了测试天气这是否给了我正确的输出?tflitetflite

标签: pythontensorflowtensorflow-lite

解决方案


您可以使用TensorFlow Lite Python 解释器在 python shell 中加载 tflite 模型,并使用输入数据对其进行测试。

代码将是这样的:

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

以上代码来自 TensorFlow Lite 官方指南更多详细信息,请阅读本文


推荐阅读