« 機械学習データ | メイン | tensorflow lite wheels windows buidできないメモ »
2022年08月13日
tensorflow lite の利用
まず学習したモデルを変換する
tflite_convert \
--saved_model_dir=モデルへの相対パス \
--output_file=モデルの保存先
拡張子は .tflite をよく使うみたい
------------------------
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model("ALL_HALF_51")
tflite_model = converter.convert()
open("test_model.tflite", "wb").write(tflite_model)
-------------------------------
pythonスクリプトでも行ける
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
推論
import numpy as np
import tensorflow as tf
# Load the 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 the 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)
投稿者 muuming : 2022年08月13日 21:43