encoder_decoder
sleap_nn.architectures.encoder_decoder
¶
Generic encoder-decoder fully convolutional backbones.
This module contains building blocks for creating encoder-decoder architectures of general form.
The encoder branch of the network forms the initial multi-scale feature extraction via repeated blocks of convolutions and pooling steps.
The decoder branch is then responsible for upsampling the low resolution feature maps to achieve the target output stride.
This pattern is generalizable and describes most fully convolutional architectures. For
example:
- simple convolutions with pooling form the structure in LEAP CNN
<https://www.nature.com/articles/s41592-018-0234-5>
;
- adding skip connections forms U-Net <https://arxiv.org/pdf/1505.04597.pdf>
;
- using residual blocks with skip connections forms the base module in stacked
hourglass <https://arxiv.org/pdf/1603.06937.pdf>
;
- using dense blocks with skip connections forms FC-DenseNet
<https://arxiv.org/pdf/1611.09326.pdf>
.
This module implements blocks used in all of these variants on top of a generic base classes.
See the EncoderDecoder
base class for requirements for creating new architectures.
Classes:
Name | Description |
---|---|
Decoder |
Decoder module for the UNet architecture. |
Encoder |
Encoder module for a neural network architecture. |
SimpleConvBlock |
A simple convolutional block module. |
SimpleUpsamplingBlock |
A simple upsampling and refining block module. |
StemBlock |
Stem block module for initial feature extraction. |
Decoder
¶
Bases: Module
Decoder module for the UNet architecture.
This class defines the decoder part of the UNet, which consists of a stack of upsampling and refining blocks for feature reconstruction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_in_shape
|
int
|
Number of input channels for the decoder's input. |
required |
output_stride
|
int
|
Minimum of the strides of the output heads. The input confidence map |
required |
current_stride
|
int
|
Current stride value to adjust during upsampling. |
required |
filters
|
int
|
Number of filters for the initial block. Default is 64. |
64
|
up_blocks
|
int
|
Number of upsampling blocks. Default is 4. |
4
|
down_blocks
|
int
|
Number of downsampling blocks. Default is 3. |
3
|
stem_blocks
|
int
|
If >0, will create additional "down" blocks for initial downsampling. These will be configured identically to the down blocks below. |
0
|
filters_rate
|
int
|
Factor to adjust the number of filters per block. Default is 2. |
2
|
convs_per_block
|
int
|
Number of convolutional layers per block. Default is 2. |
2
|
kernel_size
|
int
|
Size of the convolutional kernels. Default is 3. |
3
|
block_contraction
|
bool
|
If True, reduces the number of filters at the end of middle and decoder blocks. This has the effect of introducing an additional bottleneck before each upsampling step. The original implementation does not do this, but the CARE implementation does. |
False
|
up_interpolate
|
bool
|
If True, use bilinear interpolation instead of transposed convolutions for upsampling. Interpolation is faster but transposed convolutions may be able to learn richer or more complex upsampling to recover details from higher scales. |
True
|
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
forward |
Forward pass through the Decoder module. |
Source code in sleap_nn/architectures/encoder_decoder.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
__init__(x_in_shape, output_stride, current_stride, filters=64, up_blocks=4, down_blocks=3, stem_blocks=0, filters_rate=2, convs_per_block=2, kernel_size=3, block_contraction=False, up_interpolate=True, prefix='dec')
¶
Initialize the class.
Source code in sleap_nn/architectures/encoder_decoder.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
forward(x, features)
¶
Forward pass through the Decoder module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor for the decoder. |
required |
features
|
List[Tensor]
|
List of feature tensors from different encoder levels. |
required |
Returns:
Name | Type | Description |
---|---|---|
outputs |
Tuple[List[Tensor], List]
|
List of output tensors after applying the decoder operations. current_strides: the current strides from the decoder blocks. |
Source code in sleap_nn/architectures/encoder_decoder.py
Encoder
¶
Bases: Module
Encoder module for a neural network architecture.
This class defines the encoder part of a neural network architecture, which consists of a stack of convolutional blocks for feature extraction.
The Encoder consists of a stack of SimpleConvBlocks designed for feature extraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels. Default is 3. |
3
|
filters
|
int
|
Number of filters for the initial block. Default is 64. |
64
|
down_blocks
|
int
|
Number of downsampling blocks. Default is 4. |
4
|
filters_rate
|
Union[float, int]
|
Factor to increase the number of filters per block. Default is 2. |
2
|
current_stride
|
int
|
Initial stride for pooling operations. Default is 2. |
2
|
convs_per_block
|
int
|
Number of convolutional layers per block. Default is 2. |
2
|
kernel_size
|
Union[int, Tuple[int, int]]
|
Size of the convolutional kernels. Default is 3. |
3
|
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
forward |
Forward pass through the Encoder module. |
Source code in sleap_nn/architectures/encoder_decoder.py
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 |
|
__init__(in_channels=3, filters=64, down_blocks=4, filters_rate=2, current_stride=2, convs_per_block=2, kernel_size=3, stem_blocks=0, prefix='enc')
¶
Initialize the class.
Source code in sleap_nn/architectures/encoder_decoder.py
forward(x)
¶
Forward pass through the Encoder module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Output tensor after applying the encoder operations. list: List of intermediate feature tensors from different levels of the encoder. |
Source code in sleap_nn/architectures/encoder_decoder.py
SimpleConvBlock
¶
Bases: Module
A simple convolutional block module.
This class defines a convolutional block that consists of convolutional layers, optional pooling layers, batch normalization, and activation functions.
The layers within the SimpleConvBlock are organized as follows:
- Optional max pooling (with same padding) layer (before convolutional layers).
- Convolutional layers with specified number of filters, kernel size, and activation.
- Optional batch normalization layer after each convolutional layer (if batch_norm is True).
- Activation function after each convolutional layer (ReLU, Sigmoid, Tanh, etc.).
- Optional max pooling (with same padding) layer (after convolutional layers).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels. |
required |
pool
|
bool
|
Whether to include pooling layers. Default is True. |
True
|
pooling_stride
|
int
|
Stride for pooling layers. Default is 2. |
2
|
pool_before_convs
|
bool
|
Whether to apply pooling before convolutional layers. Default is False. |
False
|
num_convs
|
int
|
Number of convolutional layers. Default is 2. |
2
|
filters
|
int
|
Number of filters for convolutional layers. Default is 32. |
32
|
kernel_size
|
int
|
Size of the convolutional kernels. Default is 3. |
3
|
use_bias
|
bool
|
Whether to use bias in convolutional layers. Default is True. |
True
|
batch_norm
|
bool
|
Whether to apply batch normalization. Default is False. |
False
|
activation
|
Text
|
Activation function name. Default is "relu". |
'relu'
|
Note
The 'same' padding is applied using custom MaxPool2dWithSamePadding layers.
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
forward |
Forward pass through the SimpleConvBlock module. |
Source code in sleap_nn/architectures/encoder_decoder.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 |
|
__init__(in_channels, pool=True, pooling_stride=2, pool_before_convs=False, num_convs=2, filters=32, kernel_size=3, use_bias=True, batch_norm=False, activation='relu', prefix='', name='')
¶
Initialize the class.
Source code in sleap_nn/architectures/encoder_decoder.py
forward(x)
¶
Forward pass through the SimpleConvBlock module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Output tensor after applying the convolutional block operations. |
Source code in sleap_nn/architectures/encoder_decoder.py
SimpleUpsamplingBlock
¶
Bases: Module
A simple upsampling and refining block module.
This class defines an upsampling and refining block that consists of upsampling layers, convolutional layers for refinement, batch normalization, and activation functions.
The block includes: 1. Upsampling layers with adjustable stride and interpolation method. 2. Refinement convolutional layers with customizable parameters. 3. BatchNormalization layers (if specified; can be before or after activation function). 4. Activation functions (default is ReLU) applied before or after BatchNormalization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_in_shape
|
int
|
Number of input channels for the feature map. |
required |
current_stride
|
int
|
Current stride value to adjust during upsampling. |
required |
upsampling_stride
|
int
|
Stride for upsampling. Default is 2. |
2
|
interp_method
|
Text
|
Interpolation method for upsampling. Default is "bilinear". |
'bilinear'
|
refine_convs
|
int
|
Number of convolutional layers for refinement. Default is 2. |
2
|
refine_convs_filters
|
int
|
Number of filters for refinement convolutional layers. Default is 64. |
64
|
refine_convs_kernel_size
|
int
|
Size of the refinement convolutional kernels. Default is 3. |
3
|
refine_convs_use_bias
|
bool
|
Whether to use bias in refinement convolutional layers. Default is True. |
True
|
refine_convs_batch_norm
|
bool
|
Whether to apply batch normalization. Default is True. |
False
|
refine_convs_batch_norm_before_activation
|
bool
|
Whether to apply batch normalization before activation. |
True
|
refine_convs_activation
|
Text
|
Activation function name. Default is "relu". |
'relu'
|
transpose_convs_filters
|
int
|
Number of filters for Transpose convolutional layers. Default is 64. |
64
|
transpose_convs_use_bias
|
bool
|
Whether to use bias in Transpose convolutional layers. Default is True. |
True
|
transpose_convs_batch_norm
|
bool
|
Whether to apply batch normalization for Transpose Conv layers. Default is True. |
True
|
transpose_convs_batch_norm_before_activation
|
bool
|
Whether to apply batch normalization before activation. |
True
|
transpose_convs_activation
|
Text
|
Activation function name for Transpose Conv layers. Default is "relu". |
'relu'
|
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
forward |
Forward pass through the SimpleUpsamplingBlock module. |
Source code in sleap_nn/architectures/encoder_decoder.py
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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
__init__(x_in_shape, current_stride, upsampling_stride=2, up_interpolate=True, interp_method='bilinear', refine_convs=2, refine_convs_filters=64, refine_convs_kernel_size=3, refine_convs_use_bias=True, refine_convs_batch_norm=False, refine_convs_batch_norm_before_activation=True, refine_convs_activation='relu', transpose_convs_filters=64, transpose_convs_kernel_size=3, transpose_convs_use_bias=True, transpose_convs_batch_norm=True, transpose_convs_batch_norm_before_activation=True, transpose_convs_activation='relu', feat_concat=True, prefix='')
¶
Initialize the class.
Source code in sleap_nn/architectures/encoder_decoder.py
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 503 504 505 506 507 508 509 510 511 |
|
forward(x, feature)
¶
Forward pass through the SimpleUpsamplingBlock module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
feature
|
Tensor
|
Feature tensor to be concatenated with the upsampled tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Output tensor after applying the upsampling and refining operations. |
Source code in sleap_nn/architectures/encoder_decoder.py
StemBlock
¶
Bases: Module
Stem block module for initial feature extraction.
This class defines a stem block that consists of a stack of convolutional blocks for initial feature extraction before the main encoder. The stem blocks are typically used for initial downsampling and feature extraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels. Default is 3. |
3
|
filters
|
int
|
Number of filters for the initial block. Default is 64. |
64
|
stem_blocks
|
int
|
Number of stem blocks. Default is 0. |
0
|
filters_rate
|
Union[float, int]
|
Factor to increase the number of filters per block. Default is 2. |
2
|
convs_per_block
|
int
|
Number of convolutional layers per block. Default is 2. |
2
|
kernel_size
|
int
|
Size of the convolutional kernels. Default is 7. |
7
|
prefix
|
str
|
Prefix for layer naming. Default is "stem". |
'stem'
|
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
forward |
Forward pass through the StemBlock module. |
Source code in sleap_nn/architectures/encoder_decoder.py
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 |
|
__init__(in_channels=3, filters=64, stem_blocks=0, filters_rate=2, convs_per_block=2, kernel_size=7, prefix='stem')
¶
Initialize the class.
Source code in sleap_nn/architectures/encoder_decoder.py
forward(x)
¶
Forward pass through the StemBlock module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Output tensor after applying the stem operations. |