2

Update 1: When removing the batch normalisation layers it works well. Apparently the way batch normalisation works was changed between iterations. Still investigating.

Update 2: Questions below report two possible remedies; changing the momentum value in batchnormalisation (this didn't work for me) and simply commenting out the batchnorm in the discriminator. Commenting out BN in the discriminator seems to work for me. No idea why yet.

Questions/links reporting similar problems:

Poor Result with BatchNormalization

https://datascience.stackexchange.com/questions/56860/dc-gan-with-batch-normalization-not-working

Keras train partial model issue (about GAN model)

https://medium.com/@nagabhushansn95/using-batch-normalization-in-discriminator-is-making-my-dc-gan-model-to-not-work-8c0b4a869a2a


Question Starts:

I'm trying to run a DCGAN from "GANs in Action". The GAN generates images from the MNIST dataset.

The source code can be found here GANs in action github page

The code performs well and the generated images are good.

When I change the source code so that it becomes compatible with tf.keras rather than keras, the DCGAN model's ability to generate images becomes useless.

The following is the only bit of the code I've changed. From:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from keras.datasets import mnist
from keras.layers import Activation, BatchNormalization, Dense, Dropout, Flatten, Reshape
from keras.layers import LeakyReLU
from keras.layers import Conv2D, Conv2DTranspose
from keras.models import Sequential
from keras.optimizers import Adam

to:

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Activation, BatchNormalization, Dense, Dropout, Flatten, Reshape
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Conv2D, Conv2DTranspose
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

I've even tried to enforce tf_1 compatability with

tf.compat.v1.disable_v2_behavior()

-performance remains poor.

Why is this? Have I missed something obvious?

RNs_Ghost
  • 1,687
  • 5
  • 25
  • 39
  • I encountered this a while ago - if I recall correctly, default learning rate differs between tf.keras and keras. I can't seem to find it anymore, but I believe tf.keras has a default of 1e-3 while keras has a default of 1. Not 100% sure, but worth checking. – Stanley Nov 09 '20 at 18:40
  • thanks @StanleyZheng - I don't suppose you know which optimiser that's for? I'm using adam – RNs_Ghost Nov 09 '20 at 19:40
  • The default for every optimizer - if you don't set lr, then learning rate will be different. – Stanley Nov 10 '20 at 00:07
  • @StanleyZheng Batchnormalisation seems suspect, see my edit – RNs_Ghost Nov 11 '20 at 16:49
  • 1
    Hmm, that's super interesting, never seen this before. Good job figuring that out, it would take me forever. – Stanley Nov 11 '20 at 23:25

1 Answers1

1

Another solution is to import the previous version of the BatchNormalization by

from tensorflow.compat.v1.keras.layers import BatchNormalization

And then use the original settings from the book. That worked for me.

Morc
  • 381
  • 2
  • 9