Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
| from tensorflow.keras.applications.efficientnet import preprocess_input | |
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import os | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| print("β³ Loading AI Model...") | |
| # π Ensure this is your correct model name! | |
| model = tf.keras.models.load_model(os.path.join(BASE_DIR, '..', '..', 'mission_model.h5')) | |
| print("π Loading Test Dataset...") | |
| # π Pointing to the new TEST split folder | |
| test_dir = os.path.join(BASE_DIR, '..', '..', '..', 'dataset', 'mission_dataset_split', 'test') | |
| test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) | |
| test_generator = test_datagen.flow_from_directory( | |
| test_dir, | |
| target_size=(224, 224), | |
| batch_size=32, | |
| class_mode='categorical', | |
| shuffle=False | |
| ) | |
| print("π€ Running Predictions (This may take a minute)...") | |
| Y_pred = model.predict(test_generator) | |
| y_pred_classes = np.argmax(Y_pred, axis=1) # π FIXED: Grabs the top prediction per image | |
| y_true = test_generator.classes | |
| print("\n" + "="*50) | |
| print("π CAPSTONE AI PERFORMANCE METRICS π") | |
| print("="*50) | |
| # π FIXED: Added average='weighted' to handle all 10 classes correctly | |
| accuracy = accuracy_score(y_true, y_pred_classes) | |
| precision = precision_score(y_true, y_pred_classes, average='weighted', zero_division=0) | |
| recall = recall_score(y_true, y_pred_classes, average='weighted', zero_division=0) | |
| f1 = f1_score(y_true, y_pred_classes, average='weighted', zero_division=0) | |
| print(f"β Accuracy: {accuracy * 100:.2f}%") | |
| print(f"π― Precision: {precision * 100:.2f}%") | |
| print(f"π Recall: {recall * 100:.2f}%") | |
| print(f"βοΈ F1-Score: {f1 * 100:.2f}%") | |
| print("="*50) | |
| # Make the confusion matrix chart larger to fit 10 classes | |
| cm = confusion_matrix(y_true, y_pred_classes) | |
| plt.figure(figsize=(10, 8)) | |
| sns.heatmap(cm, annot=True, fmt='d', cmap='Blues') | |
| plt.title('AI Confusion Matrix (10 Classes)') | |
| plt.ylabel('Actual Image Class') | |
| plt.xlabel('AI Prediction') | |
| plt.savefig(os.path.join(BASE_DIR, '..', '..', 'outputs', 'confusion_matrix.png')) | |
| print("\nπ Saved 'confusion_matrix.png' to your outputs folder. Put this in your presentation!") |