o
    پi                     @   st   d Z ddlZddlmZ ddlm  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
 d
ejZdS )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
    N)nn   )create_act_layerget_act_layer)create_conv2d)make_divisible)ConvMlpc                       sD   e Zd ZdZddddddddejejdf fd	d
	Zdd Z  Z	S )GatherExcitez$ Gather-Excite Attention Module
    NFr   Tg      ?r   sigmoidc                    s  t t|   |	| _t|
}
|| _|rt | _|dkr?|d us$J d| j	dt
|||ddd |r>| j	dt| ns|d dksGJ tt|}t|D ];}| j	d	|d  t
||d
ddd |rx| j	d|d  t| ||d kr| j	d|d  |
dd qRn#d | _| jdkrd| _d| _n|d dksJ | jd d | _| j| _|st|| |dd}|rt|||
dnt | _t|| _d S )Nr   zBspatial feature size must be specified for global extent w/ paramsconv1r   T)kernel_sizestride	depthwisenorm1   conv   normact)inplaceg        )round_limit)	act_layer)superr	   __init__add_maxpoolr   extentr   
Sequentialgather
add_moduler   BatchNorm2dintmathlog2rangegkgsr   r   Identitymlpr   gate)selfchannels	feat_sizeextra_paramsr   use_mlprd_ratiord_channels
rd_divisorr   r   
norm_layer
gate_layernum_convi	__class__ M/home/ubuntu/.local/lib/python3.10/site-packages/timm/layers/gather_excite.pyr      sL   
	
zGatherExcite.__init__c                 C   s   |j dd  }| jd ur| |}nC| jdkr/|jddd}| jr.d| d|jddd  }n&tj|| j| j	| jd d	d
}| jrUd| dtj
|| j| j	| jd d  }| |}|j d dksh|j d dkrotj||d}|| | S )Nr   )r   r   T)dimkeepdimsg      ?)keepdimr   F)r   r   paddingcount_include_pad)r   r   r=   r   )size)shaper   r   meanr   amaxF
avg_pool2dr$   r%   
max_pool2dr'   interpolater(   )r)   xr@   x_ger7   r7   r8   forwardF   s"   

(
zGatherExcite.forward)
__name__
__module____qualname____doc__r   ReLUr   r   rJ   __classcell__r7   r7   r5   r8   r	      s    
*r	   )rN   r!   torchr   torch.nn.functional
functionalrD   
create_actr   r   r   helpersr   r'   r   Moduler	   r7   r7   r7   r8   <module>   s    