Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions monai/apps/deepgrow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from monai.transforms import Resize, SpatialCrop
from monai.transforms.transform import MapTransform, Randomizable, Transform
from monai.transforms.utils import generate_spatial_bounding_box
from monai.utils import InterpolateMode, ensure_tuple, ensure_tuple_rep, min_version, optional_import
from monai.utils import InterpolateMode, deprecated_arg, ensure_tuple, ensure_tuple_rep, min_version, optional_import

measure, _ = optional_import("skimage.measure", "0.14.2", min_version)
distance_transform_cdt, _ = optional_import("scipy.ndimage.morphology", name="distance_transform_cdt")
Expand Down Expand Up @@ -476,7 +476,7 @@ class AddGuidanceFromPointsd(Transform):
background: key that represents user background (-ve) clicks.
axis: axis that represents slices in 3D volume. (axis to Depth)
depth_first: if depth (slices) is positioned at first dimension.
dimensions: dimensions based on model used for deepgrow (2D vs 3D).
spatial_dims: dimensions based on model used for deepgrow (2D vs 3D).
slice_key: key that represents applicable slice to add guidance.
meta_keys: explicitly indicate the key of the meta data dictionary of `ref_image`.
for example, for data with key `image`, the metadata by default is in `image_meta_dict`.
Expand All @@ -486,8 +486,13 @@ class AddGuidanceFromPointsd(Transform):
to the key data, default is `meta_dict`, the meta data is a dictionary object.
For example, to handle key `image`, read/write affine matrices from the
metadata `image_meta_dict` dictionary's `affine` field.

.. deprecated:: 0.6.0
``dimensions`` is deprecated, use ``spatial_dims`` instead.

"""

@deprecated_arg(name="dimensions", since="0.6", msg_suffix="Please use `spatial_dims` instead.")
def __init__(
self,
ref_image,
Expand All @@ -496,18 +501,19 @@ def __init__(
background: str = "background",
axis: int = 0,
depth_first: bool = True,
dimensions: int = 2,
spatial_dims: int = 2,
slice_key: str = "slice",
meta_keys: Optional[str] = None,
meta_key_postfix: str = "meta_dict",
dimensions: Optional[int] = None,
):
self.ref_image = ref_image
self.guidance = guidance
self.foreground = foreground
self.background = background
self.axis = axis
self.depth_first = depth_first
self.dimensions = dimensions
self.dimensions = spatial_dims if dimensions is None else dimensions
self.slice = slice_key
self.meta_keys = meta_keys
self.meta_key_postfix = meta_key_postfix
Expand Down
17 changes: 12 additions & 5 deletions monai/losses/image_dissimilarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
# 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 typing import Tuple, Union
from typing import Optional, Tuple, Union

import torch
from torch.nn import functional as F
from torch.nn.modules.loss import _Loss

from monai.networks.layers import gaussian_1d, separable_filtering
from monai.utils import LossReduction
from monai.utils import LossReduction, deprecated_arg


def make_rectangular_kernel(kernel_size: int) -> torch.Tensor:
Expand Down Expand Up @@ -59,18 +59,20 @@ class LocalNormalizedCrossCorrelationLoss(_Loss):
DeepReg (https://github.com/DeepRegNet/DeepReg)
"""

@deprecated_arg(name="ndim", since="0.6", msg_suffix="Please use `spatial_dims` instead.")
def __init__(
self,
ndim: int = 3,
spatial_dims: int = 3,
kernel_size: int = 3,
kernel_type: str = "rectangular",
reduction: Union[LossReduction, str] = LossReduction.MEAN,
smooth_nr: float = 1e-5,
smooth_dr: float = 1e-5,
ndim: Optional[int] = None,
) -> None:
"""
Args:
ndim: number of spatial ndimensions, {``1``, ``2``, ``3``}. Defaults to 3.
spatial_dims: number of spatial ndimensions, {``1``, ``2``, ``3``}. Defaults to 3.
kernel_size: kernel spatial size, must be odd.
kernel_type: {``"rectangular"``, ``"triangular"``, ``"gaussian"``}. Defaults to ``"rectangular"``.
reduction: {``"none"``, ``"mean"``, ``"sum"``}
Expand All @@ -81,10 +83,15 @@ def __init__(
- ``"sum"``: the output will be summed.
smooth_nr: a small constant added to the numerator to avoid nan.
smooth_dr: a small constant added to the denominator to avoid nan.

.. deprecated:: 0.6.0
``ndim`` is deprecated, use ``spatial_dims``.
"""
super(LocalNormalizedCrossCorrelationLoss, self).__init__(reduction=LossReduction(reduction).value)

