-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Got different accuracy between history and evaluate #10014
Comments
I have the same issue that is why i post it also here. First i tested my model after training in a separate session. But then i implemented testing directly after training and also on the training dataset, I compile using the fit_generator: If i then use model.evaluate with the same batch_size the result is: My guess is that the model trains correctly but at the end of training storing/ updating the model or model weights something goes wrong. update: Sorry i forgot to mention that i use python 3.6 and keras 1.2.4 and to save the model I use keras.callbacks.ModelCheckpoint with the following setup: Any help appriciated |
@z888888861 Do you use Batch Normalization layers? Are you fine-tuning the network (trainable=False for some of the layers)? If not, there is a very high chance you are overfitting the network. |
@datumbox "If not, there is a very high chance you are overfitting the network." btw. i upgrades tf to version 1.0.7 with cuda 9.0 and the problem remains. |
@SpecKROELLchen Did not notice he was testing on training data. Are you using BN layers and finetuning? If yes you might be affected by what is currently discussed here #9965 |
@datumbox Sorry for the late response, and thanks for your response. I don't use BN layers but fine tuning. READ IN AND STUFF... pmodel = resnet50.ResNet50(include_top=False, input_tensor=custom_input, weights="imagenet", history = pmodel.fit_generator(TRAIN_DATA, validation_data = VAL_DATA, shufle= True, score = pmodel.evaluate(TRAIN_DATA,verbose=1, batch_size=12) And the test-accuracy ON TRAINING DATA is always something between 50-70%, while I still did not solve this problem. So any help is appriciated. |
@SpecKROELLchen You are using BN layers, ResNet50 is full of them. Unfortunately you are also affected by how Keras implements this type of layer. Check the discussion on the PR for potential workarounds. |
@datumbox Ah, thank you. I thought you meant the layers behind the basemodel. Thx, i will keep an eye on that. |
Sorry i have to repost here again. I think my problem might still be a little bit different or a mix of the BN problem and another one. |
Okay, 1.) Read in the data, doing padding when resizing the image 2.) doing a train-test-split 80-20. READ IN DATA AND LABELS...
Comments: |
have the same issue, any updates on this issue? |
Same issue here. When training using fit_generator, I get a training and validation accuracy that are both much higher than the ones I get when I evaluate the model manually, on training and testing data. |
Same problem here. I'm training with
The loss and accuracy that are stored in
|
Try to downgrade to keras 2.1.6 and see if you still face such issues. I could solve it by downgrading. |
Even I am facing a problem similar to this one. I am using resnet as my base model. |
Thanks @adityapatadia! This solved my problem, too. |
I suggest you guys can save model/weights and load it and test, that might avoid the problem. |
I also got this problem, and when I save model/weights and load it to test, this problem just occurs. Also applying fine-tune with fit_generator; the accuracy of training data using fit_geterator and evaluate are greatly different. |
Loading the model from the saved model and evaluating it on the test set gives me 16% while |
Try the following might help:
|
what is the difference between what you suggested and load_model method? |
if there is your own custom layers/functions in your model. it is just my suggestion, you can try it tho. Or check if you load your custom layers/functions correctly. |
Hello, I think I got similar issue. Actually when I use keras.fit() to train the model and use the training data as validation data, I get different training accuracy and validation accuracy during the training process. Have you found the solution for the different training/test accuracy? |
I have found a fix for this issue. validation_datagen=ImageDataGenerator(rescale=1./255)
validation_generator=validation_datagen.flow_from_directory(
validation_directory,
target_size=target_size,
batch_size=validation_batch_size,
class_mode=class_mode,
shuffle=False
) |
Thank you very much @RaviBansal7717. |
@RaviBansal7717 Nothing made sense, world was turning gray, you are a lifesaver ! Why on earth would shuffle=True be the default though. |
I still have the problem that the training accuracy in history is 100% but when I use the model. evaluate(Train_image, Train_label), it gives me 86%, plus I already turned off the shuffle, regularization, dropout, and set batch size equal to the whole dataset number during training. Really have no idea what went wrong? |
I have a slightly different issue. Whether my training accuracy is 70, 80, or 98%, I routinely run |
I fit a model as follow:
history = model.fit(x_train, y_train, epochs=50, verbose=1, validation_data=(x_val,y_val))
Got the answer :
Epoch 48/50
49/49 [==============================] - 0s 3ms/step - loss: 0.0228 - acc: 0.9796 - val_loss: 3.3064 - val_acc: 0.6923
Epoch 49/50
49/49 [==============================] - 0s 3ms/step - loss: 0.0186 - acc: 1.0000 - val_loss: 3.3164 - val_acc: 0.6923
Epoch 50/50
49/49 [==============================] - 0s 2ms/step - loss: 0.0150 - acc: 1.0000 - val_loss: 3.3186 - val_acc: 0.6923
While, when I try to evaluate my model in train set with
model.evaluate(x_train,y_train)
I got this [4.552013397216797, 0.44897958636283875]
I have no idea how this happen? Thank you.
The text was updated successfully, but these errors were encountered: