
    RЦi                         S r SSKJrJrJrJr  SSKrSSKJr  SSK	Js  J
r  SSKJrJr  SSKJr  SSKJr  SS	KJr   " S
 S\R(                  5      rg)a  Gather-Excite Attention Block

Paper: `Gather-Excite: Exploiting Feature Context in CNNs` - https://arxiv.org/abs/1810.12348

Official code here, but it's only partial impl in Caffe: https://github.com/hujie-frank/GENet

I've tried to support all of the extent both w/ and w/o params. I don't believe I've seen another
impl that covers all of the cases.

NOTE: extent=0 + extra_params=False is equivalent to Squeeze-and-Excitation

Hacked together by / Copyright 2021 Ross Wightman
    )OptionalTupleTypeUnionN)nn   )create_act_layerget_act_layer)create_conv2d)make_divisible)ConvMlpc                     ^  \ rS rSrSrSSSSSSSS\R                  \R                  S	SS4S
\S\	\
\\4      S\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$ )GatherExcite   z#Gather-Excite Attention Module
    NFr   Tg      ?r   sigmoidchannels	feat_sizeextra_paramsextentuse_mlprd_ratiord_channels
rd_divisoradd_maxpool	act_layer
norm_layer
gate_layerc                   > XS.n[         TU ]  5         Xl        [        U
5      n
X@l        U(       Gal  [
        R                  " 5       U l        US:X  ao  Uc   S5       eU R                  R                  S[        X/UQ7USSS.65        U(       a1  U R                  R                  S[
        R                  " U/UQ76 5        GO6US	-  S:X  d   e[        [        R                  " U5      5      n[        U5       H  nU R                  R                  S
US-    3[        X/UQ7SS	SS.65        U(       a7  U R                  R                  SUS-    3[
        R                  " U/UQ76 5        UUS-
  :w  d  M}  U R                  R                  SUS-    3U
" SS95        M     OYS U l        U R                  S:X  a  SU l        SU l        O3US	-  S:X  d   eU R                  S	-  S-
  U l        U R                  U l        U(       d  [#        X-  USS9nU(       a  [%        X/UQ7SU
06O[
        R&                  " 5       U l        [+        U5      U l        g )N)devicedtyper   zBspatial feature size must be specified for global extent w/ paramsconv1r   T)kernel_sizestride	depthwisenorm1   conv   normact)inplaceg        )round_limitr   )super__init__r   r
   r   r   
Sequentialgather
add_moduler   BatchNorm2dintmathlog2rangegkgsr   r   Identitymlpr	   gate)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r    ddnum_convi	__class__s                     X/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/gather_excite.pyr.   GatherExcite.__init__   s   " /&!),	--/DK{ ,r.rr,&&]8tqst9]^jntvKK**UR^^H5Rr5RSzQ&tyy01xAKK**q1ug%hgdfgaPQ]agi "..a!eW~r~~h?\Y[?\]HqL(..QUG}iPT>UV ) DK{{azQ&++/A-++()<jVXYKOV78KKIK\^\g\g\i$Z0	    c           	         UR                   SS  nU R                  b  U R                  U5      nOU R                  S:X  a;  UR                  SSS9nU R                  (       a  SU-  SUR                  SSS9-  -   nO[        R                  " XR                  U R                  U R                  S-  S	S
9nU R                  (       a@  SU-  S[        R                  " XR                  U R                  U R                  S-  S9-  -   nU R                  U5      nUR                   S   S:w  d  UR                   S   S:w  a  [        R                  " X2S9nXR                  U5      -  $ )Nr   )r&   r(   T)dimkeepdimsg      ?)keepdimr&   F)r"   r#   paddingcount_include_pad)r"   r#   rI   r   )size)shaper0   r   meanr   amaxF
avg_pool2dr7   r8   
max_pool2dr:   interpolater;   )r<   xrL   x_ges       rA   forwardGatherExcite.forwardU   s%   wwrs|;;";;q>D{{avv&4v8##:affVTf.J(JJD||77477DGGqLdik##:all1''Z^ZaZakokrkrvwkw.x(xxDxx~::b>Q$**R.A"5==1D99T?""rC   )r   r   r;   r0   r7   r8   r:   )__name__
__module____qualname____firstlineno____doc__r   ReLUr2   r3   r   r   boolfloatr   Moduler   strr.   rV   __static_attributes____classcell__)r@   s   @rA   r   r      s    
 48!& #)- %)+*,..6?6161  c3h061 	61
 61 61 61 "#61 61 61 BII61 RYY61 c4		?2361 61p# #rC   r   )r\   typingr   r   r   r   r4   torchr   torch.nn.functional
functionalrP   
create_actr	   r
   r   helpersr   r:   r   r`   r    rC   rA   <module>rk      s<    0 /     7 ( # O#299 O#rC   