File size: 11,686 Bytes
853e22b |
1 2 3 4 5 6 7 8 9 10 11 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 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 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 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 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 |
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from collections.abc import Sequence
import numpy as np
import torch
import torch.nn as nn
from monai.networks.blocks import ADN
from monai.networks.layers.convutils import same_padding, stride_minus_kernel_padding
from monai.networks.layers.factories import Conv
class Convolution(nn.Sequential):
"""
Constructs a convolution with normalization, optional dropout, and optional activation layers::
-- (Conv|ConvTrans) -- (Norm -- Dropout -- Acti) --
if ``conv_only`` set to ``True``::
-- (Conv|ConvTrans) --
For example:
.. code-block:: python
from monai.networks.blocks import Convolution
conv = Convolution(
spatial_dims=3,
in_channels=1,
out_channels=1,
adn_ordering="ADN",
act=("prelu", {"init": 0.2}),
dropout=0.1,
norm=("layer", {"normalized_shape": (10, 10, 10)}),
)
print(conv)
output::
Convolution(
(conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(adn): ADN(
(A): PReLU(num_parameters=1)
(D): Dropout(p=0.1, inplace=False)
(N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
)
)
Args:
spatial_dims: number of spatial dimensions.
in_channels: number of input channels.
out_channels: number of output channels.
strides: convolution stride. Defaults to 1.
kernel_size: convolution kernel size. Defaults to 3.
adn_ordering: a string representing the ordering of activation, normalization, and dropout.
Defaults to "NDA".
act: activation type and arguments. Defaults to PReLU.
norm: feature normalization type and arguments. Defaults to instance norm.
dropout: dropout ratio. Defaults to no dropout.
dropout_dim: determine the spatial dimensions of dropout. Defaults to 1.
- When dropout_dim = 1, randomly zeroes some of the elements for each channel.
- When dropout_dim = 2, Randomly zeroes out entire channels (a channel is a 2D feature map).
- When dropout_dim = 3, Randomly zeroes out entire channels (a channel is a 3D feature map).
The value of dropout_dim should be no larger than the value of `spatial_dims`.
dilation: dilation rate. Defaults to 1.
groups: controls the connections between inputs and outputs. Defaults to 1.
bias: whether to have a bias term. Defaults to True.
conv_only: whether to use the convolutional layer only. Defaults to False.
is_transposed: if True uses ConvTrans instead of Conv. Defaults to False.
padding: controls the amount of implicit zero-paddings on both sides for padding number of points
for each dimension. Defaults to None.
output_padding: controls the additional size added to one side of the output shape.
Defaults to None.
See also:
:py:class:`monai.networks.layers.Conv`
:py:class:`monai.networks.blocks.ADN`
"""
def __init__(
self,
spatial_dims: int,
in_channels: int,
out_channels: int,
strides: Sequence[int] | int = 1,
kernel_size: Sequence[int] | int = 3,
adn_ordering: str = "NDA",
act: tuple | str | None = "PRELU",
norm: tuple | str | None = "INSTANCE",
dropout: tuple | str | float | None = None,
dropout_dim: int | None = 1,
dilation: Sequence[int] | int = 1,
groups: int = 1,
bias: bool = True,
conv_only: bool = False,
is_transposed: bool = False,
padding: Sequence[int] | int | None = None,
output_padding: Sequence[int] | int | None = None,
) -> None:
super().__init__()
self.spatial_dims = spatial_dims
self.in_channels = in_channels
self.out_channels = out_channels
self.is_transposed = is_transposed
if padding is None:
padding = same_padding(kernel_size, dilation)
conv_type = Conv[Conv.CONVTRANS if is_transposed else Conv.CONV, self.spatial_dims]
conv: nn.Module
if is_transposed:
if output_padding is None:
output_padding = stride_minus_kernel_padding(1, strides)
conv = conv_type(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=strides,
padding=padding,
output_padding=output_padding,
groups=groups,
bias=bias,
dilation=dilation,
)
else:
conv = conv_type(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=strides,
padding=padding,
dilation=dilation,
groups=groups,
bias=bias,
)
self.add_module("conv", conv)
if conv_only:
return
if act is None and norm is None and dropout is None:
return
self.add_module(
"adn",
ADN(
ordering=adn_ordering,
in_channels=out_channels,
act=act,
norm=norm,
norm_dim=self.spatial_dims,
dropout=dropout,
dropout_dim=dropout_dim,
),
)
class ResidualUnit(nn.Module):
"""
Residual module with multiple convolutions and a residual connection.
For example:
.. code-block:: python
from monai.networks.blocks import ResidualUnit
convs = ResidualUnit(
spatial_dims=3,
in_channels=1,
out_channels=1,
adn_ordering="AN",
act=("prelu", {"init": 0.2}),
norm=("layer", {"normalized_shape": (10, 10, 10)}),
)
print(convs)
output::
ResidualUnit(
(conv): Sequential(
(unit0): Convolution(
(conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(adn): ADN(
(A): PReLU(num_parameters=1)
(N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
)
)
(unit1): Convolution(
(conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(adn): ADN(
(A): PReLU(num_parameters=1)
(N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
)
)
)
(residual): Identity()
)
Args:
spatial_dims: number of spatial dimensions.
in_channels: number of input channels.
out_channels: number of output channels.
strides: convolution stride. Defaults to 1.
kernel_size: convolution kernel size. Defaults to 3.
subunits: number of convolutions. Defaults to 2.
adn_ordering: a string representing the ordering of activation, normalization, and dropout.
Defaults to "NDA".
act: activation type and arguments. Defaults to PReLU.
norm: feature normalization type and arguments. Defaults to instance norm.
dropout: dropout ratio. Defaults to no dropout.
dropout_dim: determine the dimensions of dropout. Defaults to 1.
- When dropout_dim = 1, randomly zeroes some of the elements for each channel.
- When dropout_dim = 2, Randomly zero out entire channels (a channel is a 2D feature map).
- When dropout_dim = 3, Randomly zero out entire channels (a channel is a 3D feature map).
The value of dropout_dim should be no larger than the value of `dimensions`.
dilation: dilation rate. Defaults to 1.
bias: whether to have a bias term. Defaults to True.
last_conv_only: for the last subunit, whether to use the convolutional layer only.
Defaults to False.
padding: controls the amount of implicit zero-paddings on both sides for padding number of points
for each dimension. Defaults to None.
See also:
:py:class:`monai.networks.blocks.Convolution`
"""
def __init__(
self,
spatial_dims: int,
in_channels: int,
out_channels: int,
strides: Sequence[int] | int = 1,
kernel_size: Sequence[int] | int = 3,
subunits: int = 2,
adn_ordering: str = "NDA",
act: tuple | str | None = "PRELU",
norm: tuple | str | None = "INSTANCE",
dropout: tuple | str | float | None = None,
dropout_dim: int | None = 1,
dilation: Sequence[int] | int = 1,
bias: bool = True,
last_conv_only: bool = False,
padding: Sequence[int] | int | None = None,
) -> None:
super().__init__()
self.spatial_dims = spatial_dims
self.in_channels = in_channels
self.out_channels = out_channels
self.conv = nn.Sequential()
self.residual = nn.Identity()
if not padding:
padding = same_padding(kernel_size, dilation)
schannels = in_channels
sstrides = strides
subunits = max(1, subunits)
for su in range(subunits):
conv_only = last_conv_only and su == (subunits - 1)
unit = Convolution(
self.spatial_dims,
schannels,
out_channels,
strides=sstrides,
kernel_size=kernel_size,
adn_ordering=adn_ordering,
act=act,
norm=norm,
dropout=dropout,
dropout_dim=dropout_dim,
dilation=dilation,
bias=bias,
conv_only=conv_only,
padding=padding,
)
self.conv.add_module(f"unit{su:d}", unit)
# after first loop set channels and strides to what they should be for subsequent units
schannels = out_channels
sstrides = 1
# apply convolution to input to change number of output channels and size to match that coming from self.conv
if np.prod(strides) != 1 or in_channels != out_channels:
rkernel_size = kernel_size
rpadding = padding
if np.prod(strides) == 1: # if only adapting number of channels a 1x1 kernel is used with no padding
rkernel_size = 1
rpadding = 0
conv_type = Conv[Conv.CONV, self.spatial_dims]
self.residual = conv_type(in_channels, out_channels, rkernel_size, strides, rpadding, bias=bias)
def forward(self, x: torch.Tensor) -> torch.Tensor:
res: torch.Tensor = self.residual(x) # create the additive residual from x
cx: torch.Tensor = self.conv(x) # apply x to sequence of operations
return cx + res # add the residual to the output
|