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. |
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
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)
¶
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
|
Source code in sleap_nn/legacy_models.py
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 |
|
map_legacy_to_pytorch_layers(legacy_weights, pytorch_model)
¶
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 |
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Dictionary mapping legacy layer paths to PyTorch parameter names |
Source code in sleap_nn/legacy_models.py
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 |
|
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 |