o
    -iT                     @  s   d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	m  m
Z d dlmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ g d
ZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZ G dd dejZ!e! Z" Z#Z$dS )    )annotations)Sequence)OptionalN)
checkpoint)Convolution)UpSample)ConvPool)ensure_tuple_rep)BasicUnetPlusPlusBasicunetPlusPlusbasicunetplusplusBasicUNetPlusPlusTransc                      s(   e Zd ZdZ fddZdd Z  ZS )Attention_blockz
    Attention Block
    c              
     s   t t|   ttj||dddddt|| _ttj||dddddt|| _ttj|ddddddtdt	 | _
tjdd| _d S )N   r   T)kernel_sizestridepaddingbias)inplace)superr   __init__nn
SequentialConv3dBatchNorm3dW_gW_xSigmoidpsiReLUrelu)selfF_gF_lF_int	__class__ w/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/basic_unetplusplus_modified_trans.pyr   )   s   zAttention_block.__init__c                 C  s\   |  |}| |}|j|jkrtj||jdd  ddd}| || }| |}|| S )N   	trilinearF)sizemodealign_corners)r   r   shapeFinterpolater!   r   )r"   gxg1x1r   r(   r(   r)   forward=   s   


zAttention_block.forward__name__
__module____qualname____doc__r   r6   __classcell__r(   r(   r&   r)   r   $   s    r   c                   @  s   e Zd ZdZdddZdS )	MCDropout3du>   MC Dropout：无论 model.train()/eval() 都启用随机失活inputtorch.Tensorreturnc                 C  s   t j|| jd| jdS )NT)ptrainingr   )r0   	dropout3drA   r   )r"   r>   r(   r(   r)   r6   N   s   zMCDropout3d.forwardN)r>   r?   r@   r?   )r8   r9   r:   r;   r6   r(   r(   r(   r)   r=   K   s    r=   c                      ,   e Zd ZdZ				dd fddZ  ZS )TwoConvztwo convolutions.           rH   rH   r   r   rH   r   spatial_dimsintin_chnsout_chnsactstr | tuplenormr   booldropoutfloat | tupler   Sequence[int]r   Optional[tuple]r   Sequence[int] | intc                   s\   t    t|||||||||	|
d
}t|||||||||	d	}| d| | d| dS )a  
        Args:
            spatial_dims: number of spatial dimensions.
            in_chns: number of input channels.
            out_chns: number of output channels.
            act: activation type and arguments.
            norm: feature normalization type and arguments.
            bias: whether to have a bias term in convolution blocks.
            dropout: dropout ratio. Defaults to no dropout.

        )rN   rP   rR   r   r   r   strides)rN   rP   rR   r   r   r   conv_0conv_1N)r   r   r   
add_module)r"   rJ   rL   rM   rN   rP   r   rR   r   r   r   rX   rY   r&   r(   r)   r   U   s4   
zTwoConv.__init__)rF   rG   rI   r   )rJ   rK   rL   rK   rM   rK   rN   rO   rP   rO   r   rQ   rR   rS   r   rT   r   rU   r   rV   r8   r9   r:   r;   r   r<   r(   r(   r&   r)   rE   R       
rE   c                      rD   )Downz-maxpooling downsampling and two convolutions.rF   rG   rI   maxpoolrJ   rK   rL   rM   rN   rO   rP   r   rQ   rR   rS   r   rT   r   rU   downsample_modestrc                   s~   t    |
dkrtd|f dd}| d| d}n|
dkr"d}ntd|
 t|||||||||	|d	
}| d
| dS )a  
        Args:
            spatial_dims: number of spatial dimensions.
            in_chns: number of input channels.
            out_chns: number of output channels.
            act: activation type and arguments.
            norm: feature normalization type and arguments.
            bias: whether to have a bias term in convolution blocks.
            dropout: dropout ratio. Defaults to no dropout.
            downsample_mode: 'maxpool' or 'strideconv'. Defaults to 'maxpool'.

        r^   MAXr*   r*   r   r   max_poolingr   
