Validation accuracy stays 0.5 when Retraining ResNet50, others are fine
See original GitHub issueI 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:
- Created 5 years ago
- Comments:10
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.instead of:
I haven’t seen any more posts about this issue on google or github… Any help is really appreciated