
    RЦiX                         S r SSKJrJrJrJr  SSKJr  SSKJ	r	  SSK
Jr   " S S\R                  5      r\r " S	 S
\R                  5      r\r " S S\R                  5      rg)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
    )OptionalTupleTypeUnion)nn   )create_act_layer)make_divisiblec                      ^  \ rS rSrSrSSSSS\R                  SSSS4
S	\S
\S\	\   S\S\
S\
S\\R                     S\	\\R                        S\\\\R                     4   4U 4S jjjrS rSrU =r$ )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sigmoidchannelsrd_ratiord_channels
rd_divisoradd_maxpoolbias	act_layer
norm_layer
gate_layerc                 l  > XS.n[         TU ]  5         XPl        U(       d  [        X-  USS9n[        R
                  " X4SUS.UD6U l        U(       a	  U" U40 UD6O[        R                  " 5       U l        [        USS9U l
        [        R
                  " X14SUS.UD6U l        [        U	5      U l        g )Ndevicedtype        round_limitr   )kernel_sizer   Tinplace)super__init__r   r
   r   Conv2dfc1Identitybnr	   actfc2gate)selfr   r   r   r   r   r   r   r   r   r   r   dd	__class__s                Y/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/squeeze_excite.pyr&   SEModule.__init__   s     /&()<jVXYK99XSSPRS3=*[/B/2;;=#It<99[SSPRS$Z0	    c                     UR                  SSS9nU R                  (       a  SU-  SUR                  SSS9-  -   nU R                  U5      nU R	                  U R                  U5      5      nU R                  U5      nXR                  U5      -  $ N)      T)keepdimg      ?)meanr   amaxr(   r+   r*   r,   r-   r.   xx_ses      r1   forwardSEModule.forward7   s~    vvfdv+:affVTf&B BBDxx~xx&xx~99T?""r3   )r+   r   r*   r(   r,   r-   __name__
__module____qualname____firstlineno____doc__r   ReLUintfloatr   boolr   Moduler   strr&   r>   __static_attributes____classcell__r0   s   @r1   r   r      s     &)- %)+486?11 1 "#	1
 1 1 1 BII1 !bii11 c4		?231 12# #r3   r   c            
       r   ^  \ rS rSrSr    S	S\S\S\\\	\
R                     4   4U 4S jjjrS rSrU =r$ )
EffectiveSEModuleE   z'Effective Squeeze-Excitation
From `CenterMask : Real-Time Anchor-Free Instance Segmentation` - https://arxiv.org/abs/1911.06667
r   r   r   c           	         > XES.n[         TU ]  5         X l        [        R                  " XSSXES9U l        [        U5      U l        g )Nr   r   r   )r"   paddingr   r   )r%   r&   r   r   r'   fcr	   r-   )	r.   r   r   r   r   r   _r/   r0   s	           r1   r&   EffectiveSEModule.__init__I   sA     /&))HAqQWe$Z0	r3   c                     UR                  SSS9nU R                  (       a  SU-  SUR                  SSS9-  -   nU R                  U5      nXR	                  U5      -  $ r5   )r9   r   r:   rT   r-   r;   s      r1   r>   EffectiveSEModule.forwardX   s[    vvfdv+:affVTf&B BBDwwt}99T?""r3   )r   rT   r-   )Fhard_sigmoidNN)rA   rB   rC   rD   rE   rG   rI   r   rK   r   r   rJ   r&   r>   rL   rM   rN   s   @r1   rP   rP   E   sX     !&6D11 1 c4		?23	1 1# #r3   rP   c                      ^  \ rS rSrSrSSSS\R                  SSS4S\S	\S
\	\   S\S\
S\\R                     S\\\\R                     4   4U 4S jjjrS rSrU =r$ )SqueezeExciteCld   r   r   Nr   Tr   r   r   r   r   r   r   r   c
                   > XS.n
[         TU ]  5         U(       d  [        X-  USS9n[        R                  " X4SU0U
D6U l        [        USS9U l        [        R                  " X14SU0U
D6U l        [        U5      U l	        g )Nr   r   r    r   Tr#   )
r%   r&   r
   r   Linearr(   r	   r+   r,   r-   )r.   r   r   r   r   r   r   r   r   r   r/   r0   s              r1   r&   SqueezeExciteCl.__init__m   s{     /()<jVXYK99XDDD#It<99[DDD$Z0	r3   c                     UR                  SSS9nU R                  U5      nU R                  U5      nU R                  U5      nXR	                  U5      -  $ )N)r   r6   T)keepdims)r9   r(   r+   r,   r-   r;   s      r1   r>   SqueezeExciteCl.forward   sL    vvftv,xx~xx~xx~99T?""r3   )r+   r(   r,   r-   r@   rN   s   @r1   r[   r[   d   s     &)-)+6?11 1 "#	1
 1 1 BII1 c4		?231 1*# #r3   r[   N)rE   typingr   r   r   r   torchr   
create_actr	   helpersr
   rJ   r   SqueezeExciterP   EffectiveSqueezeExciter[    r3   r1   <module>rj      s[    0 /  ( #*#ryy *#Z #		 #8 + ##bii ##r3   