o & i4@sddlmZddlmZddlZddlmZddlmZm Z ddl m Z ddl m Z mZmZgdZGdd d ejZGd d d ejZGd d d ejZeZeZZdS)) annotations)SequenceN)ConvPool)pixelunshuffle)DownsampleModeensure_tuple_replook_up_option) MaxAvgPool DownSample DownsampleSubpixelDownsampleSubpixelDownSampleSubpixeldownsamplecs4eZdZdZ   ddfdd ZdddZZS)r z Downsample with both maxpooling and avgpooling, double the channel size by concatenating the downsampled feature maps. NrF spatial_dimsint kernel_sizeSequence[int] | intstrideSequence[int] | int | Nonepadding ceil_modeboolreturnNonecsntt|||durdnt||t|||d}ttj|fdi||_ttj|fdi||_dS)a Args: spatial_dims: number of spatial dimensions of the input image. kernel_size: the kernel size of both pooling operations. stride: the stride of the window. Default value is `kernel_size`. padding: implicit zero padding to be added to both pooling operations. ceil_mode: when True, will use ceil instead of floor to compute the output shape. N)rrrr)super__init__rrMAXmax_poolAVGavg_pool)selfrrrrr_params __class__rb/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/downsample.pyr s zMaxAvgPool.__init__x torch.TensorcCstj||||gddS)z Args: x: Tensor in shape (batch, channel, spatial_1[, spatial_2, ...]). Returns: Tensor in shape (batch, 2*channel, spatial_1[, spatial_2, ...]). )dim)torchcatrr!r"r'rrr&forward:szMaxAvgPool.forward)NrF) rrrrrrrrrrrrr'r(rr(__name__ __module__ __qualname____doc__rr. __classcell__rrr$r&r s r cs6eZdZdZddddejdddfdfdd ZZS)r aJ Downsamples data by `scale_factor`. Supported modes are: - "conv": uses a strided convolution for learnable downsampling. - "convgroup": uses a grouped strided convolution for efficient feature reduction. - "nontrainable": uses :py:class:`torch.nn.Upsample` with inverse scale factor. - "pixelunshuffle": uses :py:class:`monai.networks.blocks.PixelUnshuffle` for channel-space rearrangement. This operation will cause non-deterministic behavior when ``mode`` is ``DownsampleMode.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`). NdefaultTrr in_channels int | None out_channels scale_factorSequence[float] | floatrSequence[float] | float | NonemodeDownsampleMode | strpre_convnn.Module | str | None post_convnn.Module | Nonebiasrrrc  s0tt||} t|t} |s| } td|} nt||} tdd| D} | tjkrI|s2td|dt t j|f||p?|| | | | ddS| tj krz|sTtd|durZ|}||dkrb|nd }|d t t j|f||| | | || d dS| tj kr|d kr||kr|std|d t t j|f||p|d | d|dt t j |f| | | d|r|d|dSdS| tjkr|d kr||kr|std|d t t j|f||p|d | d|dt t j|f| | | d|r|d|dSdS| tjkr|dt|||| d|| ddSdS)a Downsamples data by `scale_factor`. Supported modes are: - DownsampleMode.CONV: uses a strided convolution for learnable downsampling. - DownsampleMode.CONVGROUP: uses a grouped strided convolution for efficient feature reduction. - DownsampleMode.MAXPOOL: uses maxpooling for non-learnable downsampling. - DownsampleMode.AVGPOOL: uses average pooling for non-learnable downsampling. - DownsampleMode.PIXELUNSHUFFLE: uses :py:class:`monai.networks.blocks.SubpixelDownsample`. This operation will cause non-deterministic behavior when ``mode`` is ``DownsampleMode.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 and post-convolution (often used to map the number of features from `in_channels` to `out_channels`). 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 reduction. Has to match input size if it is a tuple. Defaults to 2. kernel_size: kernel size used during convolutions. Defaults to `scale_factor`. mode: {``DownsampleMode.CONV``, ``DownsampleMode.CONVGROUP``, ``DownsampleMode.MAXPOOL``, ``DownsampleMode.AVGPOOL``, ``DownsampleMode.PIXELUNSHUFFLE``}. Defaults to ``DownsampleMode.CONV``. pre_conv: a conv block applied before downsampling. Defaults to "default". When ``conv_block`` is ``"default"``, one reserved conv layer will be utilized. Only used in the "maxpool", "avgpool" or "pixelunshuffle" modes. post_conv: a conv block applied after downsampling. Defaults to None. Only used in the "maxpool" and "avgpool" modes. bias: whether to have a bias term in the default preconv and conv layers. Defaults to True. rcss|] }|ddVqdS)r)r6Nr).0krrr& sz&DownSample.__init__..z.in_channels needs to be specified in conv modeconvr8r:rrrrDz!in_channels needs to be specifiedNr) convgroup)r8r:rrrgroupsrDr7Zpreconv)r8r:rrDmaxpool)rrrZpostconvavgpoolr)rr8r:r; conv_blockrD)rrrr rtupleCONV ValueError add_moduler CONVGROUPMAXPOOLrrAVGPOOLr PIXELUNSHUFFLEr )r"rr8r:r;rr>r@rBrDZ scale_factor_Z down_mode kernel_size_rrKr$rr&rXs +               zDownSample.__init__)rrr8r9r:r9r;r<rr=r>r?r@rArBrCrDrrr)r1r2r3r4rrPrr5rrr$r&r Esr cs6eZdZdZ    ddfdd ZdddZZS)r u Downsample via using a subpixel CNN. This module supports 1D, 2D and 3D input images. The module consists of two parts. First, a convolutional layer is employed to adjust the number of channels. Secondly, a pixel unshuffle manipulation rearranges the spatial information into channel space, effectively reducing spatial dimensions while increasing channel depth. The pixel unshuffle operation is the inverse of pixel shuffle, rearranging dimensions from (B, C, H*r, W*r) to (B, C*r², H, W) for 2D images or from (B, C, H*r, W*r, D*r) to (B, C*r³, H, W, D) in 3D case. Example: (1, 1, 4, 4) with r=2 becomes (1, 4, 2, 2). See: Shi et al., 2016, "Real-Time Single Image and Video Super-Resolution Using a nEfficient Sub-Pixel Convolutional Neural Network." The pixel unshuffle mechanism is the inverse operation of: https://github.com/Project-MONAI/MONAI/blob/dev/monai/networks/blocks/upsample.py Nr6r7Trrr8r9r:r;rNrArDrrrcst|dkrtd|d||_||_|dkr8|s!td|p$|}ttj|jf||ddd|d|_d S|d urCt |_d S||_d S) a Downsamples data by rearranging spatial information into channel space. This reduces spatial dimensions while increasing channel depth. 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: factor to reduce the spatial dimensions by. Defaults to 2. conv_block: a conv block to adjust channels before downsampling. 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 input number of channels matches requirements. 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 .r7z!in_channels need to be specified.r)rIN) rrrQ dimensionsr;rrPrNnnIdentity)r"rr8r:r;rNrDr$rr&rs    zSubpixelDownsample.__init__r'r(cs^|}tfdd|jddDs%td|jdddjt|jj}|S)z Args: x: Tensor in shape (batch, channel, spatial_1[, spatial_2, ...). Returns: Tensor with reduced spatial dimensions and increased channel depth. c3s|] }|jdkVqdS)rN)r;)rEdr"rr&rG%sz-SubpixelDownsample.forward..r6NzAll spatial dimensions z* must be evenly divisible by scale_factor )rNallshaperQr;rrZr-rr^r&r.s  zSubpixelDownsample.forward)Nr6r7T)rrr8r9r:r9r;rrNrArDrrrr/r0rrr$r&r s,r ) __future__rcollections.abcrr+torch.nnr[monai.networks.layers.factoriesrrmonai.networks.utilsr monai.utilsrrr __all__Moduler Sequentialr r r rrrrrr&s   +P