
    khg"              	          d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ dd	lmZ  G d
 dej$                        Z G d dej$                        ZddZddZ e ed       ed       e        ed       e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y)a   Selective Kernel Networks (ResNet base)

Paper: Selective Kernel Networks (https://arxiv.org/abs/1903.06586)

This was inspired by reading 'Compounding the Performance Improvements...' (https://arxiv.org/abs/2001.06268)
and a streamlined impl at https://github.com/clovaai/assembled-cnn but I ended up building something closer
to the original paper with some modifications of my own to better balance param count vs accuracy.

Hacked together by / Copyright 2020 Ross Wightman
    N)nnIMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STD)SelectiveKernelConvNormActcreate_attn   )build_model_with_cfg)register_modelgenerate_default_cfgs)ResNetc                   t     e Zd ZdZddddddddej
                  ej                  ddddf fd	Zd Zd Z	 xZ
S )SelectiveKernelBasicr
   N@   c                 l   t         t        |           |xs i }t        ||      }|dk(  sJ d       |dk(  sJ d       ||z  }|| j                  z  }|
xs |	}
t        ||f||
||d||| _        t        ||fd|	dd	|| _        t        ||      | _
         |d
      | _        || _        || _        y )N	act_layer
norm_layerr
   z)BasicBlock only supports cardinality of 1r   z0BasicBlock doest not support changing base width)stridedilationaa_layer
drop_layer   F)kernel_sizer   	apply_actTinplace)superr   __init__dict	expansionr   conv1r   conv2r	   seact
downsample	drop_path)selfinplanesplanesr   r'   cardinality
base_width	sk_kwargsreduce_firstr   first_dilationr   r   
attn_layerr   
drop_blockr(   conv_kwargsfirst_planes	outplanes	__class__s                       M/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/models/sknet.pyr    zSelectiveKernelBasic.__init__   s    & 	"D24O	Y:FaL!LLRS!SS-T^^+	'38$lR+1N*R8CRGPR
 !)g12XQVgZeg
j)4T*$"    c                     t        | j                  j                  dd       >t        j                  j                  | j                  j                  j                         y y Nweight)getattrr$   bnr   initzeros_r;   r)   s    r7   zero_init_lastz#SelectiveKernelBasic.zero_init_last@   <    4::==(D1=GGNN4::==//0 >r8   c                 (   |}| j                  |      }| j                  |      }| j                  | j                  |      }| j                  | j                  |      }| j                  | j	                  |      }||z  }| j                  |      }|S N)r#   r$   r%   r(   r'   r&   r)   xshortcuts      r7   forwardzSelectiveKernelBasic.forwardD   s    JJqMJJqM77
A>>%q!A??&x0H	XHHQKr8   __name__
__module____qualname__r"   r   ReLUBatchNorm2dr    rA   rH   __classcell__r6   s   @r7   r   r      sL    I gg~~#%#N1r8   r   c                   t     e Zd ZdZddddddddej
                  ej                  ddddf fd	Zd Zd Z	 xZ
S )SelectiveKernelBottleneck   r
   Nr   c           	         t         t        |           |xs i }t        ||      }t	        t        j                  ||dz  z        |z        }||z  }|| j                  z  }|
xs |	}
t        ||fddi|| _	        t        ||f||
|||d||| _        t        ||fddd|| _        t        ||      | _         |d	      | _        || _        || _        y )
Nr   r   r   r
   )r   r   groupsr   r   F)r   r   Tr   )r   rR   r    r!   intmathfloorr"   r   r#   r   r$   conv3r	   r%   r&   r'   r(   )r)   r*   r+   r   r'   r,   r-   r.   r/   r   r0   r   r   r1   r   r2   r(   r3   widthr4   r5   r6   s                        r7   r    z"SelectiveKernelBottleneck.__init__V   s    & 	'79O	Y:FDJJvb9:[HI,T^^+	'38 <VQV+V
