
    ^j+&              	          d Z ddl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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)OptionalType)nnIMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STD)SelectiveKernelConvNormActcreate_attn   )build_model_with_cfg)register_modelgenerate_default_cfgs)ResNetc            "           e Zd ZdZddddddddej
                  ej                  ddddddfdedededeej                     ded	ed
ee
   dededee   deej                     deej                     deeej                        deeej                        deej                     deej                     f  fdZd Zd Z xZS )SelectiveKernelBasicr   N@   inplanesplanesstride
downsamplecardinality
base_width	sk_kwargsreduce_firstdilationfirst_dilation	act_layer
norm_layer
attn_layeraa_layer
drop_block	drop_pathc                 r   ||d}t         |           |xs i }t        d||d|}|dk(  sJ d       |dk(  sJ d       ||z  }|| j                  z  }|
xs |	}
t	        ||f||
||d||| _        t        ||fd|	d	d
|| _        t        ||fi || _	         |d      | _
        || _        || _        y )Ndevicedtyper   r   r   z)BasicBlock only supports cardinality of 1r   z0BasicBlock doest not support changing base width)r   r   r!   
drop_layer   F)kernel_sizer   	apply_actTinplace )super__init__dict	expansionr	   conv1r
   conv2r   seactr   r#   )selfr   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r&   r'   ddconv_kwargsfirst_planes	outplanes	__class__s                          \/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/models/sknet.pyr1   zSelectiveKernelBasic.__init__   s   * /O	LY:LLaL!LLRS!SS-T^^+	'38$	
 #!	
 	
 	

 !
 
 

 j):r:T*$"    c                     t        | j                  j                  dd       >t        j                  j                  | j                  j                  j                         y y Nweight)getattrr5   bnr   initzeros_rB   r8   s    r>   zero_init_lastz#SelectiveKernelBasic.zero_init_lastQ   <    4::==(D1=GGNN4::==//0 >r?   c                 (   |}| j                  |      }| j                  |      }| j                  | j                  |      }| j                  | j                  |      }| j                  | j	                  |      }||z  }| j                  |      }|S N)r4   r5   r6   r#   r   r7   r8   xshortcuts      r>   forwardzSelectiveKernelBasic.forwardU   s    JJqMJJqM77
A>>%q!A??&x0H	XHHQKr?   __name__
__module____qualname__r3   r   ReLUBatchNorm2dintr   Moduler2   r   r1   rH   rO   __classcell__r=   s   @r>   r   r      s=   I .2  (, !,0)+*,..4826.2-1'5#5# 5# 	5#
 !+5# 5# 5#  ~5# 5# 5# %SM5# BII5# RYY5# !bii15# tBII/5#  !+!5#"  		*#5#n1r?   r   c            "           e Zd ZdZddddddddej
                  ej                  ddddddfdedededeej                     d	ed
edee
   dededee   deej                     deej                     deeej                        deeej                        deej                     deej                     f  fdZd Zd Z xZS )SelectiveKernelBottleneck   r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   c           	         ||d}t         |           |xs i }t        d||d|}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        ||fi || _         |d	
      | _        || _        || _        y )Nr%   r(   r   r+   r   )r   r   groupsr!   r)   F)r+   r,   Tr-   r/   )r0   r1   r2   rV   mathfloorr3   r
   r4   r	   r5   conv3r   r6   r7   r   r#   )r8   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r&   r'   r9   r:   widthr;   r<   r=   s                           r>   r1   z"SelectiveKernelBottleneck.__init__g   s   * /O	LY:LLDJJvb9:[HI,T^^+	'38 <VQV+V
$

 #!

 

 


 !	aqEaU`a
j):r:T*$"r?   c                     t        | j                  j                  dd       >t        j                  j                  | j                  j                  j                         y y rA   )rC   ra   rD   r   rE   rF   rB   rG   s    r>   rH   z(SelectiveKernelBottleneck.zero_init_last   rI   r?   c                 J   |}| j                  |      }| j                  |      }| j                  |      }| j                  | j                  |      }| j                  | j	                  |      }| j
                  | j                  |      }||z  }| j                  |      }|S rK   )r4   r5   ra   r6   r#   r   r7   rL   s      r>   rO   z!SelectiveKernelBottleneck.forward   s    JJqMJJqMJJqM77
A>>%q!A??&x0H	XHHQKr?   rP   rY   s   @r>   r[   r[   d   s=   I .2  (, !,0)+*,..4826.2-1'/#/# /# 	/#
 !+/# /# /#  ~/# /# /# %SM/# BII/# RYY/# !bii1/# tBII//#  !+!/#"  		*#/#b1r?   r[   c                 &    t        t        | |fi |S rK   )r   r   )variant
pretrainedkwargss      r>   _create_skresnetri      s"     	 r?   c                 2    | dddddt         t        dddd	|S )
Ni  )r*      rk   )   rl   g      ?bicubicr4   fcz
apache-2.0)urlnum_classes
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierlicenser   )ro   rh   s     r>   _cfgrz      s3    =vI%.BT  r?   ztimm/)	hf_hub_idzconv1.0)rw   )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_argsrH   
skresnet18r2   r   ri   rg   rh   r   
model_argss       r>   r   r      J     eEI"<DS\D]J L*SZ8R68RSSr?   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.
    r~   r   Tr   r*   r\      r*   r   Fr   
skresnet34r   r   s       r>   r   r      r   r?   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r   r   r   Fr   
skresnet50r2   r[   ri   r   s       r>   r   r      sF     &I'XaIbJ L*SZ8R68RSSr?   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   rH   skresnet50dr   r   s       r>   r   r      sN     &I'W]hl),UDJ M:Tj9SF9STTr?   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   Fr   r   r\   r   )r   r   r   r   r   rH   skresnext50_32x4dr   r   s       r>   r   r     sQ    
 druEI'"YZ),UDJ /ZtJ?YRX?YZZr?   )F) ) __doc__r_   typingr   r   torchr   	timm.datar   r   timm.layersr	   r
   r   _builderr   	_registryr   r   resnetr   rW   r   r[   ri   rz   default_cfgsr   r   r   r   r   r/   r?   r>   <module>r      s&  	  !  A A A * < J299 JZE		 EP	 %11 F!!%!8&  
Tf 
T 
T 
Tf 
T 
T 
Tf 
T 
T 
Uv 
U 
U [V [ [r?   