CFM#

Lightning wrapper for CFM (Cross-modal Feature Mapping).

class anomalib.models.image.cfm.lightning_model.CFM(lr=0.0001, rgb_backbone='vit_base_patch8_224.dino', group_size=128, num_group=1024, pointmae_weights=None, pre_processor=True, post_processor=True, evaluator=True, visualizer=True)#

Bases: AnomalibModule

AnomalibModule wrapper for CFM model.

Parameters:
  • lr (float) – Learning rate.

  • rgb_backbone (str) – Name of the backbone DINO for RGB.

  • group_size (int) – Dimension of the groups for PointTransformer.

  • num_group (int) – Number of groups for PointTransformer.

  • pointmae_weights (str | Path | None) – Path to Point-MAE pretrained weights. If None, weights are automatically downloaded to the anomalib cache.

  • pre_processor (PreProcessor | bool) – Pre-processor used to transform input data before passing to model.

  • post_processor (PostProcessor | bool) – Post-processor used to process model predictions.

  • evaluator (Evaluator | bool) – Evaluator used to compute metrics.

  • visualizer (Visualizer | bool) – Visualizer used to create visualizations.

Note

Use a depth datamodule such as MVTec3D and set category to train on a single object class. See examples/configs/model/cfm.yaml.

Example

>>> from anomalib.models import CFM
>>> model = CFM(lr=1e-4, num_group=512)
configure_optimizers()#

Configuration of the optimizer (Adam) for trainable modules.

Return type:

Optimizer

static configure_pre_processor(image_size=None)#

Configure the pre-processor dynamically based on config/data.

Return type:

PreProcessor

forward(batch, *_args, **_kwargs)#

Forward pass used by predict/export code paths.

Return type:

InferenceBatch

Notes

AnomalibModule’s default forward assumes single-input models and calls self.model(image). CFM is multimodal, so we override forward to require a Batch or mapping with both ‘image’ and ‘point_cloud’/’depth_map’ keys.

Raises:
  • TypeError – If called with a raw image tensor (missing 3D modality).

  • TypeError – If the underlying model did not return an InferenceBatch (e.g. model is in training mode).

property learning_type: LearningType#

Returns the model’s learning type (One-Class).

predict_step(batch, batch_idx, dataloader_idx=0)#

Same as validation_step (multimodal; do not use the image-only base implementation).

Return type:

Union[Tensor, Mapping[str, Any], None]

test_step(batch, batch_idx, *args, **kwargs)#

Same as validation_step (multimodal; do not use the image-only base implementation).

Return type:

Union[Tensor, Mapping[str, Any], None]

property trainer_arguments: dict[str, Any]#

Returns specific arguments for the trainer.

training_step(batch, _batch_idx, *_args, **_kwargs)#

Executes a training step evaluating the loss between multimodal projections.

Return type:

Tensor

validation_step(batch, *args, **kwargs)#

Run validation / test / predict: forward plus attach scores and anomaly maps to the batch.

Return type:

Union[Tensor, Mapping[str, Any], None]

PyTorch model implementation for CFM.

class anomalib.models.image.cfm.torch_model.CFMModel(rgb_backbone='vit_base_patch8_224.dino', group_size=128, num_group=1024, pointmae_weights=None)#

Bases: Module

Crossmodal Feature Mapping (CFM) model.

Model learns from the correspondence between geometry(3D) and appearance (2D).

compute_losses(rgb_feat, xyz_feat, pred_rgb, pred_xyz)#

Evaluates the loss based on discrepancy between real and mapped features.

Return type:

dict[str, Tensor]

extract_features(rgb, xyz)#

Extract the real features from the sensors(2D e 3D).

Return type:

tuple[Tensor, Tensor]

forward(rgb, xyz)#

It manages the data flow based on the model’s status.

Return type:

dict[str, Tensor] | InferenceBatch

map_features(rgb_feat, xyz_feat)#

Crossmodal mapping.

Return type:

tuple[Tensor, Tensor]

mapper_parameters()#

It returns only the mapping network parameters for the optimiser.

Return type:

list[Parameter]