python - 如何在脚本中加载 tflite 模型?
问题描述
我已经使用bazel.pb
将文件转换为文件。现在我想在我的python脚本中加载这个模型只是为了测试天气这是否给了我正确的输出?tflite
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 官方指南,更多详细信息,请阅读本文。
推荐阅读
- javascript - 如何在 reactjs 项目中添加 JavaScript 标签
- android - 无法将任何数据添加到实时数据库 Firebase
- r - R read.xlsx '在工作表上找不到数据'
- python - 使用 python 将数据从 MongoDB 插入到 Oracle (RDBMS)
- windows - AWS 中的 NAT 问题与 RDP
- ruby-on-rails - Rails 登出用户从 Rails 控制台设计
- r - 如何在包含与特定单词列表匹配的推文的数据集中查找观察结果
- visual-studio-code - VSCodeVim:如何让 CTRL+P / CTRL+N 非 IntelliSense 完成与它在 VIM 中的工作方式相同?
- c++ - emmintrin.h:31:3: error: #error "SSE2 指令集未启用" #error "SSE2 指令集未启用", "scaling solution"
- php - 从与主模型列的关系中获取列的更好解决方案