Post-processing#
Base class for post-processing.
Post-processor for one-class anomaly detection.
Base Post-processor#
Base class for post-processing anomaly detection results.
This module provides the abstract base class PostProcessor
that defines
the interface for post-processing anomaly detection outputs.
- The post-processors handle:
Normalizing anomaly scores
Thresholding and anomaly classification
Mask generation and refinement
Result aggregation and formatting
Example
>>> from anomalib.post_processing import PostProcessor
>>> class MyPostProcessor(PostProcessor):
... def forward(self, batch):
... # Post-process the batch
... return batch
The post-processors are implemented as both torch.nn.Module
and
lightning.pytorch.Callback
to support both inference and training
workflows.
- class anomalib.post_processing.base.PostProcessor(*args, **kwargs)#
-
Base class for post-processing anomaly detection results.
The post-processor is implemented as both a
torch.nn.Module
andlightning.pytorch.Callback
to support inference and training workflows. It handles tasks like score normalization, thresholding, and mask refinement.The class must be inherited and the
forward()
method must be implemented to define the post-processing logic.Example
>>> from anomalib.post_processing import PostProcessor >>> class MyPostProcessor(PostProcessor): ... def forward(self, batch): ... # Normalize scores between 0 and 1 ... batch.anomaly_scores = normalize(batch.anomaly_scores) ... return batch
- abstract forward(batch)#
Post-process a batch of model predictions.
- Parameters:
batch (
anomalib.data.InferenceBatch
) – Batch containing model predictions and metadata.- Returns:
- Post-processed batch with
normalized scores, thresholded predictions, and/or refined masks.
- Return type:
anomalib.data.InferenceBatch
- Raises:
NotImplementedError – This is an abstract method that must be implemented by subclasses.
One-class Post-processor#
Post-processing module for one-class anomaly detection results.
This module provides post-processing functionality for one-class anomaly detection
outputs through the OneClassPostProcessor
class.
- The post-processor handles:
Normalizing image and pixel-level anomaly scores
Computing adaptive thresholds for anomaly classification
Applying sensitivity adjustments to thresholds
Formatting results for downstream use
Example
>>> from anomalib.post_processing import OneClassPostProcessor
>>> post_processor = OneClassPostProcessor(image_sensitivity=0.5)
>>> predictions = post_processor(anomaly_maps=anomaly_maps)
- class anomalib.post_processing.one_class.OneClassPostProcessor(image_sensitivity=None, pixel_sensitivity=None, **kwargs)#
Bases:
PostProcessor
Post-processor for one-class anomaly detection.
- This class handles post-processing of anomaly detection results by:
Normalizing image and pixel-level anomaly scores
Computing adaptive thresholds for anomaly classification
Applying sensitivity adjustments to thresholds
Formatting results for downstream use
- Parameters:
image_sensitivity (float | None, optional) – Sensitivity value for image-level predictions. Higher values make the model more sensitive to anomalies. Defaults to None.
pixel_sensitivity (float | None, optional) – Sensitivity value for pixel-level predictions. Higher values make the model more sensitive to anomalies. Defaults to None.
**kwargs – Additional keyword arguments passed to parent class.
Example
>>> from anomalib.post_processing import OneClassPostProcessor >>> post_processor = OneClassPostProcessor(image_sensitivity=0.5) >>> predictions = post_processor(anomaly_maps=anomaly_maps)
- forward(predictions)#
Post-process model predictions.
- Parameters:
predictions (InferenceBatch) – Batch containing model predictions.
- Returns:
- Post-processed batch with normalized scores and
thresholded predictions.
- Return type:
- Raises:
ValueError – If neither pred_score nor anomaly_map is provided.
- property image_max: float#
Get the maximum value for image-level normalization.
- Returns:
Maximum image-level value.
- Return type:
- property image_min: float#
Get the minimum value for image-level normalization.
- Returns:
Minimum image-level value.
- Return type:
- normalize_batch(batch)#
Normalize predicted scores and anomaly maps.
- property normalized_image_threshold: float#
Get the normalized image-level threshold.
- Returns:
Normalized image-level threshold value, adjusted by sensitivity.
- Return type:
- property normalized_pixel_threshold: float#
Get the normalized pixel-level threshold.
- Returns:
Normalized pixel-level threshold value, adjusted by sensitivity.
- Return type:
- on_predict_batch_end(trainer, pl_module, outputs, *args, **kwargs)#
Normalize predicted scores and anomaly maps.
- on_test_batch_end(trainer, pl_module, outputs, *args, **kwargs)#
Apply post-processing steps to current batch of predictions.
- on_validation_batch_end(trainer, pl_module, outputs, *args, **kwargs)#
Update normalization and thresholding metrics using batch output.
- Parameters:
trainer (Trainer) – PyTorch Lightning trainer instance.
pl_module (LightningModule) – PyTorch Lightning module instance.
outputs (Batch) – Batch containing model predictions and ground truth.
*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Return type:
- on_validation_epoch_end(trainer, pl_module)#
Compute final threshold and normalization values.
- Parameters:
trainer (Trainer) – PyTorch Lightning trainer instance.
pl_module (LightningModule) – PyTorch Lightning module instance.
- Return type:
- property pixel_max: float#
Get the maximum value for pixel-level normalization.
- Returns:
Maximum pixel-level value.
- Return type:
- property pixel_min: float#
Get the minimum value for pixel-level normalization.
- Returns:
Minimum pixel-level value.
- Return type:
- post_process_batch(batch)#
Post-process a batch of predictions.
Applies normalization and thresholding to the batch predictions.
- property raw_image_threshold: float#
Get the raw image-level threshold.
- Returns:
Raw image-level threshold value.
- Return type: