From 577b5708f0a3975083329f05e6e4b118987f9828 Mon Sep 17 00:00:00 2001 From: gdamms Date: Wed, 19 Jun 2024 16:59:43 +0200 Subject: mise en place fid --- utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 utils.py (limited to 'utils.py') diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..19a0426 --- /dev/null +++ b/utils.py @@ -0,0 +1,25 @@ +import numpy as np + + +def fid(reals, fakes): + """FID score calculation. + + Args: + reals (numpy.array): Real images. + fakes (numpy.array): Fake images. + """ + print(reals.shape, fakes.shape) + reals = reals.reshape(reals.shape[0], -1) + fakes = fakes.reshape(fakes.shape[0], -1) + + mu_real = np.mean(reals, axis=0) + mu_fake = np.mean(fakes, axis=0) + sigma_real = np.cov(reals, rowvar=False) + sigma_fake = np.cov(fakes, rowvar=False) + + diff = mu_real - mu_fake + covmean = np.dot(sigma_real, sigma_fake.T) + covmean = np.sqrt(covmean * (covmean > 0)) + print(np.trace(covmean)) + + return diff @ diff + np.trace(sigma_real) + np.trace(sigma_fake) - 2 * np.trace(covmean) -- cgit v1.3.1