Untitled

mail@pastecode.io avatar
unknown
python
12 days ago
1.4 kB
8
Indexable
Never
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report

# Load the trained model
model = load_model('final_model.h5')

# Create data generator for the test set
datagen = ImageDataGenerator(featurewise_center=True)
datagen.mean = [123.68, 116.779, 103.939]

# Prepare the test iterator
test_it = datagen.flow_from_directory('dataset/test_set/',
                                      class_mode='binary', batch_size=64, target_size=(224, 224), shuffle=False)

# Classify the test images
predictions = model.predict(test_it, steps=len(test_it), verbose=1)
predicted_classes = (predictions > 0.5).astype(int)
true_classes = test_it.classes
class_labels = list(test_it.class_indices.keys())

# Print confusion matrix and classification report
cm = confusion_matrix(true_classes, predicted_classes)
print('Confusion Matrix')
print(cm)
print('Classification Report')
print(classification_report(true_classes, predicted_classes, target_names=class_labels))

# Plot confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=class_labels, yticklabels=class_labels)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title('Confusion Matrix')
plt.show()
Leave a Comment