InvalidArgumentError: can't parse serialized example when using model.fit() on keras model

29 views Asked by At

I am using Google Colab with Datasets from Roboflow (.tfrecord format) and am trying to train an object detection model.

whenever i run the following method:

model.fit(x = train_dataset, epochs=num_epochs, steps_per_epoch = 1000)

I get this error:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-49-4d3eb8becb33> in <cell line: 8>()
6 train_dataset = create_dataset(TRAINING_TFRecord, batch_size)
7 tf.debugging.disable_traceback_filtering() # voller Stack trace
8 model.fit(x = train_dataset,
9           epochs=num_epochs,
10           steps_per_epoch = 1000)
 
10 frames
/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
51   try:
52     ctx.ensure_initialized()
53     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
54                                         inputs, attrs, num_outputs)
55   except core._NotOkStatusException as e:
 
InvalidArgumentError: Graph execution error:

Detected at node ParseSingleExample/ParseExample/ParseExampleV2 defined at (most recent call last):
<stack traces unavailable>
Detected at node ParseSingleExample/ParseExample/ParseExampleV2 defined at (most recent call last):
<stack traces unavailable>
2 root error(s) found.
  (0) INVALID_ARGUMENT:  Key: image/object/class/label.  Can't parse serialized Example.
[[{{node ParseSingleExample/ParseExample/ParseExampleV2}}]]
[[IteratorGetNext]]
[[IteratorGetNext/_2]]
  (1) INVALID_ARGUMENT:  Key: image/object/class/label.  Can't parse serialized Example.
[[{{node ParseSingleExample/ParseExample/ParseExampleV2}}]]
[[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_6277]

for Reference, here is one example from the .tfrecord-file, after extracting the features to a readable format:

{'image/object/bbox/ymax': array([0.10576923, 0.13461539]),
'image/object/class/label': array([2, 2]),
 'image/object/bbox/xmax': array([0.61298078, 0.29086539]),
 'image/object/bbox/xmin': array([0.44951922, 0.11057692]),
 'image/object/bbox/ymin': array([0.00721154, 0.01682692]),
 'image/height': array([416]),
 'image/filename': array([b'555_jpg.rf.03d4644448b50fde6632292a13007912.jpg'], dtype='|S47'),
 'image/object/class/text': array([b'decayed tooth', b'decayed tooth'], dtype='|S13'),
 'image/width': array([416]),
 'image/format': array([b'jpeg'], dtype='|S4'),
 'image/encoded': array([b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C
                    image encoding keeps going ...
8a(\x03\xff\xd9'],
                    dtype='|S14588')}

I probably made a mistake somewhere in my parsing function, but can't find it. Here is the relevant code:

def create_dataset(tfrecord_file, batch_size):
    dataset = tf.data.TFRecordDataset(tfrecord_file)
    dataset = dataset.map(parse_tfrecord_fn)
    dataset = dataset.batch(batch_size, drop_remainder=True)
    
    return dataset

def parse_tfrecord_fn(example):
    feature_description = {
    'image/encoded': tf.io.FixedLenFeature([], tf.string),
    'image/object/class/label': tf.io.FixedLenFeature([], tf.int64, default_value=0)
    }
    example = tf.io.parse_single_example(example, feature_description)
    image = tf.io.decode_jpeg(example['image/encoded'])
    image = tf.reshape(image, [416, 416, 3]) 
    label = example['image/object/class/label']


    return image, label

from tensorflow import keras
from keras import layers, models
def create_cnn_model(input_shape, num_classes):
    model = models.Sequential()

    
    model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
   
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(layers.MaxPooling2D((2, 2)))

    model.add(layers.Conv2D(64, (3, 3), activation='relu'))

    model.add(layers.Flatten())
    model.add(layers.Dense(64, activation='relu'))

    model.add(layers.Dense(num_classes, activation='softmax'))

    model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy']
              #,run_eagerly = True
                  )

    return model

input_shape = (416, 416, 3)  # height, width, channels
num_classes = 2

model = create_cnn_model(input_shape, num_classes)

# Model trainieren
batch_size = 16
num_epochs = 10

#train_dataset = tf.data.TFRecordDataset(TRAINING_TFRecord)
train_dataset = create_dataset(TRAINING_TFRecord, batch_size)
#tf.debugging.disable_traceback_filtering() # voller Stack trace
model.fit(x = train_dataset,
          epochs=num_epochs,
          steps_per_epoch = 1000)
0

There are 0 answers