o
    پi	                     @   sl   d 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   Global Context Attention Block

Paper: `GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond`
    - https://arxiv.org/abs/1904.11492

Official code consulted as reference: https://github.com/xvjiarui/GCNet

Hacked together by / Copyright 2021 Ross Wightman
    )nnN   )create_act_layerget_act_layer)make_divisible)ConvMlp)LayerNorm2dc                	       sB   e Zd Zdddddddejdf	 fdd	Zd	d
 Zdd Z  ZS )GlobalContextTFg      ?Nr   sigmoidc                    s   t t|   t|	}	|rtj|ddddnd | _|d u r&t|| |dd}|r2t|||	t	d| _
nd | _
|rAt|||	t	d| _nd | _t|
| _|| _|   d S )Nr   T)kernel_sizebiasg        )round_limit)	act_layer
norm_layer)superr	   __init__r   r   Conv2d	conv_attnr   r   r   mlp_add	mlp_scaler   gateinit_last_zeroreset_parameters)selfchannelsuse_attnfuse_add
fuse_scaler   rd_ratiord_channels
rd_divisorr   
gate_layer	__class__ N/home/ubuntu/.local/lib/python3.10/site-packages/timm/layers/global_context.pyr      s   
zGlobalContext.__init__c                 C   sD   | j d urtjj| j jddd | jd ur tj| jjj d S d S )Nfan_inrelu)modenonlinearity)r   r   initkaiming_normal_weightr   zeros_fc2)r   r$   r$   r%   r   +   s
   

zGlobalContext.reset_parametersc           	      C   s   |j \}}}}| jd ur9| ||d|| }tj|ddd}||||| d| }|||dd}n|jddd}| jd urQ| |}|| 	| }| j
d ur_| 
|}|| }|S )Nr   )dim   )   r1   T)r0   keepdim)shaper   reshapeFsoftmax	unsqueezeviewmeanr   r   r   )	r   xBCHWattncontextmlp_xr$   r$   r%   forward1   s   




zGlobalContext.forward)	__name__
__module____qualname__r   ReLUr   r   rC   __classcell__r$   r$   r"   r%   r	      s    r	   )__doc__torchr   torch.nn.functional
functionalr6   
create_actr   r   helpersr   mlpr   normr   Moduler	   r$   r$   r$   r%   <module>   s    	