question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation accuracy stays 0.5 when Retraining ResNet50, others are fine

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

30reactions
chrike-platinumcommented, Jan 22, 2019

I also had this problem. It’s not only related to the batch normalization. Don’t rescale your data 1/255. But rather use the preprocess_input function in your generator. from keras.applications.resnet50 import preprocess_input The Keras ResNet pretrained weights are learned on a dataset with different preprocessing than Inception or VGG.

1reaction
A3itorcommented, Apr 2, 2020

@RunshengSong I’m facing exactly the same problem today… what did you guys did to solve the problem?? I’m using Anaconda and I can’t downgrade Keras to 1.2.2 as suggested. Also used these lines of code:

    tr_datagen = ImageDataGenerator(dtype='float32',preprocessing_function=preprocess_input)
    val_datagen = ImageDataGenerator(dtype='float32',preprocessing_function=preprocess_input)

instead of:

    tr_datagen = ImageDataGenerator(rescale=1./255, zoom_range=0.3, rotation_range=50,
                                    width_shift_range=0.2,height_shift_range=0.2,
                                    shear_range=0.2, horizontal_flip=True, fill_mode='nearest')

    val_datagen = ImageDataGenerator(rescale=1./255)

I haven’t seen any more posts about this issue on google or github… Any help is really appreciated

Read more comments on GitHub >

github_iconTop Results From Across the Web

Training accuracy improving but validation ... - Stack Overflow
My model was able to achieve a training accuracy of 0.80+ but the validation accuracy is either stuck around 0.5 or just stays...
Read more >
My dogs vs cats models always have 0.5 accuracy
During training, accuracy always hovers around 0.5, and doesn't improve. Training more epochs doesn't help.
Read more >
Why is the validation accuracy fluctuating? - Cross Validated
If I understand the definition of accuracy correctly, accuracy (% of data points classified correctly) is less cumulative than let's say MSE ...
Read more >
Evaluate the Performance of Deep Learning Models in Keras
The example below demonstrates the use of an automatic validation dataset ... and accuracy of the model on both the training and validation...
Read more >
When can Validation Accuracy be greater than Training ...
It might be due to the data as suggested by others. To be specific the testing data is easy to identify for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found