
    kh7                     2   U d Z ddlmZmZmZmZmZmZmZ ddl	Z	ddl
mZ ddlmc mZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ d
gZg dg dg dg ddZeeeeeef      f   ed<   e G d dej@                               Z! G d d
ej@                        Z"de#deee	jH                  f   fdZ%dede&dede"fdZ'd(dedeeef   fdZ( e e(d       e(d       e(d       e(d       e(d       e(d       e(d       e(d      d      Z)ed)de&dede"fd        Z*ed)de&dede"fd!       Z+ed)de&dede"fd"       Z,ed)de&dede"fd#       Z-ed)de&dede"fd$       Z.ed)de&dede"fd%       Z/ed)de&dede"fd&       Z0ed)de&dede"fd'       Z1y)*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                        e Zd ZdZdddddej
                  ej                  fdeded	ed
edede	ej                     de	ej                     f fdZdej                  dej                  fdZ xZS )ConvMlpzxConvolutional MLP block for VGG head.

    Replaces traditional Linear layers with Conv2d layers in the classifier.
    r               ?g?in_featuresout_featureskernel_size	mlp_ratio	drop_rate	act_layer
conv_layerc                     t         t        |           || _        t	        ||z        } ||||d      | _         |d      | _        t        j                  |      | _	         |||dd      | _
         |d      | _        y)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.
        T)biasr   N)superr   __init__input_kernel_sizeintfc1act1nnDropoutdropfc2act2)
selfr#   r$   r%   r&   r'   r(   r)   mid_features	__class__s
            K/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/models/vgg.pyr-   zConvMlp.__init__&   ss    * 	gt%'!,<)34k<4PdO	JJy)	lL!$GdO	    xreturnc                    |j                   d   | j                  k  s|j                   d   | j                  k  r\t        | j                  |j                   d         t        | j                  |j                   d         f}t        j                  ||      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|S )zoForward pass.

        Args:
            x: Input tensor.

        Returns:
            Output tensor.
        )
shaper.   maxFadaptive_avg_pool2dr0   r1   r4   r5   r6   )r7   r<   output_sizes      r:   forwardzConvMlp.forwardD   s     772;///1772;AWAW3Wt55qwwr{CSI_I_abahahikalEmnK%%a5AHHQKIIaLIIaLHHQKIIaLr;   )__name__
__module____qualname____doc__r2   ReLUConv2dr/   floatr   Moduler-   torchTensorrF   __classcell__r9   s   @r:   r   r      s      # $ "")+*,))$$ $ 	$
 $ $ BII$ RYY$< %,, r;   r   c                       e Zd ZdZddddej
                  ej                  dddf	d	ee   d
e	de	de	de
deej                     deej                     deeej                        dede
ddf fdZej"                  j$                  d!dedeeef   fd       Zej"                  j$                  d"deddfd       Zej"                  j$                  dej                  fd       Zd#d
e	dee   ddfdZdej2                  dej2                  fdZd!dej2                  dedej2                  fdZdej2                  dej2                  fdZd$d Z xZS )%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           
      :   t         t        |           |dk(  sJ || _        |
| _        d| _        |du| _        g | _        |}d}t        j                  }g }|D ]  }t        |      dz
  }|dk(  r>| j                  j                  t        ||d|              | |dd	      gz  }|dz  }Tt        t        |      } |||d
d      }||| ||       |d      gz  }n|| |d      gz  }|} t        j                  | | _        | j                  j                  t        ||dt        |      dz
                || _        d| _        t'        || j$                  d||
||      | _        t+        | j$                  ||	|
      | _        | j/                          y)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.
        rV   FNr   r   z	features.)num_chs	reductionmodule   )r%   striderU   )r%   paddingT)inplacer    r!   )r&   r'   r(   r)   )	pool_typer'   )r,   r   r-   rY   r'   grad_checkpointinguse_normfeature_infor2   	MaxPool2dlenappenddictr	   r/   
