Overview
Themetrics module provides functions for evaluating anomaly detection performance at both the image level (classification) and pixel level (segmentation).
Functions
compute_imagewise_retrieval_metrics
Parameters
np.array or list
required
Array of anomaly scores for each image with shape
[N]. Higher values indicate higher probability of being anomalous.np.array or list
required
Binary ground truth labels with shape
[N]. Values are 1 for anomalous images and 0 for normal images.Returns
float
Area Under the Receiver Operating Characteristic curve. Values range from 0 to 1, where 1 is perfect classification.
np.ndarray
False Positive Rate values at different thresholds.
np.ndarray
True Positive Rate values at different thresholds.
np.ndarray
Threshold values corresponding to each FPR/TPR pair.
Example
Real-world Usage
AUROC Interpretation:
- 0.95-1.0: Excellent detection performance
- 0.90-0.95: Good detection performance
- 0.80-0.90: Fair detection performance
- Below 0.80: Poor detection performance
compute_pixelwise_retrieval_metrics
Parameters
list of np.ndarray or np.ndarray
required
Predicted anomaly segmentation masks. Can be:
- List of arrays, each with shape
[H, W] - Single array with shape
[N, H, W]
list of np.ndarray or np.ndarray
required
Ground truth segmentation masks with the same shape as predictions. Binary values where 1 indicates anomalous pixels and 0 indicates normal pixels.
Returns
float
Pixel-level Area Under the ROC curve.
np.ndarray
False Positive Rate values at different thresholds.
np.ndarray
True Positive Rate values at different thresholds.
float
Threshold that maximizes the F1 score, balancing precision and recall.
float
False Positive Rate at the optimal threshold.
float
False Negative Rate at the optimal threshold.
Example
Real-world Usage
Optimal Threshold: The returned optimal threshold maximizes the F1 score, which balances precision and recall. Use this threshold to convert continuous anomaly scores into binary segmentation masks.
Complete Evaluation Example
Metric Definitions
AUROC (Area Under ROC Curve)
The area under the Receiver Operating Characteristic curve, which plots True Positive Rate vs False Positive Rate at various thresholds.- Range: 0 to 1
- Perfect score: 1.0 (all anomalies ranked higher than normal samples)
- Random classifier: 0.5
True Positive Rate (TPR)
Also called Recall or Sensitivity:False Positive Rate (FPR)
False Negative Rate (FNR)
F1 Score
Harmonic mean of precision and recall:Dependencies
The metrics module uses:- numpy: For array operations
- sklearn.metrics: For ROC and precision-recall curve computation
