其他分享
首页 > 其他分享> > 如何在脚本中加载tflite模型?

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

作者:互联网

我已经使用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,tensorflow,tensorflow-lite
来源: https://codeday.me/bug/20191005/1857792.html