a
    þd/  ã                   @   s¼   d dl Z d dlZd dlmZ d dlm  mZ d dlmZ d dl	m
Z
 dZdZG dd„ dejƒZdd	„ ZG d
d„ de
jjƒZG dd„ de
jjƒZG dd„ de
jjƒZG dd„ de
jjƒZdS )é    N)Úload_url)Úmodelszjhttps://github.com/mseitzer/pytorch-fid/releases/download/fid_weights/pt_inception-2015-12-05-6726825d.pthzBexperiments/pretrained_models/pt_inception-2015-12-05-6726825d.pthc                       sF   e Zd ZdZdZdddddœZeddddf‡ fd	d
„	Zdd„ Z‡  ZS )ÚInceptionV3z5Pretrained InceptionV3 network returning feature mapsé   r   é   é   )é@   éÀ   é   é   TFc                    s~  t t| ƒ ¡  || _|| _t|ƒ| _t|ƒ| _| jdks@J dƒ‚t	 
¡ | _|rVtƒ }n2ztjddd}W n ty†   tjdd}Y n0 |j|j|jt	jdddg}| j t	j|Ž ¡ | jd	krê|j|jt	jdddg}| j t	j|Ž ¡ | jdkr,|j|j|j|j|j|j|j|jg}	| j t	j|	Ž ¡ | jdkrd|j |j!|j"t	j#d
dg}
| j t	j|
Ž ¡ |  $¡ D ]}||_%qldS )aÍ  Build pretrained InceptionV3.

        Args:
            output_blocks (list[int]): Indices of blocks to return features of.
                Possible values are:
                - 0: corresponds to output of first max pooling
                - 1: corresponds to output of second max pooling
                - 2: corresponds to output which is fed to aux classifier
                - 3: corresponds to output of final average pooling
            resize_input (bool): If true, bilinearly resizes input to width and
                height 299 before feeding input to model. As the network
                without fully connected layers is fully convolutional, it
                should be able to handle inputs of arbitrary size, so resizing
                might not be strictly needed. Default: True.
            normalize_input (bool): If true, scales the input from range (0, 1)
                to the range the pretrained Inception network expects,
                namely (-1, 1). Default: True.
            requires_grad (bool): If true, parameters of the model require
                gradients. Possibly useful for finetuning the network.
                Default: False.
            use_fid_inception (bool): If true, uses the pretrained Inception
                model used in Tensorflow's FID implementation.
                If false, uses the pretrained Inception model available in
                torchvision. The FID Inception model has different weights
                and a slightly different structure from torchvision's
                Inception model. If you want to compute FID scores, you are
                strongly advised to set this parameter to true to get
                comparable results. Default: True.
        r   z%Last possible output block index is 3TF)Ú
