【问题标题】:AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_keras_model_file'AttributeError:类型对象“TFLiteConverterV2”没有属性“from_keras_model_file”
【发布时间】:2025-11-30 12:55:01
【问题描述】:

我有一个 TensorFlow Keras 模块“model.h5”。我想从中生成 tflite 。我正在使用下面提到的代码。我正在使用 tensorflow 版本“2.0.0”。

import tensorflow as tf
import numpy as np
from tensorflow import lite

dataset_dir = "C:\\Users\\Ravi\\dataset"
IMAGE_SIZE = 224
saved_keras_model = "C:\\Users\\Ravi\\model.h5"

def representative_data_gen():
  dataset_list = tf.data.Dataset.list_files(dataset_dir + '/*/*')
  for i in range(100):
    image = next(iter(dataset_list))
    image = tf.io.read_file(image)
    image = tf.io.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE])
    image = tf.cast(image / 255., tf.float32)
    image = tf.expand_dims(image, 0)
    yield [image]

converter =  lite.TFLiteConverter.from_keras_model_file(saved_keras_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# These set the input and output tensors to uint8
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
# And this sets the representative dataset so we can quantize the activations
converter.representative_dataset = representative_data_gen
tflite_model = converter.convert()

with open('mobilenet_v2_1.0_224_quant.tflite', 'wb') as f:
      f.write(tflite_model)

我遇到了这种错误。

Traceback (most recent call last):

  File "C:\Users\Ravi\face-mask-models\tflite_model.py", line 28, in <module>
    converter =  lite.TFLiteConverter.from_keras_model_file(saved_keras_model)

AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_keras_model_file'

可能是什么问题?我该如何解决这个问题?

【问题讨论】:

    标签: python keras tensorflow2.0 tensorflow-lite


    【解决方案1】:

    你从this page得到这个信息:

    仅整数 创建仅整数模型是用于微控制器和 Coral Edge TPU 的 TensorFlow Lite 的常见用例。

    此外,为了确保与纯整数设备(例如 8 位微控制器)和加速器(例如 Coral Edge TPU)的兼容性,您可以使用以下命令对包括输入和输出在内的所有操作强制执行全整数量化步骤:

    import tensorflow as tf
    converter = tf.lite.TFLiteConverter.from_keras_model('model.h5')
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    def representative_dataset_gen():
      for _ in range(num_calibration_steps):
        # Get sample input data as a numpy array in a method of your choosing.
        yield [input]
    converter.representative_dataset = representative_dataset_gen
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.int8  # or tf.uint8
    converter.inference_output_type = tf.int8  # or tf.uint8
    tflite_quant_model = converter.convert()
    

    所以我假设您希望使用上述代码确保与仅整数设备的兼容性。

    如果您不需要它并且只想转换模型,请执行以下操作:

    # WHOLE MODEL
    tflite_model = tf.keras.models.load_model('model.h5')
    converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model)
    tflite_save = converter.convert()
    open("generated.tflite", "wb").write(tflite_save)
    

    一般来说,您的代码在“lite”前面缺少一个“tf”,因此特定行应该是:

    converter =  tf.lite.TFLiteConverter.from_keras_model_file(saved_keras_model)
    

    代替:

    converter =  lite.TFLiteConverter.from_keras_model_file(saved_keras_model)
    

    如果出现问题,您可以随时升级到 TF v 2.2.0。

    如果您需要其他任何东西,请标记我

    【讨论】:

    • in for _ in range(num_calibration_steps),什么是 num_calibration_steps?如果可能的话,你能详细说明一下吗?
    • 嗨 mevada.. 你想量化模型吗?
    • @Farmaker 我也不清楚num_calibration_steps 是什么。你介意解释一下吗?我对 ML 非常陌生,如果它应该是显而易见的,我很抱歉。您提供的链接似乎没有解释。
    • 我从 Tensorflow 的官方链接复制代码。它没有提供有关您所询问内容的更多详细信息。如果你想让我也看看并尝试将你的文件发送 model.h5 到我的电子邮件地址 farmaker47@gmail.com
    • 也检查这个答案*.com/a/62675805/7118084
    最近更新 更多