【问题标题】:How to validate tensorflow-lite model converted from keras如何验证从 keras 转换的 tensorflow-lite 模型
【发布时间】:2020-06-14 12:19:38
【问题描述】:

我是新机器学习。我尝试使用此代码将训练有素的模型从 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

【问题讨论】:

  • 这个模型需要什么样的输入?它是一个简单的浮点数组还是一个多维数组?您始终可以在 android 中正确加载它,并作为输入传递与在计算机上传递它时相同的数组。结果应该是一样的。您还可以查看包含 Java 和 Python 推理代码示例的文档here
  • 先生,我在两类树上训练了我的模型。现在我不知道如何在 tensorflowlite 中进行验证
  • 使用 Python API here 检查推理。将结果与 Keras 模型的结果进行比较。
  • 先生,我有点困惑。我试过了,但我不知道如何在其中发送我的测试图像
  • 我知道这可能很困难......但为此您必须制作一个 python 笔记本供我检查。你使用 colaboratory 吗?

标签: python tensorflow keras tensorflow-lite


【解决方案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

希望我能帮上忙!

【讨论】:

    【解决方案2】:

    您可以定义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。我使用的代码来自这个 TensorFlow resource

    【讨论】:

    • 谢谢先生,我应该在代码中进行哪些更改,因为我必须从计算机中获取图像,然后使用经过训练的模型进行测试?
    • 先生您好,这行出现错误:expected = model.predict(x_test[i:i+1]) 错误是:NameError: name 'model' is not defined
    • 将“模型”更改为“已加载模型”
    • 先生,我如何加载模型,因为我现在不使用 keras 模型,我正在验证 tensorflow-lite 模型
    • 如果您使用了我的代码,interpreter = tf.lite.Interpreter(model_content=tflite_model) 应该可以工作。您可以将参数更改为 model_path 或 model_dir。
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2019-04-14
    • 1970-01-01
    • 2021-12-21
    • 2020-04-30
    相关资源
    最近更新 更多