a
    þd·  ã                   @   sj   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dd	„Z
e ¡ dd
d„ƒZddd„ZdS )é    N)Úlinalg)Útqdm)ÚInceptionV3ÚcudaTFc                 C   s(   t dg||d}t |¡ ¡  | ¡}|S )Né   )Úresize_inputÚnormalize_input)r   ÚnnZDataParallelÚevalÚto)Údevicer   r   Ú	inception© r   ú\/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/metrics/fid.pyÚload_patched_inception_v3
   s    r   c                 C   sˆ   |durt |ddd}nd}g }| D ]F}|r6| d¡ | |¡}||ƒd  |jd d¡}| | d¡¡ q$|rx| ¡  t |d¡}|S )	a^  Extract inception features.

    Args:
        data_generator (generator): A data generator.
        inception (nn.Module): Inception model.
        len_generator (int): Length of the data_generator to show the
            progressbar. Default: None.
        device (str): Device. Default: cuda.

    Returns:
        Tensor: Extracted features.
    NÚbatchZExtract)ÚtotalÚunitÚdescé   r   éÿÿÿÿÚcpu)	r   Úupdater   ÚviewÚshapeÚappendÚcloseÚtorchÚcat)Zdata_generatorr   Zlen_generatorr   ZpbarÚfeaturesÚdataÚfeaturer   r   r   Úextract_inception_features   s    

r"   çíµ ÷Æ°>c                 C   s  | j |j ksJ dƒ‚|j |j ks(J dƒ‚tj|| dd\}}t |¡ ¡ s~tdƒ t |j d ¡| }t || ||  ¡}t |¡rÆtj	t 
|¡jdddsÀt t |j¡¡}td	|› ƒ‚|j}| | }	|	|	 }
t |¡t |¡ d
t |¡  }|
| }|S )aÞ  Numpy implementation of the Frechet Distance.

    The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1)
    and X_2 ~ N(mu_2, C_2) is
        d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)).
    Stable version by Dougal J. Sutherland.

    Args:
        mu1 (np.array): The sample mean over activations.
        sigma1 (np.array): The covariance matrix over activations for
            generated samples.
        mu2 (np.array): The sample mean over activations, precalculated on an
               representative data set.
        sigma2 (np.array): The covariance matrix over activations,
            precalculated on an representative data set.

    Returns:
        float: The Frechet Distance.
    z'Two mean vectors have different lengthsz)Two covariances have different dimensionsF)ZdispzNProduct of cov matrices is singular. Adding {eps} to diagonal of cov estimatesr   gü©ñÒMbP?)ZatolzImaginary component é   )r   r   ZsqrtmÚnpÚisfiniteÚallÚprintZeyeZiscomplexobjZallcloseZdiagonalÚimagÚmaxÚabsÚ
ValueErrorÚrealÚtrace)Zmu1Zsigma1Zmu2Zsigma2ZepsZcov_sqrtÚ_ÚoffsetÚmZ	mean_diffZ	mean_normr.   Zfidr   r   r   Úcalculate_fid2   s"    
"r2   )r   TF)Nr   )r#   )Únumpyr%   r   Ztorch.nnr	   Zscipyr   r   Zbasicsr.archs.inceptionr   r   Zno_gradr"   r2   r   r   r   r   Ú<module>   s   
