Skip to content
58 changes: 58 additions & 0 deletions src/UAV_navigation_system/cnn_evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# cnn_evaluate.py

import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

# Paths
base_dir = os.path.abspath("../data")
test_dir = os.path.join(base_dir, "test")

# Match image size with training
img_size = (128, 128)
batch_size = 32

# Load and preprocess test data
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=img_size,
batch_size=batch_size,
class_mode='categorical',
shuffle=False # Required for accurate predictions alignment
)

# Load trained model
model_path = os.path.join(base_dir, "best_model.h5")
model = tf.keras.models.load_model(model_path)

# Evaluate
loss, accuracy = model.evaluate(test_generator)
print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")

# ---------- 🧠 Add this section below to analyze predictions ----------
# Predict labels
Y_pred = model.predict(test_generator)
y_pred = np.argmax(Y_pred, axis=1)

# Classification report
print("\nClassification Report:")
print(classification_report(test_generator.classes, y_pred, target_names=test_generator.class_indices.keys()))

# Confusion matrix
cm = confusion_matrix(test_generator.classes, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt="d",
xticklabels=test_generator.class_indices.keys(),
yticklabels=test_generator.class_indices.keys(),
cmap="Blues")
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.tight_layout()
plt.show()
Loading