pretrainedÚinit_weights)r   r   )Úkernel_sizeÚstrider   )r   r   )Zoutput_sizeN)&Úsuperr   Ú__init__Úresize_inputÚnormalize_inputÚsortedÚoutput_blocksÚmaxÚlast_needed_blockÚnnZ
ModuleListÚblocksÚfid_inception_v3r   Úinception_v3Ú	TypeErrorZConv2d_1a_3x3ZConv2d_2a_3x3ZConv2d_2b_3x3Z	MaxPool2dÚappendZ
SequentialZConv2d_3b_1x1ZConv2d_4a_3x3ÚMixed_5bÚMixed_5cÚMixed_5dZMixed_6aÚMixed_6bÚMixed_6cÚMixed_6dÚMixed_6eZMixed_7aÚMixed_7bÚMixed_7cZAdaptiveAvgPool2dÚ
parametersÚrequires_grad)Úselfr   r   r   r(   Zuse_fid_inceptionÚ	inceptionZblock0Zblock1Zblock2Zblock3Úparam©Ú	__class__© ú`/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/archs/inception.pyr       sL    #


þ
ø

þzInceptionV3.__init__c                 C   sp   g }| j rtj|dddd}| jr.d| d }t| jƒD ]2\}}||ƒ}|| jv r\| |¡ || jkr8 qlq8|S )ay  Get Inception feature maps.

        Args:
            x (Tensor): Input tensor of shape (b, 3, h, w).
                Values are expected to be in range (-1, 1). You can also input
                (0, 1) with setting normalize_input = True.

        Returns:
            list[Tensor]: Corresponding to the selected output block, sorted
            ascending by index.
        )é+  r0   ZbilinearF)ÚsizeÚmodeZalign_cornersr   r   )	r   ÚFZinterpolater   Ú	enumerater   r   r   r   )r)   ÚxÚoutputÚidxÚblockr.   r.   r/   Úforward|   s    


zInceptionV3.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZDEFAULT_BLOCK_INDEXZBLOCK_INDEX_BY_DIMr   r9   Ú__classcell__r.   r.   r,   r/   r      s   üû\r   c                  C   sê   zt jddddd} W n" ty8   t jdddd} Y n0 tddd| _tdd	d| _td
d	d| _tddd| _tddd| _	tddd| _
tddd| _tdƒ| _tdƒ| _tj t¡rÐtjtdd„ d}nttdd}|  |¡ | S )ab  Build pretrained Inception model for FID computation.

    The Inception model for FID computation uses a different set of weights
    and has a slightly different structure than torchvision's Inception.

    This method first constructs torchvision's Inception and then patches the
    necessary parts that are different in the FID Inception model.
    ið  F)Únum_classesÚ
aux_logitsr   r   )r?   r@   r   r	   é    )Úpool_featuresé   r   i   r
   é€   )Úchannels_7x7é    i   r   c                 S   s   | S ©Nr.   )ZstorageÚlocr.   r.   r/   Ú<lambda>µ   ó    z"fid_inception_v3.<locals>.<lambda>)Zmap_locationT)Úprogress)r   r   r   ÚFIDInceptionAr   r   r    ÚFIDInceptionCr!   r"   r#   r$   ÚFIDInceptionE_1r%   ÚFIDInceptionE_2r&   ÚosÚpathÚexistsÚLOCAL_FID_WEIGHTSÚtorchÚloadr   ÚFID_WEIGHTS_URLZload_state_dict)r*   Z
state_dictr.   r.   r/   r   ›   s$    	


r   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rL   z,InceptionA block patched for FID computationc                    s   t t| ƒ ||¡ d S rG   )r   rL   r   )r)   Úin_channelsrB   r,   r.   r/   r   À   s    zFIDInceptionA.__init__c                 C   sr   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}tj|ddddd}|  |¡}||||g}t	 
|d¡S ©Nr   r   F©r   r   ÚpaddingZcount_include_pad)Ú	branch1x1Zbranch5x5_1Zbranch5x5_2Úbranch3x3dbl_1Úbranch3x3dbl_2Zbranch3x3dbl_3r3   Ú
avg_pool2dÚbranch_poolrT   Úcat)r)   r5   r[   Z	branch5x5Úbranch3x3dblr_   Úoutputsr.   r.   r/   r9   Ã   s    






zFIDInceptionA.forward©r:   r;   r<   r=   r   r9   r>   r.   r.   r,   r/   rL   ½   s   rL   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rM   z,InceptionC block patched for FID computationc                    s   t t| ƒ ||¡ d S rG   )r   rM   r   )r)   rW   rE   r,   r.   r/   r   Ù   s    zFIDInceptionC.__init__c                 C   s   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}t	j
|ddddd}|  |¡}||||g}t |d¡S rX   )r[   Zbranch7x7_1Zbranch7x7_2Zbranch7x7_3Zbranch7x7dbl_1Zbranch7x7dbl_2Zbranch7x7dbl_3Zbranch7x7dbl_4Zbranch7x7dbl_5r3   r^   r_   rT   r`   )r)   r5   r[   Z	branch7x7Zbranch7x7dblr_   rb   r.   r.   r/   r9   Ü   s    









zFIDInceptionC.forwardrc   r.   r.   r,   r/   rM   Ö   s   rM   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rN   z2First InceptionE block patched for FID computationc                    s   t t| ƒ |¡ d S rG   )r   rN   r   ©r)   rW   r,   r.   r/   r   õ   s    zFIDInceptionE_1.__init__c                 C   sž   |   |¡}|  |¡}|  |¡|  |¡g}t |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}t |d¡}t
j|ddddd}|  |¡}||||g}t |d¡S )Nr   r   FrY   )r[   Úbranch3x3_1Úbranch3x3_2aÚbranch3x3_2brT   r`   r\   r]   Úbranch3x3dbl_3aÚbranch3x3dbl_3br3   r^   r_   ©r)   r5   r[   Z	branch3x3ra   r_   rb   r.   r.   r/   r9   ø   s     

þ

þ
zFIDInceptionE_1.forwardrc   r.   r.   r,   r/   rN   ò   s   rN   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rO   z3Second InceptionE block patched for FID computationc                    s   t t| ƒ |¡ d S rG   )r   rO   r   rd   r,   r.   r/   r     s    zFIDInceptionE_2.__init__c                 C   sœ   |   |¡}|  |¡}|  |¡|  |¡g}t |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}t |d¡}t
j|dddd}|  |¡}||||g}t |d¡S )Nr   r   )r   r   rZ   )r[   re   rf   rg   rT   r`   r\   r]   rh   ri   r3   Z
max_pool2dr_   rj   r.   r.   r/   r9     s     

þ

þ
zFIDInceptionE_2.forwardrc   r.   r.   r,   r/   rO     s   rO   )rP   rT   Ztorch.nnr   Ztorch.nn.functionalZ
functionalr3   Ztorch.utils.model_zoor   Ztorchvisionr   rV   rS   ÚModuler   r   r*   Z
InceptionArL   Z
InceptionCrM   Z
InceptionErN   rO   r.   r.   r.   r/   Ú<module>   s    "!