a
    d                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ dZ	g dg dg d	g d
dZ
dd Ze G dd dejZdS )    N)OrderedDict)nn)vgg)ARCH_REGISTRYz0experiments/pretrained_models/vgg19-dcbb9e9d.pth)conv1_1relu1_1pool1conv2_1relu2_1pool2conv3_1relu3_1conv3_2relu3_2pool3conv4_1relu4_1conv4_2relu4_2pool4conv5_1relu5_1conv5_2relu5_2pool5)r   r   conv1_2relu1_2r   r	   r
   conv2_2relu2_2r   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   conv3_3relu3_3r   r   r   r   r   conv4_3relu4_3r   r   r   r   r   conv5_3relu5_3r   )%r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r    Zconv3_4Zrelu3_4r   r   r   r   r   r!   r"   Zconv4_4Zrelu4_4r   r   r   r   r   r#   r$   Zconv5_4Zrelu5_4r   )Zvgg11Zvgg13Zvgg16vgg19c                 C   s>   g }| D ]0}| | d|v r|dd}| d|  q|S )zInsert bn layer after each conv.

    Args:
        names (list): The list of layer names.

    Returns:
        list: The list of layer names with bn layers.
    conv bn)appendreplace)namesZnames_bnnameposition r.   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/archs/vgg_arch.py	insert_bn$   s    	
r0   c                       s*   e Zd ZdZd
 fdd	Zdd	 Z  ZS )VGGFeatureExtractora7  VGG network for feature extraction.

    In this implementation, we allow users to choose whether use normalization
    in the input feature and the type of vgg network. Note that the pretrained
    path must fit the vgg type.

    Args:
        layer_name_list (list[str]): Forward function returns the corresponding
            features according to the layer_name_list.
            Example: {'relu1_1', 'relu2_1', 'relu3_1'}.
        vgg_type (str): Set the type of vgg network. Default: 'vgg19'.
        use_input_norm (bool): If True, normalize the input image. Importantly,
            the input feature must in the range [0, 1]. Default: True.
        range_norm (bool): If True, norm images with range [-1, 1] to [0, 1].
            Default: False.
        requires_grad (bool): If true, the parameters of VGG network will be
            optimized. Default: False.
        remove_pooling (bool): If true, the max pooling operations in VGG net
            will be removed. Default: False.
        pooling_stride (int): The stride of max pooling operation. Default: 2.
    r%   TF   c              	      s  t t|   || _|| _|| _t|dd | _d|v rFt	| j| _d}|D ]}	| j
|	}
|
|krN|
}qNtjtrtt|dd}tjtdd d	}|| ntt|d
d}|jd |d  }t }t| j|D ]6\}}	d|v r|rqntjd|d||< q|	||< qt|| _|sF| j  |  D ]}d|_q6n | j  |  D ]}d
|_qX| jr| dt g d!dddd | dt g d!dddd d S )NZ_bnr'   r(   r   F)Z
pretrainedc                 S   s   | S )Nr.   )Zstoragelocr.   r.   r/   <lambda>i       z.VGGFeatureExtractor.__init__.<locals>.<lambda>)Zmap_locationT   poolr2   )Zkernel_sizeZstridemean)g
ףp=
?gv/?gCl?   std)gZd;O?gy&1?g?)"superr1   __init__layer_name_listuse_input_norm
range_normNAMESr*   r+   r0   indexospathexistsVGG_PRETRAIN_PATHgetattrr   torchloadZload_state_dictfeaturesr   zipr   Z	MaxPool2dZ
Sequentialvgg_neteval
parametersrequires_gradZtrainZregister_bufferZTensorview)selfr=   Zvgg_typer>   r?   rN   Zremove_poolingZpooling_strideZmax_idxvidxrK   Z
state_dictrI   Zmodified_netkparam	__class__r.   r/   r<   N   sH    




"zVGGFeatureExtractor.__init__c                 C   sd   | j r|d d }| jr(|| j | j }i }| jj D ]&\}}||}|| jv r8| ||< q8|S )zForward function.

        Args:
            x (Tensor): Input tensor with shape (n, c, h, w).

        Returns:
            Tensor: Forward results.
        r6   r2   )	r?   r>   r8   r:   rK   Z_modulesitemsr=   clone)rP   xoutputkeyZlayerr.   r.   r/   forward   s    	
zVGGFeatureExtractor.forward)r%   TFFFr2   )__name__
__module____qualname____doc__r<   r\   __classcell__r.   r.   rU   r/   r1   6   s         ?r1   )rB   rG   collectionsr   r   Ztorchvision.modelsr   Zbasicsr.utils.registryr   rE   r@   r0   registerModuler1   r.   r.   r.   r/   <module>   s   