【发布时间】: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