如何在脚本中加载 tflite 模型? [英] How to load a tflite model in script?

查看:29
本文介绍了如何在脚本中加载 tflite 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have converted the .pb file to tflite file using the bazel. Now I want to load this tflite model in my python script just to test that weather this is giving me correct output or not ?

推荐答案

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

You can use TensorFlow Lite Python interpreter to load the tflite model in a python shell, and test it with your input data.

代码如下:

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官方指南更多详细信息,请阅读这个.

这篇关于如何在脚本中加载 tflite 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