
    kh                         d Z ddlmZ ddlmZ ddlmZ  G d dej                        ZeZ	 G d d	ej                        Z
e
Z G d
 dej                        Zy)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                   N     e Zd ZdZdddddej
                  ddf fd	Zd	 Z x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
                 `   t         t        |           || _        |st	        ||z  |d      }t        j                  ||d|      | _        |r ||      nt        j                         | _	        t        |d      | _        t        j                  ||d|      | _        t        |	      | _        y )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__s             V/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/layers/squeeze_excite.pyr   zSEModule.__init__   s     	h&(&(H)<jVXYK99X{M-7*[)R[[]#It<99[(M$Z0	    c                 $   |j                  dd      }| j                  rd|z  d|j                  dd      z  z   }| j                  |      }| j	                  | j                  |            }| j                  |      }|| j                  |      z  S N)      T)keepdimg      ?)meanr   amaxr   r   r   r   r   r   xx_ses      r(   forwardzSEModule.forward)   s    vvfdv+:affVTf&B BBDxx~xx&xx~499T?""r)   	__name__
__module____qualname____doc__r   ReLUr   r4   __classcell__r'   s   @r(   r   r      s+     &-$1Z_Ti1#r)   r   c                   *     e Zd ZdZd fd	Zd Z xZS )EffectiveSEModulez 'Effective Squeeze-Excitation
    From `CenterMask : Real-Time Anchor-Free Instance Segmentation` - https://arxiv.org/abs/1911.06667
    c                     t         t        |           || _        t	        j
                  ||dd      | _        t        |      | _        y )Nr   r   )r   padding)	r   r>   r   r   r   r   fcr   r   )r   r    r   r&   _r'   s        r(   r   zEffectiveSEModule.__init__;   s;    /1&))HhAqI$Z0	r)   c                     |j                  dd      }| j                  rd|z  d|j                  dd      z  z   }| j                  |      }|| j	                  |      z  S r+   )r/   r   r0   rA   r   r1   s      r(   r4   zEffectiveSEModule.forwardA   s]    vvfdv+:affVTf&B BBDwwt}499T?""r)   )Fhard_sigmoid)r6   r7   r8   r9   r   r4   r;   r<   s   @r(   r>   r>   7   s    1#r)   r>   c                   J     e Zd ZdZddddej
                  df fd	Zd Z xZS )	SqueezeExciteClr	   r
   Nr   Tr   c                     t         |           |st        ||z  |d      }t        j                  |||      | _        t        |d      | _        t        j                  |||      | _        t        |      | _	        y )Nr   r   )r   Tr   )
r   r   r   r   Linearr   r   r   r   r   )	r   r    r!   r"   r#   r   r$   r&   r'   s	           r(   r   zSqueezeExciteCl.__init__V   si     	(H)<jVXYK99X{>#It<99[(>$Z0	r)   c                     |j                  dd      }| j                  |      }| j                  |      }| j                  |      }|| j	                  |      z  S )N)r   r,   T)keepdims)r/   r   r   r   r   r1   s      r(   r4   zSqueezeExciteCl.forwarda   sN    vvftv,xx~xx~xx~499T?""r)   r5   r<   s   @r(   rF   rF   M   s&     &-$1Y	1#r)   rF   N)r9   torchr   
create_actr   helpersr   Moduler   SqueezeExciter>   EffectiveSqueezeExciterF    r)   r(   <module>rR      sU     ( ##ryy #B #		 #& + #bii #r)   