
    ^j                         d Z ddlmZmZmZmZ ddlZddlmZ ddl	mc m
Z ddlmZmZ ddlmZ ddlmZ dd	lmZ  G d
 dej(                        Zy)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                       e Zd ZdZddddddddej
                  ej                  dddfd	ed
ee	eef      de
dede
dedee   dede
deej                     deej                     deeeej                     f   f fdZd Z xZS )GatherExcitez$ 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                 h   ||d}t         |           |	| _        t        |
      }
|| _        |r]t        j                         | _        |dk(  rj|J d       | j                  j                  dt        ||g||ddd       |r^| j                  j                  dt        j                  |g|        n,|d	z  dk(  sJ t        t        j                  |            }t        |      D ]  }| j                  j                  d
|dz    t        ||g|dd	dd       |r6| j                  j                  d|dz    t        j                  |g|        ||dz
  k7  sv| j                  j                  d|dz     |
d              nWd | _        | j                  dk(  rd| _        d| _        n2|d	z  dk(  sJ | j                  d	z  dz
  | _        | j                  | _        |st#        ||z  |d      }|rt%        ||g|d|
int        j&                         | _        t+        |      | _        y )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                     d/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/layers/gather_excite.pyr-   zGatherExcite.__init__   s!   " /&!),	--/DK{ ,r.rr,&&]8Xtqst9]^jntvKK**UR^^H5Rr5RSzQ&tyy01x WAKK**q1ug%hgdfgaPQ]agi "..a!eW~r~~h?\Y[?\]HqL(..QUG}iPT>UVW DK{{azQ&++/A-++(H)<jVXYKOV78[KKIK\^\g\g\i$Z0	    c           	         |j                   dd  }| j                  | j                  |      }n| j                  dk(  r<|j                  dd      }| j                  rd|z  d|j                  dd      z  z   }nt        j                  || j                  | j                  | j                  dz  d	
      }| j                  rCd|z  dt        j                  || j                  | j                  | j                  dz        z  z   }| j                  |      }|j                   d   dk7  s|j                   d   dk7  rt        j                  ||      }|| j                  |      z  S )Nr   )r%   r'   T)dimkeepdimsg      ?)keepdimr%   F)r!   r"   paddingcount_include_pad)r!   r"   rG   r   )size)shaper/   r   meanr   amaxF
avg_pool2dr6   r7   
max_pool2dr9   interpolater:   )r;   xrJ   x_ges       r@   forwardzGatherExcite.forwardU   s,   wwrs|;;";;q>D{{avv&4v8##:affVTf.J(JJD||477477DGGqLdik##:all1$''Z^ZaZakokrkrvwkw.x(xxDxx~::b>Q$**R.A"5==D1D499T?""rA   )__name__
__module____qualname____doc__r   ReLUr1   r2   r   r   boolfloatr   Moduler   strr-   rT   __classcell__)r?   s   @r@   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1p#rA   r   )rX   typingr   r   r   r   r3   torchr   torch.nn.functional
functionalrN   
create_actr	   r
   r   helpersr   r9   r   r\   r    rA   r@   <module>rf      s<    0 /     7 ( # O#299 O#rA   