spatial_backbone
spatial_backbone
¶
Shared spatial image encoder producing (B, C, H, W) feature maps via timm.
SpatialBackboneEncoder
¶
SpatialBackboneEncoder(input_keys, backbone=value, pooling_method=value, batch_norm_handling=value, intermediate_layer_index=None, pretrained=False, frozen=False, model_dtype=None, lora_config=None)
Bases: ImageEncoderMixin, Encoder
Shared implementation for spatial image encoders backed by timm.
Supports any timm backbone compatible with features_only=True,
regardless of whether the architecture is convolutional (ResNet,
EfficientNet, ConvNeXt) or attention-based (Swin, TinyViT).
Handles both NCHW and NHWC output layouts transparently. Subclasses
pair this with a camera-modality mixin and set _input_channels
and _camera_modality.
Initialize the spatial encoder with a timm backbone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_keys
|
str | list[str]
|
Camera observation keys. |
required |
backbone
|
str
|
timm model name from SpatialBackboneType. |
value
|
pooling_method
|
str
|
Feature pooling strategy. |
value
|
batch_norm_handling
|
str
|
How to handle batch normalization layers. |
value
|
intermediate_layer_index
|
int | None
|
Optional timm intermediate layer index
to pool. Negative values index from the end; |
None
|
pretrained
|
bool
|
Whether to load pretrained weights. |
False
|
frozen
|
bool
|
Whether to freeze all parameters. |
False
|
model_dtype
|
str | None
|
Precision string from experiment config (e.g. |
None
|
lora_config
|
LoRAAdaptation | None
|
Optional PEFT LoRA adapter configuration. |
None
|
Source code in src/versatil/models/encoding/encoders/spatial_backbone.py
encode
¶
Encode images into features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
dict[str, Tensor]
|
Dict mapping camera keys to image tensors (B*T, C, H, W);
|
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dict with image features. Single camera: key is the modality name. |
dict[str, Tensor]
|
Multiple cameras: keys are |
Source code in src/versatil/models/encoding/encoders/spatial_backbone.py
set_image_size
¶
Compute feature map dimensions and create pooling head.
For backbones with strict input size requirements (e.g. Swin), this rebuilds the backbone with the target dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_height
|
int
|
Target image height. |
required |
image_width
|
int
|
Target image width. |
required |
Source code in src/versatil/models/encoding/encoders/spatial_backbone.py
validate_input_metadata
¶
Validate that input metadata is camera metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Observation key being validated. |
required |
metadata
|
BaseMetadata
|
Metadata from the observation space. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Error message if incompatible, None if valid. |
Source code in src/versatil/models/encoding/encoders/spatial_backbone.py
get_explainability_targets
¶
Return spatial feature-map targets for visual explanations.
The target layout follows the backbone output layout detected in
set_image_size(). CNN backbones usually emit NCHW; Swin/TinyViT
style feature backbones may emit NHWC.
Returns:
| Type | Description |
|---|---|
list[VisionExplanationTarget]
|
One spatial feature-map target for visual attribution methods. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no supported backbone target layer can be selected. |
Source code in src/versatil/models/encoding/encoders/spatial_backbone.py
get_output_specification
¶
Get structured output specification with feature names and dimensions.
Returns:
| Type | Description |
|---|---|
list[FeatureMetadata]
|
List of FeatureMetadata with per-camera feature names and pooled dimensions. |