legacy_models
sleap_nn.legacy_models
¶
Utilities for loading legacy SLEAP models.
This module provides functions to convert SLEAP models trained with the TensorFlow/Keras backend to PyTorch format compatible with sleap-nn.
Functions:
| Name | Description |
|---|---|
convert_keras_to_pytorch_conv2d |
Convert Keras Conv2D weights to PyTorch format. |
convert_keras_to_pytorch_conv2d_transpose |
Convert Keras Conv2DTranspose weights to PyTorch format. |
create_model_from_legacy_config |
Create a PyTorch model from a legacy training config. |
filter_legacy_weights_by_component |
Filter legacy weights based on component type. |
get_keras_first_layer_channels |
Extract the number of input channels from the first layer of a Keras model. |
load_keras_weights |
Load all weights from a Keras HDF5 model file. |
load_legacy_model |
Load a complete legacy SLEAP model including weights. |
load_legacy_model_weights |
Load legacy Keras weights into a PyTorch model. |
map_legacy_to_pytorch_layers |
Create mapping between legacy Keras layers and PyTorch model layers. |
parse_keras_layer_name |
Parse a Keras layer path to extract basic information. |
update_backbone_in_channels |
Update the backbone configuration's in_channels if it's different from the Keras model. |
convert_keras_to_pytorch_conv2d(keras_weight)
¶
Convert Keras Conv2D weights to PyTorch format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keras_weight
|
ndarray
|
Numpy array with shape (H, W, C_in, C_out) from Keras |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
PyTorch tensor with shape (C_out, C_in, H, W) |
Source code in sleap_nn/legacy_models.py
convert_keras_to_pytorch_conv2d_transpose(keras_weight)
¶
Convert Keras Conv2DTranspose weights to PyTorch format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keras_weight
|
ndarray
|
Numpy array with shape (H, W, C_out, C_in) from Keras |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
PyTorch tensor with shape (C_in, C_out, H, W) |
Note
Keras stores transposed conv weights differently than regular conv.
Source code in sleap_nn/legacy_models.py
create_model_from_legacy_config(config_path)
¶
Create a PyTorch model from a legacy training config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the legacy training_config.json file |
required |
Returns:
| Type | Description |
|---|---|
Model
|
Model instance configured to match the legacy architecture |
Source code in sleap_nn/legacy_models.py
filter_legacy_weights_by_component(legacy_weights, component)
¶
Filter legacy weights based on component type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
legacy_weights
|
Dict[str, ndarray]
|
Dictionary of legacy weights from load_keras_weights() |
required |
component
|
Optional[str]
|
Component type to filter for. One of: - "backbone": Keep only encoder/decoder weights (exclude heads) - "head": Keep only head layer weights - None: No filtering (keep all weights) |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, ndarray]
|
Filtered dictionary of legacy weights |
Source code in sleap_nn/legacy_models.py
get_keras_first_layer_channels(h5_path)
¶
Extract the number of input channels from the first layer of a Keras model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h5_path
|
str
|
Path to the .h5 model file |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
Number of input channels in the first layer, or None if not found |
Source code in sleap_nn/legacy_models.py
load_keras_weights(h5_path)
¶
Load all weights from a Keras HDF5 model file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h5_path
|
str
|
Path to the .h5 model file |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, ndarray]
|
Dictionary mapping layer paths to weight arrays |
Source code in sleap_nn/legacy_models.py
load_legacy_model(model_dir, load_weights=True)
¶
Load a complete legacy SLEAP model including weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_dir
|
str
|
Path to the legacy model directory containing training_config.json and best_model.h5 |
required |
load_weights
|
bool
|
Whether to load the weights. If False, only creates the model architecture. |
True
|
Returns:
| Type | Description |
|---|---|
Model
|
Model instance with loaded weights |
Source code in sleap_nn/legacy_models.py
load_legacy_model_weights(pytorch_model, h5_path, mapping=None, component=None)
¶
Load legacy Keras weights into a PyTorch model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pytorch_model
|
Module
|
PyTorch model to load weights into |
required |
h5_path
|
str
|
Path to the legacy .h5 model file |
required |
mapping
|
Optional[Dict[str, str]]
|
Optional manual mapping of layer names. If None, will attempt automatic mapping. |
None
|
component
|
Optional[str]
|
Optional component type for filtering weights. One of: - "backbone": Only load encoder/decoder weights (exclude heads) - "head": Only load head layer weights - None: Load all weights (default, for full model loading) |
None
|
Source code in sleap_nn/legacy_models.py
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 | |
map_legacy_to_pytorch_layers(legacy_weights, pytorch_model, component=None)
¶
Create mapping between legacy Keras layers and PyTorch model layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
legacy_weights
|
Dict[str, ndarray]
|
Dictionary of legacy weights from load_keras_weights() |
required |
pytorch_model
|
Module
|
PyTorch model instance to map to |
required |
component
|
Optional[str]
|
Optional component type for filtering weights before mapping. One of "backbone", "head", or None (no filtering). |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dictionary mapping legacy layer paths to PyTorch parameter names |
Source code in sleap_nn/legacy_models.py
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 | |
parse_keras_layer_name(layer_path)
¶
Parse a Keras layer path to extract basic information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_path
|
str
|
Full path like "model_weights/stack0_enc0_conv0/stack0_enc0_conv0/kernel:0" |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with parsed information: - layer_name: Base layer name (e.g., "stack0_enc0_conv0") - weight_type: "kernel" or "bias" |
Source code in sleap_nn/legacy_models.py
update_backbone_in_channels(backbone_config, keras_in_channels)
¶
Update the backbone configuration's in_channels if it's different from the Keras model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone_config
|
The backbone configuration object |
required | |
keras_in_channels
|
int
|
Number of input channels from the Keras model |
required |