diff options
| author | gdamms <damguillotin@gmail.com> | 2024-06-25 09:31:24 +0200 |
|---|---|---|
| committer | gdamms <damguillotin@gmail.com> | 2024-06-25 09:31:24 +0200 |
| commit | 1011f68c3eafb8b468cf1df23117b98c7c7b55ed (patch) | |
| tree | ccfc0bcddeb5402d2001b08e9a283ed1ab7f36cf | |
| parent | 47b3bc6fbfed3360e7526dcf5275f9175e0fde10 (diff) | |
| download | diffusion-mnist-1011f68c3eafb8b468cf1df23117b98c7c7b55ed.tar.gz diffusion-mnist-1011f68c3eafb8b468cf1df23117b98c7c7b55ed.zip | |
implement ation JS divergence
| -rw-r--r-- | main.py | 6 | ||||
| -rw-r--r-- | utils.py | 32 |
2 files changed, 30 insertions, 8 deletions
@@ -423,5 +423,11 @@ if __name__ == '__main__': kl_score = kl(reals, fakes) print(f"KL divergence: {kl_score}") + rkl_score = kl(fakes, reals) + print(f"Reverse KL divergence: {rkl_score}") + + jsd_score = jsd(reals, fakes) + print(f"Jensen-Shannon divergence: {jsd_score}") + # plt.show() @@ -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 |
