|
1 | 1 | {
|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 |
| - "attachments": {}, |
5 | 4 | "cell_type": "markdown",
|
6 | 5 | "id": "0aed74fd",
|
7 | 6 | "metadata": {},
|
|
17 | 16 | "See the License for the specific language governing permissions and \n",
|
18 | 17 | "limitations under the License.\n",
|
19 | 18 | "\n",
|
20 |
| - "# UNet input size constrains\n", |
| 19 | + "# UNet input size constraints\n", |
21 | 20 | "\n",
|
22 |
| - "MONAI provides an enhanced version of UNet (``monai.networks.nets.UNet ``), which not only supports residual units, but also can use more hyperparameters (like ``strides``, ``kernel_size`` and ``up_kernel_size``) than ``monai.networks.nets.BasicUNet``. However, ``UNet`` has some constrains for both network hyperparameters and sizes of input.\n", |
| 21 | + "MONAI provides an enhanced version of UNet (``monai.networks.nets.UNet ``), which not only supports residual units, but also can use more hyperparameters (like ``strides``, ``kernel_size`` and ``up_kernel_size``) than ``monai.networks.nets.BasicUNet``. However, ``UNet`` has some constraints for both network hyperparameters and sizes of input.\n", |
23 | 22 | "\n",
|
24 |
| - "The constrains of hyperparameters can be found in the docstring of the network, and this tutorial is focused on how to determine a reasonable input size.\n", |
| 23 | + "The constraints of hyperparameters can be found in the docstring of the network, and this tutorial is focused on how to determine a reasonable input size.\n", |
25 | 24 | "\n",
|
26 |
| - "The last section: **Constrains of UNet** shows the conclusions." |
| 25 | + "The last section: **Constraints of UNet** shows the conclusions." |
27 | 26 | ]
|
28 | 27 | },
|
29 | 28 | {
|
|
197 | 196 | "3. Normalization layers (`InstanceNorm3d`).\n",
|
198 | 197 | "4. Convolution layers (`Conv` and `ConvTranspose`).\n",
|
199 | 198 | "\n",
|
200 |
| - "As for the layers, convolution layers may change the size of the input, and normalization layers may have extra constrains of the input size.\n", |
201 |
| - "As for the modules, the `SkipConnection` module also has some constrains.\n", |
| 199 | + "As for the layers, convolution layers may change the size of the input, and normalization layers may have extra constraints of the input size.\n", |
| 200 | + "As for the modules, the `SkipConnection` module also has some constraints.\n", |
202 | 201 | "\n",
|
203 |
| - "Consequently, This tutorial shows the constrains of convolution layers, normalization layers and the `SkipConnection` module respectively." |
| 202 | + "Consequently, This tutorial shows the constraints of convolution layers, normalization layers and the `SkipConnection` module respectively." |
204 | 203 | ]
|
205 | 204 | },
|
206 | 205 | {
|
207 | 206 | "cell_type": "markdown",
|
208 | 207 | "id": "bded0633",
|
209 | 208 | "metadata": {},
|
210 | 209 | "source": [
|
211 |
| - "## Constrains of convolution layers" |
| 210 | + "## Constraints of convolution layers" |
212 | 211 | ]
|
213 | 212 | },
|
214 | 213 | {
|
|
378 | 377 | "id": "391b93e6",
|
379 | 378 | "metadata": {},
|
380 | 379 | "source": [
|
381 |
| - "## Constrains of normalization layers" |
| 380 | + "## Constraints of normalization layers" |
382 | 381 | ]
|
383 | 382 | },
|
384 | 383 | {
|
|
407 | 406 | "id": "9e47a8ef",
|
408 | 407 | "metadata": {},
|
409 | 408 | "source": [
|
410 |
| - "In MONAI's norm factories, There are six normalization layers can be used. The official docs can be found in [here](https://pytorch.org/docs/stable/nn.html#normalization-layers), and their constrains is shown in [torch.nn.functional](https://pytorch.org/docs/stable/_modules/torch/nn/functional.html).\n", |
| 409 | + "In MONAI's norm factories, There are six normalization layers can be used. The official docs can be found in [here](https://pytorch.org/docs/stable/nn.html#normalization-layers), and their constraints is shown in [torch.nn.functional](https://pytorch.org/docs/stable/_modules/torch/nn/functional.html).\n", |
411 | 410 | "\n",
|
412 | 411 | "However, the following normalization layers will not be discussed:\n",
|
413 | 412 | "1. SyncBatchNorm, since it only supports `DistributedDataParallel`, please check the official docs for more details.\n",
|
|
447 | 446 | "id": "07347476",
|
448 | 447 | "metadata": {},
|
449 | 448 | "source": [
|
450 |
| - "In reality, when batch size is 1, it's not practical to use batch normalizaton. Therefore, the constrain can be converted to **the batch size should be larger than 1**." |
| 449 | + "In reality, when batch size is 1, it's not practical to use batch normalizaton. Therefore, the constraints can be converted to **the batch size should be larger than 1**." |
451 | 450 | ]
|
452 | 451 | },
|
453 | 452 | {
|
|
482 | 481 | "source": [
|
483 | 482 | "### local response normalization\n",
|
484 | 483 | "\n",
|
485 |
| - "**No constrain**. For example:" |
| 484 | + "**No constraint**. For example:" |
486 | 485 | ]
|
487 | 486 | },
|
488 | 487 | {
|
|
511 | 510 | "id": "bd830ec6",
|
512 | 511 | "metadata": {},
|
513 | 512 | "source": [
|
514 |
| - "## Constrains of SkipConnection" |
| 513 | + "## Constraints of SkipConnection" |
515 | 514 | ]
|
516 | 515 | },
|
517 | 516 | {
|
518 | 517 | "cell_type": "markdown",
|
519 | 518 | "id": "37f2aff4",
|
520 | 519 | "metadata": {},
|
521 | 520 | "source": [
|
522 |
| - "In this section, we will check if the module [SkipConnection](https://github.com/Project-MONAI/MONAI/blob/dev/monai/networks/layers/simplelayers.py) itself has more constrains for the input size.\n", |
| 521 | + "In this section, we will check if the module [SkipConnection](https://github.com/Project-MONAI/MONAI/blob/dev/monai/networks/layers/simplelayers.py) itself has more constraints for the input size.\n", |
523 | 522 | "\n",
|
524 | 523 | "In `UNet`, the `SkipConnection` is called via:\n",
|
525 | 524 | "\n",
|
|
545 | 544 | "id": "cdd7033e",
|
546 | 545 | "metadata": {},
|
547 | 546 | "source": [
|
548 |
| - "If `len(channels) = 2`, there will only have one `SkipConnection` module in the network, and the module is built by a single down layer with `stride = 1`. From the formulas we achieved in the previous section, we know that this layer will not change the size, thus we only need to meet the constrains from the inside normalization layer:\n", |
| 547 | + "If `len(channels) = 2`, there will only have one `SkipConnection` module in the network, and the module is built by a single down layer with `stride = 1`. From the formulas we achieved in the previous section, we know that this layer will not change the size, thus we only need to meet the constraints from the inside normalization layer:\n", |
549 | 548 | "\n",
|
550 | 549 | "1. When using batch normalization, the batch size should larger than 1.\n",
|
551 | 550 | "\n",
|
|
565 | 564 | "id": "2efce3e2",
|
566 | 565 | "metadata": {},
|
567 | 566 | "source": [
|
568 |
| - "If `len(channels) > 2`, more `SkipConnection` module will be built and each of the module is consisted with one down layer and one up layer. Consequently, **the output of the up layer should has the same spatial sizes as the input before entering into the down layer**. The corresponding stride values for these modules are coming from `strides[1:]`, hence for each stride value `s` from `strides[1:]`, for each spatial size value `v` of the input, the constrain of the corresponding `SkipConnection` module is:\n", |
| 567 | + "If `len(channels) > 2`, more `SkipConnection` module will be built and each of the module is consisted with one down layer and one up layer. Consequently, **the output of the up layer should has the same spatial sizes as the input before entering into the down layer**. The corresponding stride values for these modules are coming from `strides[1:]`, hence for each stride value `s` from `strides[1:]`, for each spatial size value `v` of the input, the constraint of the corresponding `SkipConnection` module is:\n", |
569 | 568 | "\n",
|
570 | 569 | "```\n",
|
571 | 570 | "math.floor((v + s - 1) / s) = v / s\n",
|
|
590 | 589 | "\n",
|
591 | 590 | "**`np.remainder(v, np.prod(strides[1:])) == 0`**\n",
|
592 | 591 | "\n",
|
593 |
| - "In addition, there may have more constrains from normalization layers:\n", |
| 592 | + "In addition, there may have more constraints from normalization layers:\n", |
594 | 593 | "\n",
|
595 | 594 | "1. When using batch normalization, the batch size of the input should be larger than 1.\n",
|
596 | 595 | "\n",
|
|
602 | 601 | "id": "8e2d99ef",
|
603 | 602 | "metadata": {},
|
604 | 603 | "source": [
|
605 |
| - "## Constrains of UNet" |
| 604 | + "## Constraints of UNet" |
606 | 605 | ]
|
607 | 606 | },
|
608 | 607 | {
|
609 | 608 | "cell_type": "markdown",
|
610 | 609 | "id": "554744bc",
|
611 | 610 | "metadata": {},
|
612 | 611 | "source": [
|
613 |
| - "As the first section discussed, UNet is consisted with 1) a down layer, 2) one or mode skip connection module(s) and 3) an up layer. Based on the analyses for each single layer/module, the constrains of the network can be summarized as follow." |
| 612 | + "As the first section discussed, UNet is consisted with 1) a down layer, 2) one or mode skip connection module(s) and 3) an up layer. Based on the analyses for each single layer/module, the constraints of the network can be summarized as follow." |
614 | 613 | ]
|
615 | 614 | },
|
616 | 615 | {
|
|
626 | 625 | "id": "8cd1d3b5",
|
627 | 626 | "metadata": {},
|
628 | 627 | "source": [
|
629 |
| - "If `len(channels) == 2`, `strides` must be a single value, thus assume `s = strides`, and the input size is `[B, C, H, W, D]`. The constrains are:\n", |
| 628 | + "If `len(channels) == 2`, `strides` must be a single value, thus assume `s = strides`, and the input size is `[B, C, H, W, D]`. The constraints are:\n", |
630 | 629 | "\n",
|
631 | 630 | "1. If using batch normalization: **`B > 1`.**\n",
|
632 |
| - "2. If using local response normalization: no constrain.\n", |
| 631 | + "2. If using local response normalization: no constraint.\n", |
633 | 632 | "3. If using instance normalization, assume `d = max(H, W, D)`, then `math.floor((d + s - 1) / s) >= 2`, which means **`d >= s + 1`.**\n",
|
634 | 633 | "\n",
|
635 | 634 | "The following are the corresponding examples:"
|
|
749 | 748 | "id": "c804fa49",
|
750 | 749 | "metadata": {},
|
751 | 750 | "source": [
|
752 |
| - "Assume the input size is `[B, C, H, W, D]`, and `s = strides`. The common constrains are:\n", |
| 751 | + "Assume the input size is `[B, C, H, W, D]`, and `s = strides`. The common constraints are:\n", |
753 | 752 | "\n",
|
754 | 753 | "```\n",
|
755 | 754 | "For v in [H, W, D]:\n",
|
|
758 | 757 | "```\n",
|
759 | 758 | "In addition,\n",
|
760 | 759 | "1. If using batch normalization: **`B > 1`.**\n",
|
761 |
| - "2. If using local response normalization: no more constrain.\n", |
| 760 | + "2. If using local response normalization: no more constraint.\n", |
762 | 761 | "3. If using instance normalization, then:\n",
|
763 | 762 | "```\n",
|
764 | 763 | "d = max(H, W, D)\n",
|
|
0 commit comments