Home > other >  No trained GAN generator using 3 channel produce only grey images
No trained GAN generator using 3 channel produce only grey images

Time:08-26

I'm trying to build my first GAN to generate 128x128 colored images (3 channels). This is the code of my generator :

def make_generator_model():
    model = tf.keras.Sequential()
    model.add(layers.Dense(16*16*256, use_bias=False, input_shape=(100,)))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Reshape((16, 16, 256)))
    assert model.output_shape == (None, 16, 16, 256)

    model.add(layers.Conv2DTranspose(128, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    assert model.output_shape == (None, 32, 32, 128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    assert model.output_shape == (None, 64, 64, 64)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(3, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='sigmoid'))
    assert model.output_shape == (None, 128, 128, 3)

    return model

My problem is that when I try to generate an image from my generator (without training it), it only produce some full grey images. I decided to check the RGB values of my generated image and I saw that all the values are very close to 0.5, as you can see it :

tf.Tensor(
[[[[0.5000413  0.49998185 0.5000663 ]
   [0.4999744  0.5002648  0.50000244]
   [0.49989837 0.5001663  0.500137  ]
   ...
   [0.5002731  0.50007665 0.5000753 ]
   [0.4999043  0.49969015 0.49998668]
   [0.5000766  0.5000091  0.5000697 ]]

  [[0.5000816  0.4998091  0.4998903 ]
   [0.50036883 0.49986517 0.5002175 ]
   [0.5000233  0.49990743 0.4998112 ]
   ...
   [0.50071466 0.50031507 0.5001373 ]
   [0.5007575  0.49987203 0.49977642]
   [0.49996772 0.5000209  0.49997622]]

  [[0.4999025  0.5001095  0.49996302]
   [0.5004691  0.5006067  0.5008443 ]
   [0.4999871  0.4997597  0.49996534]
   ...
   [0.49960494 0.49902678 0.49905738]
   [0.4994848  0.49970582 0.4997409 ]
   [0.500582   0.5006976  0.49881294]]
...

With all these 0.5 values, it's normal that the generated image looks grey. So my question is : why my generator produce only values that are very close to 0.5 ?

CodePudding user response:

why my generator produce only values that are very close to 0.5 ?... because you have not trained the net

If you have RGB image, to have greyscaled images, you just need to take the avg or the 3 channels, and assign to the 3 channels that mean... in other words, given 3 values for the 3 channels that are very close, the output color will be gray.

This, in addition to the initialization of sigmoid, which tends to have mean in 0, thus an activation of 0.5, leads to your greyscaled images

In other words, is fine, is the expected behavior, and you will have to train the net to see more colors

  • Related