
    RЦi"9                     *   % S r SSKJrJrJrJrJrJrJr  SSK	r	SSK
Jr  SSKJs  Jr  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJrJr  S
/r/ SQ/ SQ/ SQ/ SQS.r\\\\\\4      4   \S'   \ " S S\R@                  5      5       r! " S S
\R@                  5      r"S\#S\\\	RH                  4   4S jr%S\S\&S\S\"4S jr'S(S\S\\\4   4S jjr(\" \(" SS9\(" SS9\(" SS9\(" SS9\(" SS9\(" SS9\(" SS9\(" SS9S.5      r)\S)S\&S\S\"4S  jj5       r*\S)S\&S\S\"4S! jj5       r+\S)S\&S\S\"4S" jj5       r,\S)S\&S\S\"4S# jj5       r-\S)S\&S\S\"4S$ jj5       r.\S)S\&S\S\"4S% jj5       r/\S)S\&S\S\"4S& jj5       r0\S)S\&S\S\"4S' jj5       r1g)*zVGG

Adapted from https://github.com/pytorch/vision 'vgg.py' (BSD-3-Clause) with a few changes for
timm functionality.

Copyright 2021 Ross Wightman
    )AnyDictListOptionalTypeUnioncastNIMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STD)ClassifierHead   )build_model_with_cfg)register_notrace_module)register_modelgenerate_default_cfgsVGG)@   M   r      r   r      r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )vgg11vgg13vgg16vgg19cfgsc                      ^  \ rS rSrSrSSSSS\R                  \R                  SS4	S	\S
\S\S\	S\	S\
\R                     S\
\R                     SS4U 4S jjjrS\R                  S\R                  4S jrSrU =r$ )ConvMlp   zpConvolutional MLP block for VGG head.

Replaces traditional Linear layers with Conv2d layers in the classifier.
r               ?g?Nin_featuresout_featureskernel_size	mlp_ratio	drop_rate	act_layer
conv_layerreturnc
                    > XS.n
[         TU ]  5         X0l        [        X$-  5      nU" XU4SS0U
D6U l        U" S5      U l        [        R                  " U5      U l        U" XS4SS0U
D6U l	        U" S5      U l
        g)a=  Initialize ConvMlp.

Args:
    in_features: Number of input features.
    out_features: Number of output features.
    kernel_size: Kernel size for first conv layer.
    mlp_ratio: Ratio for hidden layer size.
    drop_rate: Dropout rate.
    act_layer: Activation layer type.
    conv_layer: Convolution layer type.
devicedtypebiasTr   N)super__init__input_kernel_sizeintfc1act1nnDropoutdropfc2act2)selfr$   r%   r&   r'   r(   r)   r*   r.   r/   ddmid_features	__class__s               N/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/models/vgg.pyr2   ConvMlp.__init__&   s    . /!,<34kV4VSUVdO	JJy)	l!M$M"MdO	    xc                    UR                   S   U R                  :  d  UR                   S   U R                  :  a\  [        U R                  UR                   S   5      [        U R                  UR                   S   5      4n[        R                  " X5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ )zGForward pass.

Args:
    x: Input tensor.

Returns:
    Output tensor.
)
shaper3   maxFadaptive_avg_pool2dr5   r6   r9   r:   r;   )r<   rC   output_sizes      r@   forwardConvMlp.forwardG   s     772;///1772;AWAW3Wt55qwwr{CSI_I_abahahikalEmnK%%a5AHHQKIIaLIIaLHHQKIIaLrB   )r6   r;   r9   r5   r:   r3   )__name__
__module____qualname____firstlineno____doc__r7   ReLUConv2dr4   floatr   Moduler2   torchTensorrL   __static_attributes____classcell__r?   s   @r@   r   r      s      # $ "")+*,))$$ $ 	$
 $ $ BII$ RYY$ 
$ $B %,,  rB   r   c                     ^  \ rS rSrSrSSSS\R                  \R                  SSS	SS4S
\\	   S\
S\
S\
S\S\\R                     S\\R                     S\\\R                        S\S\SS4U 4S jjjr\R$                  R&                  S#S\S\\\	4   4S jj5       r\R$                  R&                  S$S\SS4S jj5       r\R$                  R&                  S\R                  4S j5       rS%S\
S\\   SS4S jjrS\R4                  S\R4                  4S jrS#S\R4                  S\S\R4                  4S jjrS\R4                  S\R4                  4S  jrS&S! jrS"rU =r $ )'r   \   zVGG model architecture.

Based on `Very Deep Convolutional Networks for Large-Scale Image Recognition`
- https://arxiv.org/abs/1409.1556
         r#   Navgg        cfgnum_classesin_chansoutput_strider'   r)   r*   
