:py:mod:`anomalib.models.dfkde.torch_model` =========================================== .. py:module:: anomalib.models.dfkde.torch_model .. autoapi-nested-parse:: Normality model of DFKDE. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.dfkde.torch_model.DfkdeModel Attributes ~~~~~~~~~~ .. autoapisummary:: anomalib.models.dfkde.torch_model.logger .. py:data:: logger .. py:class:: DfkdeModel(backbone: str, pre_trained: bool = True, n_comps: int = 16, pre_processing: str = 'scale', filter_count: int = 40000, threshold_steepness: float = 0.05, threshold_offset: float = 12.0) Bases: :py:obj:`torch.nn.Module` Normality Model for the DFKDE algorithm. :param backbone: Pre-trained model backbone. :type backbone: str :param pre_trained: Boolean to check whether to use a pre_trained backbone. :type pre_trained: bool, optional :param n_comps: Number of PCA components. Defaults to 16. :type n_comps: int, optional :param pre_processing: Preprocess features before passing to KDE. Options are between `norm` and `scale`. Defaults to "scale". :type pre_processing: str, optional :param filter_count: Number of training points to fit the KDE model. Defaults to 40000. :type filter_count: int, optional :param threshold_steepness: Controls how quickly the value saturates around zero. Defaults to 0.05. :type threshold_steepness: float, optional :param threshold_offset: Offset of the density function from 0. Defaults to 12.0. :type threshold_offset: float, optional .. py:method:: get_features(batch: torch.Tensor) -> torch.Tensor Extract features from the pretrained network. :param batch: Image batch. :type batch: Tensor :returns: Tensor containing extracted features. :rtype: Tensor .. py:method:: pre_process(feature_stack: torch.Tensor, max_length: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor] Pre-process the CNN features. :param feature_stack: Features extracted from CNN :type feature_stack: Tensor :param max_length: Used to unit normalize the feature_stack vector. If ``max_len`` is not provided, the length is calculated from the ``feature_stack``. Defaults to None. :type max_length: Optional[Tensor] :returns: Stacked features and length :rtype: (Tuple) .. py:method:: fit(embeddings: List[torch.Tensor]) -> bool Fit a kde model to embeddings. :param embeddings: Input embeddings to fit the model. :type embeddings: Tensor :returns: Boolean confirming whether the training is successful. .. py:method:: compute_kde_scores(features: torch.Tensor, as_log_likelihood: Optional[bool] = False) -> torch.Tensor Compute the KDE scores. The scores calculated from the KDE model are converted to densities. If `as_log_likelihood` is set to true then the log of the scores are calculated. :param features: Features to which the PCA model is fit. :type features: Tensor :param as_log_likelihood: If true, gets log likelihood scores. Defaults to False. :type as_log_likelihood: Optional[bool], optional :returns: Score :rtype: (Tensor) .. py:method:: compute_probabilities(scores: torch.Tensor) -> torch.Tensor Converts density scores to anomaly probabilities (see https://www.desmos.com/calculator/ifju7eesg7). :param scores: density of an image. :type scores: Tensor :returns: probability that image with {density} is anomalous .. py:method:: predict(features: torch.Tensor) -> torch.Tensor Predicts the probability that the features belong to the anomalous class. :param features: Feature from which the output probabilities are detected. :type features: Tensor :returns: Detection probabilities .. py:method:: forward(batch: torch.Tensor) -> torch.Tensor Prediction by normality model. :param batch: Input images. :type batch: Tensor :returns: Predictions :rtype: Tensor