Search code examples
tensorflowkerasconv-neural-networkdcgan

What is the code and kernel value of the respective CNN(DCGAN) model? TF KERAS


I am trying to replicate a paper, "Unsupervised fabric defect detection based on a deep convolutional generative adversarial network", by Guanghua Hu. It's a DCGAN, but has also a inverter, that is similar to the discriminator.

My question is how should I transfer to code the model? I will post the pictures of the model and a explanatory table. And also the code I tried to do.

Here are the models(Discriminator and the Inverter)

enter image description here

Here is the table explaining the model.

MY MAIN QUESTION IS THE LAYER 1 HAS NO KERNEL VALUE, HOW SHOULD I DEAL WITH THAT?

enter image description here

Here is the code I tried to do. Correct me what is wrong please.

def make_discriminator_model():
    model = tf.keras.Sequential()
    model.add(layers.Conv2D(64,kernel_size=(3, 3), strides=(2, 2), padding='same', input_shape=[32, 32, 1]))
    model.add(layers.LeakyReLU())
    #print("First conv discriminator:",y.shape)
   
    
    model.add(layers.Conv2D(128, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 8, 8, 128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Conv2D(192, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 4, 4, 192)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Conv2D(256, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 2, 2, 256)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Flatten())
    model.add(layers.Dense(1,activation='sigmoid'))

    return model


def make_inverter_model():
    model = tf.keras.Sequential()
    model.add(layers.Conv2D(64, kernel_size=(3, 3), strides=(2, 2), padding='same', input_shape=[32, 32, 1]))
    model.add(layers.LeakyReLU())

   
    
    model.add(layers.Conv2D(128, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 8, 8, 128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Conv2D(192, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 4, 4, 192)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Conv2D(256, kernel_size=(3, 3), strides=(2, 2), padding='same'))
    #assert model.output_shape == (None, 2, 2, 256)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())


    model.add(layers.Flatten())
    model.add(layers.Dense(64,activation='tanh',use_bias=False,)) #layers.Dense(2*2*256, use_bias=False,
    #model.add(layers.Activation('tanh'))
    #model.add(layers.Dense(64,activation='sigmoid'))

    return model



Solution

  • To answer your main question, from the diagram you have posted, you have correctly used 3x3 kernel in the first layer.

    And rest of the code looks correct to me. Are you facing some error?