Why would validation loss be exceptionally high while fitting with efficientnet?

Viewed 169

I'm trying to train with efficientnet on keras, following somewhat their tutorial on transfer learning here. But the validation losses are super high (more epochs show the same behavior), and go up and down, with accuracy not improving

effnet = EfficientNetB0(weights='imagenet', input_shape = (224, 336, 3), include_top=False)

inputs = tf.keras.Input(shape = (224, 336, 3))
x = effnet(inputs, training=False)
x = layers.GlobalAveragePooling2D()(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.2)(x)
outputs = layers.Dense(12, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)

model.compile(optimizer=tf.keras.optimizers.Adam(), loss='categorical_crossentropy', metrics = ['accuracy'])

train_dir = 'train_images'
train = pd.read_csv('train.csv')

train_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range=20,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    validation_split = 0.3)

train_generator = train_datagen.flow_from_dataframe(
        train,
        directory = train_dir,
        x_col = 'image',
        y_col = 'labels',
        target_size = (224, 336), 
        batch_size = 16,
        class_mode = 'categorical', subset='training')

valid_generator = train_datagen.flow_from_dataframe(
        train,
        directory = train_dir,
        x_col = 'image',
        y_col = 'labels',
        target_size = (224, 336),
        batch_size = 16,
        class_mode ='categorical', subset='validation')

model.fit(train_generator, epochs=3, verbose=2, validation_data = valid_generator)

Which shows

Epoch 1/3
932/932 - 2285s - loss: 2.0492 - accuracy: 0.2466 - val_loss: 10590.5576 - val_accuracy: 0.1487
Epoch 2/3
932/932 - 2305s - loss: 1.9277 - accuracy: 0.2576 - val_loss: 255.5423 - val_accuracy: 0.1487
Epoch 3/3
932/932 - 2305s - loss: 1.9053 - accuracy: 0.2608 - val_loss: 257663.1406 - val_accuracy: 0.0075

Any ideas on why the validation loss could be so high?

1 Answers

AFAIK, there is some issue or bug in the original efficientnet and I don't know if it fixed or not yet. GitHub Issue #48103

A quick solution is to remove the following normalization:

rescale = 1./255,

Or, instead, use the unofficial efficientnet.

# Install non-official efficient network 
!pip install -U git+https://github.com/qubvel/efficientnet

import efficientnet.keras as efn 
base_model = efn.EfficientNetB0(include_top=False,
                                        weights="imagenet", 
                                        input_shape=input_shape)
Related