web-dev-qa-db-ja.com

スクリプトでtfliteモデルを読み込む方法は?

bazelを使用して、.pbファイルをtfliteファイルに変換しました。ここで、このtfliteモデルをpythonスクリプトにロードして、天気が正しいかどうかをテストしますか?

7
Harshit Mishra

TensorFlow Lite Pythonインタープリターを使用して、tfliteモデルをpython =シェル、入力データでテストします。

コードは次のようになります。

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

23
Jing Zhao