Val_accuracy doesn't change when I'm training my model

Viewed 776

I am working on a skin cancer recognition model. I am trying to train my model using keras and tensorflow but I noticed that the val_accuracy doesn't change it's stuck in 54% while the accuracy and loss as well as val_loss are changing. I used pre trained weights for ResNet50 layer then I added other layers and freezed the first layer to train the rest. Here is my model:

model = keras.Sequential()
model.add(ResNet50(include_top=False,input_tensor=None, input_shape=(224,224,3)
,pooling='avg',classes=2,weights=resnet_weights_path))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.layers[0].trainable = False
model.summary()
model.compile(optimizer=SGD(lr=0.001),loss='binary_crossentropy',metrics=['accuracy'])
red_lr= ReduceLROnPlateau(monitor='val_accuracy',patience=3,verbose=1,factor=0.7)
batch_size=64
epochs=150
History = model.fit_generator(datagen.flow(x_train,y_train,batch_size=batch_size),validation_data=(x_val,y_val),
                          epochs= epochs, steps_per_epoch=x_train.shape[0]//batch_size,verbose=1,callbacks=[red_lr]
                         )

And here is the log for the trainig:

Epoch 1/150
25/25 [==============================] - 200s 8s/step - loss: 0.1236 - accuracy: 0.9498 - val_loss: 2.0365 - val_accuracy: 0.5470
Epoch 2/150
25/25 [==============================] - 195s 8s/step - loss: 0.1251 - accuracy: 0.9530 - val_loss: 2.0819 - val_accuracy: 0.5470
Epoch 3/150
25/25 [==============================] - 199s 8s/step - loss: 0.1314 - accuracy: 0.9500 - val_loss: 2.1083 - val_accuracy: 0.5470

And so on So if you can see what's wrong with my model please help! Thank you!

2 Answers

The reason why this is happening in your case is that you do not:

  1. Unfreeze some of the layers in order to make them trainable, thus enabling further adaptation to your new problem.
  2. Decrease the learning rate since you are applying transfer learning, in order not to 'spoil' everything that was learned before.

Combining these two together will solve your problem. At the same time, you may want to remove the BatchNormalization, as it was suggested in the comment below.

  1. Your val loss is changing but not enough to cause a change in the hard classes that the predictions are getting converted to
  2. This is a typical case of overfitting. What is the size of your dataset? You are using very heavy layers on top of resnet. Also, make sure the resnet is frozen and that its weights (both trainable and updatable like the moving means adn std of batch norm layers) are not changing.
Related