|
| 1 | +"""Timm backbone wrapper for feature extraction.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | + |
| 5 | +import torch.nn as nn |
| 6 | +from torch import Tensor |
| 7 | + |
| 8 | +try: |
| 9 | + import timm |
| 10 | +except ImportError: |
| 11 | + timm = None # type: ignore[assignment] |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class TimmBackboneConfig: |
| 16 | + """Configuration for TimmBackbone. |
| 17 | +
|
| 18 | + Attributes: |
| 19 | + name: Name of the timm model to use. |
| 20 | + pretrained: Whether to load pretrained weights. |
| 21 | + out_indices: Which feature stages to return (0=stem, 1-4=stages). |
| 22 | + """ |
| 23 | + |
| 24 | + name: str = "mobilenetv3_large_100" |
| 25 | + pretrained: bool = True |
| 26 | + out_indices: tuple[int, ...] = field(default_factory=lambda: (0, 1, 2, 3, 4)) |
| 27 | + |
| 28 | + |
| 29 | +class TimmBackbone(nn.Module): |
| 30 | + """Wrapper around timm models for multi-scale feature extraction. |
| 31 | +
|
| 32 | + This module creates a feature extractor from any timm model that supports |
| 33 | + `features_only=True`, returning feature maps at multiple scales. |
| 34 | +
|
| 35 | + Example: |
| 36 | + ``` py |
| 37 | + cfg = TimmBackboneConfig(name="mobilenetv3_small_100", pretrained=True) |
| 38 | + backbone = TimmBackbone(cfg) |
| 39 | + x = torch.randn(1, 3, 256, 256) |
| 40 | + features = backbone(x) # List of feature tensors |
| 41 | + print([f.shape for f in features]) |
| 42 | + ``` |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, cfg: TimmBackboneConfig) -> None: |
| 46 | + super().__init__() |
| 47 | + |
| 48 | + if timm is None: |
| 49 | + raise ImportError( |
| 50 | + "timm is required for TimmBackbone. " |
| 51 | + "Install it with: pip install 'jabs-vision[timm]' or pip install timm" |
| 52 | + ) |
| 53 | + |
| 54 | + self.cfg = cfg |
| 55 | + |
| 56 | + # Create feature extractor |
| 57 | + self.model = timm.create_model( |
| 58 | + cfg.name, |
| 59 | + pretrained=cfg.pretrained, |
| 60 | + features_only=True, |
| 61 | + out_indices=cfg.out_indices, |
| 62 | + ) |
| 63 | + |
| 64 | + # Get feature info for channels and strides |
| 65 | + self._channels = [info["num_chs"] for info in self.model.feature_info] |
| 66 | + self._strides = [info["reduction"] for info in self.model.feature_info] |
| 67 | + |
| 68 | + @property |
| 69 | + def channels(self) -> list[int]: |
| 70 | + """Number of channels at each feature level.""" |
| 71 | + return self._channels |
| 72 | + |
| 73 | + @property |
| 74 | + def strides(self) -> list[int]: |
| 75 | + """Spatial reduction (stride) at each feature level.""" |
| 76 | + return self._strides |
| 77 | + |
| 78 | + def forward(self, x: Tensor) -> list[Tensor]: |
| 79 | + """Extract multi-scale features. |
| 80 | +
|
| 81 | + Args: |
| 82 | + x: Input tensor of shape (B, C, H, W). |
| 83 | +
|
| 84 | + Returns: |
| 85 | + List of feature tensors, one per output index. |
| 86 | + """ |
| 87 | + return self.model(x) |
0 commit comments