1

when feeding a tf.data.Dataset to train EfficientnetB0 model I get the following error:

ValueError: in converted code:

    C:\Users\fconrad\AppData\Local\Continuum\anaconda3\envs\venv_spielereien\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py:677 map_fn
        batch_size=None)
    C:\Users\fconrad\AppData\Local\Continuum\anaconda3\envs\venv_spielereien\lib\site-packages\tensorflow_core\python\keras\engine\training.py:2410 _standardize_tensors
        exception_prefix='input')
    C:\Users\fconrad\AppData\Local\Continuum\anaconda3\envs\venv_spielereien\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py:573 standardize_input_data
        'with shape ' + str(data_shape))

    ValueError: Error when checking input: expected efficientnet-b0_input to have 4 dimensions, but got array with shape (224, 224, 3)

I realy wonder why this happens, since when I create a batch from my Dataset:

train_generator = (tf.data.Dataset
                  .from_tensor_slices((train_imgs, train_labels))
                  .map(read_img)
                  .map(flip_img)
                  .map(brightness)
                  .map(blur)
                  .map(noise)
                   .map(rotate_90)
                  .repeat()
                  .shuffle(512)
                  .batch(BATCH_SIZE)
                  .prefetch(True))

validation_generator = (tf.data.Dataset
                       .from_tensor_slices((validation_imgs, validation_labels))
                       .map(read_img)
                       )

print(train_generator.__iter__().__next__()[0].shape)

I get the expected result (64, 224, 224, 3).

But after creating the model the error above raises when I start training:

effn = tfkeras.EfficientNetB0(include_top=False, input_shape=img_shape, classes=4)
effn_model = tf.keras.Sequential()
effn_model.add(effn)
effn_model.add(tf.keras.layers.GlobalAveragePooling2D())
effn_model.add(tf.keras.layers.Dense(4, 'softmax'))

effn_model.compile(optimizer= 'adam', loss='categorical_crossentropy', metrics= ['categorical_accuracy'])

effn_model.fit(train_generator,
               epochs=20,
               steps_per_epoch=train_imgs.shape[0] // BATCH_SIZE,
               validation_data= validation_generator)

Does anyone know why the slices from dataset have shape (64,224,224,3) but the model doesnt recognize the batch dimension? when I try to train a keras.application model, everything works fine. I use tensorflow 2.1 and the pip install of efficientnet. Thanks

1 Answers1

0

as explained here keras.io/api/applications/efficientnet/

input_shape: Optional shape tuple, only to be specified if include_top is False. It should have exactly 3 inputs channels.

as so try this->

from tensorflow.keras.applications.efficientnet import EfficientNetB0, EfficientNetB5
mm = EfficientNetB0(include_top=True, weights=None, input_tensor=None, input_shape=(128, 128, 3), pooling=None, classes=2, classifier_activation="sigmoid")
mm.summary()

note the input_shape=(128, 128, 3) It has 3 channels.

bitbang
  • 1,804
  • 14
  • 18