norm_layerglobal_poolr(   r+   c           
      4  > [         TU ]  5         XS.nUS:X  d   eX l        X0l        Xl        SU l        USLU l        / U l        UnSn[        R                  n/ nU H  n[        U5      S-
  nUS:X  a8  U R                  R                  [        XSU 3S95        UU" S	S	S
9/-  nUS	-  nMO  [        [        U5      nU" UU4SSS.UD6nUb  UUU" U40 UD6U" SS9/-  nOUUU" SS9/-  nUnM     [        R                  " U6 U l        U R                  R                  [        XS[        U5      S-
   3S95        Xl        SU l        ['        UU R$                  S4UU
UUS.UD6U l        [+        U R$                  U4U	U
S.UD6U l        U R/                  5         g)a  Initialize VGG model.

Args:
    cfg: Configuration list defining network architecture.
    num_classes: Number of classes for classification.
    in_chans: Number of input channels.
    output_stride: Output stride of network.
    mlp_ratio: Ratio for MLP hidden layer size.
    act_layer: Activation layer type.
    conv_layer: Convolution layer type.
    norm_layer: Normalization layer type.
    global_pool: Global pooling type.
    drop_rate: Dropout rate.
r-   r`   FNr   r   z	features.)num_chs	reductionmodule   )r&   strider_   )r&   paddingT)inplacer!   r"   )r'   r(   r)   r*   )	pool_typer(   )r1   r2   rc   rd   r(   grad_checkpointinguse_normfeature_infor7   	MaxPool2dlenappenddictr	   r4   
Sequentialfeaturesnum_featureshead_hidden_sizer   
pre_logitsr   head_initialize_weights)r<   rb   rc   rd   re   r'   r)   r*   rf   rg   r(   r.   r/   r=   prev_chs
net_stride
pool_layerlayersvlast_idxconv2dr?   s                        r@   r2   VGG.__init__c   s   : 	/"""& ""'"$.
\\
"$A6{QHCx!!((h_hiqhr]s)tu:!A>??a
aL#HaPQPRP)vz!':r':Id<STTFvy'>??F  v.  hW`adekalopap`qUr!st$ $!!!	
  !	
 	
 #!!
 "	

 
	 	  "rB   coarsec                     [        SSS9$ )zGroup matcher for parameter groups.

Args:
    coarse: Whether to use coarse grouping.

Returns:
    Dictionary of grouped parameters.
z^features\.0z^features\.(\d+))stemblocks)rw   )r<   r   s     r@   group_matcherVGG.group_matcher   s     1DEErB   enablec                      U(       a   S5       eg)zgEnable or disable gradient checkpointing.

Args:
    enable: Whether to enable gradient checkpointing.
z$gradient checkpointing not supportedN )r<   r   s     r@   set_grad_checkpointingVGG.set_grad_checkpointing   s     AAAz6rB   c                 .    U R                   R                  $ )z<Get the classifier module.

Returns:
    Classifier module.
)r}   fc)r<   s    r@   get_classifierVGG.get_classifier   s     yy||rB   c                 F    Xl         U R                  R                  X5        g)zzReset the classifier.

Args:
    num_classes: Number of classes for new classifier.
    global_pool: Global pooling type.
N)rc   r}   reset)r<   rc   rg   s      r@   reset_classifierVGG.reset_classifier   s     '		1rB   rC   c                 (    U R                  U5      nU$ )zjForward pass through feature extraction layers.

Args:
    x: Input tensor.

Returns:
    Feature tensor.
)ry   r<   rC   s     r@   forward_featuresVGG.forward_features   s     MM!rB   r|   c                 r    U R                  U5      nU(       a  U R                  XS9$ U R                  U5      $ )zForward pass through head.

Args:
    x: Input features.
    pre_logits: Return features before final linear layer.

Returns:
    Classification logits or features.
)r|   )r|   r}   )r<   rC   r|   s      r@   forward_headVGG.forward_head   s3     OOA6@tyyy2RdiiPQlRrB   c                 J    U R                  U5      nU R                  U5      nU$ )zGForward pass.

Args:
    x: Input tensor.

Returns:
    Output logits.
)r   r   r   s     r@   rL   VGG.forward   s)     !!!$a rB   c                    U R                  5        GHs  n[        U[        R                  5      (       ad  [        R                  R                  UR                  SSS9  UR                  b,  [        R                  R                  UR                  S5        M  M  [        U[        R                  5      (       aV  [        R                  R                  UR                  S5        [        R                  R                  UR                  S5        M  [        U[        R                  5      (       d  GM  [        R                  R                  UR                  SS5        [        R                  R                  UR                  S5        GMv     g)zInitialize model weights.fan_outrelu)modenonlinearityNr   r   g{Gz?)modules
isinstancer7   rT   initkaiming_normal_weightr0   	constant_BatchNorm2dLinearnormal_)r<   ms     r@   r~   VGG._initialize_weights  s    A!RYY''''yv'V66%GG%%affa0 &Ar~~..!!!((A.!!!&&!,Aryy))!T2!!!&&!,  rB   )r(   rs   ry   rq   r}   r{   rd   rc   rz   r|   rr   F)T)N)r+   N)!rN   rO   rP   rQ   rR   r7   rS   rT   r   r   r4   rU   r   rV   r   strr2   rW   jitignoreboolr   r   r   r   r   rX   r   r   rL   r~   rY   rZ   r[   s   @r@   r   r   \   s     $!#")+*,))48$!P#cP# P# 	P#
 P# P# BIIP# RYYP# !bii1P# P# P# 
P# P#d YY
FD 
FT#s(^ 
F 
F YYBT BT B B YY		  2C 2hsm 2W[ 2
%,, 
5<< 
Sell S S S %,, - -rB   
state_dictr+   c                    0 nU R                  5        Hu  u  p#UnUR                  SS5      nUR                  SS5      nUR                  SS5      nSU;   a  UR                  SS	S
S
5      nSU;   a  UR                  SSSS5      nX1U'   Mw     U$ )zConvert patch embedding weight from manual patchify + linear proj to conv.

Args:
    state_dict: State dictionary to filter.

Returns:
    Filtered state dictionary.
zclassifier.0zpre_logits.fc1zclassifier.3zpre_logits.fc2zclassifier.6head.fczclassifier.0.weightrF   r   r"   zclassifier.3.weightr!   r   )itemsreplacereshape)r   out_dictkr   k_rs        r@   
_filter_fnr     s     H  "kk.*:;kk.*:;kk.)4 A%		"c1a(A A%		"dAq)A # OrB   variant
pretrainedkwargsc           	          U R                  S5      S   nUR                  SS5      n[        [        U U4[        U   [        SUS9[        S.UD6nU$ )zCreate a VGG model.

Args:
    variant: Model variant name.
    pretrained: Load pretrained weights.
    **kwargs: Additional model arguments.

Returns:
    VGG model instance.
_r   out_indices)r   r   rl   r_         T)flatten_sequentialr   )	model_cfgfeature_cfgpretrained_filter_fn)splitpopr   r   r   rw   r   )r   r   r   rb   r   models         r@   _create_vggr   *  sg     --
Q
C**],>?K  s)DkJ' E LrB   urlc                 2    U SSSSS[         [        SSSS	.UE$ )
zCreate default configuration dictionary.

Args:
    url: Model weight URL.
    **kwargs: Additional configuration options.

Returns:
    Configuration dictionary.
r^   )r_      r   )r"   r"   g      ?bilinearz
features.0r   zbsd-3-clause)r   rc   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierlicenser
   )r   r   s     r@   _cfgr   D  s5     =vJ%.B")!  rB   ztimm/)	hf_hub_id)zvgg11.tv_in1kzvgg13.tv_in1kzvgg16.tv_in1kzvgg19.tv_in1kzvgg11_bn.tv_in1kzvgg13_bn.tv_in1kzvgg16_bn.tv_in1kzvgg19_bn.tv_in1kc                 2    [        S0 UD6n[        SSU 0UD6$ )zVGG 11-layer model (configuration "A") from
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
r   r   )r   rw   r   r   r   
model_argss      r@   r   r   e  "    
 JD:DDDrB   c                 R    [        SS[        R                  0UD6n[        SSU 0UD6$ )zVGG 11-layer model (configuration "A") with batch normalization
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
rf   r   r   )vgg11_bnrw   r7   r   r   r   s      r@   r   r   n  -    
 ::6:JGjGJGGrB   c                 2    [        S0 UD6n[        SSU 0UD6$ )zVGG 13-layer model (configuration "B")
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
r   r   )r   r   r   s      r@   r   r   w  r   rB   c                 R    [        SS[        R                  0UD6n[        SSU 0UD6$ )zVGG 13-layer model (configuration "B") with batch normalization
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
rf   r   r   )vgg13_bnr   r   s      r@   r   r     r   rB   c                 2    [        S0 UD6n[        SSU 0UD6$ )zVGG 16-layer model (configuration "D")
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
r   r   )r   r   r   s      r@   r   r     r   rB   c                 R    [        SS[        R                  0UD6n[        SSU 0UD6$ )zVGG 16-layer model (configuration "D") with batch normalization
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
rf   r   r   )vgg16_bnr   r   s      r@   r   r     r   rB   c                 2    [        S0 UD6n[        SSU 0UD6$ )zVGG 19-layer model (configuration "E")
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
r   r   )r   r   r   s      r@   r   r     r   rB   c                 R    [        SS[        R                  0UD6n[        SSU 0UD6$ )zVGG 19-layer model (configuration 'E') with batch normalization
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
rf   r   r   )vgg19_bnr   r   s      r@   r   r     r   rB   ) r   )2rR   typingr   r   r   r   r   r   r	   rW   torch.nnr7   torch.nn.functional
functionalrI   	timm.datar   r   timm.layersr   _builderr   _features_fxr   	_registryr   r   __all__r   r   r4   __annotations__rV   r   r   rw   rX   r   r   r   r   default_cfgsr   r   r   r   r   r   r   r   r   rB   r@   <module>r      s   @ ? ?     A & * 1 <' NVet	*d3U38_%%&  9bii 9 9xt-")) t-n4 Dell):$; . $ # # 4c T#s(^ * %G,G,G,G,w/w/w/w/	& 	 Ed Ec Ec E E H H H H H Ed Ec Ec E E H H H H H Ed Ec Ec E E H H H H H Ed Ec Ec E E H H H H HrB   