a
    d*                     @   s(  U d Z ddl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d Z!ee"ee dddZ#d1d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d2e"ee d d!d"Z&ed3e"ee d d#d$Z'ed4e"ee d d%d&Z(ed5e"ee d d'd(Z)ed6e"ee d d)d*Z*ed7e"ee d d+d,Z+ed8e"ee d d-d.Z,ed9e"ee d d/d0Z-dS ):zVGG

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

Copyright 2021 Ross Wightman
    )UnionListDictAny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                       s4   e Zd Zdeejejd fdd	Zd
d Z  ZS )ConvMlpr               ?皙?N)	drop_rate	act_layer
conv_layerc           	         sh   t t|   || _t|| }||||dd| _|d| _t|| _	|||ddd| _
|d| _d S )NT)biasr   )superr   __init__input_kernel_sizeintfc1act1nnZDropoutdropfc2act2)	selfZin_featuresZout_featureskernel_size	mlp_ratior    r!   r"   Zmid_features	__class__ X/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/models/vgg.pyr%   "   s    

zConvMlp.__init__c                 C   s   |j d | jk s |j d | jk rPt| j|j d t| j|j d f}t||}| |}| |}| |}| |}| 	|}|S )N)
shaper&   maxFZadaptive_avg_pool2dr(   r)   r+   r,   r-   )r.   xZoutput_sizer3   r3   r4   forward5   s     $




zConvMlp.forward)r   r   r   r   r   NN)	__name__
__module____qualname__floatr*   Moduler%   r;   __classcell__r3   r3   r1   r4   r      s          r   c                       s   e Zd Zddddejejdddf	ee eeee	ej
ej
ej
ee	dd fd	d
Zejjd ddZejjd!ddZejjdd Zd"ddZejejdddZd#ejedddZejejdddZddddZ  ZS )$r            r   Navgg        )cfgnum_classesin_chansoutput_strider0   r!   r"   
norm_layerglobal_poolr    returnc              	      sf  t t|   |dksJ || _d| _|
| _d| _|d u| _g | _|}d}t	j
}g }|D ]}t|d }|dkr| jt||d| d ||ddd	g7 }|d9 }qXtt|}|||d
dd}|d ur|||||ddg7 }n|||ddg7 }|}qXt	j| | _| jt||dt|d  d t|| jd||
||d| _t| j||	|
d| _|   d S )NrD   r   Fr   r   z	features.)Znum_chsZ	reductionmodule   )r/   ZstriderC   )r/   paddingT)Zinplacer   )r0   r    r!   r"   Z	pool_typer    )r$   r   r%   rG   num_featuresr    Zgrad_checkpointingZuse_normZfeature_infor*   Z	MaxPool2dlenappenddictr   r'   Z
Sequentialfeaturesr   
pre_logitsr
   head_initialize_weights)r.   rF   rG   rH   rI   r0   r!   r"   rJ   rK   r    Zprev_chsZ
net_strideZ
pool_layerZlayersvZlast_idxZconv2dr1   r3   r4   r%   D   sT    


$	zVGG.__init__Fc                 C   s   t dddS )Nz^features\.0z^features\.(\d+))stemblocks)rT   )r.   Zcoarser3   r3   r4   group_matcher   s    zVGG.group_matcherTc                 C   s   |rJ dd S )Nz$gradient checkpointing not supportedr3   )r.   enabler3   r3   r4   set_grad_checkpointing   s    zVGG.set_grad_checkpointingc                 C   s   | j jS N)rW   Zfc)r.   r3   r3   r4   get_classifier   s    zVGG.get_classifierc                 C   s"   || _ t| j| j || jd| _d S )NrP   )rG   r
   rQ   r    rW   )r.   rG   rK   r3   r3   r4   reset_classifier   s    zVGG.reset_classifier)r:   rL   c                 C   s   |  |}|S r_   )rU   r.   r:   r3   r3   r4   forward_features   s    
