3

我在 Keras 中实现了一个生成对抗网络。我的训练数据大小约为 16,000,其中每张图像的大小为 32*32。我所有的训练图像都是来自 imagenet 数据集的图像在对象检测任务方面的调整大小版本。我将图像矩阵直接输入到网络中,而不进行中心裁剪。我使用了 AdamOptimizer,学习率为 1e-4,beta1 为 0.5,我还将 dropout 率设置为 0.1。我首先在 3000 个真实图像和 3000 个假图像上训练了判别器,它达到了 93% 的准确率。然后,我训练了 500 个 epoch,批量大小为 32。但是,我的模型似乎只在几个 epoch(<10)内收敛,并且它生成的图像很难看。

损失函数图

生成器生成的随机样本

我想知道我的训练数据集是否太小(与 DCGAN 论文中的那些超过 300,000 相比)或我的模型配置不正确。更重要的是,我是否应该按照 Ian Goodfellow 在原始论文中的建议在 D 上训练 SGD 进行 k 次迭代(其中 k 很小,可能是 1),然后在 G 上使用 SGD 进行一次迭代训练?(我刚刚尝试训练他们一次一个)

下面是生成器的配置。

g_input = Input(shape=[100])
H = Dense(1024*4*4, init='glorot_normal')(g_input)
H = BatchNormalization(mode=2)(H)
H = Activation('relu')(H)
H = Reshape( [4, 4,1024] )(H)
H = UpSampling2D(size=( 2, 2))(H)
H = Convolution2D(512, 3, 3, border_mode='same', init='glorot_uniform')(H)
H = BatchNormalization(mode=2)(H)
H = Activation('relu')(H)
H = UpSampling2D(size=( 2, 2))(H)
H = Convolution2D(256, 3, 3, border_mode='same', init='glorot_uniform')(H)
H = BatchNormalization(mode=2)(H)
H = Activation('relu')(H)
H = UpSampling2D(size=( 2, 2))(H)
H = Convolution2D(3, 3, 3, border_mode='same', init='glorot_uniform')(H)
g_V = Activation('tanh')(H)
generator = Model(g_input,g_V)
generator.compile(loss='binary_crossentropy', optimizer=opt)
generator.summary()

下面是判别器的配置:</p>

d_input = Input(shape=shp)
H = Convolution2D(64, 5, 5, subsample=(2, 2), border_mode = 'same', init='glorot_normal')(d_input)
H = LeakyReLU(0.2)(H)
#H = Dropout(dropout_rate)(H)
H = Convolution2D(128, 5, 5, subsample=(2, 2), border_mode = 'same', init='glorot_normal')(H)
H = BatchNormalization(mode=2)(H)
H = LeakyReLU(0.2)(H)
#H = Dropout(dropout_rate)(H)
H = Flatten()(H)
H = Dense(256, init='glorot_normal')(H)
H = LeakyReLU(0.2)(H)
d_V = Dense(2,activation='softmax')(H)
discriminator = Model(d_input,d_V)
discriminator.compile(loss='categorical_crossentropy', optimizer=dopt)
discriminator.summary()

下面是GAN整体的配置:

gan_input = Input(shape=[100])
H = generator(gan_input)
gan_V = discriminator(H)
GAN = Model(gan_input, gan_V)
GAN.compile(loss='categorical_crossentropy', optimizer=opt)
GAN.summary()
4

2 回答 2

1

我认为问题出在loss功能上

loss='categorical_crossentropy',
于 2021-12-25T01:40:29.477 回答
0

我怀疑您的生成器在您训练 gan 时是可训练的。可以通过使用generator.layers[-1].get_weights()来验证 GAN 训练过程中参数是否发生变化。

在将鉴别器组装到 gan 之前,您应该冻结鉴别器:

generator.trainnable = False
gan_input = Input(shape=[100])
H = generator(gan_input)
gan_V = discriminator(H)
GAN = Model(gan_input, gan_V)
GAN.compile(loss='categorical_crossentropy', optimizer=opt)
GAN.summary()

看到这个讨论: https ://github.com/fchollet/keras/issues/4674

于 2017-09-19T12:54:21.247 回答