gpt4 book ai didi

python - 如何在脚本中加载 tflite 模型?

转载 作者:太空狗 更新时间:2023-10-29 21:20:32 26 4
gpt4 key购买 nike

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

最佳答案

您可以使用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官方指南更多详细信息请阅读this .

关于python - 如何在脚本中加载 tflite 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50443411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com