def forward(self, batch: torch.Tensor) -> torch.Tensor | InferenceBatch:
        """Extract features during training or compute anomaly scores during inference.

        Args:
            batch (torch.Tensor): Batch of input images with shape
                ``(N, C, H, W)``.

        Returns:
            torch.Tensor | InferenceBatch: During training, returns extracted
                features as a tensor. During inference, returns an
                ``InferenceBatch`` containing anomaly scores.

        Example:
            >>> batch = torch.randn(32, 3, 224, 224)
            >>> # Training mode
            >>> model.train()
            >>> features = model(batch)
            >>> # Inference mode
            >>> model.eval()
            >>> predictions = model(batch)
            >>> predictions.pred_score.shape
            torch.Size([32])
        """
        # 1. apply feature extraction
        features = self.get_features(batch)
        if self.training:
            return features

        # 2. apply density estimation
        scores = self.classifier(features)
        return InferenceBatch(pred_score=scores)
Extract features during training or compute anomaly scores during inference.

        Args:
            batch (torch.Tensor): Batch of input images with shape
                ``(N, C, H, W)``.

        Returns:
            torch.Tensor | InferenceBatch: During training, returns extracted
                features as a tensor. During inference, returns an
                ``InferenceBatch`` containing anomaly scores.

        Example:
            >>> batch = torch.randn(32, 3, 224, 224)
            >>> # Training mode
            >>> model.train()
            >>> features = model(batch)
            >>> # Inference mode
            >>> model.eval()
            >>> predictions = model(batch)
            >>> predictions.pred_score.shape
            torch.Size([32])
        
src/anomalib/models/image/dfkde/torch_model.py