$%R(.P[*R8CRGPR
 !	aqEaU`a
j)4T*$"r8   c                     t        | j                  j                  dd       >t        j                  j                  | j                  j                  j                         y y r:   )r<   rY   r=   r   r>   r?   r;   r@   s    r7   rA   z(SelectiveKernelBottleneck.zero_init_last|   rB   r8   c                 J   |}| j                  |      }| j                  |      }| j                  |      }| j                  | j                  |      }| j                  | j	                  |      }| j
                  | j                  |      }||z  }| j                  |      }|S rD   )r#   r$   rY   r%   r(   r'   r&   rE   s      r7   rH   z!SelectiveKernelBottleneck.forward   s    JJqMJJqMJJqM77
A>>%q!A??&x0H	XHHQKr8   rI   rP   s   @r7   rR   rR   S   sL    I gg~~#$#L1r8   rR   c                 &    t        t        | |fi |S rD   )r   r   )variant
pretrainedkwargss      r7   _create_skresnetra      s"     	 r8   c                 0    | dddddt         t        ddd
|S )	Ni  )r      rc   )   rd   g      ?bicubicr#   fc)
urlnum_classes
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierr   )rg   r`   s     r7   _cfgrq      s0    =vI%.BT  r8   ztimm/)	hf_hub_idzconv1.0)ro   )zskresnet18.ra_in1kzskresnet34.ra_in1kzskresnet50.untrainedzskresnet50d.untrainedzskresnext50_32x4d.ra_in1kreturnc           	          t        ddd      }t        t        g dt        |      d      }t        d	| fi t        |fi |S )
zConstructs a Selective Kernel ResNet-18 model.

    Different from configs in Select Kernel paper or "Compounding the Performance Improvements..." this
    variation splits the input channels to the selective convolutions to keep param count down.
          ?   Trd_ratio
rd_divisorsplit_input)   r{   r{   r{   r.   Fblocklayers
block_argsrA   
skresnet18r!   r   ra   r_   r`   r.   
model_argss       r7   r   r      J     eEI"<DS\D]J L*SZ8R68RSSr8   c           	          t        ddd      }t        t        g dt        |      d      }t        d	| fi t        |fi |S )
zConstructs a Selective Kernel ResNet-34 model.

    Different from configs in Select Kernel paper or "Compounding the Performance Improvements..." this
    variation splits the input channels to the selective convolutions to keep param count down.
    ru   rv   Trw   r   rS      r   r|   Fr}   
skresnet34r   r   s       r7   r   r      r   r8   c           	          t        d      }t        t        g dt        |      d      }t        d| fi t        |fi |S )zConstructs a Select Kernel ResNet-50 model.

    Different from configs in Select Kernel paper or "Compounding the Performance Improvements..." this
    variation splits the input channels to the selective convolutions to keep param count down.
    Trz   r   r|   Fr}   
skresnet50r!   rR   ra   r   s       r7   r   r      sF     &I'XaIbJ L*SZ8R68RSSr8   c           
          t        d      }t        t        g ddddt        |      d      }t        d	| fi t        |fi |S )
zConstructs a Select Kernel ResNet-50-D model.

    Different from configs in Select Kernel paper or "Compounding the Performance Improvements..." this
    variation splits the input channels to the selective convolutions to keep param count down.
    Tr   r       deepr|   F)r~   r   
stem_width	stem_typeavg_downr   rA   skresnet50dr   r   s       r7   r   r      sN     &I'W]hl),UDJ M:Tj9SF9STTr8   c           	          t        ddd      }t        t        g dddt        |      d      }t        d	| fi t        |fi |S )
zConstructs a Select Kernel ResNeXt50-32x4d model. This should be equivalent to
    the SKNet-50 model in the Select Kernel Paper
    g      ?r   Frw   r   rS   r|   )r~   r   r,   r-   r   rA   skresnext50_32x4dr   r   s       r7   r   r      sQ    
 druEI'"YZ),UDJ /ZtJ?YRX?YZZr8   )F) )__doc__rW   torchr   	timm.datar   r   timm.layersr   r   r	   _builderr   	_registryr   r   resnetr   Moduler   rR   ra   rq   default_cfgsr   r   r   r   r    r8   r7   <module>r      s  	   A A A * < :299 :z:		 :z %11 F!!%!8&  
Tf 
T 
T 
Tf 
T 
T 
Tf 
T 
T 
Uv 
U 
U [V [ [r8   