o
    پi7                     @   s|  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  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 dej Z!G dd
 d
ej Z"de#deee	j$f fddZ%dede&dede"fddZ'd5dedeeef fdd Z(ee(d!d"e(d!d"e(d!d"e(d!d"e(d!d"e(d!d"e(d!d"e(d!d"d#Z)ed6de&dede"fd%d&Z*ed6de&dede"fd'd(Z+ed6de&dede"fd)d*Z,ed6de&dede"fd+d,Z-ed6de&dede"fd-d.Z.ed6de&dede"fd/d0Z/ed6de&dede"fd1d2Z0ed6de&dede"fd3d4Z1dS )7zVGG

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                       st   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dZdejdejfddZ  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           	         sh   t t|   || _t|| }||||dd| _|d| _t|| _	|||ddd| _
|d| _dS )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__ C/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/vgg.pyr*   &   s   
zConvMlp.__init__xreturnc                 C   s   |j d | jk s|j d | jk r(t| j|j d t| j|j d f}t||}| |}| |}| |}| |}| 	|}|S )zoForward pass.

        Args:
            x: Input tensor.

        Returns:
            Output tensor.
        )
shaper+   maxFadaptive_avg_pool2dr-   r.   r1   r2   r3   )r4   r:   output_sizer8   r8   r9   forwardD   s    	$




zConvMlp.forward)__name__
__module____qualname____doc__r/   ReLUConv2dr,   floatr   Moduler*   torchTensorrC   __classcell__r8   r8   r6   r9   r      s2    r   c                       sF  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dZejjd,dedeeef fddZejjd-deddfddZejjdejfddZd.d
e	dee ddfd d!Zd"ejdejfd#d$Zd,d"ejd%edejfd&d'Zd"ejdejfd(d)Zd/d*d+Z  ZS )0r   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              	      sl  t t|   |dksJ || _|
| _d| _|du| _g | _|}d}tj	}g }|D ]Q}t
|d }|dkrQ| jt||d| d ||ddd	g7 }|d9 }q)tt|}|||d
dd}|duro|||||ddg7 }n	|||ddg7 }|}q)tj| | _| jt||dt
|d  d || _d| _t|| jd||
||d| _t| j||	|
d| _|   dS )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.
        rQ   FNr   r   z	features.)num_chs	reductionmodule   )r#   striderP   )r#   paddingT)inplacer   r   )r$   r%   r&   r'   )	pool_typer%   )r)   r   r*   rT   r%   grad_checkpointinguse_normfeature_infor/   	MaxPool2dlenappenddictr   r,   
Sequentialfeaturesnum_featureshead_hidden_sizer   
pre_logitsr   head_initialize_weights)r4   rS   rT   rU   rV   r$   r&   r'   rW   rX   r%   prev_chs
net_stride
pool_layerlayersvlast_idxconv2dr6   r8   r9   r*   `   sV   


$	zVGG.__init__Fcoarsec                 C   s   t d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)rg   )r4   rv   r8   r8   r9   group_matcher   s   zVGG.group_matcherTenablec                 C   s   |rJ ddS )zEnable or disable gradient checkpointing.

        Args:
            enable: Whether to enable gradient checkpointing.
        z$gradient checkpointing not supportedNr8   )r4   rz   r8   r8   r9   set_grad_checkpointing   s   zVGG.set_grad_checkpointingc                 C   s   | j jS )zTGet the classifier module.

        Returns:
            Classifier module.
        )rm   fc)r4   r8   r8   r9   get_classifier   s   zVGG.get_classifierc                 C   s   || _ | j|| dS )zReset the classifier.

        Args:
            num_classes: Number of classes for new classifier.
            global_pool: Global pooling type.
        N)rT   rm   reset)r4   rT   rX   r8   r8   r9   reset_classifier   s   zVGG.reset_classifierr:   c                 C   s   |  |}|S )zForward pass through feature extraction layers.

        Args:
            x: Input tensor.

        Returns:
            Feature tensor.
        )ri   r4   r:   r8   r8   r9   forward_features   s   
	zVGG.forward_featuresrl   c                 C   s&   |  |}|r| j||dS | |S )zForward pass through head.

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

        Returns:
            Classification logits or features.
        )rl   )rl   rm   )r4   r:   rl   r8   r8   r9   forward_head   s   

zVGG.forward_headc                 C   s   |  |}| |}|S )zoForward pass.

        Args:
            x: Input tensor.

        Returns:
            Output logits.
        )r   r   r   r8   r8   r9   rC      s   
	
zVGG.forwardc                 C   s   |   D ]N}t|tjr$tjj|jddd |jdur#tj|jd qt|tj	r;tj|jd tj|jd qt|tj
rRtj|jdd tj|jd qdS )zInitialize model weights.fan_outrelu)modenonlinearityNr   r   g{Gz?)modules
isinstancer/   rI   initkaiming_normal_weightr(   	constant_BatchNorm2dLinearnormal_)r4   mr8   r8   r9   rn      s   
zVGG._initialize_weightsF)T)N)r;   N)rD   rE   rF   rG   r/   rH   rI   r   r   r,   rJ   r   rK   r   strr*   rL   jitignoreboolr   ry   r{   r}   r   rM   r   r   rC   rn   rN   r8   r8   r6   r9   r   Y   sZ    		
L

state_dictr;   c                 C   sz   i }|   D ]4\}}|}|dd}|dd}|dd}d|v r*|dd	d
d
}d|v r6|dddd}|||< q|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krs   k_rr8   r8   r9   
_filter_fn
  s   	
r   variant
pretrainedkwargsc                 K   sF   |  dd }|dd}tt| |ft| td|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   r\   rP         T)flatten_sequentialr   )	model_cfgfeature_cfgpretrained_filter_fn)splitpopr   r   r   rg   r   )r   r   r   rS   r   modelr8   r8   r9   _create_vgg!  s   
	r    urlc                 K   s   | 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.
    rO   )rP      r   )r   r   g      ?bilinearz
features.0r   )
r   rT   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierr	   )r   r   r8   r8   r9   _cfg;  s   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_in1kFc                 K       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   Nr8   )r   rg   r   r   r   
model_argsr8   r8   r9   r   [     r   c                 K   &   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>`._
    rW   vgg11_bnr   Nr8   )r   rg   r/   r   r   r   r8   r8   r9   r   d     r   c                 K   r   )zVGG 13-layer model (configuration "B")
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
    r   r   Nr8   )r   r   r   r8   r8   r9   r   m  r   r   c                 K   r   )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>`._
    rW   vgg13_bnr   Nr8   )r   r   r   r8   r8   r9   r   v  r   r   c                 K   r   )zVGG 16-layer model (configuration "D")
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
    r   r   Nr8   )r   r   r   r8   r8   r9   r     r   r   c                 K   r   )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>`._
    rW   vgg16_bnr   Nr8   )r   r   r   r8   r8   r9   r     r   r   c                 K   r   )zVGG 19-layer model (configuration "E")
    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`._
    r   r   Nr8   )r   r   r   r8   r8   r9   r     r   r   c                 K   r   )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>`._
    rW   vgg19_bnr   Nr8   )r   r   r   r8   r8   r9   r     r   r   )r   r   )2rG   typingr   r   r   r   r   r   r   rL   torch.nnr/   torch.nn.functional
functionalr@   	timm.datar
   r   timm.layersr   _builderr   _features_fxr   	_registryr   r   __all__r   r   r,   __annotations__rK   r   r   rg   rM   r   r   r   r   default_cfgsr   r   r   r   r   r   r   r   r8   r8   r8   r9   <module>   sb    $"9 2