strideconvzUnsupported downsample mode: )r   r   r   convsN)r   r   r	   rZ   
ValueErrorrE   )r"   rJ   rL   rM   rN   rP   r   rR   r   r   r_   rd   conv_striderf   r&   r(   r)   r      s*   
zDown.__init__)rF   rG   rI   r^   )rJ   rK   rL   rK   rM   rK   rN   rO   rP   rO   r   rQ   rR   rS   r   rT   r   rU   r_   r`   r[   r(   r(   r&   r)   r]      r\   r]   c                      s&   e Zd Zd fdd	Zdd Z  ZS )	TransformerEncoder   r   皙?c                   sL   t    || _tj|||dd}tj||d| _tt	dd|| _
d S )NT)d_modelnheadrR   batch_first)
num_layersr   i@  )r   r   in_channelsr   TransformerEncoderLayerri   transformer_encoder	Parametertorchrandnpositional_encoding)r"   rp   	num_headsro   rR   encoder_layerr&   r(   r)   r      s   

zTransformerEncoder.__init__c                 C  s   |j \}}}}}|||dddd}|j d }| jddd}	tj|	|dd}
|
ddd}|| }| |}|ddd|||||}|S )Nr   r*   r   linear)r,   r-   )r/   viewpermuterv   r0   r1   rr   )r"   r3   bcdhwZx_flatseq_lenZpos_encoding_baseZpos_encoding_resizedpos_encodingencodedoutputr(   r(   r)   r6      s   

zTransformerEncoder.forward)rj   r   rk   r8   r9   r:   r   r6   r<   r(   r(   r&   r)   ri      s    ri   c                      sB   e Zd ZdZ											d,d- fd$d%Zd.d*d+Z  ZS )/UpCatzHupsampling, concatenation with the encoder feature map, two convolutionsrF   deconvdefaultrz   TrG   rI   FrJ   rK   rL   cat_chnsrM   rN   rO   rP   r   rQ   rR   rS   upsampler`   pre_convnn.Module | str | Noneinterp_moder.   bool | Nonehalvesis_padr   rT   r   rU   	attentionc                   s   t    |	dkr|
du r|}n|r|d n|}t|||d|	|
|||d	| _t||| |||||||d	| _|| _|| _| jrKt|||d d| _	dS dS )a  
        Args:
            spatial_dims: number of spatial dimensions.
            in_chns: number of input channels to be upsampled.
            cat_chns: number of channels from the encoder.
            out_chns: number of output channels.
            act: activation type and arguments.
            norm: feature normalization type and arguments.
            bias: whether to have a bias term in convolution blocks.
            dropout: dropout ratio. Defaults to no dropout.
            upsample: upsampling mode, available options are
                ``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.
            pre_conv: a conv block applied before upsampling.
                Only used in the "nontrainable" or "pixelshuffle" mode.
            interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
                Only used in the "nontrainable" mode.
            align_corners: set the align_corners parameter for upsample. Defaults to True.
                Only used in the "nontrainable" mode.
            halves: whether to halve the number of channels during upsampling.
                This parameter does not work on ``nontrainable`` mode if ``pre_conv`` is `None`.
            is_pad: whether to pad upsampling features to fit features from encoder. Defaults to True.
            attention: whether to use attention gate. Defaults to False.

        nontrainableNr*   rb   )r-   r   r   r.   r   )r   r   )r#   r$   r%   )
r   r   r   r   rE   rf   r   use_attentionr   attention_gate)r"   rJ   rL   r   rM   rN   rP   r   rR   r   r   r   r.   r   r   r   r   r   up_chnsr&   r(   r)   r      sB   
,
zUpCat.__init__r3   r?   x_eOptional[torch.Tensor]c                 C  s   |  |}|dur\|}| jr| j||d}| jrNt|jd }dg|d  }t|D ]}|j| d  |j| d  krDd||d d < q*tjj	
||d}| tj||gdd}|S | |}|S )z

        Args:
            x: features to be upsampled.
            x_e: optional features from the encoder, if None, this branch is not in use.
        N)r2   r3   r*   r   r   	replicatedim)r   r   r   r   lenr/   rangert   r   