Sequentialfeaturesnum_featureshead_hidden_sizer   
pre_logitsr   head_initialize_weights)r7   rX   rY   rZ   r[   r&   r(   r)   r\   r]   r'   prev_chs
net_stride
pool_layerlayersvlast_idxconv2dr9   s                     r:   r-   zVGG.__init__`   s   6 	c4!#"""&""'"$.
\\
"$ 	A6{QHCx!!((h*_hiqhr]s)tu:!A>??a
aL#HaQJ)vz!}i6MNNFvy'>??F	 v.  h*W`adekalopap`qUr!st$ $!!!!
 #!!!	
	 	  "r;   coarsec                     t        dd      S )zGroup matcher for parameter groups.

        Args:
            coarse: Whether to use coarse grouping.

        Returns:
            Dictionary of grouped parameters.
        z^features\.0z^features\.(\d+))stemblocks)rm   )r7   r|   s     r:   group_matcherzVGG.group_matcher   s     1DEEr;   enablec                     |rJ d       y)zEnable or disable gradient checkpointing.

        Args:
            enable: Whether to enable gradient checkpointing.
        z$gradient checkpointing not supportedN )r7   r   s     r:   set_grad_checkpointingzVGG.set_grad_checkpointing   s     AAAz6r;   c                 .    | j                   j                  S )zTGet the classifier module.

        Returns:
            Classifier module.
        )rs   fc)r7   s    r:   get_classifierzVGG.get_classifier   s     yy||r;   c                 J    || _         | j                  j                  ||       y)zReset the classifier.

        Args:
            num_classes: Number of classes for new classifier.
            global_pool: Global pooling type.
        N)rY   rs   reset)r7   rY   r]   s      r:   reset_classifierzVGG.reset_classifier   s     '		[1r;   r<   c                 (    | j                  |      }|S )zForward pass through feature extraction layers.

        Args:
            x: Input tensor.

        Returns:
            Feature tensor.
        )ro   r7   r<   s     r:   forward_featureszVGG.forward_features   s     MM!r;   rr   c                 p    | j                  |      }|r| j                  ||      S | j                  |      S )zForward pass through head.

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

        Returns:
            Classification logits or features.
        )rr   )rr   rs   )r7   r<   rr   s      r:   forward_headzVGG.forward_head   s5     OOA6@tyyzy2RdiiPQlRr;   c                 J    | j                  |      }| j                  |      }|S )zoForward pass.

        Args:
            x: Input tensor.

        Returns:
            Output logits.
        )r   r   r   s     r:   rF   zVGG.forward   s)     !!!$a r;   c                    | j                         D ]a  }t        |t        j                        rdt        j                  j                  |j                  dd       |j                  Wt        j                  j                  |j                  d       t        |t        j                        rUt        j                  j                  |j                  d       t        j                  j                  |j                  d       t        |t        j                        st        j                  j                  |j                  dd       t        j                  j                  |j                  d       d y)zInitialize model weights.fan_outrelu)modenonlinearityNr   r   g{Gz?)modules
isinstancer2   rL   initkaiming_normal_weightr+   	constant_BatchNorm2dLinearnormal_)r7   ms     r:   rt   zVGG._initialize_weights   s     
	-A!RYY'''yv'V66%GG%%affa0Ar~~.!!!((A.!!!&&!,Aryy)!T2!!!&&!,
	-r;   F)T)N)r=   N)rG   rH   rI   rJ   r2   rK   rL   r   r   r/   rM   r   rN   r   strr-   rO   jitignoreboolr   r   r   r   r   rP   r   r   rF   rt   rQ   rR   s   @r:   r   r   Y   s     $!#")+*,))48$!J#cJ# J# 	J#
 J# J# BIIJ# RYYJ# !bii1J# J# J# 
J#X 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 %,, -r;   
state_dictr=   c                    i }| j                         D ]r  \  }}|}|j                  dd      }|j                  dd      }|j                  dd      }d|v r|j                  dd	d
d
      }d|v r|j                  dddd      }|||<   t |S )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.weightr@   r   r!   zclassifier.3.weightr    r   )itemsreplacereshape)r   out_dictkry   k_rs        r:   
_filter_fnr   
  s     H  " 	1kk.*:;kk.*:;kk.)4 A%		"c1a(A A%		"dAq)A	 Or;   variant
pretrainedkwargsc                     | j                  d      d   }|j                  dd      }t        t        | |ft        |   t        d|      t        d|}|S )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   rb   rU         T)flatten_sequentialr   )	model_cfgfeature_cfgpretrained_filter_fn)splitpopr   r   r   rm   r   )r   r   r   rX   r   models         r:   _create_vggr   !  sg     --
Q
C**],>?K  s)DkJ' E Lr;   urlc                 0    | dddddt         t        ddd
|S )	zCreate default configuration dictionary.

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

    Returns:
        Configuration dictionary.
    rT   )rU      r   )r!   r!   g      ?bilinearz
features.0r   )
r   rY   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierr
   )r   r   s     r:   _cfgr   ;  s2     =vJ%.B")  r;   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    t        di |}t        dd| i|S )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   rm   r   r   r   
model_argss      r:   r   r   [  "    
 JD:DDDr;   c                 R    t        ddt        j                  i|}t        dd| i|S )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>`._
    r\   r   r   )vgg11_bnrm   r2   r   r   r   s      r:   r   r   d  -    
 ::6:JGjGJGGr;   c                 2    t        di |}t        dd| i|S )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   m  r   r;   c                 R    t        ddt        j                  i|}t        dd| i|S )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>`._
    r\   r   r   )vgg13_bnr   r   s      r:   r   r   v  r   r;   c                 2    t        di |}t        dd| i|S )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   r;   c                 R    t        ddt        j                  i|}t        dd| i|S )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>`._
    r\   r   r   )vgg16_bnr   r   s      r:   r   r     r   r;   c                 2    t        di |}t        dd| i|S )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   r;   c                 R    t        ddt        j                  i|}t        dd| i|S )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>`._
    r\   r   r   )vgg19_bnr   r   s      r:   r   r     r   r;   ) r   )2rJ   typingr   r   r   r   r   r   r	   rO   torch.nnr2   torch.nn.functional
functionalrC   	timm.datar   r   timm.layersr   _builderr   _features_fxr   	_registryr   r   __all__r   r   r/   __annotations__rN   r   r   rm   rP   r   r   r   r   default_cfgsr   r   r   r   r   r   r   r   r   r;   r:   <module>r      s   @ ? ?     A & * 1 <' NVet	*d3U38_%%&  6bii 6 6rn-")) n-b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r;   