# GAN model predictions before training is predictable

I have a dataset of 3000 8x8 images, and I would like to train a GAN for an image generation purpose.

I am planning to start with a simple GAN model and see if it overfits. Before training, I try to do a comparison of the discriminator model prediction using real image input against the whole GAN model prediction using random seed input. My thought process is that since this model is not trained yet, the output for real images and fake images by the discriminator should not be predictable.

However, the discriminator model prediction using real image input always returns a value very close to 1.0, and the whole GAN model prediction using random seed input always returns a value near 0.5 with a small deviation. I suspect that during training, the model would simply pull the 0.5 value near 0.0 and would never actually learn from the dataset.

I try to increase the training parameters and different initializers, but the output is still the same.

By ruling out the possibility a bad dataset, what could be the reason for this situation?

This is some sneak peek of the generator and discriminator model building: https://pastebin.com/ehMDP7k6

• what do you mean by "a comparison of the discriminator model prediction ... against the whole GAN model prediction"? do you mean the output of your generator for fake and real images? – Aray Karjauv Nov 16 '20 at 19:29
• In other words, I feed real images array into the discriminator model, and it outputs values close to 1.0. Next, I feed fake images (the one that is generated by the generator) into the discriminator model, and it outputs values close to 0.5. These happens too often, and it is very predictable. – dee cue Nov 17 '20 at 11:18
• Did you try to generate fake images? Since the main goal of GANs to produce synthetic samples, the main matric will be the quality of generated images. Take a look at this answer. There is a plot of G loss and D loss of DCGAN. We can see that D loss is near 0, whereas G loss has oscillations, but G was able to produce good samples. – Aray Karjauv Nov 17 '20 at 11:29
• Yes, and it only generates noises. Although, I do notice some preference of a certain pallete, and I think this is a good start. The D accuracy went constant at 0.5 after 20 epochs, and the G accuracy went up to 1.0, and both losses gradually went down. All metrics did oscillate in the middle of the training but in small amount of deviation. Did my model overfit at epoch 20? – dee cue Nov 17 '20 at 11:54

I took a look at your model. It seems you have incorrect architecture. The Conv2D layers in your D should have following params: (n_filters, kernel=3, padding='same'), where n_filters is the number of filters and it usually should be doubled as per DCGAN architecture. You can also use strides but since your images are small it won't make any sense.

The D network also should not include UpSampling2D layer. This layer can be used in G network instead of Conv2DTranspose.

Your images should be normalized to the range of [-1, 1] and the activation function of G should be tanh.

You also included m.add(keras.layers.Reshape((8, 8, 3))) to your G network which is the final size of your data. Since you have the final size in the first layers you don't have to include upsampling Conv2DTranspose layers.

Your final model should look like

def make_discriminator_model():
model = tf.keras.Sequential()
input_shape=[8, 8, 1]))

return model

def make_generator_model():
model = tf.keras.Sequential()

return model


Note that I did not test the model, so you should adapt it to the shape of your data.

I encourage you to follow this tutorial.

Update regarding overfitting

GANs do not have overfitting problem in the classical sense. Instead, vanilla GAN have other problems like vanishing gradient. It happens when the D becomes overconfident regarding fake samples. In that case, D stops providing useful information to the G, since its error becomes 0. To find out if training fell into this problem, you should plot D and G loss. It will look as follows:

Another problem is the mode collapse. In that case, the G tricks the D by producing only one type of samples which looks realistic but G won't represent the real data distribution. Therefore, the generated samples will be homogeneous.

For more information, see Improved Techniques for Training GANs and Wasserstein GAN.

• Thank you for the guidance. I certainly built my models with baseless knowledge, and most importantly, I forgot to normalise my image array before feeding into the discriminator, which would explain a lot at my initial problem. I accepted this answer for raising the normalisation problem, and upvoted for the extra guidance. – dee cue Nov 17 '20 at 14:38
• I forgot to mention the overfitting issue. I updated the answer accordingly – Aray Karjauv Nov 17 '20 at 15:43