
    Ng                        d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZ g Z G d d	ej                  ZddZddZ e ed           ed           ed           ed           ed           ed           ed           edd           edd          d	          Zeddefd            Zeddefd            Zeddefd            Zeddefd            Zeddefd            Zeddefd            Zeddefd            Zeddefd            Zeddefd            ZdS )z Res2Net and Res2NeXt
Adapted from Official Pytorch impl at: https://github.com/gasvn/Res2Net/
Paper: `Res2Net: A New Multi-scale Backbone Architecture` - https://arxiv.org/abs/1904.01169
    NIMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STD   )build_model_with_cfg)register_modelgenerate_default_cfgs)ResNetc            
       R     e Zd ZdZdZdddddddej        ddf
 fd	Zd Zd Z	 xZ
S )	Bottle2neckzj Res2Net/Res2NeXT Bottleneck
    Adapted from https://github.com/gasvn/Res2Net/blob/master/res2net.py
       r   N   c                    t          t          |                                            || _        |dk    p|d u| _        t          d|dz
            | _        t          t          j	        ||dz  z                      |z  }|| _
        || j        z  }|	p|}	t          j        |||z  dd          | _         |||z            | _        g }g }t!          | j                  D ]O}|                    t          j        ||d||	|	|d                     |                     ||                     Pt          j        |          | _        t          j        |          | _        | j        rt          j        d|d          | _        nd | _        t          j        ||z  |dd          | _         ||          | _        | ||          nd | _         |
d	          | _        || _        d S )
Nr   g      P@F)kernel_sizebias   )r   stridepaddingdilationgroupsr   )r   r   r   T)inplace)superr   __init__scaleis_firstmax
num_scalesintmathfloorwidth	expansionnnConv2dconv1bn1rangeappend
ModuleListconvsbns	AvgPool2dpoolconv3bn3serelu
downsample)selfinplanesplanesr   r2   cardinality
base_widthr   r   first_dilation	act_layer
norm_layer
attn_layer_r!   	outplanesr*   r+   i	__class__s                      O/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/models/res2net.pyr   zBottle2neck.__init__   s     	k4  ))+++

