Base Model¶
Base classes for all anomaly components.
- class anomalib.models.components.base.AnomalyModule[source]¶
Bases:
LightningModule
,ABC
AnomalyModule to train, validate, predict and test images.
Acts as a base class for all the Anomaly Modules in the library.
- forward(batch: dict[str, str | Tensor], *args, **kwargs) Any [source]¶
Forward-pass input tensor to the module.
- Parameters:
batch (dict[str, str | Tensor]) – Input batch.
- Returns:
Output tensor from the model.
- Return type:
Tensor
- load_state_dict(state_dict: OrderedDict[str, Tensor], strict: bool = True)[source]¶
Load state dict from checkpoint.
Ensures that normalization and thresholding attributes is properly setup before model is loaded.
- predict_step(batch: Any, batch_idx: int, dataloader_idx: int = 0) Any [source]¶
Step function called during
predict()
.By default, it calls
forward()
. Override to add any processing logic.- Parameters:
batch (Any) – Current batch
batch_idx (int) – Index of current batch
dataloader_idx (int) – Index of the current dataloader
- Returns:
Predicted output
- test_epoch_end(outputs: List[Union[Tensor, Dict[str, Any]]]) None [source]¶
Compute and save anomaly scores of the test set.
- Parameters:
outputs – Batch of outputs from the validation step
- test_step(batch: dict[str, str | Tensor], batch_idx: int, *args, **kwargs) STEP_OUTPUT [source]¶
Calls validation_step for anomaly map/score calculation.
- Parameters:
batch (dict[str, str | Tensor]) – Input batch
batch_idx (int) – Batch index
- Returns:
Dictionary containing images, features, true labels and masks. These are required in validation_epoch_end for feature concatenation.
- test_step_end(test_step_outputs: Union[Tensor, Dict[str, Any]], *args, **kwargs) Union[Tensor, Dict[str, Any]] [source]¶
Called at the end of each test step.
- validation_epoch_end(outputs: List[Union[Tensor, Dict[str, Any]]]) None [source]¶
Compute threshold and performance metrics.
- Parameters:
outputs – Batch of outputs from the validation step
- class anomalib.models.components.base.DynamicBufferModule[source]¶
Bases:
ABC
,Module
Torch module that allows loading variables from the state dict even in the case of shape mismatch.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- get_tensor_attribute(attribute_name: str) Tensor [source]¶
Get attribute of the tensor given the name.
- Parameters:
attribute_name (str) – Name of the tensor
- Raises:
ValueError – attribute_name is not a torch Tensor
- Returns:
Tensor attribute
- Return type:
Tensor
- training: bool¶