Skip to content

simet.feature_extractor.inception

simet.feature_extractor.inception

InceptionFeatureExtractor

InceptionFeatureExtractor(
    cache_dir=Path("cache/features"), force_cache_recompute=False
)

Bases: FeatureExtractor

Inception-v3 feature extractor with cached, batchwise inference.

Loads a torchvision Inception v3 backbone pretrained on ImageNet (Inception_V3_Weights.IMAGENET1K_V1), replaces the final fully-connected layer with torch.nn.Identity(), and returns the penultimate features (dimension 2048) for each input image. Supports CUDA with autocast (mixed precision) when available.

Inputs are expected to be already transformed according to the Inception-v3 weights transforms (resize/crop to 299×299 and ImageNet normalization). In this project, use :class:simet.transforms.InceptionTransform to ensure compatibility.

Parameters:

Name Type Description Default
cache_dir Path | str

See :class:FeatureExtractor. Directory where feature arrays are cached.

Path('cache/features')
force_cache_recompute bool

See :class:FeatureExtractor. If True, bypass and refresh cache.

False

Attributes:

Name Type Description
device device

Device used for inference ("cuda" if available, else "cpu").

inception Module

The initialized Inception-v3 model in eval mode, with fc = Identity().

Notes
  • Output feature matrix shape is (N, 2048) for a loader that yields N images.
  • Mixed precision is used automatically on CUDA if AMP is available.
  • Progress is displayed via tqdm during feature extraction.
  • Caching behavior (keying and storage) is handled by :class:FeatureCacheService.
Example

extractor = InceptionFeatureExtractor() feats = extractor.extract_features(loader) # may hit cache feats.shape # doctest: +SKIP (N, 2048)

Source code in simet/feature_extractor/feature_extractor.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def __init__(
    self,
    cache_dir: Path = Path("cache/features"),
    force_cache_recompute: bool = False,
) -> None:
    """Initialize the cache service and the underlying model.

    Calls `_init_model()` so subclasses can set up weights/devices.

    Args:
        cache_dir: Directory where feature arrays will be cached.
        force_cache_recompute: If True, forces recomputation even when a
            cache entry for the given (loader, suffix) exists.
    """
    self.cache_service = FeatureCacheService(cache_dir)
    self.force_cache_recompute = force_cache_recompute
    self._init_model()

cache_key_suffix property

cache_key_suffix

Suffix used to disambiguate cache entries for this extractor.

Returns:

Name Type Description
str str

The cache key suffix ("inception_v3").