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

Viewed 4530

I am having one TensorFlow Keras module "model.h5". I want to generate tflite from it. I am using the below-mentioned code for that. I am using tensorflow version '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)

I am getting this kind of error.

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'

What can be the problem? How can I solve this issue?

1 Answers

From this page you get this info:

Integer only Creating integer only models is a common use case for TensorFlow Lite for Microcontrollers and Coral Edge TPUs.

Additionally, to ensure compatibility with integer only devices (such as 8-bit microcontrollers) and accelerators (such as the Coral Edge TPU), you can enforce full integer quantization for all ops including the input and output, by using the following steps:

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()

So I assume that you want to ensure compatibility with integer only devices with the above code.

If you do not need that and you only want to convert your model do:

# 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)

Generally speaking at your code is missing a 'tf' in front of 'lite'so at the specific line should be:

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

instead of:

converter =  lite.TFLiteConverter.from_keras_model_file(saved_keras_model)

and you could always upgrade to TF v 2.2.0 if something is not working.

Tag me if you need anything else

Related