MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _get_same_padding_conv_nd

Function _get_same_padding_conv_nd

monai/networks/nets/efficientnet.py:808–847  ·  view source on GitHub ↗

Helper for getting padding (nn.ConstantPadNd) to be used to get SAME padding conv operations similar to Tensorflow's SAME padding. This function is generalized for MONAI's N-Dimensional spatial operations (e.g. Conv1D, Conv2D, Conv3D) Args: image_size: input image/feature

(
    image_size: list[int], kernel_size: tuple[int, ...], dilation: tuple[int, ...], stride: tuple[int, ...]
)

Source from the content-addressed store, hash-verified

806
807
808def _get_same_padding_conv_nd(
809 image_size: list[int], kernel_size: tuple[int, ...], dilation: tuple[int, ...], stride: tuple[int, ...]
810) -> list[int]:
811 """
812 Helper for getting padding (nn.ConstantPadNd) to be used to get SAME padding
813 conv operations similar to Tensorflow's SAME padding.
814
815 This function is generalized for MONAI's N-Dimensional spatial operations (e.g. Conv1D, Conv2D, Conv3D)
816
817 Args:
818 image_size: input image/feature spatial size.
819 kernel_size: conv kernel's spatial size.
820 dilation: conv dilation rate for Atrous conv.
821 stride: stride for conv operation.
822
823 Returns:
824 paddings for ConstantPadNd padder to be used on input tensor to conv op.
825 """
826 # get number of spatial dimensions, corresponds to kernel size length
827 num_dims = len(kernel_size)
828
829 # additional checks to populate dilation and stride (in case they are single entry tuples)
830 if len(dilation) == 1:
831 dilation = dilation * num_dims
832
833 if len(stride) == 1:
834 stride = stride * num_dims
835
836 # equation to calculate (pad^+ + pad^-) size
837 _pad_size: list[int] = [
838 max((math.ceil(_i_s / _s) - 1) * _s + (_k_s - 1) * _d + 1 - _i_s, 0)
839 for _i_s, _k_s, _d, _s in zip(image_size, kernel_size, dilation, stride)
840 ]
841 # distribute paddings into pad^+ and pad^- following Tensorflow's same padding strategy
842 _paddings: list[tuple[int, int]] = [(_p // 2, _p - _p // 2) for _p in _pad_size]
843
844 # unroll list of tuples to tuples, and then to list
845 # reversed as nn.ConstantPadNd expects paddings starting with last dimension
846 _paddings_ret: list[int] = [outer for inner in reversed(_paddings) for outer in inner]
847 return _paddings_ret
848
849
850def _make_same_padder(conv_op: nn.Conv1d | nn.Conv2d | nn.Conv3d, image_size: list[int]):

Callers 1

_make_same_padderFunction · 0.85

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…