-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
287 lines (229 loc) · 8.57 KB
/
evaluate.py
File metadata and controls
287 lines (229 loc) · 8.57 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
"""
Evaluation Script for Anomaly Detection Model.
Evaluates model performance on test set and calculates metrics.
"""
import argparse
from pathlib import Path
import numpy as np
import torch
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_auc_score,
confusion_matrix,
)
from loguru import logger
from src.data.data_loader import create_dataloaders
from src.models.autoencoder import VibrationAutoencoder
from src.utils.config_loader import load_config
def calculate_reconstruction_errors(
model: torch.nn.Module,
dataloader: torch.utils.data.DataLoader,
device: str = "cuda",
) -> tuple:
"""Calculate reconstruction errors for all samples."""
model.eval()
model.to(device)
all_errors = []
all_labels = []
with torch.no_grad():
for batch in dataloader:
if isinstance(batch, (list, tuple)) and len(batch) == 2:
data, labels = batch
all_labels.extend(labels.cpu().numpy())
else:
data = batch
data = data.to(device)
# Forward pass
reconstruction = model(data)
# Calculate reconstruction error (MSE per sample)
errors = torch.mean((data - reconstruction) ** 2, dim=1)
all_errors.extend(errors.cpu().numpy())
return np.array(all_errors), np.array(all_labels) if all_labels else None
def find_optimal_threshold(
errors: np.ndarray,
labels: np.ndarray,
method: str = "f1",
) -> float:
"""Find optimal threshold based on labels."""
thresholds = np.percentile(errors, np.arange(0, 100, 1))
best_threshold = 0
best_score = 0
for threshold in thresholds:
predictions = (errors > threshold).astype(int)
if method == "f1":
score = f1_score(labels, predictions)
elif method == "accuracy":
score = accuracy_score(labels, predictions)
else:
score = f1_score(labels, predictions)
if score > best_score:
best_score = score
best_threshold = threshold
return best_threshold
def calculate_threshold_statistical(
errors: np.ndarray,
sigma_multiplier: float = 3.0,
) -> float:
"""Calculate threshold using statistical method (μ + k*σ)."""
mean = np.mean(errors)
std = np.std(errors)
threshold = mean + sigma_multiplier * std
return threshold
def evaluate_model(
model: torch.nn.Module,
dataloader: torch.utils.data.DataLoader,
threshold: float,
device: str = "cuda",
) -> dict:
"""Evaluate model and calculate metrics."""
logger.info("Calculating reconstruction errors...")
errors, labels = calculate_reconstruction_errors(model, dataloader, device)
# Predictions
predictions = (errors > threshold).astype(int)
metrics = {
"num_samples": len(errors),
"threshold": threshold,
"mean_error": float(np.mean(errors)),
"std_error": float(np.std(errors)),
"min_error": float(np.min(errors)),
"max_error": float(np.max(errors)),
}
if labels is not None:
# Classification metrics
metrics.update({
"accuracy": accuracy_score(labels, predictions),
"precision": precision_score(labels, predictions, zero_division=0),
"recall": recall_score(labels, predictions, zero_division=0),
"f1_score": f1_score(labels, predictions, zero_division=0),
})
# Confusion matrix
cm = confusion_matrix(labels, predictions)
tn, fp, fn, tp = cm.ravel() if cm.size == 4 else (0, 0, 0, 0)
metrics.update({
"true_negatives": int(tn),
"false_positives": int(fp),
"false_negatives": int(fn),
"true_positives": int(tp),
"specificity": tn / (tn + fp) if (tn + fp) > 0 else 0,
"false_positive_rate": fp / (fp + tn) if (fp + tn) > 0 else 0,
"false_negative_rate": fn / (fn + tp) if (fn + tp) > 0 else 0,
})
# ROC AUC
try:
metrics["roc_auc"] = roc_auc_score(labels, errors)
except:
metrics["roc_auc"] = None
return metrics
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Evaluate anomaly detection model")
parser.add_argument(
"--checkpoint",
type=str,
required=True,
help="Path to model checkpoint",
)
parser.add_argument(
"--config",
type=str,
default="config/config.yaml",
help="Path to configuration file",
)
parser.add_argument(
"--threshold",
type=float,
default=None,
help="Anomaly threshold (if None, calculates from data)",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
choices=["cuda", "cpu"],
help="Device to use",
)
parser.add_argument(
"--output",
type=str,
default=None,
help="Output file for metrics (.json)",
)
return parser.parse_args()
def main():
"""Main evaluation function."""
args = parse_args()
# Load configuration
logger.info(f"Loading configuration from {args.config}")
config = load_config(args.config)
# Device setup
device = args.device
if device == "cuda" and not torch.cuda.is_available():
logger.warning("CUDA not available, using CPU")
device = "cpu"
# Load model
logger.info(f"Loading model from {args.checkpoint}")
checkpoint = torch.load(args.checkpoint, map_location=device)
if "model" in checkpoint:
model = checkpoint["model"]
else:
# Load architecture
model = VibrationAutoencoder()
model.load_state_dict(checkpoint["model_state_dict"])
model.eval()
# Load data
logger.info("Loading test data...")
_, _, test_loader, preprocessor = create_dataloaders(config)
# Calculate or use provided threshold
if args.threshold is None:
logger.info("Calculating optimal threshold...")
errors, labels = calculate_reconstruction_errors(model, test_loader, device)
if labels is not None:
threshold = find_optimal_threshold(errors, labels, method="f1")
logger.info(f"Optimal threshold (F1): {threshold:.6f}")
else:
threshold = calculate_threshold_statistical(errors, sigma_multiplier=3.0)
logger.info(f"Statistical threshold (μ + 3σ): {threshold:.6f}")
else:
threshold = args.threshold
logger.info(f"Using provided threshold: {threshold:.6f}")
# Evaluate
logger.info("Evaluating model...")
metrics = evaluate_model(model, test_loader, threshold, device)
# Print results
logger.info("\n" + "=" * 50)
logger.info("EVALUATION RESULTS")
logger.info("=" * 50)
logger.info(f"\nDataset Statistics:")
logger.info(f" Samples: {metrics['num_samples']}")
logger.info(f" Mean Error: {metrics['mean_error']:.6f}")
logger.info(f" Std Error: {metrics['std_error']:.6f}")
logger.info(f" Min Error: {metrics['min_error']:.6f}")
logger.info(f" Max Error: {metrics['max_error']:.6f}")
if "accuracy" in metrics:
logger.info(f"\nClassification Metrics:")
logger.info(f" Accuracy: {metrics['accuracy']:.4f}")
logger.info(f" Precision: {metrics['precision']:.4f}")
logger.info(f" Recall: {metrics['recall']:.4f}")
logger.info(f" F1-Score: {metrics['f1_score']:.4f}")
logger.info(f" Specificity: {metrics['specificity']:.4f}")
if metrics["roc_auc"] is not None:
logger.info(f" ROC-AUC: {metrics['roc_auc']:.4f}")
logger.info(f"\nConfusion Matrix:")
logger.info(f" True Negatives: {metrics['true_negatives']}")
logger.info(f" False Positives: {metrics['false_positives']}")
logger.info(f" False Negatives: {metrics['false_negatives']}")
logger.info(f" True Positives: {metrics['true_positives']}")
logger.info(f" FPR: {metrics['false_positive_rate']:.4f}")
logger.info(f" FNR: {metrics['false_negative_rate']:.4f}")
logger.info("=" * 50 + "\n")
# Save metrics
if args.output:
import json
with open(args.output, "w") as f:
json.dump(metrics, f, indent=2)
logger.info(f"Metrics saved to {args.output}")
if __name__ == "__main__":
main()