o
    ÓÙ¾iç  ã                   @   sj   d Z ddlmZ ddlmZ ddlmZ G dd„ dejƒZeZ	G dd	„ d	ejƒZ
e
ZG d
d„ dejƒZdS )aÅ   Squeeze-and-Excitation Channel Attention

An SE implementation originally based on PyTorch SE-Net impl.
Has since evolved with additional functionality / configuration.

Paper: `Squeeze-and-Excitation Networks` - https://arxiv.org/abs/1709.01507

Also included is Effective Squeeze-Excitation (ESE).
Paper: `CenterMask : Real-Time Anchor-Free Instance Segmentation` - https://arxiv.org/abs/1911.06667

Hacked together by / Copyright 2021 Ross Wightman
é    )Únné   )Úcreate_act_layer)Úmake_divisiblec                       s<   e Zd ZdZdddddejddf‡ fdd	„	Zd
d„ Z‡  ZS )ÚSEModuleáÒ   SE Module as defined in original SE-Nets with a few additions
    Additions include:
        * divisor can be specified to keep channels % div == 0 (default: 8)
        * reduction channels can be specified directly by arg (if rd_channels is set)
        * reduction channels can be specified by float rd_ratio (default: 1/16)
        * global max pooling can be added to the squeeze aggregation
        * customizable activation, normalization, and gate layer
    ç      °?Né   FTÚsigmoidc
           
         s„   t t| ƒ ¡  || _|st|| |dd}tj||d|d| _|r%||ƒnt ¡ | _	t
|dd| _tj||d|d| _t
|	ƒ| _d S )Nç        ©Úround_limitr   )Úkernel_sizeÚbiasT©Úinplace)Úsuperr   Ú__init__Úadd_maxpoolr   r   ÚConv2dÚfc1ÚIdentityÚbnr   ÚactÚfc2Úgate)
ÚselfÚchannelsÚrd_ratioÚrd_channelsÚ
rd_divisorr   r   Ú	act_layerÚ
norm_layerÚ
gate_layer©Ú	__class__© úN/home/ubuntu/.local/lib/python3.10/site-packages/timm/layers/squeeze_excite.pyr      s   zSEModule.__init__c                 C   s`   |j ddd}| jrd| d|jddd  }|  |¡}|  |  |¡¡}|  |¡}||  |¡ S ©N)é   é   T)Úkeepdimg      à?)Úmeanr   Úamaxr   r   r   r   r   ©r   ÚxÚx_ser&   r&   r'   Úforward)   s   

zSEModule.forward©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚReLUr   r1   Ú__classcell__r&   r&   r$   r'   r      s    	
þr   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚEffectiveSEModulezŠ 'Effective Squeeze-Excitation
    From `CenterMask : Real-Time Anchor-Free Instance Segmentation` - https://arxiv.org/abs/1911.06667
    FÚhard_sigmoidc                    s6   t t| ƒ ¡  || _tj||ddd| _t|ƒ| _d S )Nr   r   )r   Úpadding)	r   r9   r   r   r   r   Úfcr   r   )r   r   r   r#   Ú_r$   r&   r'   r   ;   s   zEffectiveSEModule.__init__c                 C   sF   |j ddd}| jrd| d|jddd  }|  |¡}||  |¡ S r(   )r,   r   r-   r<   r   r.   r&   r&   r'   r1   A   s
   
zEffectiveSEModule.forward)Fr:   )r3   r4   r5   r6   r   r1   r8   r&   r&   r$   r'   r9   7   s    r9   c                       s8   e Zd ZdZddddejdf‡ fdd„	Zd	d
„ Z‡  ZS )ÚSqueezeExciteClr   r   Nr	   Tr
   c                    s`   t ƒ  ¡  |st|| |dd}tj|||d| _t|dd| _tj|||d| _t|ƒ| _	d S )Nr   r   )r   Tr   )
r   r   r   r   ÚLinearr   r   r   r   r   )r   r   r   r   r    r   r!   r#   r$   r&   r'   r   V   s   
zSqueezeExciteCl.__init__c                 C   s:   |j ddd}|  |¡}|  |¡}|  |¡}||  |¡ S )N)r   r)   T)Úkeepdims)r,   r   r   r   r   r.   r&   r&   r'   r1   a   s
   


zSqueezeExciteCl.forwardr2   r&   r&   r$   r'   r>   M   s    	þr>   N)r6   Útorchr   Ú
create_actr   Úhelpersr   ÚModuler   ÚSqueezeExciter9   ÚEffectiveSqueezeExciter>   r&   r&   r&   r'   Ú<module>   s    !