如何验证从keras转换而来的tensorflow-lite模型?

问题描述 投票:0回答:1

我是一个新的机器学习。我试着用这段代码把我训练的模型从keras改成tensorflow-lite。

# Converting a SavedModel to a TensorFlow Lite model. 
saved_model_dir = r'C:\Users\Munib\New folder\my_model.h5' 
loaded_model = tf.keras.models.load_model(saved_model_dir)
converter = tf.lite.TFLiteConverter.from_keras_model(loaded_model)# .from_saved_model(saved_model_dir) 
tflite_model = converter.convert()
open("my_model_converted_model.tflite", "wb").write(tflite_model)

现在,我想验证我的模型,它是由转换检查其工作正确与否,以便我可以比进一步使用它在Android studio为我的项目。谁能告诉我如何验证 my_model_converted_model.tflite.

python tensorflow keras tensorflow-lite
1个回答
1
投票

对于Keras模型。

import tensorflow as tf
import tensorflow.keras as K
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(threshold=sys.maxsize)
from tensorflow.keras import backend as K, models
from tensorflow.keras.models import *
from tensorflow.keras.layers import *

# load model from saved chackpoint
model = tf.keras.models.load_model('my_model.h5')

# print model layers and input/outputs
print(model.layers)
for input in model.inputs:
  print(input)
for node in model.outputs:
  print(node)

# Load and transform image
image_a = plt.imread('1017_1.jpg')
image_a = cv2.resize(image_a,(150,150))
image_a = np.asarray(image_a)/255
image_a = np.reshape(image_a,(1,150,150,3))

# view output
model.predict(image_a)
# array([[0.6071461]], dtype=float32)

对于Tensorflow-Lite。

# generate .tflite file
tflite_model = tf.keras.models.load_model('my_model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model)
tflite_save = converter.convert()
open("my_model.tflite", "wb").write(tflite_save)    

# Load the TFLite model and allocate tensors. View details
interpreter = tf.lite.Interpreter(model_path="my_model.tflite")
print(interpreter.get_input_details())
print(interpreter.get_output_details())
print(interpreter.get_tensor_details())
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on input data.
input_shape = input_details[0]['shape']
print(input_shape)

# Use same image as Keras model
input_data = np.array(image_a, 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)
# [  1 150 150   3]
# [[0.6071461]]

以上都是用。

print(tf.__version__)
print(K.__version__)
2.2.0
2.3.0-tf

希望我帮到你了


1
投票

你可以定义 Interpreter, allocate_tensorsinvoke 获取tflite的输出,并与Keras的结果进行比较,如下所示。

import numpy as np
# Run the model with TensorFlow to get expected results.
TEST_CASES = 10

# Run the model with TensorFlow Lite
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

for i in range(TEST_CASES):
  expected = model.predict(x_test[i:i+1])
  interpreter.set_tensor(input_details[0]["index"], x_test[i:i+1, :, :])
  interpreter.invoke()
  result = interpreter.get_tensor(output_details[0]["index"])

  # Assert if the result of TFLite model is consistent with the TF model.
  np.testing.assert_almost_equal(expected, result,decimal=6)
  print("Done. The result of TensorFlow matches the result of TensorFlow Lite.")

完整的示例代码是 此处. 我使用的代码来自这个TensorFlow 资源.

© www.soinside.com 2019 - 2024. All rights reserved.