anomalib.utils.metrics

Custom anomaly evaluation metrics.

Submodules

Package Contents

Classes

AdaptiveThreshold

Optimal F1 Metric.

AnomalyScoreDistribution

Mean and standard deviation of the anomaly scores of normal training data.

AUROC

Area under the ROC curve.

MinMax

Track the min and max values of the observations in each batch.

OptimalF1

Optimal F1 Metric.

class anomalib.utils.metrics.AdaptiveThreshold(default_value: float = 0.5, **kwargs)[source]

Bases: torchmetrics.Metric

Optimal F1 Metric.

Compute the optimal F1 score at the adaptive threshold, based on the F1 metric of the true labels and the predicted anomaly scores.

update(self, preds: torch.Tensor, target: torch.Tensor) None

Update the precision-recall curve metric.

compute(self) torch.Tensor

Compute the threshold that yields the optimal F1 score.

Compute the F1 scores while varying the threshold. Store the optimal threshold as attribute and return the maximum value of the F1 score.

Returns

Value of the F1 score at the optimal threshold.

class anomalib.utils.metrics.AnomalyScoreDistribution(**kwargs)[source]

Bases: torchmetrics.Metric

Mean and standard deviation of the anomaly scores of normal training data.

update(self, anomaly_scores: Optional[torch.Tensor] = None, anomaly_maps: Optional[torch.Tensor] = None) None

Update the precision-recall curve metric.

compute(self) Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]

Compute stats.

class anomalib.utils.metrics.AUROC[source]

Bases: torchmetrics.ROC

Area under the ROC curve.

compute(self) torch.Tensor

First compute ROC curve, then compute area under the curve.

Returns

Value of the AUROC metric

class anomalib.utils.metrics.MinMax(**kwargs)[source]

Bases: torchmetrics.Metric

Track the min and max values of the observations in each batch.

update(self, predictions: torch.Tensor) None

Update the min and max values.

compute(self) Tuple[torch.Tensor, torch.Tensor]

Return min and max values.

class anomalib.utils.metrics.OptimalF1(num_classes: int, **kwargs)[source]

Bases: torchmetrics.Metric

Optimal F1 Metric.

Compute the optimal F1 score at the adaptive threshold, based on the F1 metric of the true labels and the predicted anomaly scores.

update(self, preds: torch.Tensor, target: torch.Tensor) None

Update the precision-recall curve metric.

compute(self) torch.Tensor

Compute the value of the optimal F1 score.

Compute the F1 scores while varying the threshold. Store the optimal threshold as attribute and return the maximum value of the F1 score.

Returns

Value of the F1 score at the optimal threshold.