Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
90 changes: 90 additions & 0 deletions src/UAV_navigation_system/cnn_train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# cnn_train.py

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

# Paths
base_dir = os.path.abspath("../data") # ../data folder contains 'train' and 'test'
train_dir = os.path.join(base_dir, "train")
test_dir = os.path.join(base_dir, "test")

# Parameters
img_size = (128, 128)
batch_size = 32
epochs = 70

# Image Preprocessing with stronger augmentation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)

test_datagen = ImageDataGenerator(rescale=1./255)

train_gen = train_datagen.flow_from_directory(
train_dir,
target_size=img_size,
batch_size=batch_size,
class_mode="categorical"
)

test_gen = test_datagen.flow_from_directory(
test_dir,
target_size=img_size,
batch_size=batch_size,
class_mode="categorical"
)

# CNN Model - More Layers
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import GlobalAveragePooling2D

# Load pre-trained base model
base_model = MobileNetV2(input_shape=(128, 128, 3), include_top=False, weights='imagenet')
base_model.trainable = False # Freeze the base model

# Transfer learning model
model = tf.keras.Sequential([
base_model,
GlobalAveragePooling2D(),
Dense(128, activation="relu"),
Dropout(0.5),
Dense(train_gen.num_classes, activation="softmax")
])


# Compile Model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.summary()

# Callbacks for early stopping and saving the best model
early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

checkpoint = ModelCheckpoint(
filepath=os.path.join(base_dir, "best_model.h5"),
monitor='val_accuracy',
save_best_only=True,
verbose=1
)

# Train Model
history = model.fit(
train_gen,
epochs=epochs,
validation_data=test_gen,
callbacks=[early_stop, checkpoint]
)

# Save Final Model
model.save(os.path.join(base_dir, "cnn_model.h5"))
print("Model training complete and saved.")
Loading