functionalpadrf   cat)r"   r3   r   x_0x_e_non_none
dimensionsspir(   r(   r)   r6   8  s&   
 
zUpCat.forward)
rF   r   r   rz   TTTrG   rI   F)"rJ   rK   rL   rK   r   rK   rM   rK   rN   rO   rP   rO   r   rQ   rR   rS   r   r`   r   r   r   r`   r.   r   r   rQ   r   rQ   r   rT   r   rU   r   rQ   )r3   r?   r   r   r7   r(   r(   r&   r)   r      s    Nr   c                      sR   e Zd Zddddddddd	fd
ddifddddfd& fd d!Zd'd$d%Z  ZS )(r   rH   r   r*   )    r   @         r   F	LeakyReLUrk   T)negative_sloper   instanceaffinerF   r   rJ   rK   rp   out_channelsfeaturesrT   deep_supervisionrQ   rN   rO   rP   r   rR   rS   r   r`   	dropout_pfloatc                   sn  t    || _|| _t|d}td| d t|||d ||||	dd| _t||d |d ||||	dd| _	t||d |d ||||	dd| _
t||d |d	 ||||	dd
d	| _t||d	 |d ||||	dd
d	| _t|d ddd| _t||d |d |d ||||	|
dddd| _t||d |d |d ||||	|
dddd| _t||d	 |d |d ||||	|
dddd| _t||d |d	 |d	 ||||	|
dddd| _t||d |d d |d ||||	|
dddd| _t||d |d d |d ||||	|
dddd| _t||d	 |d d |d ||||	|
dddd| _t||d |d d	 |d ||||	|
dddd| _t||d |d d	 |d ||||	|
dddd| _t||d |d d |d ||||	|
dddd| _| jrx| jdkrxt| jnt | _td|f |d |dd| _td|f |d |dd| _ td|f |d |dd| _!td|f |d |dd| _"dS )a	  
        A UNet++ implementation with 1D/2D/3D supports.

        Based on:

            Zhou et al. "UNet++: A Nested U-Net Architecture for Medical Image
            Segmentation". 4th Deep Learning in Medical Image Analysis (DLMIA)
            Workshop, DOI: https://doi.org/10.48550/arXiv.1807.10165


        Args:
            spatial_dims: number of spatial dimensions. Defaults to 3 for spatial 3D inputs.
            in_channels: number of input channels. Defaults to 1.
            out_channels: number of output channels. Defaults to 2.
            features: six integers as numbers of features.
                Defaults to ``(32, 32, 64, 128, 256, 32)``,

                - the first five values correspond to the five-level encoder feature sizes.
                - the last value corresponds to the feature size after the last upsampling.

            deep_supervision: whether to prune the network at inference time. Defaults to False. If true, returns a list,
                whose elements correspond to outputs at different nodes.
            act: activation type and arguments. Defaults to LeakyReLU.
            norm: feature normalization type and arguments. Defaults to instance norm.
            bias: whether to have a bias term in convolution blocks. Defaults to True.
                According to `Performance Tuning Guide <https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html>`_,
                if a conv layer is directly followed by a batch norm layer, bias should be False.
            dropout: dropout ratio. Defaults to no dropout.
            upsample: upsampling mode, available options are
                ``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.

        Examples::

            # for spatial 2D
            >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128))

            # for spatial 2D, with deep supervision enabled
            >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128), deep_supervision=True)

            # for spatial 2D, with group norm
            >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4}))

            # for spatial 3D
            >>> net = BasicUNetPlusPlus(spatial_dims=3, features=(32, 32, 64, 128, 256, 32))

        See Also
            - :py:class:`monai.networks.nets.BasicUNet`
            - :py:class:`monai.networks.nets.DynUNet`
            - :py:class:`monai.networks.nets.UNet`

           zBasicUNetPlusPlus features: .r   )rH   rH      rc   r   r*   rH   re   )r   r_      rj   )rp   rw   ro   FT)r   r   r      convN)#r   r   r   r   r
   printrE   conv_0_0r]   conv_1_0conv_2_0conv_3_0conv_4_0ri   transformer_bottleneckr   	upcat_0_1	upcat_1_1	upcat_2_1	upcat_3_1	upcat_0_2	upcat_1_2	upcat_2_2	upcat_0_3	upcat_1_3	upcat_0_4r=   r   Identitymc_dropout_outr   final_conv_0_1final_conv_0_2final_conv_0_3final_conv_0_4)r"   rJ   rp   r   r   r   rN   rP   r   rR   r   r   fear&   r(   r)   r   W  s  
A



















zBasicUNetPlusPlusTrans.__init__r3   r?   c                 C  s4  |  |}| |}| ||}| |}| ||}| |tj||gdd}t| j	|}| 
||}	| |	tj||gdd}
| |
tj|||gdd}t| j|}t| j|}| ||}| |tj||	gdd}| |tj|||
gdd}| |tj||||gdd}| |}| |}|g}|S )a  
        Args:
            x: input should have spatially N dimensions
                ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N-1])``, N is defined by `dimensions`.
                It is recommended to have ``dim_n % 16 == 0`` to ensure all maxpooling inputs have
                even edge lengths.

        Returns:
            A torch Tensor of "raw" predictions in shape
            ``(Batch, out_channels, dim_0[, dim_1, ..., dim_N-1])``.
        r   r   )r   r   r   r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r"   r3   x_0_0x_1_0x_0_1x_2_0x_1_1x_0_2x_3_0x_2_1x_1_2x_0_3Zx_4_0_before_transx_4_0x_3_1x_2_2x_1_3x_0_4
output_0_4r   r(   r(   r)   r6   z  s(   




zBasicUNetPlusPlusTrans.forward)rJ   rK   rp   rK   r   rK   r   rT   r   rQ   rN   rO   rP   rO   r   rQ   rR   rS   r   r`   r   r   )r3   r?   r   r(   r(   r&   r)   r   V  s     
  %r   )%
__future__r   collections.abcr   typingr   rt   torch.nnr   torch.nn.functionalr   r0   torch.utils.checkpointr   "monai.networks.blocks.convolutionsr   monai.networks.blocks.upsampler   monai.networks.layers.factoriesr   r	   monai.utils.miscr
   __all__Moduler   	Dropout3dr=   r   rE   r]   ri   r   r   r   r   r   r(   r(   r(   r)   <module>   s,   '86'o  U