o & i4@sddlmZddlmZddlZddlmZddlmZm Z m Z ddl m Z m Z ddlmZmZmZmZgdZGdd d ejZGd d d ejZeZeZZdS) ) annotations)SequenceN)ConvPadPool) icnr_init pixelshuffle)InterpolateMode UpsampleModeensure_tuple_replook_up_option)UpsampleUpSampleSubpixelUpsampleSubpixelupsampleSubpixelUpSamplec s@eZdZdZdddddejddejdddf d"fd d! ZZ S)#ra Upsamples data by `scale_factor`. Supported modes are: - "deconv": uses a transposed convolution. - "deconvgroup": uses a transposed group convolution. - "nontrainable": uses :py:class:`torch.nn.Upsample`. - "pixelshuffle": uses :py:class:`monai.networks.blocks.SubpixelUpsample`. This operation will cause non-deterministic when ``mode`` is ``UpsampleMode.NONTRAINABLE``. Please check the link below for more details: https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html#torch.use_deterministic_algorithms This module can optionally take a pre-convolution (often used to map the number of features from `in_channels` to `out_channels`). NdefaultT spatial_dimsint in_channels int | None out_channels scale_factorSequence[float] | float kernel_sizeSequence[float] | float | Nonesizetuple[int] | int | NonemodeUpsampleMode | strpre_convnn.Module | str | None post_convnn.Module | None interp_modestr align_corners bool | Nonebiasboolapply_pad_poolreturnNonec s\tt||}t|t}|s|}d}}nt||}tdd|D}tddt||D}|tjkrY|sAtd|d| dt t j |f||pN|||||| dd S|tj kr|shtd|d|d urn|}||dkrv|nd }| d t t j |f|||||||| d d S|tj kr |d kr||kr|std|d| dt t j|f||p|d | dn|d ur|d kr| d|n |d ur||krtdt| } tjtjtjg}| |vr||d } tj||rd n|| j| d}| d|| r | d| d Sd S|tjkr&| dt||||d|| | dd Std|d)aK Args: spatial_dims: number of spatial dimensions of the input image. in_channels: number of channels of the input image. out_channels: number of channels of the output image. Defaults to `in_channels`. scale_factor: multiplier for spatial size. Has to match input size if it is a tuple. Defaults to 2. kernel_size: kernel size used during transposed convolutions. Defaults to `scale_factor`. size: spatial size of the output image. Only used when ``mode`` is ``UpsampleMode.NONTRAINABLE``. In torch.nn.functional.interpolate, only one of `size` or `scale_factor` should be defined, thus if size is defined, `scale_factor` will not be used. Defaults to None. mode: {``"deconv"``, ``"deconvgroup"``, ``"nontrainable"``, ``"pixelshuffle"``}. Defaults to ``"deconv"``. pre_conv: a conv block applied before upsampling. Defaults to "default". When ``conv_block`` is ``"default"``, one reserved conv layer will be utilized when Only used in the "nontrainable" or "pixelshuffle" mode. post_conv: a conv block applied after upsampling. Defaults to None. Only used in the "nontrainable" mode. interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``} Only used in the "nontrainable" mode. If ends with ``"linear"`` will use ``spatial dims`` to determine the correct interpolation. This corresponds to linear, bilinear, trilinear for 1D, 2D, and 3D respectively. The interpolation mode. Defaults to ``"linear"``. See also: https://pytorch.org/docs/stable/generated/torch.nn.Upsample.html align_corners: set the align_corners parameter of `torch.nn.Upsample`. Defaults to True. Only used in the "nontrainable" mode. bias: whether to have a bias term in the default preconv and deconv layers. Defaults to True. apply_pad_pool: if True the upsampled tensor is padded then average pooling is applied with a kernel the size of `scale_factor` with a stride of 1. See also: :py:class:`monai.networks.blocks.SubpixelUpsample`. Only used in the "pixelshuffle" mode. rcss|] }|ddVqdSrN).0kr0r0`/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/upsample.py csz$UpSample.__init__..css(|]\}}|d|ddVqdSr.r0)r1r2sr0r0r3r4ds&z*in_channels needs to be specified in the 'z' mode.deconv)rrrstridepaddingoutput_paddingr)Nr/ deconvgroup)rrrr7r8r9groupsr)rpreconv)rrrr)z\in the nontrainable mode, if not setting pre_conv, out_channels should equal to in_channels.)rrrr'Zupsample_non_trainablepostconvr)rrrr conv_blockr+r)zUnsupported upsampling mode .)super__init__r r r tuplezipDECONV ValueError add_moduler CONVTRANS DECONVGROUP NONTRAINABLECONVr LINEARBILINEAR TRILINEARnnr value PIXELSHUFFLErNotImplementedError)selfrrrrrrrr!r#r%r'r)r+ scale_factor_Zup_mode kernel_size_r9r8r;Z linear_modeupsample __class__r0r3rA+s /                zUpSample.__init__)rrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r*r,r-) __name__ __module__ __qualname____doc__r rDr rKrA __classcell__r0r0rVr3rsrcs8eZdZdZ     ddfdd ZdddZZS)ra Upsample via using a subpixel CNN. This module supports 1D, 2D and 3D input images. The module is consisted with two parts. First of all, a convolutional layer is employed to increase the number of channels into: ``in_channels * (scale_factor ** dimensions)``. Secondly, a pixel shuffle manipulation is utilized to aggregates the feature maps from low resolution space and build the super resolution space. The first part of the module is not fixed, a sequential layers can be used to replace the default single layer. See: Shi et al., 2016, "Real-Time Single Image and Video Super-Resolution Using a nEfficient Sub-Pixel Convolutional Neural Network." See: Aitken et al., 2017, "Checkerboard artifact free sub-pixel convolution". The idea comes from: https://arxiv.org/abs/1609.05158 The pixel shuffle mechanism refers to: https://pytorch.org/docs/stable/generated/torch.nn.PixelShuffle.html#torch.nn.PixelShuffle. and: https://github.com/pytorch/pytorch/pull/6340. NrrTrrrrrrr>r"r+r*r)r,r-c s t|dkrtd|d||_||_|dkrE|p|}|s%td|||j}ttj|jf||ddd|d|_t|j|jn |d urOt |_n||_t |_ |rt t j |jf} ttj|jf} t | |jddf|jd d | |jdd |_ d Sd S) a4 Args: spatial_dims: number of spatial dimensions of the input image. in_channels: number of channels of the input image. out_channels: optional number of channels of the output image. scale_factor: multiplier for spatial size. Defaults to 2. conv_block: a conv block to extract feature maps before upsampling. Defaults to None. - When ``conv_block`` is ``"default"``, one reserved conv layer will be utilized. - When ``conv_block`` is an ``nn.module``, please ensure the output number of channels is divisible ``(scale_factor ** dimensions)``. apply_pad_pool: if True the upsampled tensor is padded then average pooling is applied with a kernel the size of `scale_factor` with a stride of 1. This implements the nearest neighbour resize convolution component of subpixel convolutions described in Aitken et al. bias: whether to have a bias term in the default conv_block. Defaults to True. rzEThe `scale_factor` multiplier must be an integer greater than 0, got r?rz!in_channels need to be specified.r/)rrrr7r8r)Ng)r8rO)rr7)r@rArE dimensionsrrrJr>rrNIdentitypad_poolrAVGrZ CONSTANTPAD Sequential) rRrrrrr>r+r)conv_out_channels pool_typeZpad_typerVr0r3rAs4      zSubpixelUpsample.__init__x torch.Tensorc Csv||}|jd|j|jdkr,td|jdd|jd|jd|j|jd t||j|j}||}|S)zd Args: x: Tensor in shape (batch, channel, spatial_1[, spatial_2, ...). r/rz'Number of channels after `conv_block` (z:) must be evenly divisible by scale_factor ** dimensions (^=z).)r>shaperr^rErr`)rRrer0r0r3forwards   zSubpixelUpsample.forward)NrrTT)rrrrrrrrr>r"r+r*r)r*r,r-)rerfr,rf)rXrYrZr[rArjr\r0r0rVr3rs>r) __future__rcollections.abcrtorchtorch.nnrNmonai.networks.layers.factoriesrrrmonai.networks.utilsrr monai.utilsr r r r __all__rbrModulerr rrr0r0r0r3s  "h