Title

Fréchet distance (numpy)

Description

$$FD=\left|\mu-\mu_w\right|^2+tr\left(\Sigma+\Sigma_w-2\left(\Sigma\Sigma_w\right)^{\frac{1}{2}}\right)$$

Hints

numpy cov

numpy trace

In [0]:
# calculate frechet inception distance with numpy
import numpy as np
from scipy.linalg import sqrtm
In [0]:
# calculate frechet inception distance
def calculate_fd(act1, act2):
	# calculate mean and covariance statistics
	# The “mu_1” and “mu_2” refer to the feature-wise mean of the real 
	# and generated images, e.g. 2,048 element vectors where each 
	# element is the mean feature observed across the images.
	mu1, mu2 = ___
	
	# The sigma1 and sigma2 are the covariance matrix for the real 
	# and generated feature vectors. 
	# Remember each row is a sample/image.
	sigma1, sigma2 = ___
	
	# calculate sum squared difference between means
	ssdiff = np.sum((mu1 - mu2)**2.0)
	# calculate sqrt of product between cov
	covmean = sqrtm(sigma1.dot(sigma2))
	# check and correct imaginary numbers from sqrt
	if np.iscomplexobj(covmean):
		covmean = covmean.real

	# calculate score
	fd = ___

	return fd
In [0]:
### edTest(test_check) ###
# define two collections of activations
np.random.seed(109)
# We assume 10 images
# The output layer of the Inception model is removed and the output 
# is taken as the activations from the last pooling layer, hence 2048.
act1 = np.random.random(10*2048)
act1 = act1.reshape((10,2048))
np.random.seed(295)
act2 = np.random.random(10*2048)
act2 = act2.reshape((10,2048))
# fd between act1 and act1
fd = calculate_fd(act1, act1)
print('FD (same): %.3f' % fd)
# fd between act1 and act2
fd = calculate_fd(act1, act2)
print('FD (different): %.3f' % fd)
In [2]:

In [3]:

In [4]: