U Ph4@sddlmZddlmZddlZddlmZddlmZm Z m Z ddl m Z m Z ddlmZmZmZmZddd d d gZGd ddejZGd d d ejZeZeZZdS)) annotations)SequenceN)ConvPadPool) icnr_init pixelshuffle)InterpolateMode UpsampleModeensure_tuple_replook_up_optionUpsampleUpSampleSubpixelUpsampleSubpixelupsampleSubpixelUpSamplecsZeZdZdZdddddejdejdddf ddddd d d d d ddddd fdd 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`). NdefaultTint int | NonezSequence[float] | floatzSequence[float] | float | Noneztuple[int] | int | NonezUpsampleMode | strnn.Module | str | Nonestrz bool | NoneboolNone) spatial_dims in_channels out_channels scale_factor kernel_sizesizemodepre_conv interp_mode align_cornersbiasapply_pad_poolreturnc  sXtt||} t|t}|s0| }d}}n4t||}tdd|D}tddt|| D}|tjkr|std|d| dt t j |f||p||| ||| dn|tj kr |std|d|d kr|}||dkr|nd }| d t t j |f|||| |||| d n4|tj kr|d kr||kr|sVtd|d| dt t j|f||pr|d | dn>|d k r|d kr| d|n|d kr||krtdt| } tjtjtjg}| |kr||d } | dtj||rd n| | j| dn@|tjkrD| dt|||| d|| | dntd|dd S)a 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. 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).0kr)r)S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/blocks/upsample.py asz$UpSample.__init__..css&|]\}}|d|ddVqdSr'r))r*r+sr)r)r,r-bsz*in_channels needs to be specified in the 'z' mode.deconv)rrrstridepaddingoutput_paddingr$Nr( deconvgroup)rrrr0r1r2groupsr$rZpreconv)rrrr$z\in the nontrainable mode, if not setting pre_conv, out_channels should equal to in_channels.Zupsample_non_trainable)rrr r#r)rrrr conv_blockr%r$zUnsupported upsampling mode .)super__init__r r r tuplezipDECONV ValueError add_moduler CONVTRANS DECONVGROUP NONTRAINABLECONVr LINEARBILINEAR TRILINEARnnr value PIXELSHUFFLErNotImplementedError)selfrrrrrrr r!r"r#r$r%Z scale_factor_Zup_mode kernel_size_r2r1r4Z linear_mode __class__r)r,r8+s-                zUpSample.__init__) __name__ __module__ __qualname____doc__r r;r rBr8 __classcell__r)r)rKr,rsc sFeZdZdZddddddd d d d fd d ZdddddZZS)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. NrrTrrrrr)rrrrr5r%r$r&c st|dkr"td|d||_||_|dkr|p<|}|sJtd|||j}ttj|jf||ddd|d|_t|j|jn|d krt |_n||_t |_ |rt t j |jf} ttj|jf} t | |jddf|jd d | |jdd |_ d 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 r6rz!in_channels need to be specified.r()rrrr0r1r$Ng)r1rF)rr0)r7r8r< dimensionsrrrAr5rrEIdentitypad_poolrAVGrZ CONSTANTPAD Sequential) rIrrrrr5r%r$conv_out_channels pool_typeZpad_typerKr)r,r8s<    zSubpixelUpsample.__init__z torch.Tensor)xr&c Csv||}|jd|j|jdkrXtd|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).)r5shaperrSr<rrU)rIrZr)r)r,forwards . zSubpixelUpsample.forward)NrrTT)rMrNrOrPr8r^rQr)r)rKr,rs">) __future__rcollections.abcrtorchtorch.nnrEmonai.networks.layers.factoriesrrrmonai.networks.utilsrr monai.utilsr r r r __all__rWrModulerr rrr)r)r)r, s   h