(1)GAN
将生成数据与复杂的现实数据进行拟合的主要方法之一。引入各种新颖的架构和训练对象,以解决人们对原有理念的感知不足,从而在实践中实现更稳定的训练和更真实的生成模型。
损失函数:训练初期, 由于G 没有得到较好的训练,生成样本很差,D 会以高置信度的概率来拒绝初期生成的样本,导致log(1−D(G(z)))达到饱和,无法提供足够的梯度来更新 G。于是,采用最大化log(D(G(z)))来代替最小化log(1−D(G(z)))更新 G的参数。(实践时转化为最小化形式,加负号即可)
#最大化真实样本判为真的概率,最大化生成样本判为假的概率
#最大化生成样本判为真的概率
(2)DCGAN
DAGAN是GAN比较优秀的改进,主要是为GAN提供了一个非常良好的拓扑结构,提高了GAN的稳定性和生成质量。
其网络架构可以很好的与其他技术相结合,为GAN的发展提供了非常显著的贡献。
DCGAN使用卷积层代替了全连接层,使用带步长的卷积取代了pooling层和上采样层。
几乎每一层都使用batchnorm,将特征输出归一化。
防止梯度稀疏,激活函数使用leakrelu,生成器输出层使用tanh。
DCGAN实现:
#!/usr/bin/env python # _*_coding:utf-8 _*_ #@Time :2019/4/7 16:13 #@Author :milo #@FileName: DCGAN.py import keras from keras import layers import numpy as np import os from keras.preprocessing import image latent_dim = 32 height = 32 width = 32 channels = 3 #生成器 generator_input = keras.Input(shape=(latent_dim,)) x = layers.Dense(128 * 16 * 16)(generator_input) x = layers.LeakyReLU()(x) x = layers.Reshape((16,16,128))(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2DTranspose(256, 4, strides=2, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(256, 5, padding='same')(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(channels, 7, activation='tanh', padding='same')(x) generator = keras.models.Model(generator_input, x) generator.summary() #判决器 discriminator_input = layers.Input(shape=(height, width, channels)) x = layers.Conv2D(128, 3)(discriminator_input) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Conv2D(128, 4, strides=2)(x) x = layers.LeakyReLU()(x) x = layers.Flatten()(x) x = layers.Dropout(0.4)(x) x = layers.Dense(1, activation='sigmoid')(x) discriminator = keras.models.Model(discriminator_input, x) discriminator_optimize = keras.optimizers.RMSprop( lr=0.0008, clipvalue=1.0,#优化器中使用了梯度裁剪(限制梯度值的范围) decay=1e-8#稳定训练,使用了学习率衰减 ) discriminator.compile(optimizer=discriminator_optimize, loss='binary_crossentropy') #连接生成器和判决器 discriminator.trainable = False gan_input = keras.Input(shape=(latent_dim,)) gan_output = discriminator(generator(gan_input)) gan = keras.models.Model(gan_input, gan_output) gan_optimizer = keras.optimizers.RMSprop(lr=0.0004, clipvalue=1.0, decay=1e-8) gan.compile(optimizer=gan_optimizer, loss='binary_crossentropy') #读取数据,开始训练 (x_train, y_train), (_, _) = keras.datasets.cifar10.load_data() x_train = x_train[y_train.flatten()==6] x_train = x_train.reshape((x_train.shape[0],)+ (height, width, channels)).astype('float32')/255 iterations = 10000 batch_size = 20 save_dir = 'output_dir' start = 0 for step in range(iterations): random_latent_vectors = np.random.normal(size=(batch_size,latent_dim)) #在潜在空间采样随机点 generator_image = generator.predict(random_latent_vectors)#将随机点解码为虚假图像 stop = start + batch_size real_images = x_train[start: stop] combined_images = np.concatenate([generator_image, real_images]) labels = np.concatenate([np.ones((batch_size,1)), np.zeros((batch_size, 1))]) labels += 0.05 * np.random.random(labels.shape) #向标签中添加一个噪声,这是一个很重要的技巧 d_loss = discriminator.train_on_batch(combined_images, labels) random_latent_vectors = np.random.normal(size=(batch_size, latent_dim)) misleading_targets = np.zeros((batch_size, 1)) a_loss = gan.train_on_batch(random_latent_vectors, misleading_targets) start += batch_size if start > len(x_train) - batch_size: start = 0 if start % 100 == 0: gan.save_weights('gan.h5') print('discriminator loss', d_loss) print('adversarial loss', a_loss) img = image.array_to_img(generator_image[0] * 255., scale=False) img.save(os.path.join(save_dir, 'generated_frog' + str(step) + '.png')) img = image.array_to_img(real_images[0] * 255., scale=False) img.save(os.path.join(save_dir, 'real_frog' + str(step) + '.png'))