0

I am working on an image segmentation use case. So I created a keras model with ".h5" extension and then I am now trying to convert this keras model to a tensorflow lite model as this tensorflow lite model is required to run in the android application. But while converting the model to tflite model, I am also performing integer quantization and while performing the quantization, I am getting the above error. Please find below the code for converting the model to tflite model.

def r_parse(x):
    def _parse(x):
        x = read_image(x)        
        return x

def r_read_image(path):
    path = path.decode()
    x = cv2.imread(path, cv2.IMREAD_COLOR)
    x = x/255.0
    x = x.astype(np.float32)
    return x

# As integer quantization requires a representative dataset, the below function generates that dataset
def representative_data_gen():
    r_dataset_path = "people_segmentation/new_data"
    r_train_path = os.path.join(r_dataset_path, "train")
    train_images= sorted(glob(os.path.join(r_train_path, "image", "*png")))
    train_images = tf.data.Dataset.from_tensor_slices(train_images)
    train_images = train_images.map(r_parse)
    train_images = train_images.batch(1)
    train_images = train_images.prefetch(100)
    r_train_dataset = tf_dataset(train_x, train_y, batch=2)
    for input_value in train_images:
        yield[input_value]

""" Loading model """
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}):
    model = tf.keras.models.load_model("model.h5")

# Below code is for converting the model to tflite model
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
# Ensure that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# Set the input and output tensors to uint8 (APIs added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tf_lite_model=converter.convert()

with open('tf_lite_model_2.tflite', 'wb') as f:
   f.write(tf_lite_model)

So when I execute the above code, I am getting the error mentioned in the title. Please find below the complete stacktrace for the same error.

Traceback (most recent call last):
  File "/home/sarthak/VS_CODE_WS/HumanImageSegmentationWithDeepLabV3+/Human-Image-Segmentation-with-DeepLabV3Plus-in-TensorFlow/train.py", line 166, in <module>
    tf_lite_model=converter.convert()
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 775, in wrapper
    return self._convert_and_export_metrics(convert_func, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 761, in _convert_and_export_metrics
    result = convert_func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 1170, in convert
    saved_model_convert_result = self._convert_as_saved_model()
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 1152, in _convert_as_saved_model
    return super(TFLiteKerasModelConverterV2,
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 951, in convert
    return self._optimize_tflite_model(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/convert_phase.py", line 226, in wrapper
    raise error from None  # Re-throws the exception.
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/convert_phase.py", line 216, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 721, in _optimize_tflite_model
    model = self._quantize(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/lite.py", line 529, in _quantize
    calibrated = calibrate_quantize.calibrate(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/convert_phase.py", line 226, in wrapper
    raise error from None  # Re-throws the exception.
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/convert_phase.py", line 216, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/optimize/calibrator.py", line 228, in calibrate
    self._feed_tensors(dataset_gen, resize_input=True)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/lite/python/optimize/calibrator.py", line 97, in _feed_tensors
    for sample in dataset_gen():
  File "/home/sarthak/VS_CODE_WS/HumanImageSegmentationWithDeepLabV3+/Human-Image-Segmentation-with-DeepLabV3Plus-in-TensorFlow/train.py", line 141, in representative_data_gen
    train_images = train_images.map(r_parse)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 2004, in map
    return MapDataset(self, map_func, preserve_cardinality=True, name=name)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 5466, in __init__
    variant_tensor = gen_dataset_ops.map_dataset(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/gen_dataset_ops.py", line 3295, in map_dataset
    _ops.raise_from_not_ok_status(e, name)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py", line 7107, in raise_from_not_ok_status
    raise core._status_to_exception(e) from None  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Length for attr 'output_shapes' of 0 must be at least minimum 1
        ; NodeDef: {{node MapDataset}}; Op<name=MapDataset; signature=input_dataset:variant, other_arguments: -> handle:variant; attr=f:func; attr=Targuments:list(type),min=0; attr=output_types:list(type),min=1; attr=output_shapes:list(shape),min=1; attr=use_inter_op_parallelism:bool,default=true; attr=preserve_cardinality:bool,default=false; attr=metadata:string,default=""> [Op:MapDataset]

I tried looking on the internet but the solutions arent working for me. As I am new for tflite, I need a little bit of help here. I will be much grateful for any kind of help.

Thanks in Advance!!

  • Are you able to convert non-quantized model successfully? – Alex K. Dec 08 '21 at 10:35
  • Yes. Even I am able to convert using the Dynamic range quantization. This error comes when I try to do full-integer quantization. The reason I am doing full-integer quantization is that I need the input and output shape of the tflite model to have the dtype as "uint8" – sarthak dave Dec 08 '21 at 11:39
  • [map](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map) requires function to perform on dataset. What is yours `r_parse` ? – Alex K. Dec 09 '21 at 09:10
  • Its a function. I am adding it to above code – sarthak dave Dec 11 '21 at 07:36
  • Check this https://stackoverflow.com/questions/66624228/length-for-attr-output-shapes-of-0-must-be-at-least-minimum-1 – Alex K. Dec 13 '21 at 08:55

0 Answers0