aboutsummaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authorgdamms <damguillotin@gmail.com>2024-06-25 09:31:24 +0200
committergdamms <damguillotin@gmail.com>2024-06-25 09:31:24 +0200
commit1011f68c3eafb8b468cf1df23117b98c7c7b55ed (patch)
treeccfc0bcddeb5402d2001b08e9a283ed1ab7f36cf /utils.py
parent47b3bc6fbfed3360e7526dcf5275f9175e0fde10 (diff)
downloaddiffusion-mnist-1011f68c3eafb8b468cf1df23117b98c7c7b55ed.tar.gz
diffusion-mnist-1011f68c3eafb8b468cf1df23117b98c7c7b55ed.zip
implement ation JS divergence
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py32
1 files changed, 24 insertions, 8 deletions
diff --git a/utils.py b/utils.py
index 4e680a8..509bd11 100644
--- a/utils.py
+++ b/utils.py
@@ -47,13 +47,27 @@ def kl(reals, fakes):
hist_real = np.apply_along_axis(lambda a: np.histogram(a, bins=40, range=(-1, 1))[0], 1, reals)
hist_fake = np.apply_along_axis(lambda a: np.histogram(a, bins=40, range=(-1, 1))[0], 1, fakes)
- plt.figure()
- colors = ['#ff0000', '#00ff00', '#0000ff']
- for i in range(reals.shape[0]):
- plt.plot(hist_real[i], label='real', color=colors[i])
- plt.plot(hist_fake[i], label='fake', color=colors[i], linestyle='dashed')
- plt.legend()
- plt.savefig('hist.tmp.png')
+ hist_real = hist_real + 1
+ hist_fake = hist_fake + 1
+
+ hist_real = hist_real / np.sum(hist_real)
+ hist_fake = hist_fake / np.sum(hist_fake)
+
+ return np.mean(np.log(hist_real / hist_fake))
+
+
+def jsd(reals, fakes):
+ """Jensen-Shannon divergence calculation.
+
+ Args:
+ reals (numpy.array): Real images.
+ fakes (numpy.array): Fake images.
+ """
+ reals = reals.transpose(1, 0, 2, 3).reshape(reals.shape[1], -1)
+ fakes = fakes.transpose(1, 0, 2, 3).reshape(fakes.shape[1], -1)
+
+ hist_real = np.apply_along_axis(lambda a: np.histogram(a, bins=40, range=(-1, 1))[0], 1, reals)
+ hist_fake = np.apply_along_axis(lambda a: np.histogram(a, bins=40, range=(-1, 1))[0], 1, fakes)
hist_real = hist_real + 1
hist_fake = hist_fake + 1
@@ -61,4 +75,6 @@ def kl(reals, fakes):
hist_real = hist_real / np.sum(hist_real)
hist_fake = hist_fake / np.sum(hist_fake)
- return np.mean(np.log(hist_real / hist_fake)) \ No newline at end of file
+ hist_avg = (hist_real + hist_fake) / 2
+
+ return 0.5 * (np.mean(np.log(hist_real / hist_avg)) + np.mean(np.log(hist_fake / hist_avg))) \ No newline at end of file