napari_cellseg3d.code_models.models.unet.buildingblocks
Classes:
| Name | Description |
|---|---|
AbstractUpsampling |
Abstract class for upsampling. A given implementation should upsample a given 5D input tensor using either |
Decoder |
A single module for decoder path consisting of the upsampling layer |
DoubleConv |
A module consisting of two consecutive convolution layers (e.g. BatchNorm3d+ReLU+Conv3d). |
Encoder |
A single module from the encoder path consisting of the optional max |
ExtResNetBlock |
Basic UNet block consisting of a SingleConv followed by the residual block. |
InterpolateUpsampling |
Args: |
SingleConv |
Basic convolutional module consisting of a Conv3d, non-linearity and optional batchnorm/groupnorm. The order |
TransposeConvUpsampling |
Args: |
Functions:
| Name | Description |
|---|---|
create_conv |
Create a list of modules with together constitute a single conv layer with non-linearity |
AbstractUpsampling
Bases: Module
Abstract class for upsampling. A given implementation should upsample a given 5D input tensor using either interpolation or learned transposed convolution.
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
Decoder
Bases: Module
A single module for decoder path consisting of the upsampling layer (either learned ConvTranspose3d or nearest neighbor interpolation) followed by a basic module (DoubleConv or ExtResNetBlock).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
number of input channels |
required |
out_channels
|
int
|
number of output channels |
required |
conv_kernel_size
|
int or tuple
|
size of the convolving kernel |
3
|
scale_factor
|
tuple
|
used as the multiplier for the image H/W/D in case of nn.Upsample or as stride in case of ConvTranspose3d, must reverse the MaxPool3d operation from the corresponding encoder |
(2, 2, 2)
|
basic_module
|
Module
|
either ResNetBlock or DoubleConv |
DoubleConv
|
conv_layer_order
|
string
|
determines the order of layers
in |
'gcr'
|
num_groups
|
int
|
number of groups for the GroupNorm |
8
|
padding
|
int or tuple
|
add zero-padding added to all three sides of the input |
1
|
upsample
|
boole
|
should the input be upsampled |
True
|
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
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 | |
DoubleConv
Bases: Sequential
A module consisting of two consecutive convolution layers (e.g. BatchNorm3d+ReLU+Conv3d). We use (Conv3d+ReLU+GroupNorm3d) by default. This can be changed however by providing the 'order' argument, e.g. in order to change to Conv3d+BatchNorm3d+ELU use order='cbe'. Use padded convolutions to make sure that the output (H_out, W_out) is the same as (H_in, W_in), so that you don't have to crop in the decoder path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
number of input channels |
required |
out_channels
|
int
|
number of output channels |
required |
encoder
|
bool
|
if True we're in the encoder path, otherwise we're in the decoder |
required |
kernel_size
|
int or tuple
|
size of the convolving kernel |
3
|
order
|
string
|
determines the order of layers, e.g. 'cr' -> conv + ReLU 'crg' -> conv + ReLU + groupnorm 'cl' -> conv + LeakyReLU 'ce' -> conv + ELU |
'gcr'
|
num_groups
|
int
|
number of groups for the GroupNorm |
8
|
padding
|
int or tuple
|
add zero-padding added to all three sides of the input |
1
|
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
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 | |
Encoder
Bases: Module
A single module from the encoder path consisting of the optional max pooling layer (one may specify the MaxPool kernel_size to be different than the standard (2,2,2), e.g. if the volumetric data is anisotropic (make sure to use complementary scale_factor in the decoder path) followed by a DoubleConv module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
number of input channels |
required |
out_channels
|
int
|
number of output channels |
required |
conv_kernel_size
|
int or tuple
|
size of the convolving kernel |
3
|
apply_pooling
|
bool
|
if True use MaxPool3d before DoubleConv |
True
|
pool_kernel_size
|
int or tuple
|
the size of the window |
2
|
pool_type
|
str
|
pooling layer: 'max' or 'avg' |
'max'
|
basic_module
|
Module
|
either ResNetBlock or DoubleConv |
DoubleConv
|
conv_layer_order
|
string
|
determines the order of layers
in |
'gcr'
|
num_groups
|
int
|
number of groups for the GroupNorm |
8
|
padding
|
int or tuple
|
add zero-padding added to all three sides of the input |
1
|
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
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 | |
ExtResNetBlock
Bases: Module
Basic UNet block consisting of a SingleConv followed by the residual block. The SingleConv takes care of increasing/decreasing the number of channels and also ensures that the number of output channels is compatible with the residual block that follows. This block can be used instead of standard DoubleConv in the Encoder module. Motivated by: https://arxiv.org/pdf/1706.00120.pdf.
Notice we use ELU instead of ReLU (order='cge') and put non-linearity after the groupnorm.
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
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 | |
InterpolateUpsampling
Bases: AbstractUpsampling
mode (str): algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' | 'trilinear' | 'area'. Default: 'nearest' used only if transposed_conv is False.
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
SingleConv
Bases: Sequential
Basic convolutional module consisting of a Conv3d, non-linearity and optional batchnorm/groupnorm. The order
of operations can be specified via the order parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
number of input channels |
required |
out_channels
|
int
|
number of output channels |
required |
kernel_size
|
int or tuple
|
size of the convolving kernel |
3
|
order
|
string
|
determines the order of layers, e.g. 'cr' -> conv + ReLU 'crg' -> conv + ReLU + groupnorm 'cl' -> conv + LeakyReLU 'ce' -> conv + ELU |
'gcr'
|
num_groups
|
int
|
number of groups for the GroupNorm |
8
|
padding
|
int or tuple
|
|
1
|
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
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 | |
TransposeConvUpsampling
Bases: AbstractUpsampling
in_channels (int): number of input channels for transposed conv used only if transposed_conv is True out_channels (int): number of output channels for transpose conv used only if transposed_conv is True kernel_size (int or tuple): size of the convolving kernel used only if transposed_conv is True scale_factor (int or tuple): stride of the convolution used only if transposed_conv is True.
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
create_conv
create_conv(in_channels, out_channels, kernel_size, order, num_groups, padding)
Create a list of modules with together constitute a single conv layer with non-linearity and optional batchnorm/groupnorm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
number of input channels |
required |
out_channels
|
int
|
number of output channels |
required |
kernel_size
|
int or tuple
|
size of the convolving kernel |
required |
order
|
string
|
order of things, e.g. 'cr' -> conv + ReLU 'gcr' -> groupnorm + conv + ReLU 'cl' -> conv + LeakyReLU 'ce' -> conv + ELU 'bcr' -> batchnorm + conv + ReLU |
required |
num_groups
|
int
|
number of groups for the GroupNorm |
required |
padding
|
int or tuple
|
add zero-padding added to all three sides of the input |
required |
Return
list of tuple (name, module)
Source code in napari_cellseg3d/code_models/models/unet/buildingblocks.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 | |