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

How to validate tensorflow-lite model converted from keras

我是机器学习新手。我尝试使用此代码将我训练的模型从 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 工作室中为我的项目进一步使用它。谁能告诉我如何验证 my_model_converted_model.tflite

您可以定义 Interpreterallocate_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.")

完整的示例代码是 here. The code I used is from this TensorFlow resource

对于 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

希望对您有所帮助!