23.20 GAN 辅助分类器
优质
小牛编辑
130浏览
2023-12-01
你应该在大约 5 个轮次后开始看到合理的图像,而在大约 15 个轮次后开始看到良好的图像。 你应该使用 GPU,因为大量卷积运算在 CPU 上非常慢。 如果你打算进行迭代,请首选 TensorFlow 后端,因为使用 Theano 的话编译时间可能会称为阻碍。
耗时:
硬件 | 后端 | Time / Epoch |
---|---|---|
CPU | TF | 3 hrs |
Titan X (maxwell) | TF | 4 min |
Titan X (maxwell) | TH | 7 min |
有关更多信息和示例输出,请咨询 Keras 中的辅助分类器生成对抗网络。
from __future__ import print_function
from collections import defaultdict
try:
import cPickle as pickle
except ImportError:
import pickle
from PIL import Image
from six.moves import range
from keras.datasets import mnist
from keras import layers
from keras.layers import Input, Dense, Reshape, Flatten, Embedding, Dropout
from keras.layers import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2DTranspose, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.utils.generic_utils import Progbar
import numpy as np
np.random.seed(1337)
num_classes = 10
def build_generator(latent_size):
# 我们将一对 (z, L) 映射到图像空间 (..., 28, 28, 1),其中 z 是隐向量,L 是从 P_c 绘制的标签。
cnn = Sequential()
cnn.add(Dense(3 * 3 * 384, input_dim=latent_size, activation='relu'))
cnn.add(Reshape((3, 3, 384)))
# 上采样至 (7, 7, ...)
cnn.add(Conv2DTranspose(192, 5, strides=1, padding='valid',
activation='relu',
kernel_initializer='glorot_normal'))
cnn.add(BatchNormalization())
# 上采样至 (14, 14, ...)
cnn.add(Conv2DTranspose(96, 5, strides=2, padding='same',
activation='relu',
kernel_initializer='glorot_normal'))
cnn.add(BatchNormalization())
# 上采样至 (28, 28, ...)
cnn.add(Conv2DTranspose(1, 5, strides=2, padding='same',
activation='tanh',
kernel_initializer='glorot_normal'))
# 这是 GAN 论文中通常提到的 z 空间
latent = Input(shape=(latent_size, ))
# 这将是我们的标签
image_class = Input(shape=(1,), dtype='int32')
cls = Embedding(num_classes, latent_size,
embeddings_initializer='glorot_normal')(image_class)
# z 空间和一类条件嵌入之间的 hadamard 积
h = layers.multiply([latent, cls])
fake_image = cnn(h)
return Model([latent, image_class], fake_image)
def build_discriminator():
# 根据参考文献中的建议,使用 LeakyReLU 构建相对标准的转换网络
cnn = Sequential()
cnn.add(Conv2D(32, 3, padding='same', strides=2,
input_shape=(28, 28, 1)))
cnn.add(LeakyReLU(0.2))
cnn.add(Dropout(0.3))
cnn.add(Conv2D(64, 3, padding='same', strides=1))
cnn.add(LeakyReLU(0.2))
cnn.add(Dropout(0.3))
cnn.add(Conv2D(128, 3, padding='same', strides=2))
cnn.add(LeakyReLU(0.2))
cnn.add(Dropout(0.3))
cnn.add(Conv2D(256, 3, padding='same', strides=1))
cnn.add(LeakyReLU(0.2))
cnn.add(Dropout(0.3))
cnn.add(Flatten())
image = Input(shape=(28, 28, 1))
features = cnn(image)
# 第一个输出 (name=generation) 是鉴别是否认为所显示的图像是伪造的,
# 而第二个输出 (name=auxiliary) 是鉴别认为图像所属的类。
fake = Dense(1, activation='sigmoid', name='generation')(features)
aux = Dense(num_classes, activation='softmax', name='auxiliary')(features)
return Model(image, [fake, aux])
if __name__ == '__main__':
# 论文的批次和潜在大小
epochs = 100
batch_size = 100
latent_size = 100
# https://arxiv.org/abs/1511.06434 建议的 Adam 参数
adam_lr = 0.0002
adam_beta_1 = 0.5
# 建立鉴别器
print('Discriminator model:')
discriminator = build_discriminator()
discriminator.compile(
optimizer=Adam(learning_rate=adam_lr, beta_1=adam_beta_1),
loss=['binary_crossentropy', 'sparse_categorical_crossentropy']
)
discriminator.summary()
# 建立生成器
generator = build_generator(latent_size)
latent = Input(shape=(latent_size, ))
image_class = Input(shape=(1,), dtype='int32')
# 取得假图片
fake = generator([latent, image_class])
# 我们只希望能够训练组合模型的生成
discriminator.trainable = False
fake, aux = discriminator(fake)
combined = Model([latent, image_class], [fake, aux])
print('Combined model:')
combined.compile(
optimizer=Adam(learning_rate=adam_lr, beta_1=adam_beta_1),
loss=['binary_crossentropy', 'sparse_categorical_crossentropy']
)
combined.summary()
# 获取我们的 mnist 数据,并强制其形状为 (..., 28, 28, 1),范围为 [-1, 1]
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = np.expand_dims(x_train, axis=-1)
x_test = (x_test.astype(np.float32) - 127.5) / 127.5
x_test = np.expand_dims(x_test, axis=-1)
num_train, num_test = x_train.shape[0], x_test.shape[0]
train_history = defaultdict(list)
test_history = defaultdict(list)
for epoch in range(1, epochs + 1):
print('Epoch {}/{}'.format(epoch, epochs))
num_batches = int(np.ceil(x_train.shape[0] / float(batch_size)))
progress_bar = Progbar(target=num_batches)
epoch_gen_loss = []
epoch_disc_loss = []
for index in range(num_batches):
# 得到一批真实的图像
image_batch = x_train[index * batch_size:(index + 1) * batch_size]
label_batch = y_train[index * batch_size:(index + 1) * batch_size]
# 产生一批新的噪音
noise = np.random.uniform(-1, 1, (len(image_batch), latent_size))
# 从 p_c 采样一些标签
sampled_labels = np.random.randint(0, num_classes, len(image_batch))
# 使用生成的标签作为调节器,生成一批假图像。
# 我们将采样的标签重塑为 (len(image_batch),1),
# 以便我们可以将它们作为一个序列的长度送入嵌入层
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=0)
x = np.concatenate((image_batch, generated_images))
# 使用单面 soft real/fake 标签
# Salimans et al., 2016
# https://arxiv.org/pdf/1606.03498.pdf (Section 3.4)
soft_zero, soft_one = 0, 0.95
y = np.array(
[soft_one] * len(image_batch) + [soft_zero] * len(image_batch))
aux_y = np.concatenate((label_batch, sampled_labels), axis=0)
# 我们不希望鉴别器也能最大化生成图像上辅助分类器的分类精度,
# 因此我们不训练鉴别器为生成图像生成类标签(请参阅 https://openreview.net/forum?id=rJXTf9Bxg)。
# 为了保留辅助分类器的样本权重总和,我们将样本权重 2 分配给实际图像。
disc_sample_weight = [np.ones(2 * len(image_batch)),
np.concatenate((np.ones(len(image_batch)) * 2,
np.zeros(len(image_batch))))]
# 看看鉴别器是否能弄清楚自己...
epoch_disc_loss.append(discriminator.train_on_batch(
x, [y, aux_y], sample_weight=disc_sample_weight))
# 制造新的声音。我们在这里生成 2 倍批量大小,
# 这样我们就可以使生成器对与鉴别器相同数量的图像进行优化
noise = np.random.uniform(-1, 1, (2 * len(image_batch), latent_size))
sampled_labels = np.random.randint(0, num_classes, 2 * len(image_batch))
# 我们想训练生成器来欺骗鉴别器
# 对于生成器,我们希望所有 {fake,not-fake} 标签都说不假
trick = np.ones(2 * len(image_batch)) * soft_one
epoch_gen_loss.append(combined.train_on_batch(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels]))
progress_bar.update(index + 1)
print('Testing for epoch {}:'.format(epoch))
#在这里评估测试损失
# 产生一批新的噪音
noise = np.random.uniform(-1, 1, (num_test, latent_size))
# 从 p_c 采样一些标签并从中生成图像
sampled_labels = np.random.randint(0, num_classes, num_test)
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=False)
x = np.concatenate((x_test, generated_images))
y = np.array([1] * num_test + [0] * num_test)
aux_y = np.concatenate((y_test, sampled_labels), axis=0)
# 看看鉴别器是否能弄清楚自己...
discriminator_test_loss = discriminator.evaluate(
x, [y, aux_y], verbose=False)
discriminator_train_loss = np.mean(np.array(epoch_disc_loss), axis=0)
# 制造新的噪声
noise = np.random.uniform(-1, 1, (2 * num_test, latent_size))
sampled_labels = np.random.randint(0, num_classes, 2 * num_test)
trick = np.ones(2 * num_test)
generator_test_loss = combined.evaluate(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels], verbose=False)
generator_train_loss = np.mean(np.array(epoch_gen_loss), axis=0)
# 生成有关性能的轮次报告
train_history['generator'].append(generator_train_loss)
train_history['discriminator'].append(discriminator_train_loss)
test_history['generator'].append(generator_test_loss)
test_history['discriminator'].append(discriminator_test_loss)
print('{0:<22s} | {1:4s} | {2:15s} | {3:5s}'.format(
'component', *discriminator.metrics_names))
print('-' * 65)
ROW_FMT = '{0:<22s} | {1:<4.2f} | {2:<15.4f} | {3:<5.4f}'
print(ROW_FMT.format('generator (train)',
*train_history['generator'][-1]))
print(ROW_FMT.format('generator (test)',
*test_history['generator'][-1]))
print(ROW_FMT.format('discriminator (train)',
*train_history['discriminator'][-1]))
print(ROW_FMT.format('discriminator (test)',
*test_history['discriminator'][-1]))
# 在每个轮次保存权重
generator.save_weights(
'params_generator_epoch_{0:03d}.hdf5'.format(epoch), True)
discriminator.save_weights(
'params_discriminator_epoch_{0:03d}.hdf5'.format(epoch), True)
# 生成一些数字来显示
num_rows = 40
noise = np.tile(np.random.uniform(-1, 1, (num_rows, latent_size)),
(num_classes, 1))
sampled_labels = np.array([
[i] * num_rows for i in range(num_classes)
]).reshape(-1, 1)
# 批量显示
generated_images = generator.predict(
[noise, sampled_labels], verbose=0)
# 准备按类别标签排序的真实图像
real_labels = y_train[(epoch - 1) * num_rows * num_classes:
epoch * num_rows * num_classes]
indices = np.argsort(real_labels, axis=0)
real_images = x_train[(epoch - 1) * num_rows * num_classes:
epoch * num_rows * num_classes][indices]
# 显示生成的图像,白色分隔符,真实图像
img = np.concatenate(
(generated_images,
np.repeat(np.ones_like(x_train[:1]), num_rows, axis=0),
real_images))
# 将它们排列成网格
img = (np.concatenate([r.reshape(-1, 28)
for r in np.split(img, 2 * num_classes + 1)
], axis=-1) * 127.5 + 127.5).astype(np.uint8)
Image.fromarray(img).save(
'plot_epoch_{0:03d}_generated.png'.format(epoch))
with open('acgan-history.pkl', 'wb') as f:
pickle.dump({'train': train_history, 'test': test_history}, f)