:py:mod:`anomalib.models.components.feature_extractors` ======================================================= .. py:module:: anomalib.models.components.feature_extractors .. autoapi-nested-parse:: Feature extractors. Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 feature_extractor/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: anomalib.models.components.feature_extractors.FeatureExtractor .. py:class:: FeatureExtractor(backbone: torch.nn.Module, layers: Iterable[str]) Bases: :py:obj:`torch.nn.Module` Extract features from a CNN. :param backbone: The backbone to which the feature extraction hooks are attached. :type backbone: nn.Module :param layers: List of layer names of the backbone to which the hooks are attached. :type layers: Iterable[str] .. rubric:: Example >>> import torch >>> import torchvision >>> from anomalib.core.model.feature_extractor import FeatureExtractor >>> model = FeatureExtractor(model=torchvision.models.resnet18(), layers=['layer1', 'layer2', 'layer3']) >>> input = torch.rand((32, 3, 256, 256)) >>> features = model(input) >>> [layer for layer in features.keys()] ['layer1', 'layer2', 'layer3'] >>> [feature.shape for feature in features.values()] [torch.Size([32, 64, 64, 64]), torch.Size([32, 128, 32, 32]), torch.Size([32, 256, 16, 16])] .. py:method:: get_features(layer_id: str) -> Callable Get layer features. :param layer_id: Layer ID :type layer_id: str :returns: Layer features .. py:method:: forward(input_tensor: torch.Tensor) -> Dict[str, torch.Tensor] Forward-pass input tensor into the CNN. :param input_tensor: Input tensor :type input_tensor: Tensor :returns: Feature map extracted from the CNN