<j&<a++DJvd):;<<==K
T^+	'38YxAERRR
:eem,,t'' 	* 	*ALLu!FN'%I I I J J J JJzz%(())))]5))
=%%= 	61MMMDIIDIYuu}iQUSSS
:i((+5+A**Y'''tId+++	$    c                     t          | j        dd           +t          j                            | j        j                   d S d S )Nweight)getattrr/   r#   initzeros_rC   )r3   s    r@   zero_init_lastzBottle2neck.zero_init_lastJ   s<    48Xt,,8GNN48?+++++ 98rA   c                    |}|                      |          }|                     |          }|                     |          }t          j        || j        d          }g }|d         }t          t          | j        | j	                            D ]i\  }\  }}	|dk    s| j
        r	||         }n|||         z   } ||          } |	|          }|                     |          }|                    |           j| j        dk    rQ| j        /|                    |                     |d                              n|                    |d                    t          j        |d          }|                     |          }|                     |          }| j        |                     |          }| j        |                     |          }||z  }|                     |          }|S )Nr   r   )r%   r&   r1   torchsplitr!   	enumeratezipr*   r+   r   r(   r   r-   catr.   r/   r0   r2   )
r3   xshortcutoutspxspospr>   convbns
             r@   forwardzBottle2neck.forwardN   s   jjmmhhsmmiinnk#tz1--V&s4:tx'@'@AA 	 	MAzbAvvvV#a&[bBBB2BJJrNNNN:>>y$

499SW--....

3r7###iQjjoohhsmm7''#,,C?&q))Hxiinn
rA   )__name__
__module____qualname____doc__r"   r#   ReLUr   rG   rW   __classcell__)r?   s   @r@   r   r      s          I g0% 0% 0% 0% 0% 0%d, , ,& & & & & & &rA   r   Fc                 *    t          t          | |fi |S )N)r   r
   )variant
pretrainedkwargss      r@   _create_res2netrb   w   s    FFvFFFrA    c                 4    | dddddt           t          ddd
|S )	Ni  )r      re   )   rf   g      ?bilinearr%   fc)
urlnum_classes
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierr   )ri   ra   s     r@   _cfgrs   {   s5    =vJ%.BT   rA   ztimm/)	hf_hub_idzconv1.0)rt   rq   )	zres2net50_26w_4s.in1kzres2net50_48w_2s.in1kzres2net50_14w_8s.in1kzres2net50_26w_6s.in1kzres2net50_26w_8s.in1kzres2net101_26w_4s.in1kzres2next50.in1kzres2net50d.in1kzres2net101d.in1kreturnc           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z)Constructs a Res2Net-50 26w4s model.
    r   r      r   r   r   r   blocklayersr7   
block_argsres2net50_26w_4sdictr   rb   r`   ra   
model_argss      r@   r~   r~      [     ,,,2$UV---Y Y YJ-zXXT*=W=WPV=W=WXXXrA   c           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z*Constructs a Res2Net-101 26w4s model.
    r   r      r   r   r   ry   rz   res2net101_26w_4sr   r   s      r@   r   r      s[     ---B4VW===Z Z ZJ.
YYd:>X>XQW>X>XYYYrA   c           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z)Constructs a Res2Net-50 26w6s model.
    rw   r   rx   ry   rz   res2net50_26w_6sr   r   s      r@   r   r      r   rA   c           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z)Constructs a Res2Net-50 26w8s model.
    rw   r      ry   rz   res2net50_26w_8sr   r   s      r@   r   r      r   rA   c           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z)Constructs a Res2Net-50 48w2s model.
    rw   0      ry   rz   res2net50_48w_2sr   r   s      r@   r   r      r   rA   c           	          t          t          g ddt          d                    }t          d| fi t          |fi |S )z)Constructs a Res2Net-50 14w8s model.
    rw      r   ry   rz   res2net50_14w_8sr   r   s      r@   r   r      r   rA   c           	          t          t          g dddt          d                    }t          d| fi t          |fi |S )zConstruct Res2NeXt-50 4s
    rw   r   r   ry   )r{   r|   r7   r6   r}   
res2next50r   r   s      r@   r   r      s_     ,,,1!X\cdXeXeXeg g gJ<RRtJ7Q7Q&7Q7QRRRrA   c                     t          t          g dddddt          d                    }t          d	| fi t          |fi |S )
Construct Res2Net-50
    rw   r   deepT    r   ry   r{   r|   r7   	stem_typeavg_down
stem_widthr}   
res2net50dr   r   s      r@   r   r      sa     ,,,2"A@ @ @J <RRtJ7Q7Q&7Q7QRRRrA   c                     t          t          g dddddt          d                    }t          d	| fi t          |fi |S )
r   r   r   r   Tr   r   ry   r   res2net101dr   r   s      r@   r   r      sa     ---B&"A@ @ @J =*SSZ8R8R68R8RSSSrA   )F)rc   )r[   r   rJ   torch.nnr#   	timm.datar   r   _builderr   	_registryr   r	   resnetr
   __all__Moduler   rb   rs   default_cfgsr~   r   r   r   r   r   r   r   r    rA   r@   <module>r      so            A A A A A A A A * * * * * * < < < < < < < <      
b b b b b") b b bJG G G G    %$!TG444!TG444!TG444!TG444!TG444"dW555tg...tg)DDDw9EEE
& 
& 
 
 Y YF Y Y Y Y Z ZV Z Z Z Z Y YF Y Y Y Y Y YF Y Y Y Y Y YF Y Y Y Y Y YF Y Y Y Y S Sf S S S S S Sf S S S S T Tv T T T T T TrA   