首页 > 解决方案 > 如何测试加载 tflite 模型并在一张图像上进行测试

问题描述

我已经使用 tflite 模型制作器训练了跌倒和非跌倒人员检测模型,并且我在训练时对其进行了测试,但我想通过加载 tflite 文件并仅提供一张图像来进行测试。

标签: pythontensorflowtensorflow-lite

解决方案


此页面包含有关如何使用 python 加载 TFLite 模型的说明:

import numpy as np
import tensorflow as tf

# Load the 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 the 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)

将 替换为input_data您的输入图像。


推荐阅读