a
    þdç  ã                   @   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| _|rJ||ƒnt ¡ | _	t
|dd| _tj||d|d| _t
|	ƒ| _d S )Nç        ©Zround_limitr   )Úkernel_sizeÚbiasT©Zinplace)Úsuperr   Ú__init__Úadd_maxpoolr   r   ÚConv2dÚfc1ZIdentityÚbnr   ÚactÚfc2Úgate)
ÚselfÚchannelsÚrd_ratioÚrd_channelsÚ
rd_divisorr   r   Ú	act_layerZ
norm_layerÚ
gate_layer©Ú	__class__© úc/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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)Zkeepdimg      à?)Úmeanr   Úamaxr   r   r   r   r   ©r   ÚxZx_ser"   r"   r#   Úforward)   s    

zSEModule.forward©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZReLUr   r+   Ú__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   r2   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(   r5   r   r)   r"   r"   r#   r+   A   s
    
zEffectiveSEModule.forward)Fr3   )r-   r.   r/   r0   r   r+   r1   r"   r"   r    r#   r2   7   s   r2   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   Z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)Zkeepdims)r'   r   r   r   r   r)   r"   r"   r#   r+   a   s
    


zSqueezeExciteCl.forwardr,   r"   r"   r    r#   r7   M   s
   	þr7   N)r0   Ztorchr   Z
create_actr   Zhelpersr   ÚModuler   ZSqueezeExciter2   ZEffectiveSqueezeExciter7   r"   r"   r"   r#   Ú<module>   s   !