
    NgI"              	          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dS )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                   `     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                    t          t          |                                            |pi }t          ||          }|dk    s
J d            |dk    s
J d            ||z  }|| j        z  }|
p|	}
t          ||f||
||d||| _        t          ||fd|	dd	|| _        t          ||          | _
         |d
          | _        || _        || _        d S )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/html/ai-engine/env/lib/python3.11/site-packages/timm/models/sknet.pyr    zSelectiveKernelBasic.__init__   s?   & 	"D))22444O	Y:FFFa!LR!S-T^+	'38$lR+1N*R R8CRGPR R
 !)g12XQVg gZeg g
j)449T***$"    c                     t          | j        j        dd           0t          j                            | j        j        j                   d S d S Nweight)getattrr$   bnr   initzeros_r;   r)   s    r7   zero_init_lastz#SelectiveKernelBasic.zero_init_last@   A    4:=(D11=GNN4:=/00000 >=r8   c                 :   |}|                      |          }|                     |          }| j        |                     |          }| j        |                     |          }| j        |                     |          }||z  }|                     |          }|S N)r#   r$   r%   r(   r'   r&   r)   xshortcuts      r7   forwardzSelectiveKernelBasic.forwardD   s    JJqMMJJqMM7

A>%q!!A?&x00H	XHHQKKr8   __name__
__module____qualname__r"   r   ReLUBatchNorm2dr    rA   rH   __classcell__r6   s   @r7   r   r      s        I g~#%# %# %# %# %# %#N1 1 1      r8   r   c                   `     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          |                                            |pi }t          ||          }t	          t          j        ||dz  z            |z            }||z  }|| j        z  }|
p|	}
t          ||fddi|| _	        t          ||f||
|||d||| _        t          ||fddd|| _        t          ||          | _         |d	          | _        || _        || _        d S )
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7999O	Y:FFFDJvb9::[HII,T^+	'38 <VVQV+VV
$%R(.P[*R R8CRGPR R
 !	aqEaaU`aa
j)449T***$"r8   c                     t          | j        j        dd           0t          j                            | j        j        j                   d S d S r:   )r<   rY   r=   r   r>   r?   r;   r@   s    r7   rA   z(SelectiveKernelBottleneck.zero_init_last|   rB   r8   c                 d   |}|                      |          }|                     |          }|                     |          }| j        |                     |          }| j        |                     |          }| j        |                     |          }||z  }|                     |          }|S rD   )r#   r$   rY   r%   r(   r'   r&   rE   s      r7   rH   z!SelectiveKernelBottleneck.forward   s    JJqMMJJqMMJJqMM7

A>%q!!A?&x00H	XHHQKKr8   rI   rP   s   @r7   rR   rR   S   s        I g~#$# $# $# $# $# $#L1 1 1      r8   rR   Fc                 *    t          t          | |fi |S rD   )r   r   )variant
pretrainedkwargss      r7   _create_skresnetra      s,      	  r8    c                 4    | dddddt           t          ddd
|S )	Ni  )r      rd   )   re   g      ?bicubicr#   fc)
urlnum_classes
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierr   )rh   r`   s     r7   _cfgrr      s5    =vI%.BT   r8   ztimm/)	hf_hub_idzconv1.0)rp   )zskresnet18.ra_in1kzskresnet34.ra_in1kzskresnet50.untrainedzskresnet50d.untrainedzskresnext50_32x4d.ra_in1kreturnc                     t          ddd          }t          d
t          g dt          |          dd|}t          d	| 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      m     eEEEI ("<<<DS\D]D]D]( ( &( (J L*CC
CCCr8   c                     t          ddd          }t          d
t          g dt          |          dd|}t          d	| 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.
    rv   rw   Trx   r   rS      r   r}   Fr~   
skresnet34r   r   r   s       r7   r   r      r   r8   c                     t          d          }t          dt          g dt          |          dd|}t          d| 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~   
skresnet50r   r!   rR   ra   r   s       r7   r   r      si     &&&I ('XaIbIbIb( ( &( (J L*CC
CCCr8   c                     t          d          }t          d
t          g ddddt          |          dd|}t          d	| 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   r   s       r7   r   r      su     &&&I N'W]hl),,,UN NFLN NJ M:DDDDDr8   c           
          t          ddd          }t          d
t          g dddt          |          dd|}t          d	| 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   Frx   r   rS   r}   )r   r   r,   r-   r   rA   skresnext50_32x4dr   r   r   s       r7   r   r      sw    
 druEEEI N'"YZ),,,UN NFLN NJ /JJzJJJr8   )F)rb   )__doc__rW   torchr   	timm.datar   r   timm.layersr   r   r	   _builderr   	_registryr   r   resnetr   Moduler   rR   ra   rr   default_cfgsr   r   r   r   r   r   r8   r7   <module>r      s  	 	        A A A A A A A A A A A A A A A A A A * * * * * * < < < < < < < <      : : : : :29 : : :z: : : : :	 : : :z       %$$111$111 DFF!T  !%!8!8!8& &   
D 
Df 
D 
D 
D 
D 
D 
Df 
D 
D 
D 
D 
D 
Df 
D 
D 
D 
D 
E 
Ev 
E 
E 
E 
E K KV K K K K K Kr8   