self.ndim = ndim
if ndim is not None:
spatial_dims = ndim
self.ndim = spatial_dims
if self.ndim not in [1, 2, 3]:
raise ValueError(f"Unsupported ndim: {self.ndim}-d, only 1-d, 2-d, and 3-d inputs are supported")

Expand Down
6 changes: 2 additions & 4 deletions monai/networks/blocks/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class Swish(nn.Module):


Shape:
- Input: :math:`(N, *)` where `*` means, any number of additional
dimensions
- Input: :math:`(N, *)` where `*` means, any number of additional dimensions
- Output: :math:`(N, *)`, same shape as the input


Expand Down Expand Up @@ -143,8 +142,7 @@ class Mish(nn.Module):
this class will utilize `torch.nn.functional.mish` to do the calculation if meets the version.

Shape:
- Input: :math:`(N, *)` where `*` means, any number of additional
dimensions
- Input: :math:`(N, *)` where `*` means, any number of additional dimensions
- Output: :math:`(N, *)`, same shape as the input


Expand Down
2 changes: 1 addition & 1 deletion monai/networks/blocks/aspp.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(

out_channels = conv_out_channels * len(pads) # final conv. output channels
self.conv_k1 = Convolution(
dimensions=spatial_dims,
spatial_dims=spatial_dims,
in_channels=out_channels,
out_channels=out_channels,
kernel_size=1,
Expand Down
39 changes: 26 additions & 13 deletions monai/networks/blocks/convolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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
from monai.utils.deprecated import deprecated_arg


class Convolution(nn.Sequential):
Expand Down Expand Up @@ -59,7 +60,7 @@ class Convolution(nn.Sequential):
)

Args:
dimensions: number of spatial dimensions.
spatial_dims: number of spatial dimensions.
in_channels: number of input channels.
out_channels: number of output channels.
strides: convolution stride. Defaults to 1.
Expand All @@ -69,13 +70,13 @@ class Convolution(nn.Sequential):
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.
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 no larger than the value of `dimensions`.
The value of dropout_dim should be no 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.
Expand All @@ -86,16 +87,22 @@ class Convolution(nn.Sequential):
output_padding: controls the additional size added to one side of the output shape.
Defaults to None.

.. deprecated:: 0.6.0
``dimensions`` is deprecated, use ``spatial_dims`` instead.

See also:

:py:class:`monai.networks.layers.Conv`
:py:class:`monai.networks.blocks.ADN`

"""

@deprecated_arg(
name="dimensions", new_name="spatial_dims", since="0.6", msg_suffix="Please use `spatial_dims` instead."
)
def __init__(
self,
dimensions: int,
spatial_dims: int,
in_channels: int,
out_channels: int,
strides: Union[Sequence[int], int] = 1,
Expand All @@ -112,15 +119,16 @@ def __init__(
is_transposed: bool = False,
padding: Optional[Union[Sequence[int], int]] = None,
output_padding: Optional[Union[Sequence[int], int]] = None,
dimensions: Optional[int] = None,
) -> None:
super().__init__()
self.dimensions = dimensions
self.dimensions = spatial_dims if dimensions is None else dimensions
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, dimensions]
conv_type = Conv[Conv.CONVTRANS if is_transposed else Conv.CONV, self.dimensions]

conv: nn.Module
if is_transposed:
Expand Down Expand Up @@ -159,7 +167,7 @@ def __init__(
in_channels=out_channels,
act=act,
norm=norm,
norm_dim=dimensions,
norm_dim=self.dimensions,
dropout=dropout,
dropout_dim=dropout_dim,
),
Expand All @@ -177,7 +185,7 @@ class ResidualUnit(nn.Module):
from monai.networks.blocks import ResidualUnit

convs = ResidualUnit(
dimensions=3,
spatial_dims=3,
in_channels=1,
out_channels=1,
adn_ordering="AN",
Expand Down Expand Up @@ -209,7 +217,7 @@ class ResidualUnit(nn.Module):
)

Args:
dimensions: number of spatial dimensions.
spatial_dims: number of spatial dimensions.
in_channels: number of input channels.
out_channels: number of output channels.
strides: convolution stride. Defaults to 1.
Expand All @@ -234,15 +242,19 @@ class ResidualUnit(nn.Module):
padding: controls the amount of implicit zero-paddings on both sides for padding number of points
for each dimension. Defaults to None.

.. deprecated:: 0.6.0
``dimensions`` is deprecated, use ``spatial_dims`` instead.

See also:

:py:class:`monai.networks.blocks.Convolution`

"""

@deprecated_arg(name="dimensions", since="0.6", msg_suffix="Please use `spatial_dims` instead.")
def __init__(
self,
dimensions: int,
spatial_dims: int,
in_channels: int,
out_channels: int,
strides: Union[Sequence[int], int] = 1,
Expand All @@ -257,9 +269,10 @@ def __init__(
bias: bool = True,
last_conv_only: bool = False,
padding: Optional[Union[Sequence[int], int]] = None,
dimensions: Optional[int] = None,
) -> None:
super().__init__()
self.dimensions = dimensions
self.dimensions = spatial_dims if dimensions is None else dimensions
self.in_channels = in_channels
self.out_channels = out_channels
self.conv = nn.Sequential()
Expand All @@ -273,7 +286,7 @@ def __init__(
for su in range(subunits):
conv_only = last_conv_only and su == (subunits - 1)
unit = Convolution(
dimensions,
self.dimensions,
schannels,
out_channels,
strides=sstrides,
Expand Down Expand Up @@ -304,7 +317,7 @@ def __init__(
rkernel_size = 1
rpadding = 0

conv_type = Conv[Conv.CONV, dimensions]
conv_type = Conv[Conv.CONV, self.dimensions]
self.residual = conv_type(in_channels, out_channels, rkernel_size, strides, rpadding, bias=bias)

def forward(self, x: torch.Tensor) -> torch.Tensor:
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/blocks/fcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(

if self.upsample_mode == "transpose":
self.up_conv = UpSample(
dimensions=2,
spatial_dims=2,
in_channels=self.out_channels,
scale_factor=2,
mode="deconv",
Expand Down Expand Up @@ -236,7 +236,7 @@ def __init__(
)

self.init_proj = Convolution(
dimensions=2,
spatial_dims=2,
in_channels=in_channels,
out_channels=3,
kernel_size=1,
Expand Down
11 changes: 7 additions & 4 deletions monai/networks/blocks/localnet_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_conv_block(
norm: Optional[Union[Tuple, str]] = "BATCH",
) -> nn.Module:
padding = same_padding(kernel_size)
return Convolution(
mod: nn.Module = Convolution(
spatial_dims,
in_channels,
out_channels,
Expand All @@ -40,6 +40,7 @@ def get_conv_block(
conv_only=False,
padding=padding,
)
return mod


def get_conv_layer(
Expand All @@ -49,7 +50,7 @@ def get_conv_layer(
kernel_size: Union[Sequence[int], int] = 3,
) -> nn.Module:
padding = same_padding(kernel_size)
return Convolution(
mod: nn.Module = Convolution(
spatial_dims,
in_channels,
out_channels,
Expand All @@ -58,15 +59,16 @@ def get_conv_layer(
conv_only=True,
padding=padding,
)
return mod


def get_deconv_block(
spatial_dims: int,
in_channels: int,
out_channels: int,
) -> nn.Module:
return Convolution(
dimensions=spatial_dims,
mod: nn.Module = Convolution(
spatial_dims=spatial_dims,
in_channels=in_channels,
out_channels=out_channels,
strides=2,
Expand All @@ -77,6 +79,7 @@ def get_deconv_block(
padding=1,
output_padding=1,
)
return mod


class ResidualBlock(nn.Module):
Expand Down
Loading