From 2528c51f20db72754c959e57b60c892094ef87ff Mon Sep 17 00:00:00 2001 From: wucf20 Date: Sun, 26 May 2019 14:31:19 +0700 Subject: [PATCH] In the GAN notebook, generated images have the undesired effect of bright, single-colored pots. I happened to realize that it is not entirely due to GAN itself (if at all), but to the way we convert it to PIL images and plot it afterwards. This is due to the fact that the final layer of the generator model uses tanh activation, which results in neuron output in the range of [-1, 1], unlike real_images, which are in [0, 1]. So I find it best to leave, for the lines of code involving image.array_to_img() [I believe there are only three], leave real_images part unchanged, and modify generated_images part to sth like: image.array_to_img(generated_images[i], scale=True). This will remove the above-mentioned undesired effect. The reason is that the [-1, 0] part of [-1, 1] when multiplied by 255 and then converted to unit8 will have unexpected color behaviour. (The negative values will circle periodically back like in Z_256) Actually, the training process and hyperparameters of the optimizers can also be altered accordingly if we have the generator output's range be consistent with the real images range and train. --- 8.5-introduction-to-gans.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/8.5-introduction-to-gans.ipynb b/8.5-introduction-to-gans.ipynb index bd8a16babd..6b8d7bd5e3 100644 --- a/8.5-introduction-to-gans.ipynb +++ b/8.5-introduction-to-gans.ipynb @@ -611,7 +611,7 @@ " print('adversarial loss at step %s: %s' % (step, a_loss))\n", "\n", " # Save one generated image\n", - " img = image.array_to_img(generated_images[0] * 255., scale=False)\n", + " img = image.array_to_img(generated_images[0], scale=True)\n", " img.save(os.path.join(save_dir, 'generated_frog' + str(step) + '.png'))\n", "\n", " # Save one real image, for comparison\n", @@ -744,7 +744,7 @@ "generated_images = generator.predict(random_latent_vectors)\n", "\n", "for i in range(generated_images.shape[0]):\n", - " img = image.array_to_img(generated_images[i] * 255., scale=False)\n", + " img = image.array_to_img(generated_images[i], scale=True)\n", " plt.figure()\n", " plt.imshow(img)\n", " \n",