0

I am implementing general adversarial network model on my own data set but I encountered error when calculating discriminator loss on real data, how to resolve the error? Is it due to shape mismatch due to colored images?

img_rows = 28
img_cols = 28
channels = 1
latent_dim = 100
img_shape = (img_rows, img_cols, channels)

The following is the discriminator function:

def build_discriminator():

    model = Sequential()

    model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=img_shape, padding="same"))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dropout(0.25))
    model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
    model.add(ZeroPadding2D(padding=((0,1),(0,1))))
    model.add(BatchNormalization(momentum=0.8))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dropout(0.25))
    model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dropout(0.25))
    model.add(Conv2D(256, kernel_size=3, strides=1, padding="same"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(1, activation='sigmoid'))

    model.summary()

    img = Input(shape=img_shape)
    validity = model(img)

    return Model(img, validity)

The following is the Generator function:

def build_generator():

    model = Sequential()

    model.add(Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))
    model.add(Reshape((7, 7, 128)))
    model.add(UpSampling2D())
    model.add(Conv2D(128, kernel_size=3, padding="same"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(Activation("relu"))
    model.add(UpSampling2D())
    model.add(Conv2D(64, kernel_size=3, padding="same"))
    model.add(BatchNormalization(momentum=0.8))
    model.add(Activation("relu"))
    model.add(Conv2D(channels, kernel_size=3, padding="same"))
    model.add(Activation("tanh"))

    model.summary()

    noise = Input(shape=(latent_dim,))
    img = model(noise)
    return Model(noise,img)

The following is the training function:

def train(epochs, batch_size=128):

    # Adversarial ground truths
    valid = np.ones((batch_size, 1)
    fake = np.zeros((batch_size, 1))

    for epoch in range(epochs):

        # ---------------------
        #  Train Discriminator
        # ---------------------

        # Select a random half of images
        idx = np.random.randint(0, images.shape[0], batch_size)
        imgs = images[idx]

        # Sample noise and generate a batch of new images
        noise = np.random.normal(0, 1, (batch_size, latent_dim))
        gen_imgs = generator.predict(noise)

        d_loss_real = discriminator.train_on_batch(imgs,valid)
        d_loss_fake = discriminator.train_on_batch(gen_imgs, fake)
        d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

        # ---------------------
        #  Train Generator
        # ---------------------

        # Train the generator (wants discriminator to mistake images as real)
        g_loss = combined.train_on_batch(noise, valid)

        # Plot the progress
        print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

train(epochs=2, batch_size=12)
Hamsa
  • 476
  • 5
  • 23
  • Where are you getting the error exactly? Seems like you are passing output of discriminator to the discriminator because, discriminator is expecting an image of 4 dimensions, and output of discriminator is `(batch_size, 1)` and `batch_size=12`. – Vitrioil Jun 05 '19 at 09:54
  • Actually I'm not able to change training set input shape from (28,28,1) to any other arbitrary shape be it RGB or Gray-Scale for eg (112,112,1) or (112,112,3). I'm using DC Gan architecture – Hamsa Jun 06 '19 at 10:07
  • Well you cannot reshape `(28, 28, 1)` to `(num, num, 1)` for any `num`. My question is which line is giving the error? And also provide the generator code. – Vitrioil Jun 07 '19 at 07:48
  • @Vitrioil I have provided the generator code.Although this particular issue I have solved myself. Whatever be the input shape, the generator function produces (28x28) output. My testing images are quite big than training images, so I have thought to keep my testing images to be (112x112), will it be an efficient model? in a way that generator is convolving on a bigger area than discriminator – Hamsa Jun 09 '19 at 14:52
  • Oh, so your test size is different. Maybe try downsampling test set to `(28, 28, 1)` so that it remains consistent with the training data. Upscaling is also an option to upscale your training data to `(112, 112, 1)` but that will result in loss of information. – Vitrioil Jun 10 '19 at 12:11
  • Thanks! for your help – Hamsa Jun 11 '19 at 07:49
  • I will have to downsample it to 1/4th size so i have used the code `model.add(MaxPooling2D(pool_size=(4, 4), strides=None, padding='valid'))` before the `model.add(Dense(128 * 7 * 7, activation="relu", input_dim=latent_dim))` line in the generator function, but it seems to not compile the generator model. If I use it after the dense layer, it gives a shape mismatch error. Where am I missing out? -----------Thanks in advance – Hamsa Jun 12 '19 at 09:33
  • I would suggest downsampling in preprocessing. Also, `MaxPooling2D` deals with images while generator receives latent vector as an input which is what `Dense` is expecting. – Vitrioil Jun 12 '19 at 15:45

0 Answers0