pretrained
sleap_nn.architectures.pretrained
¶
Pretrained image backbones from HuggingFace transformers as sleap-nn encoders.
This module wires external pretrained vision encoders (via
transformers.AutoBackbone / the model-specific *Backbone classes) into the
existing :class:sleap_nn.architectures.model.Model contract, so any head
(pose, centroid, segmentation, class-vectors) can sit on a frozen or fine-tuned
foundation/ImageNet backbone. It reuses the same backbone dict-contract as the
native UNet/ConvNeXt/SwinT wrappers — no new Model class and no head changes.
Two integration surfaces are supported (the wrapper auto-selects, or you can
force via mode):
-
Case A — hierarchical encoder + sleap decoder (
mode="decoder"). A hierarchical backbone (ConvNeXtV2, ResNet, Swinv2, DINOv3-ConvNeXt, ...) emits a multi-scalefeature_mapspyramid (strides 4/8/16/32). We feed those maps as skip connections into the existing :class:sleap_nn.architectures.encoder_decoder.Decoder(exactly whatConvNextWrapper/SwinTWrapperdo for torchvision), so spatial heads work unchanged. This is the primary path for pose/segmentation/centroid models. -
Case B — encoder-only pooled bottleneck (
mode="encoder"). An isotropic ViT (DINOv2 / DINOv2-with-registers) emits a single spatial bottleneck map (CLS/register tokens stripped, LayerNorm applied). We expose it asmiddle_outputwithmiddle_blocks[-1].filters == hidden_sizeso a pooled head (class-vectors / re-ID / embedding) consumes it. No decoder is built.
Key behaviors baked in here (rather than in the data pipeline, which is uint8 /
[0, 1] only): model-specific mean/std normalization (read from the matching
AutoImageProcessor, or passed explicitly), grayscale->3ch is handled upstream
in Model.forward, a float32 dtype force (transformers v5 defaults to
dtype="auto" which can silently load fp16/bf16), an optional pinned
revision, and a one-shot stride probe (HF backbones do not expose
stride metadata).
Classes:
| Name | Description |
|---|---|
PretrainedBackbone |
Wrap a HuggingFace pretrained backbone as a sleap-nn encoder. |
PretrainedBackbone
¶
Bases: Module
Wrap a HuggingFace pretrained backbone as a sleap-nn encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Backbone source. Only |
'hf'
|
model_name
|
str
|
HuggingFace model id (e.g. |
'facebook/convnextv2-nano-22k-224'
|
in_channels
|
int
|
Number of input channels the wrapped stem expects. Pretrained
vision stems are 3-channel; grayscale inputs are replicated to 3
channels upstream in |
3
|
output_stride
|
int
|
Stride of the finest decoder output (Case A). Ignored for Case B (encoder-only). |
2
|
max_stride
|
int
|
Deepest stride the encoder reaches. For hierarchical CNN/Swin
backbones this is |
32
|
weights
|
bool
|
If |
True
|
mode
|
str
|
One of |
'auto'
|
out_indices
|
Optional[List[int]]
|
Optional explicit stage indices to tap (Case A). If |
None
|
freeze
|
bool
|
If |
False
|
revision
|
Optional[str]
|
Optional HuggingFace revision (commit sha / tag) to pin for reproducibility. |
None
|
normalize
|
bool
|
If |
True
|
image_mean
|
Optional[List[float]]
|
Optional explicit per-channel mean (length 3). If |
None
|
image_std
|
Optional[List[float]]
|
Optional explicit per-channel std (length 3). |
None
|
filters_rate
|
float
|
Decoder filter growth factor (Case A). Default |
2.0
|
convs_per_block
|
int
|
Refinement convs per decoder block (Case A). Default |
2
|
kernel_size
|
int
|
Decoder conv kernel size (Case A). Default |
3
|
up_interpolate
|
bool
|
If |
True
|
Methods:
| Name | Description |
|---|---|
__init__ |
Build the wrapped backbone and (Case A) decoder. |
forward |
Forward pass emitting the sleap-nn backbone dict contract. |
freeze_encoder |
Freeze the pretrained encoder (feature extraction; decoder/head train). |
from_config |
Create a |
reload_pretrained_weights |
Re-apply the snapshotted pretrained weights to the encoder. |
Source code in sleap_nn/architectures/pretrained.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
__init__(source='hf', model_name='facebook/convnextv2-nano-22k-224', in_channels=3, output_stride=2, max_stride=32, weights=True, mode='auto', out_indices=None, freeze=False, revision=None, normalize=True, image_mean=None, image_std=None, filters_rate=2.0, convs_per_block=2, kernel_size=3, up_interpolate=True)
¶
Build the wrapped backbone and (Case A) decoder.
Source code in sleap_nn/architectures/pretrained.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
forward(x)
¶
Forward pass emitting the sleap-nn backbone dict contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Case A: |
Source code in sleap_nn/architectures/pretrained.py
freeze_encoder()
¶
Freeze the pretrained encoder (feature extraction; decoder/head train).
from_config(config)
classmethod
¶
Create a PretrainedBackbone from a config leaf.
Source code in sleap_nn/architectures/pretrained.py
reload_pretrained_weights()
¶
Re-apply the snapshotted pretrained weights to the encoder.
The LightningModule applies xavier init to the whole model (clobbering the encoder), then calls this to restore the pretrained weights — mirroring the convnext/swint ImageNet-load ordering.