-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_models.py
More file actions
388 lines (305 loc) · 15.4 KB
/
Copy pathevaluate_models.py
File metadata and controls
388 lines (305 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
"""
Model Evaluation Script for Metal Detector AI
Provides comprehensive evaluation metrics including:
- Cross-validation scores
- Confusion matrix
- Precision/Recall/F1 scores
- ROC curves
- Feature importance analysis
"""
import argparse
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import (
confusion_matrix, classification_report, roc_curve, auc,
precision_recall_curve, average_precision_score
)
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.preprocessing import label_binarize
import json
import pandas as pd
from datetime import datetime
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from src.ml.advanced_classifier import AdvancedMetalClassifier
from src.ml.classifier import MetalClassifier
console = Console()
class ModelEvaluator:
"""Comprehensive model evaluation tools."""
def __init__(self, model_path: Path = Path("models/advanced")):
self.model_path = model_path
self.results_dir = Path("evaluation_results")
self.results_dir.mkdir(exist_ok=True)
# Create subdirectories
self.plots_dir = self.results_dir / "plots"
self.plots_dir.mkdir(exist_ok=True)
self.timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
def evaluate_advanced_model(self, data_dir: Path):
"""
Evaluates ensemble model with metrics, plots, and feature analysis [ref: sklearn.metrics].
"""
console.print(Panel.fit("🔬 Advanced Model Evaluation", style="bold blue"))
# Load classifier
classifier = AdvancedMetalClassifier(model_path=self.model_path)
if not classifier._model_exists():
console.print("❌ No trained models found! Train models first.")
return
classifier.load_model()
console.print("✅ Models loaded successfully!")
# Prepare test data
console.print("\n📊 Preparing evaluation data...")
audio_data, labels, features, _ = classifier.prepare_training_data(data_dir)
if len(audio_data) == 0:
console.print("❌ No evaluation data found!")
return
# Encode labels
y_true = classifier.label_encoder.transform(labels)
classes = classifier.label_encoder.classes_
n_classes = len(classes)
console.print(f"Found {len(audio_data)} samples across {n_classes} classes")
# Get predictions
console.print("\n🔮 Getting predictions...")
y_pred = []
y_proba = []
with console.status("Classifying samples..."):
for i, audio in enumerate(audio_data):
# Get predictions from each model
pred_cnn, prob_cnn = classifier._predict_cnn(audio)
pred_trans, prob_trans = classifier._predict_transformer(audio)
pred_trad, prob_trad = classifier._predict_traditional(audio)
# Ensemble prediction (same as in classify_audio_file)
ensemble_prob = (prob_cnn * 0.4 + prob_trans * 0.4 + prob_trad * 0.2)
ensemble_pred = np.argmax(ensemble_prob)
y_pred.append(ensemble_pred)
y_proba.append(ensemble_prob)
if (i + 1) % 50 == 0:
console.print(f"Processed {i + 1}/{len(audio_data)} samples")
y_pred = np.array(y_pred)
y_proba = np.array(y_proba)
# Generate evaluation metrics
self._generate_classification_report(y_true, y_pred, classes, "advanced")
self._generate_confusion_matrix(y_true, y_pred, classes, "advanced")
self._generate_roc_curves(y_true, y_proba, classes, "advanced")
self._analyze_feature_importance(classifier, features, labels, "advanced")
# Save results summary
results = {
"timestamp": self.timestamp,
"model_type": "advanced_ensemble",
"n_samples": len(y_true),
"n_classes": n_classes,
"classes": classes.tolist(),
"accuracy": float((y_pred == y_true).mean()),
"results_dir": str(self.results_dir)
}
results_file = self.results_dir / f"evaluation_summary_{self.timestamp}.json"
with open(results_file, 'w') as f:
json.dump(results, f, indent=2)
console.print(f"\n✅ Evaluation complete! Results saved to: {self.results_dir}")
def evaluate_baseline_model(self, data_dir: Path):
"""
Evaluates traditional ML model including cross-validation [ref: sklearn.model_selection].
"""
console.print(Panel.fit("🔬 Baseline Model Evaluation", style="bold yellow"))
# Load classifier
classifier = MetalClassifier(model_path=Path("models/basic"))
if not classifier.model_exists():
console.print("❌ No trained baseline model found! Train model first.")
return
classifier.load_model()
console.print("✅ Baseline model loaded successfully!")
# Prepare test data
console.print("\n📊 Preparing evaluation data...")
X, y, label_names = classifier.prepare_training_data(data_dir)
if len(X) == 0:
console.print("❌ No evaluation data found!")
return
# Scale features
X_scaled = classifier.scaler.transform(X)
y_encoded = classifier.label_encoder.transform(y)
classes = classifier.label_encoder.classes_
console.print(f"Found {len(X)} samples across {len(classes)} classes")
# Get predictions
y_pred = classifier.model.predict(X_scaled)
y_proba = classifier.model.predict_proba(X_scaled)
# Generate evaluation metrics
self._generate_classification_report(y_encoded, y_pred, classes, "baseline")
self._generate_confusion_matrix(y_encoded, y_pred, classes, "baseline")
self._generate_roc_curves(y_encoded, y_proba, classes, "baseline")
self._analyze_baseline_feature_importance(classifier, X, y, "baseline")
# Cross-validation
self._perform_cross_validation(classifier.model, X_scaled, y_encoded, "baseline")
console.print(f"\n✅ Baseline evaluation complete! Results saved to: {self.results_dir}")
def _generate_classification_report(self, y_true, y_pred, classes, model_name):
"""
Computes and displays classification metrics, saves JSON report.
"""
console.print("\n📋 Classification Report:")
report = classification_report(y_true, y_pred, target_names=classes, output_dict=True)
# Display in console
table = Table(title="Per-Class Metrics")
table.add_column("Class", style="cyan")
table.add_column("Precision", style="green")
table.add_column("Recall", style="yellow")
table.add_column("F1-Score", style="magenta")
table.add_column("Support", style="blue")
for class_name in classes:
metrics = report[class_name]
table.add_row(
class_name,
f"{metrics['precision']:.3f}",
f"{metrics['recall']:.3f}",
f"{metrics['f1-score']:.3f}",
str(int(metrics['support']))
)
console.print(table)
# Overall metrics
console.print(f"\n🎯 Overall Accuracy: {report['accuracy']:.3f}")
console.print(f"📊 Macro Avg F1: {report['macro avg']['f1-score']:.3f}")
console.print(f"📊 Weighted Avg F1: {report['weighted avg']['f1-score']:.3f}")
# Save report
report_file = self.results_dir / f"{model_name}_classification_report_{self.timestamp}.json"
with open(report_file, 'w') as f:
json.dump(report, f, indent=2)
def _generate_confusion_matrix(self, y_true, y_pred, classes, model_name):
"""
Creates and saves confusion matrix heatmap using seaborn.
"""
console.print("\n🔲 Generating Confusion Matrix...")
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=classes, yticklabels=classes)
plt.title(f'Confusion Matrix - {model_name.title()} Model')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.tight_layout()
plot_file = self.plots_dir / f"{model_name}_confusion_matrix_{self.timestamp}.png"
plt.savefig(plot_file, dpi=300, bbox_inches='tight')
plt.close()
console.print(f"💾 Confusion matrix saved to: {plot_file}")
def _generate_roc_curves(self, y_true, y_proba, classes, model_name):
"""
Plots multi-class ROC curves and saves figure.
"""
console.print("\n📈 Generating ROC Curves...")
# Binarize labels for multi-class ROC
y_true_bin = label_binarize(y_true, classes=range(len(classes)))
# Compute ROC curve for each class
plt.figure(figsize=(10, 8))
for i, class_name in enumerate(classes):
fpr, tpr, _ = roc_curve(y_true_bin[:, i], y_proba[:, i])
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, label=f'{class_name} (AUC = {roc_auc:.3f})')
plt.plot([0, 1], [0, 1], 'k--', label='Random Classifier')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title(f'ROC Curves - {model_name.title()} Model')
plt.legend(loc="lower right")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plot_file = self.plots_dir / f"{model_name}_roc_curves_{self.timestamp}.png"
plt.savefig(plot_file, dpi=300, bbox_inches='tight')
plt.close()
console.print(f"💾 ROC curves saved to: {plot_file}")
def _analyze_feature_importance(self, classifier, features, labels, model_name):
"""
Extracts and plots top feature importances from RandomForest component.
"""
console.print("\n🔍 Analyzing Feature Importance...")
# For traditional ensemble component
if hasattr(classifier, 'traditional_ensemble'):
# Get feature importance from Random Forest in the ensemble
rf_model = classifier.traditional_ensemble.estimators_[0]
if hasattr(rf_model, 'feature_importances_'):
importances = rf_model.feature_importances_
# Create feature names (you might want to update this based on actual features)
n_features = len(importances)
feature_names = [f'Feature_{i}' for i in range(n_features)]
# Sort features by importance
indices = np.argsort(importances)[::-1][:20] # Top 20 features
plt.figure(figsize=(12, 6))
plt.bar(range(len(indices)), importances[indices])
plt.xticks(range(len(indices)), [feature_names[i] for i in indices], rotation=45, ha='right')
plt.title(f'Top 20 Feature Importances - {model_name.title()} Model (Traditional Component)')
plt.xlabel('Features')
plt.ylabel('Importance')
plt.tight_layout()
plot_file = self.plots_dir / f"{model_name}_feature_importance_{self.timestamp}.png"
plt.savefig(plot_file, dpi=300, bbox_inches='tight')
plt.close()
console.print(f"💾 Feature importance plot saved to: {plot_file}")
def _analyze_baseline_feature_importance(self, classifier, X, y, model_name):
"""
Plots top feature importances for baseline model.
"""
if hasattr(classifier.model, 'feature_importances_'):
importances = classifier.model.feature_importances_
feature_names = classifier.feature_names
# Sort features by importance
indices = np.argsort(importances)[::-1][:20] # Top 20 features
plt.figure(figsize=(12, 6))
plt.bar(range(len(indices)), importances[indices])
plt.xticks(range(len(indices)), [feature_names[i] for i in indices], rotation=45, ha='right')
plt.title(f'Top 20 Feature Importances - {model_name.title()} Model')
plt.xlabel('Features')
plt.ylabel('Importance')
plt.tight_layout()
plot_file = self.plots_dir / f"{model_name}_feature_importance_{self.timestamp}.png"
plt.savefig(plot_file, dpi=300, bbox_inches='tight')
plt.close()
console.print(f"💾 Feature importance plot saved to: {plot_file}")
def _perform_cross_validation(self, model, X, y, model_name):
"""
Runs stratified k-fold CV with multiple metrics, saves results.
"""
console.print("\n🔄 Performing Cross-Validation...")
cv_folds = min(5, len(np.unique(y)))
if cv_folds < 2:
console.print("⚠️ Not enough samples for cross-validation")
return
skf = StratifiedKFold(n_splits=cv_folds, shuffle=True, random_state=42)
# Different scoring metrics
scoring_metrics = ['accuracy', 'f1_macro', 'precision_macro', 'recall_macro']
cv_results = {}
for metric in scoring_metrics:
scores = cross_val_score(model, X, y, cv=skf, scoring=metric)
cv_results[metric] = {
'mean': float(scores.mean()),
'std': float(scores.std()),
'scores': scores.tolist()
}
console.print(f"{metric}: {scores.mean():.3f} (+/- {scores.std() * 2:.3f})")
# Save CV results
cv_file = self.results_dir / f"{model_name}_cv_results_{self.timestamp}.json"
with open(cv_file, 'w') as f:
json.dump(cv_results, f, indent=2)
def main():
"""Main evaluation function."""
parser = argparse.ArgumentParser(description="Evaluate Metal Detector AI models")
parser.add_argument("--data-dir", type=str, default="data",
help="Directory containing evaluation data")
parser.add_argument("--model", type=str, choices=['advanced', 'baseline', 'all'],
default='advanced', help="Which model to evaluate")
parser.add_argument("--model-dir", type=str, help="Custom model directory")
args = parser.parse_args()
data_dir = Path(args.data_dir)
if not data_dir.exists():
console.print(f"❌ Data directory not found: {data_dir}")
return
# Initialize evaluator
model_path = Path(args.model_dir) if args.model_dir else Path("models/advanced")
evaluator = ModelEvaluator(model_path)
# Run evaluation
if args.model in ['advanced', 'all']:
evaluator.evaluate_advanced_model(data_dir)
if args.model in ['baseline', 'all']:
evaluator.evaluate_baseline_model(data_dir)
console.print("\n🎉 Evaluation complete!")
if __name__ == "__main__":
main()