DC-GAN 生成错误图像

问题描述

我想在 Tensorflow 2.4.0 中从头开始构建 DC-GAN 算法。我正在使用我自己的数据集,我可以将其加载到我的 DC-GAN 模型中。现在我想训练我的模型并且训练有效但输出不正确。我正在使用 DCGAN (https://www.tensorflow.org/tutorials/generative/dcgan) 的官方 tensorflow 教程。在我的训练集中是机器人的图像,但生成生成灰色图像。我已经对我的模型进行了 500 多个 epoch 的训练,但是灰色图像变得越来越白。我不明白为什么生成不生成机器人的操纵图像,训练数据的内容很好,算法基于 tensorflow 教程。

images in the training dataset

images generated by the enerator

我的代码在这里

import tensorflow as tf
from tensorflow.keras import layers
from dataHandler.DataLoader.loadData1 import load_dataset #function to load dataset
from dataHandler.preProcessData.configuratorPath import * #path of the files 

BUFFER_SIZE = 60000
BATCH_SIZE = 256

train_dataset=load_dataset(tfRecordpath,classesPath,size=64)
train_dataset=train_dataset.shuffle(BUFFER_SIZE)
train_dataset=train_dataset.batch(BATCH_SIZE)

EPOCHS = 50
noise_dim = 100
num_examples_to_generate = 16
seed = tf.random.normal([num_examples_to_generate,noise_dim])


def make_generator_model():
    # In Keras,you assemble layers to build models. A model is (usually) a graph of layers. The most common type of model is a stack of layers
    model = tf.keras.Sequential()

    """
    Add a densely-connected layer to the model
    the model will take as input arrays of shape (*,100).
    And and output arrays of shape (*,7*7*256)

    after the first layer,you don't need to specify the size of the input anymore
    Afterwards,we do automatic shape inference
    """
    model.add(layers.Dense(4 * 4 * 1024,use_bias=False,input_shape=(100,)))

    """
    You can think about batch normalization as doing preprocessing at every layer of the network.
    Accelerating Deep Network Training by Reducing Internal Covariate Shift
    """
    model.add(layers.Batchnormalization())

    """
    ReLU is linear (identity) for all positive values,and zero for all negative values. 
    Leaky ReLU has a small slope for negative values,instead of altogether zero. For example,leaky ReLU may have y = 0.01x when x < 0
    """
    model.add(layers.LeakyReLU())

    # reshape the output from something flattened to something with a shape of (7,7,256)
    model.add(layers.Reshape((4,4,1024)))
    assert model.output_shape == (None,1024)  # Note: None is the batch size

    """
    The generator uses a transposed convolutional layer (Upsampling) layers to produce an image from seed (random noise).

    128 is the dimensionality of the output space
    (5,5) specifies the height and width of the 2D convolution window
    strides = (1,1) specifies the strides of the convolution along the height and width 

    """
    model.add(layers.Conv2DTranspose(512,(5,5),strides=(2,2),padding="same",use_bias=False))
    assert model.output_shape == (None,8,512)
    model.add(layers.Batchnormalization())
    model.add(layers.LeakyReLU())

    # Another transposed convolutional layer (upsampling)
    model.add(layers.Conv2DTranspose(256,16,256)
    model.add(layers.Batchnormalization())
    model.add(layers.LeakyReLU())

    # Another transposed convolutional layer (upsampling)
    model.add(layers.Conv2DTranspose(128,32,128)
    model.add(layers.Batchnormalization())
    model.add(layers.LeakyReLU())

    # Final output layer also a convolutional layer (upsampling),sigmoid goes from 0 to 1
    model.add(layers.Conv2DTranspose(3,activation="sigmoid"))
    # assert model.output_shape == (None,64,3)
    assert model.output_shape == (None,3)
    return model


noise = tf.random.normal([1,100])

generator = make_generator_model()

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

    #model.add(layers.Conv2D(64,padding='same',#input_shape=[64,3]))
    model.add(layers.Conv2D(64,input_shape=[64,3]))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(
        0.3))  # Dropout consists in randomly setting a fraction rate of input units to 0 at each update during training time,which helps prevent overfitting.

    model.add(layers.Conv2D(128,padding='same'))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(0.3))

    """
    Flattens the input. Does not affect the batch size.
    If inputs are shaped (batch,) without a channel dimension,then flattening adds an extra channel dimension and output shapes are (batch,1).
    """
    model.add(layers.Flatten())
    model.add(layers.Dense(1))
    return model




cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def discriminator_loss(real_output,fake_output):
    real_loss = cross_entropy(tf.ones_like(real_output),real_output)
    fake_loss = cross_entropy(tf.zeros_like(fake_output),fake_output)
    total_loss = real_loss + fake_loss
    return total_loss

def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output),fake_output)

generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator=make_discriminator_model()




# Notice the use of `tf.function`
# This annotation causes the function to be "compiled".
@tf.function
def train_step(images):
    noise = tf.random.normal([BATCH_SIZE,noise_dim])

    with tf.GradientTape() as gen_tape,tf.GradientTape() as disc_tape:
      generated_images = generator(noise,training=True)

      real_output = discriminator(images,training=True)
      fake_output = discriminator(generated_images,training=True)

      gen_loss = generator_loss(fake_output)
      disc_loss = discriminator_loss(real_output,fake_output)

    gradients_of_generator = gen_tape.gradient(gen_loss,generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss,discriminator.trainable_variables)

    generator_optimizer.apply_gradients(zip(gradients_of_generator,generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,discriminator.trainable_variables))

def train(dataset,epochs):
    for epoch in range(1,epochs):
        for image_batch in dataset:
            train_step(image_batch)


train(train_dataset,EPOCHS)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)