Residual network block used SegResNet based on `3D MRI brain tumor segmentation using autoencoder regularization `_.
| 67 | |
| 68 | |
| 69 | class SegResBlock(nn.Module): |
| 70 | """ |
| 71 | Residual network block used SegResNet based on `3D MRI brain tumor segmentation using autoencoder regularization |
| 72 | <https://arxiv.org/pdf/1810.11654.pdf>`_. |
| 73 | """ |
| 74 | |
| 75 | def __init__( |
| 76 | self, |
| 77 | spatial_dims: int, |
| 78 | in_channels: int, |
| 79 | norm: tuple | str, |
| 80 | kernel_size: tuple | int = 3, |
| 81 | act: tuple | str = "relu", |
| 82 | ) -> None: |
| 83 | """ |
| 84 | Args: |
| 85 | spatial_dims: number of spatial dimensions, could be 1, 2 or 3. |
| 86 | in_channels: number of input channels. |
| 87 | norm: feature normalization type and arguments. |
| 88 | kernel_size: convolution kernel size. Defaults to 3. |
| 89 | act: activation type and arguments. Defaults to ``RELU``. |
| 90 | """ |
| 91 | super().__init__() |
| 92 | |
| 93 | if isinstance(kernel_size, (tuple, list)): |
| 94 | padding = tuple(k // 2 for k in kernel_size) |
| 95 | else: |
| 96 | padding = kernel_size // 2 # type: ignore |
| 97 | |
| 98 | self.norm1 = get_norm_layer(name=norm, spatial_dims=spatial_dims, channels=in_channels) |
| 99 | self.act1 = get_act_layer(act) |
| 100 | self.conv1 = Conv[Conv.CONV, spatial_dims]( |
| 101 | in_channels=in_channels, |
| 102 | out_channels=in_channels, |
| 103 | kernel_size=kernel_size, |
| 104 | stride=1, |
| 105 | padding=padding, |
| 106 | bias=False, |
| 107 | ) |
| 108 | |
| 109 | self.norm2 = get_norm_layer(name=norm, spatial_dims=spatial_dims, channels=in_channels) |
| 110 | self.act2 = get_act_layer(act) |
| 111 | self.conv2 = Conv[Conv.CONV, spatial_dims]( |
| 112 | in_channels=in_channels, |
| 113 | out_channels=in_channels, |
| 114 | kernel_size=kernel_size, |
| 115 | stride=1, |
| 116 | padding=padding, |
| 117 | bias=False, |
| 118 | ) |
| 119 | |
| 120 | def forward(self, x): |
| 121 | identity = x |
| 122 | x = self.conv2(self.act2(self.norm2(self.conv1(self.act1(self.norm1(x)))))) |
| 123 | x += identity |
| 124 | return x |
| 125 | |
| 126 |