tensorflow: TFLite Conversion Error: Element Shape Required to be 1D

1. System information

  • Windows 10 PC, TensorFlow version 2.5.0

2. Code

import tensorflow as tf
import model as modellib
import coco
import os 
import sys

# Enable eager execution
tf.compat.v1.enable_eager_execution()

class InferenceConfig(coco.CocoConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
config = InferenceConfig()
model = modellib.MaskRCNN(mode="inference", model_dir='logs', config=config)
model.load_weights('mask_rcnn_coco.h5', by_name=True)
model = model.keras_model

tf.saved_model.save(model, "tflite")

# Preparing before conversion - making the representative dataset
ROOT_DIR = os.path.abspath("../")
CARS = os.path.join(ROOT_DIR, 'Mask_RCNN\\mrcnn\\smallCar')

IMAGE_SIZE = 224
datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

def representative_data_gen():
    dataset_list = tf.data.Dataset.list_files(CARS)
    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 = tf.lite.TFLiteConverter.from_keras_model(model)
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This sets the representative dataset for quantization
converter.representative_dataset = representative_data_gen
# 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]
# For full integer quantization, though supported types defaults to int8 only, we explicitly declare it for clarity.
converter.target_spec.supported_types = [tf.int8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

with open('modelQuantized.tflite', 'wb') as f:
  f.write(tflite_model)

3. Failure after conversion

I get the following error for the tflite_model = converter.convert() line:

error: 'tf.TensorListReserve' op requires element_shape to be 1D tensor during TF Lite transformation pass,

FULL:

loc(callsite("mask_rcnn/mrcnn_detection/map/TensorArrayV2_1"("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\util\\deprecation.py":535:0) at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\util\\deprecation.py":602:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":774:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":839:0 at callsite("d:\\Mask_RCNN\\mrcnn\\utils.py":820:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":837:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\autograph\\impl\\api.py":645:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\base_layer.py":1030:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\functional.py":556:0 at "C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\functional.py":420:0)))))))))): error: 'tf.TensorListReserve' op requires element_shape to be 1D tensor during TF Lite transformation pass
loc(callsite("mask_rcnn/mrcnn_detection/map/TensorArrayV2_1"("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\util\\deprecation.py":535:0) at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\util\\deprecation.py":602:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":774:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":839:0 at callsite("d:\\Mask_RCNN\\mrcnn\\utils.py":820:0 at callsite("d:\\Mask_RCNN\\mrcnn\\model.py":837:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\autograph\\impl\\api.py":645:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\base_layer.py":1030:0 at callsite("C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\functional.py":556:0 at "C:\\Python39\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\functional.py":420:0)))))))))): error: failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\convert.py", line 291, in toco_convert_protos
    model_str = wrap_toco.wrapped_toco_convert(model_flags_str,
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\wrap_toco.py", line 32, in wrapped_toco_convert
    return _pywrap_toco_api.TocoConvert(
Exception: C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:535:0: error: 'tf.TensorListReserve' op requires element_shape to be 1D tensor during TF Lite transformation pass
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:602:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:774:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:839:0: note: called from
d:\Mask_RCNN\mrcnn\utils.py:820:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:837:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\autograph\impl\api.py:645:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:556:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:420:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:535:0: error: failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:602:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:774:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:839:0: note: called from
d:\Mask_RCNN\mrcnn\utils.py:820:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:837:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\autograph\impl\api.py:645:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:556:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:420:0: note: called from


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Mask_RCNN\mrcnn\convertQuantize.py", line 53, in <module>
    tflite_model = converter.convert()
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\lite.py", line 1057, in convert
    result = super(TFLiteKerasModelConverterV2,
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\lite.py", line 782, in convert
    result = _toco_convert_impl(
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\convert.py", line 698, in toco_convert_impl
    data = toco_convert_protos(
  File "C:\Python39\lib\site-packages\tensorflow\lite\python\convert.py", line 297, in toco_convert_protos
    raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:535:0: error: 'tf.TensorListReserve' op requires element_shape to be 1D tensor during TF Lite transformation pass
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:602:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:774:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:839:0: note: called from
d:\Mask_RCNN\mrcnn\utils.py:820:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:837:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\autograph\impl\api.py:645:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:556:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:420:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:535:0: error: failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
C:\Python39\lib\site-packages\tensorflow\python\util\deprecation.py:602:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:774:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:839:0: note: called from
d:\Mask_RCNN\mrcnn\utils.py:820:0: note: called from
d:\Mask_RCNN\mrcnn\model.py:837:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\autograph\impl\api.py:645:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1030:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:556:0: note: called from
C:\Python39\lib\site-packages\tensorflow\python\keras\engine\functional.py:420:0: note: called from

Not sure what the error is, or how to fix it. Any help is appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

converter.target_spec.supported_ops = [ tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS ] converter._experimental_lower_tensor_list_ops = True

Please use the Select TF ops and disable tensor list op lowering.