zVGG.forward_features)r:   rV   c                 C   s   |  |}|r|S | |S r_   )rV   rW   )r.   r:   rV   r3   r3   r4   forward_head   s    
zVGG.forward_headc                 C   s   |  |}| |}|S r_   )rc   rd   rb   r3   r3   r4   r;      s    

zVGG.forward)rL   c                 C   s   |   D ]}t|tjrHtjj|jddd |jd urtj|jd qt|tj	rvtj|jd tj|jd qt|tj
rtj|jdd tj|jd qd S )NZfan_outZrelu)modeZnonlinearityr   r   g{Gz?)modules
isinstancer*   Conv2dinitZkaiming_normal_Zweightr#   Z	constant_BatchNorm2dZLinearZnormal_)r.   mr3   r3   r4   rX      s    
zVGG._initialize_weights)F)T)rE   )F)r<   r=   r>   r*   ZReLUrh   r   r   r'   r?   r@   strr%   torchZjitignorer\   r^   r`   ra   ZTensorrc   boolrd   r;   rX   rA   r3   r3   r1   r4   r   B   sB   <

	c                 C   sz   i }|   D ]h\}}|}|dd}|dd}|dd}d|v rT|dd	d
d
}d|v rl|dddd}|||< q|S )zJ convert patch embedding weight from manual patchify + linear proj to convzclassifier.0zpre_logits.fc1zclassifier.3zpre_logits.fc2zclassifier.6head.fczclassifier.0.weightr6   r   r   zclassifier.3.weightr   r   )itemsreplaceZreshape)Z
state_dictZout_dictkrY   Zk_rr3   r3   r4   
_filter_fn   s    
rt   )variant
pretrainedkwargsrL   c                 K   sF   |  dd }|dd}tt| |ft| td|dtd|}|S )N_r   out_indices)r   r   rN   rC         T)Zflatten_sequentialry   )Z	model_cfgZfeature_cfgZpretrained_filter_fn)splitpopr   r   r   rT   rt   )ru   rv   rw   rF   ry   modelr3   r3   r4   _create_vgg   s    
	r    c                 K   s   | dddddt tddd
|S )	NrB   )rC      r   )r   r   g      ?Zbilinearz
features.0rp   )
urlrG   Z
input_sizeZ	pool_sizeZcrop_pctinterpolationmeanZstdZ
first_conv
classifierr   )r   rw   r3   r3   r4   _cfg   s    r   ztimm/)Z	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_in1kF)rv   rw   rL   c                 K   s    t f 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   rv   )r   rT   r   rv   rw   Z
model_argsr3   r3   r4   r      s    r   c                 K   s&   t f 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>`._
    rJ   vgg11_bnrv   )r   rT   r*   rj   r   r   r3   r3   r4   r      s    r   c                 K   s    t f 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   rv   )r   r   r   r3   r3   r4   r      s    r   c                 K   s&   t f 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>`._
    rJ   vgg13_bnrv   )r   r   r   r3   r3   r4   r     s    r   c                 K   s    t f 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   rv   )r   r   r   r3   r3   r4   r     s    r   c                 K   s&   t f 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>`._
    rJ   vgg16_bnrv   )r   r   r   r3   r3   r4   r     s    r   c                 K   s    t f 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   rv   )r   r   r   r3   r3   r4   r     s    r   c                 K   s&   t f 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>`._
    rJ   vgg19_bnrv   )r   r   r   r3   r3   r4   r   '  s    r   )r   )F)F)F)F)F)F)F)F).__doc__typingr   r   r   r   r   rm   Ztorch.nnr*   Ztorch.nn.functionalZ
functionalr9   Z	timm.datar   r	   Ztimm.layersr
   Z_builderr   Z_features_fxr   	_registryr   r   __all__r   rl   r'   __annotations__r@   r   r   rt   ro   r   r   Zdefault_cfgsr   r   r   r   r   r   r   r   r3   r3   r3   r4   <module>   s^   ""o
