Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14360

Model.evaluate vs Model.predict: Keras , Transfer Learning : Why such difference in accuracy?

$
0
0

I am using a pre trained resnet 50 model for image classification. After running the model for a few epochs here are the results I've gotten:Epoch 8/20 loss: 0.5705 - accuracy: 0.8785 - val_loss: 0.9226 - val_accuracy: 0.8135

Epoch 9/20 loss: 0.5509 - accuracy: 0.8780 - val_loss: 0.9321 - val_accuracy: 0.7979

These results obtained can be improved but let's just keep these for now.

When I run model.evaluate() I obtain a value I would expect:

# Evaluate the model test_loss, test_acc = model.evaluate(val_data_flow) print("Test accuracy:", test_acc)

Test accuracy: 0.7978515625

When I run Model.predict() the accuracy is abysmal:

from sklearn.metrics import classification_report# Generate predictions on the test datasetpredictions = model.predict(val_data_flow)# Convert predictions to class labelspredicted_labels = np.argmax(predictions, axis=1)# Extract true labels from test datasettrue_labels = val_data_flow.labels# Generate classification reportclass_names = list(val_data_flow.class_indices.keys())print(classification_report(true_labels, predicted_labels, target_names=class_names)) precision    recall  f1-score   support       1       0.33      0.38      0.36       330       2       0.00      0.00      0.00        14       3       0.17      0.17      0.17       133       4       0.09      0.11      0.10       102       5       0.00      0.00      0.00         5       6       0.00      0.00      0.00        21       7       0.12      0.12      0.12       133       8       0.18      0.16      0.17       154       9       0.18      0.13      0.15       132accuracy                           0.21      1024macro avg       0.12      0.12      0.12      1024weighted avg       0.20      0.21      0.21      1024

What could be causing such issue? Or is It because the functions model.predict and model.evaluate dont work the same way?

My data set is very imbalanced


Viewing all articles
Browse latest Browse all 14360

Trending Articles