[Python] Fréchet inception distance (FID) Score for same vectors is not zero

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to use the implementation of FID score from this resource: https://machinelearningmastery.com/how-to-implement-the-frechet-inception-distance-fid-from-scratch/ but when I run the FID score for the same vectors (act1) I expected the FID score to be 0, instead I am getting a large number. Where am I going wrong?

    # example of calculating the frechet inception distance
    import numpy
    from numpy import cov
    from numpy import trace
    from numpy import iscomplexobj
    from numpy.random import random
    from scipy.linalg import sqrtm

    # calculate frechet inception distance
    def calculate_fid(act1, act2):
    # calculate mean and covariance statistics
    mu1, sigma1 = act1.mean(axis=0), cov(act1, rowvar=False)
    mu2, sigma2 = act2.mean(axis=0), cov(act2, rowvar=False)
    # calculate sum squared difference between means
    ssdiff = numpy.sum((mu1 - mu2)**2.0)
    # calculate sqrt of product between cov
    covmean = sqrtm(sigma1.dot(sigma2))
    # check and correct imaginary numbers from sqrt
    if iscomplexobj(covmean):
    covmean = covmean.real
    # calculate score
    fid = ssdiff + trace(sigma1 + sigma2 - 2.0 * covmean)
    return fid

    # define two collections of activations
    act1 = random(10*2048)
    act1 = act1.reshape((10,2048))
    act2 = random(10*2048)
    act2 = act2.reshape((10,2048))
    # fid between act1 and act1
    fid = calculate_fid(act1, act1)
    print('FID (same): %.3f' % fid)
    # fid between act1 and act2
    fid = calculate_fid(act1, act2)
    print('FID (different): %.3f' % fid)


    Results:

    FID (same): -66113130760175032991744.000
    FID (different): -55213970774324510299478046898216203619608871777363092441300193790394368.000


    I was expecting the fid score for the same vectors to be zero but it is coming up as large number which was contrary to what I understood of FID scores and what is demonstrated in the source linked above.

    Continue reading...

Compartilhe esta Página