o
    ϯiY                     @   sZ   d dl mZ d dlZd dlmZ G dd dejZG dd dejZG dd dejZdS )	    )OptionalNc                       sx   e Zd ZU dZejed< eed< 				ddede	e d	e
ded
e	ej ddf fddZdejdejfddZ  ZS )SqueezeExcitationz
    Generic 2d/3d extension of Squeeze-and-Excitation (SE) block described in:
        *Hu et al., Squeeze-and-Excitation Networks, arXiv:1709.01507*
    Squeezing spatially and exciting channel-wise
    blockis_3dN       @Fnum_channelsnum_channels_reducedreduction_ratio
activationreturnc                    s   t    |du rt|| }|du rt }|r,tj||ddd}tj||ddd}ntj||ddd}tj||ddd}|| _t|||t	 | _
dS )  
        Args:
            num_channels (int): Number of input channels.
            num_channels_reduced (int):
                Number of reduced channels. If none, uses reduction_ratio to calculate.
            reduction_ratio (float):
                How much num_channels should be reduced if num_channels_reduced is not provided.
            is_3d (bool): Whether we're operating on 3d data (or 2d), default 2d.
            activation (nn.Module): Activation function used, defaults to ReLU.
        N   Tkernel_sizebias)super__init__intnnReLUConv3dConv2dr   
SequentialSigmoidr   )selfr   r   r	   r   r
   conv1conv2	__class__ P/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/nn/squeeze_excitation.pyr      s2   

zSqueezeExcitation.__init__input_tensorc                 C   s@   | j r|jg dddn|jddgdd}t|| |}|S )
        Args:
            input_tensor: X, shape = (batch_size, num_channels, H, W).
                For 3d X, shape = (batch_size, num_channels, T, H, W).
            output tensor
        )         T)dimkeepdimr#   r$   )r   meantorchmulr   )r   r!   mean_tensoroutput_tensorr   r   r    forwardD   s   	zSqueezeExcitation.forward)Nr   FN)__name__
__module____qualname____doc__r   Module__annotations__boolr   r   floatr   r)   Tensorr-   __classcell__r   r   r   r    r   	   s,   
 
1r   c                       sT   e Zd ZU dZejed< 	ddededdf fdd	Z	d
e
jde
jfddZ  ZS )SpatialSqueezeExcitationz
    Generic 2d/3d extension of SE block
        squeezing channel-wise and exciting spatially described in:
        *Roy et al., Concurrent Spatial and Channel Squeeze & Excitation
        in Fully Convolutional Networks, MICCAI 2018*
    r   Fr   r   r   Nc                    sJ   t    |rtj|dddd}n	tj|dddd}t|t | _dS )z
        Args:
            num_channels (int): Number of input channels.
            is_3d (bool): Whether we're operating on 3d data.
        r   Tr   N)r   r   r   r   r   r   r   r   )r   r   r   convr   r   r    r   _   s   


z!SpatialSqueezeExcitation.__init__r!   c                 C   s   t || |}|S )r"   )r)   r*   r   r   r!   r,   r   r   r    r-   u   s   z SpatialSqueezeExcitation.forward)F)r.   r/   r0   r1   r   r2   r3   r   r4   r   r)   r6   r-   r7   r   r   r   r    r8   U   s   
 
r8   c                       sd   e Zd ZdZ				ddedee deded	eej	 d
df fddZ
dejd
ejfddZ  ZS )ChannelSpatialSqueezeExcitationz
    Generic 2d/3d extension of concurrent spatial and channel squeeze & excitation:
         *Roy et al., Concurrent Spatial and Channel Squeeze & Excitation
         in Fully Convolutional Networks, arXiv:1803.02579*
    N      0@Fr   r   r	   r   r
   r   c                    s0   t    t|||||d| _t||d| _dS )r   )r   r   r	   r   r
   )r   r   N)r   r   r   channelr8   spatial)r   r   r   r	   r   r
   r   r   r    r      s   
z(ChannelSpatialSqueezeExcitation.__init__r!   c                 C   s   t | || |}|S )z
        Args:
            input_tensor: X, shape = (batch_size, num_channels, H, W)
                For 3d X, shape = (batch_size, num_channels, T, H, W)
            output tensor
        )r)   maxr=   r>   r:   r   r   r    r-      s   z'ChannelSpatialSqueezeExcitation.forward)Nr<   FN)r.   r/   r0   r1   r   r   r5   r4   r   r2   r   r)   r6   r-   r7   r   r   r   r    r;      s(    	r;   )	typingr   r)   torch.nnr   r2   r   r8   r;   r   r   r   r    <module>   s   L,