Anomaly Score Distribution#

class anomalib.metrics.AnomalyScoreDistribution(**kwargs)#

Bases: Metric

Compute distribution statistics of anomaly scores.

This class tracks and computes the mean and standard deviation of anomaly scores. Statistics are computed for both image-level scores and pixel-level anomaly maps.

The metric maintains internal state to accumulate scores, anomaly maps, and labels across batches before computing final statistics.

Example

>>> dist = AnomalyScoreDistribution()
>>> # Update with batch of scores
>>> scores = torch.tensor([0.1, 0.2, 0.3])
>>> dist.update(anomaly_scores=scores)
>>> # Compute statistics
>>> img_mean, img_std, pix_mean, pix_std = dist.compute()
compute()#

Compute distribution statistics from accumulated scores and maps.

Returns:

  • image_mean: Mean of log-transformed image anomaly scores

  • image_std: Standard deviation of log-transformed image scores

  • pixel_mean: Mean of log-transformed pixel anomaly maps

  • pixel_std: Standard deviation of log-transformed pixel maps

Return type:

tuple[Tensor, Tensor, Tensor, Tensor]

plot(bins=30, good_color='skyblue', bad_color='salmon', xlabel='Score', ylabel='Relative Count', title='Score Histogram', legend_labels=('Good', 'Bad'))#

Generate a histogram of scores.

Parameters:
  • bins (int) – Number of histogram bins. Defaults to 30.

  • good_color (str) – Color for good samples. Defaults to “skyblue”.

  • bad_color (str) – Color for bad samples. Defaults to “salmon”.

  • xlabel (str) – Label for the x-axis. Defaults to “Score”.

  • ylabel (str) – Label for the y-axis. Defaults to “Relative Count”.

  • title (str) – Title of the plot. Defaults to “Score Histogram”.

  • legend_labels (tuple[str, str]) – Legend labels for good and bad samples. Defaults to (“Good”, “Bad”).

Returns:

Tuple containing both the figure and the figure

title to be used for logging

Return type:

tuple[Figure, str]

Raises:

ValueError – If no anomaly scores or labels are available.

update(*args, anomaly_scores=None, anomaly_maps=None, labels=None, **kwargs)#

Update the internal state with new scores and maps.

Parameters:
  • *args – Unused positional arguments.

  • anomaly_scores (Tensor | None) – Batch of image-level anomaly scores.

  • anomaly_maps (Tensor | None) – Batch of pixel-level anomaly maps.

  • labels (Tensor | None) – Batch of binary labels.

  • **kwargs – Unused keyword arguments.

Return type:

None