Validation accuracy stays 0.5 when Retraining ResNet50, others are fine
Author: hkoelewijnCreated May 25, 2018Updated Dec 23, 2023
I am comparing several architectures for retraining. Using the Kaggle 'dogs vs cats' dataset, I set up the following: Data generator:
train_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
train_data = train_data_generator.flow_from_directory(
directory='{}/train'.format(PATH), shuffle=True, target_size=(sz, sz),
batch_size=batch_size, class_mode='binary')
test_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
validation_data = test_data_generator.flow_from_directory(
directory='{}/valid'.format(PATH), shuffle=True, batch_size=batch_size,
target_size=(sz, sz), class_mode='binary')Model:
pretrained_architecture = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet', input_shape=(sz,sz,3))
# add a global spatial average pooling layer
x = pretrained_architecture.output
x = tf.keras.layers.Flatten()(x)
# let's add a fully-connected layer
x = tf.keras.layers.Dense(1024, activation='relu')(x)
# and final prediction layer
predictions = tf.keras.layers.Dense(1, activation='sigmoid')(x)
# this is the model we will train
model = tf.keras.Model(inputs=pretrained_architecture.input, outputs=predictions)
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in pretrained_architecture.layers:
layer.trainable = False
Training:
model.compile(loss='binary_crossentropy', optimizer='SGD', metrics=['accuracy'])
model.fit_generator(train_data, epochs=1, validation_data=validation_data) #, callbacks=[lr_finder])
I am using default sizes of each model with batch sizes of 32. I ran this setup on my local machine: (Windows10/GTX970) and on Googles Colab (Tesla K80) Using InceptionV3 and VGG19, the training and validation run as expected. When I use Resnet50, the training looks fine, but validation returns 0.5 accuracy A sample of some predictions after training:
array([[0.14550358],
[0.15015373],
[0.14434135],
...
[0.13943274],
[0.14167683],
[0.15093516],
[0.14429297],
[0.13792236]], dtype=float32)It looks very suspicious. Also, it does not matter if I predict samples from my validation or training set, the results are similar.
Source: fchollet/deep-learning-models