代码示例 / 生成式深度学习 / 使用自适应判别器增强的数据高效 GAN

使用自适应判别器增强的数据高效 GAN

作者: András Béres
创建日期 2021/10/28
上次修改日期 2021/10/28
描述:使用 Caltech Birds 数据集从有限数据生成图像。

ⓘ 此示例使用 Keras 2

在 Colab 中查看 GitHub 源代码


介绍

GAN

生成对抗网络 (GAN) 是一类流行的生成式深度学习模型,通常用于图像生成。它们由一对对抗的神经网络组成,称为判别器和生成器。判别器的任务是区分真实图像和生成的(伪造)图像,而生成器网络则试图通过生成越来越真实的图像来欺骗判别器。但是,如果生成器太容易或太难欺骗,它可能会无法为生成器提供有用的学习信号,因此训练 GAN 通常被认为是一项困难的任务。

GAN 的数据增强

数据增强是深度学习中一种流行的技术,它是指随机应用语义保持变换到输入数据以生成其多个真实版本的过程,从而有效地增加了可用的训练数据量。最简单的例子是左右翻转图像,这保留了其内容,同时生成了第二个唯一的训练样本。数据增强通常用于监督学习,以防止过拟合并增强泛化能力。

StyleGAN2-ADA 的作者表明,判别器过拟合可能是 GAN 中的一个问题,尤其是在仅有少量训练数据可用时。他们提出了自适应判别器增强来缓解这个问题。

然而,将数据增强应用于 GAN 并不简单。由于生成器使用判别器的梯度进行更新,如果生成的图像被增强,则增强管道必须是可微的,并且为了计算效率也必须与 GPU 兼容。幸运的是,Keras 图像增强层 满足这两个要求,因此非常适合此任务。

可逆数据增强

在生成模型中使用数据增强时,一个可能的问题是“泄漏增强”(第 2.2 节)的问题,即模型生成已经增强的图像。这意味着它无法将增强与底层数据分布分离,这可能是由于使用不可逆数据变换引起的。例如,如果以相等的概率执行 0、90、180 或 270 度旋转,则无法推断图像的原始方向,并且此信息会被破坏。

使数据增强可逆的一个简单技巧是以一定的概率应用它们。这样,图像的原始版本将更加常见,并且可以推断出数据分布。通过正确选择此概率,可以有效地正则化判别器,而不会使增强泄漏。


设置

import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds

from tensorflow import keras
from tensorflow.keras import layers

超参数

# data
num_epochs = 10  # train for 400 epochs for good results
image_size = 64
# resolution of Kernel Inception Distance measurement, see related section
kid_image_size = 75
padding = 0.25
dataset_name = "caltech_birds2011"

# adaptive discriminator augmentation
max_translation = 0.125
max_rotation = 0.125
max_zoom = 0.25
target_accuracy = 0.85
integration_steps = 1000

# architecture
noise_size = 64
depth = 4
width = 128
leaky_relu_slope = 0.2
dropout_rate = 0.4

# optimization
batch_size = 128
learning_rate = 2e-4
beta_1 = 0.5  # not using the default value of 0.9 is important
ema = 0.99

数据管道

在本例中,我们将使用Caltech Birds (2011)数据集来生成鸟类的图像,这是一个多样化的自然数据集,包含不到 6000 张用于训练的图像。在使用如此少量的数据时,必须格外小心以保持尽可能高的数据质量。在本例中,我们使用提供的鸟类边界框来裁剪它们,同时在可能的情况下保留其纵横比。

def round_to_int(float_value):
    return tf.cast(tf.math.round(float_value), dtype=tf.int32)


def preprocess_image(data):
    # unnormalize bounding box coordinates
    height = tf.cast(tf.shape(data["image"])[0], dtype=tf.float32)
    width = tf.cast(tf.shape(data["image"])[1], dtype=tf.float32)
    bounding_box = data["bbox"] * tf.stack([height, width, height, width])

    # calculate center and length of longer side, add padding
    target_center_y = 0.5 * (bounding_box[0] + bounding_box[2])
    target_center_x = 0.5 * (bounding_box[1] + bounding_box[3])
    target_size = tf.maximum(
        (1.0 + padding) * (bounding_box[2] - bounding_box[0]),
        (1.0 + padding) * (bounding_box[3] - bounding_box[1]),
    )

    # modify crop size to fit into image
    target_height = tf.reduce_min(
        [target_size, 2.0 * target_center_y, 2.0 * (height - target_center_y)]
    )
    target_width = tf.reduce_min(
        [target_size, 2.0 * target_center_x, 2.0 * (width - target_center_x)]
    )

    # crop image
    image = tf.image.crop_to_bounding_box(
        data["image"],
        offset_height=round_to_int(target_center_y - 0.5 * target_height),
        offset_width=round_to_int(target_center_x - 0.5 * target_width),
        target_height=round_to_int(target_height),
        target_width=round_to_int(target_width),
    )

    # resize and clip
    # for image downsampling, area interpolation is the preferred method
    image = tf.image.resize(
        image, size=[image_size, image_size], method=tf.image.ResizeMethod.AREA
    )
    return tf.clip_by_value(image / 255.0, 0.0, 1.0)


def prepare_dataset(split):
    # the validation dataset is shuffled as well, because data order matters
    # for the KID calculation
    return (
        tfds.load(dataset_name, split=split, shuffle_files=True)
        .map(preprocess_image, num_parallel_calls=tf.data.AUTOTUNE)
        .cache()
        .shuffle(10 * batch_size)
        .batch(batch_size, drop_remainder=True)
        .prefetch(buffer_size=tf.data.AUTOTUNE)
    )


train_dataset = prepare_dataset("train")
val_dataset = prepare_dataset("test")

预处理后的训练图像如下所示:birds dataset


核感知距离

核感知距离 (KID) 被提出作为流行的Fréchet 感知距离 (FID) 度量的替代方案,用于衡量图像生成质量。这两个度量都测量了在InceptionV3 网络(在ImageNet 上预训练)的表示空间中生成的分布和训练分布之间的差异。

根据论文,提出 KID 是因为 FID 没有无偏估计量,当它在较少的图像上测量时,其期望值较高。KID 更适合小型数据集,因为其期望值不依赖于其测量的样本数量。根据我的经验,它在计算上也更轻量级、数值上更稳定,并且更容易实现,因为它可以以每批方式进行估计。

在本例中,图像在 Inception 网络的最小可能分辨率(75x75 而不是 299x299)处进行评估,并且为了计算效率,度量仅在验证集上进行测量。

class KID(keras.metrics.Metric):
    def __init__(self, name="kid", **kwargs):
        super().__init__(name=name, **kwargs)

        # KID is estimated per batch and is averaged across batches
        self.kid_tracker = keras.metrics.Mean()

        # a pretrained InceptionV3 is used without its classification layer
        # transform the pixel values to the 0-255 range, then use the same
        # preprocessing as during pretraining
        self.encoder = keras.Sequential(
            [
                layers.InputLayer(input_shape=(image_size, image_size, 3)),
                layers.Rescaling(255.0),
                layers.Resizing(height=kid_image_size, width=kid_image_size),
                layers.Lambda(keras.applications.inception_v3.preprocess_input),
                keras.applications.InceptionV3(
                    include_top=False,
                    input_shape=(kid_image_size, kid_image_size, 3),
                    weights="imagenet",
                ),
                layers.GlobalAveragePooling2D(),
            ],
            name="inception_encoder",
        )

    def polynomial_kernel(self, features_1, features_2):
        feature_dimensions = tf.cast(tf.shape(features_1)[1], dtype=tf.float32)
        return (features_1 @ tf.transpose(features_2) / feature_dimensions + 1.0) ** 3.0

    def update_state(self, real_images, generated_images, sample_weight=None):
        real_features = self.encoder(real_images, training=False)
        generated_features = self.encoder(generated_images, training=False)

        # compute polynomial kernels using the two sets of features
        kernel_real = self.polynomial_kernel(real_features, real_features)
        kernel_generated = self.polynomial_kernel(
            generated_features, generated_features
        )
        kernel_cross = self.polynomial_kernel(real_features, generated_features)

        # estimate the squared maximum mean discrepancy using the average kernel values
        batch_size = tf.shape(real_features)[0]
        batch_size_f = tf.cast(batch_size, dtype=tf.float32)
        mean_kernel_real = tf.reduce_sum(kernel_real * (1.0 - tf.eye(batch_size))) / (
            batch_size_f * (batch_size_f - 1.0)
        )
        mean_kernel_generated = tf.reduce_sum(
            kernel_generated * (1.0 - tf.eye(batch_size))
        ) / (batch_size_f * (batch_size_f - 1.0))
        mean_kernel_cross = tf.reduce_mean(kernel_cross)
        kid = mean_kernel_real + mean_kernel_generated - 2.0 * mean_kernel_cross

        # update the average KID estimate
        self.kid_tracker.update_state(kid)

    def result(self):
        return self.kid_tracker.result()

    def reset_state(self):
        self.kid_tracker.reset_state()

自适应判别器增强

StyleGAN2-ADA 的作者建议在训练期间自适应地改变增强概率。虽然在论文中解释有所不同,但他们对增强概率使用积分控制,以使判别器在真实图像上的准确率保持在目标值附近。请注意,他们的控制变量实际上是判别器 logits 的平均符号(论文中的 r_t),对应于 2 * accuracy - 1。

此方法需要两个超参数

  1. target_accuracy:真实图像上判别器准确率的目标值。我建议从 80-90% 的范围内选择其值。
  2. integration_steps:100% 的准确率误差转换为 100% 的增强概率增量的更新步数。为了直观地说明,这定义了增强概率变化的速度。我建议将其设置为相对较高的值(在本例中为 1000),以便增强强度仅缓慢调整。

此过程的主要动机是目标准确率的最佳值在不同的数据集大小之间是相似的(参见论文中的图 4 和 5),因此无需重新调整,因为该过程会在需要时自动应用更强的数据增强。

# "hard sigmoid", useful for binary accuracy calculation from logits
def step(values):
    # negative values -> 0.0, positive values -> 1.0
    return 0.5 * (1.0 + tf.sign(values))


# augments images with a probability that is dynamically updated during training
class AdaptiveAugmenter(keras.Model):
    def __init__(self):
        super().__init__()

        # stores the current probability of an image being augmented
        self.probability = tf.Variable(0.0)

        # the corresponding augmentation names from the paper are shown above each layer
        # the authors show (see figure 4), that the blitting and geometric augmentations
        # are the most helpful in the low-data regime
        self.augmenter = keras.Sequential(
            [
                layers.InputLayer(input_shape=(image_size, image_size, 3)),
                # blitting/x-flip:
                layers.RandomFlip("horizontal"),
                # blitting/integer translation:
                layers.RandomTranslation(
                    height_factor=max_translation,
                    width_factor=max_translation,
                    interpolation="nearest",
                ),
                # geometric/rotation:
                layers.RandomRotation(factor=max_rotation),
                # geometric/isotropic and anisotropic scaling:
                layers.RandomZoom(
                    height_factor=(-max_zoom, 0.0), width_factor=(-max_zoom, 0.0)
                ),
            ],
            name="adaptive_augmenter",
        )

    def call(self, images, training):
        if training:
            augmented_images = self.augmenter(images, training)

            # during training either the original or the augmented images are selected
            # based on self.probability
            augmentation_values = tf.random.uniform(
                shape=(batch_size, 1, 1, 1), minval=0.0, maxval=1.0
            )
            augmentation_bools = tf.math.less(augmentation_values, self.probability)

            images = tf.where(augmentation_bools, augmented_images, images)
        return images

    def update(self, real_logits):
        current_accuracy = tf.reduce_mean(step(real_logits))

        # the augmentation probability is updated based on the discriminator's
        # accuracy on real images
        accuracy_error = current_accuracy - target_accuracy
        self.probability.assign(
            tf.clip_by_value(
                self.probability + accuracy_error / integration_steps, 0.0, 1.0
            )
        )

网络架构

这里我们指定两个网络的架构

  • 生成器:将随机向量映射到图像,该图像应该尽可能真实
  • 判别器:将图像映射到标量分数,对于真实图像,该分数应该很高,对于生成图像,该分数应该很低

GAN 往往对网络架构敏感,在本例中我实现了 DCGAN 架构,因为它在训练期间相对稳定,同时易于实现。我们在整个网络中使用恒定的过滤器数量,在生成器的最后一层使用 sigmoid 而不是 tanh,并使用默认初始化而不是随机正态作为进一步的简化。

作为一个良好的实践,我们禁用了批归一化层中的可学习比例参数,因为一方面后续的 relu + 卷积层使其冗余(如文档中所述)。但也因为根据理论,在使用谱归一化(第 4.1 节)时应该禁用它,这里没有使用,但在 GAN 中很常见。我们还在全连接层和卷积层中禁用了偏差,因为后续的批归一化使其冗余。

# DCGAN generator
def get_generator():
    noise_input = keras.Input(shape=(noise_size,))
    x = layers.Dense(4 * 4 * width, use_bias=False)(noise_input)
    x = layers.BatchNormalization(scale=False)(x)
    x = layers.ReLU()(x)
    x = layers.Reshape(target_shape=(4, 4, width))(x)
    for _ in range(depth - 1):
        x = layers.Conv2DTranspose(
            width, kernel_size=4, strides=2, padding="same", use_bias=False,
        )(x)
        x = layers.BatchNormalization(scale=False)(x)
        x = layers.ReLU()(x)
    image_output = layers.Conv2DTranspose(
        3, kernel_size=4, strides=2, padding="same", activation="sigmoid",
    )(x)

    return keras.Model(noise_input, image_output, name="generator")


# DCGAN discriminator
def get_discriminator():
    image_input = keras.Input(shape=(image_size, image_size, 3))
    x = image_input
    for _ in range(depth):
        x = layers.Conv2D(
            width, kernel_size=4, strides=2, padding="same", use_bias=False,
        )(x)
        x = layers.BatchNormalization(scale=False)(x)
        x = layers.LeakyReLU(alpha=leaky_relu_slope)(x)
    x = layers.Flatten()(x)
    x = layers.Dropout(dropout_rate)(x)
    output_score = layers.Dense(1)(x)

    return keras.Model(image_input, output_score, name="discriminator")

GAN 模型

class GAN_ADA(keras.Model):
    def __init__(self):
        super().__init__()

        self.augmenter = AdaptiveAugmenter()
        self.generator = get_generator()
        self.ema_generator = keras.models.clone_model(self.generator)
        self.discriminator = get_discriminator()

        self.generator.summary()
        self.discriminator.summary()

    def compile(self, generator_optimizer, discriminator_optimizer, **kwargs):
        super().compile(**kwargs)

        # separate optimizers for the two networks
        self.generator_optimizer = generator_optimizer
        self.discriminator_optimizer = discriminator_optimizer

        self.generator_loss_tracker = keras.metrics.Mean(name="g_loss")
        self.discriminator_loss_tracker = keras.metrics.Mean(name="d_loss")
        self.real_accuracy = keras.metrics.BinaryAccuracy(name="real_acc")
        self.generated_accuracy = keras.metrics.BinaryAccuracy(name="gen_acc")
        self.augmentation_probability_tracker = keras.metrics.Mean(name="aug_p")
        self.kid = KID()

    @property
    def metrics(self):
        return [
            self.generator_loss_tracker,
            self.discriminator_loss_tracker,
            self.real_accuracy,
            self.generated_accuracy,
            self.augmentation_probability_tracker,
            self.kid,
        ]

    def generate(self, batch_size, training):
        latent_samples = tf.random.normal(shape=(batch_size, noise_size))
        # use ema_generator during inference
        if training:
            generated_images = self.generator(latent_samples, training)
        else:
            generated_images = self.ema_generator(latent_samples, training)
        return generated_images

    def adversarial_loss(self, real_logits, generated_logits):
        # this is usually called the non-saturating GAN loss

        real_labels = tf.ones(shape=(batch_size, 1))
        generated_labels = tf.zeros(shape=(batch_size, 1))

        # the generator tries to produce images that the discriminator considers as real
        generator_loss = keras.losses.binary_crossentropy(
            real_labels, generated_logits, from_logits=True
        )
        # the discriminator tries to determine if images are real or generated
        discriminator_loss = keras.losses.binary_crossentropy(
            tf.concat([real_labels, generated_labels], axis=0),
            tf.concat([real_logits, generated_logits], axis=0),
            from_logits=True,
        )

        return tf.reduce_mean(generator_loss), tf.reduce_mean(discriminator_loss)

    def train_step(self, real_images):
        real_images = self.augmenter(real_images, training=True)

        # use persistent gradient tape because gradients will be calculated twice
        with tf.GradientTape(persistent=True) as tape:
            generated_images = self.generate(batch_size, training=True)
            # gradient is calculated through the image augmentation
            generated_images = self.augmenter(generated_images, training=True)

            # separate forward passes for the real and generated images, meaning
            # that batch normalization is applied separately
            real_logits = self.discriminator(real_images, training=True)
            generated_logits = self.discriminator(generated_images, training=True)

            generator_loss, discriminator_loss = self.adversarial_loss(
                real_logits, generated_logits
            )

        # calculate gradients and update weights
        generator_gradients = tape.gradient(
            generator_loss, self.generator.trainable_weights
        )
        discriminator_gradients = tape.gradient(
            discriminator_loss, self.discriminator.trainable_weights
        )
        self.generator_optimizer.apply_gradients(
            zip(generator_gradients, self.generator.trainable_weights)
        )
        self.discriminator_optimizer.apply_gradients(
            zip(discriminator_gradients, self.discriminator.trainable_weights)
        )

        # update the augmentation probability based on the discriminator's performance
        self.augmenter.update(real_logits)

        self.generator_loss_tracker.update_state(generator_loss)
        self.discriminator_loss_tracker.update_state(discriminator_loss)
        self.real_accuracy.update_state(1.0, step(real_logits))
        self.generated_accuracy.update_state(0.0, step(generated_logits))
        self.augmentation_probability_tracker.update_state(self.augmenter.probability)

        # track the exponential moving average of the generator's weights to decrease
        # variance in the generation quality
        for weight, ema_weight in zip(
            self.generator.weights, self.ema_generator.weights
        ):
            ema_weight.assign(ema * ema_weight + (1 - ema) * weight)

        # KID is not measured during the training phase for computational efficiency
        return {m.name: m.result() for m in self.metrics[:-1]}

    def test_step(self, real_images):
        generated_images = self.generate(batch_size, training=False)

        self.kid.update_state(real_images, generated_images)

        # only KID is measured during the evaluation phase for computational efficiency
        return {self.kid.name: self.kid.result()}

    def plot_images(self, epoch=None, logs=None, num_rows=3, num_cols=6, interval=5):
        # plot random generated images for visual evaluation of generation quality
        if epoch is None or (epoch + 1) % interval == 0:
            num_images = num_rows * num_cols
            generated_images = self.generate(num_images, training=False)

            plt.figure(figsize=(num_cols * 2.0, num_rows * 2.0))
            for row in range(num_rows):
                for col in range(num_cols):
                    index = row * num_cols + col
                    plt.subplot(num_rows, num_cols, index + 1)
                    plt.imshow(generated_images[index])
                    plt.axis("off")
            plt.tight_layout()
            plt.show()
            plt.close()

训练

从训练期间的指标中可以看到,如果真实准确率(判别器在真实图像上的准确率)低于目标准确率,则增强概率会增加,反之亦然。根据我的经验,在健康的 GAN 训练中,判别器的准确率应该保持在 80-95% 的范围内。低于此,判别器太弱,高于此,判别器太强。

请注意,我们跟踪生成器权重的指数移动平均值,并将其用于图像生成和 KID 评估。

# create and compile the model
model = GAN_ADA()
model.compile(
    generator_optimizer=keras.optimizers.Adam(learning_rate, beta_1),
    discriminator_optimizer=keras.optimizers.Adam(learning_rate, beta_1),
)

# save the best model based on the validation KID metric
checkpoint_path = "gan_model"
checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path,
    save_weights_only=True,
    monitor="val_kid",
    mode="min",
    save_best_only=True,
)

# run training and plot generated images periodically
model.fit(
    train_dataset,
    epochs=num_epochs,
    validation_data=val_dataset,
    callbacks=[
        keras.callbacks.LambdaCallback(on_epoch_end=model.plot_images),
        checkpoint_callback,
    ],
)
Model: "generator"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 64)]              0         
_________________________________________________________________
dense (Dense)                (None, 2048)              131072    
_________________________________________________________________
batch_normalization (BatchNo (None, 2048)              6144      
_________________________________________________________________
re_lu (ReLU)                 (None, 2048)              0         
_________________________________________________________________
reshape (Reshape)            (None, 4, 4, 128)         0         
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 8, 8, 128)         262144    
_________________________________________________________________
batch_normalization_1 (Batch (None, 8, 8, 128)         384       
_________________________________________________________________
re_lu_1 (ReLU)               (None, 8, 8, 128)         0         
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 16, 16, 128)       262144    
_________________________________________________________________
batch_normalization_2 (Batch (None, 16, 16, 128)       384       
_________________________________________________________________
re_lu_2 (ReLU)               (None, 16, 16, 128)       0         
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 32, 32, 128)       262144    
_________________________________________________________________
batch_normalization_3 (Batch (None, 32, 32, 128)       384       
_________________________________________________________________
re_lu_3 (ReLU)               (None, 32, 32, 128)       0         
_________________________________________________________________
conv2d_transpose_3 (Conv2DTr (None, 64, 64, 3)         6147      
=================================================================
Total params: 930,947
Trainable params: 926,083
Non-trainable params: 4,864
_________________________________________________________________
Model: "discriminator"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 64, 64, 3)]       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 32, 32, 128)       6144      
_________________________________________________________________
batch_normalization_4 (Batch (None, 32, 32, 128)       384       
_________________________________________________________________
leaky_re_lu (LeakyReLU)      (None, 32, 32, 128)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 16, 16, 128)       262144    
_________________________________________________________________
batch_normalization_5 (Batch (None, 16, 16, 128)       384       
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 16, 16, 128)       0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 8, 8, 128)         262144    
_________________________________________________________________
batch_normalization_6 (Batch (None, 8, 8, 128)         384       
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 8, 8, 128)         0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 4, 4, 128)         262144    
_________________________________________________________________
batch_normalization_7 (Batch (None, 4, 4, 128)         384       
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU)    (None, 4, 4, 128)         0         
_________________________________________________________________
flatten (Flatten)            (None, 2048)              0         
_________________________________________________________________
dropout (Dropout)            (None, 2048)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 2049      
=================================================================
Total params: 796,161
Trainable params: 795,137
Non-trainable params: 1,024
_________________________________________________________________
Epoch 1/10
46/46 [==============================] - 36s 307ms/step - g_loss: 3.3293 - d_loss: 0.1576 - real_acc: 0.9387 - gen_acc: 0.9579 - aug_p: 0.0020 - val_kid: 9.0999
Epoch 2/10
46/46 [==============================] - 10s 215ms/step - g_loss: 4.9824 - d_loss: 0.0912 - real_acc: 0.9704 - gen_acc: 0.9798 - aug_p: 0.0077 - val_kid: 8.3523
Epoch 3/10
46/46 [==============================] - 10s 218ms/step - g_loss: 5.0587 - d_loss: 0.1248 - real_acc: 0.9530 - gen_acc: 0.9625 - aug_p: 0.0131 - val_kid: 6.8116
Epoch 4/10
46/46 [==============================] - 10s 221ms/step - g_loss: 4.2580 - d_loss: 0.1002 - real_acc: 0.9686 - gen_acc: 0.9740 - aug_p: 0.0179 - val_kid: 5.2327
Epoch 5/10
46/46 [==============================] - 10s 225ms/step - g_loss: 4.6022 - d_loss: 0.0847 - real_acc: 0.9655 - gen_acc: 0.9852 - aug_p: 0.0234 - val_kid: 3.9004

png

Epoch 6/10
46/46 [==============================] - 10s 224ms/step - g_loss: 4.9362 - d_loss: 0.0671 - real_acc: 0.9791 - gen_acc: 0.9895 - aug_p: 0.0291 - val_kid: 6.6020
Epoch 7/10
46/46 [==============================] - 10s 222ms/step - g_loss: 4.4272 - d_loss: 0.1184 - real_acc: 0.9570 - gen_acc: 0.9657 - aug_p: 0.0345 - val_kid: 3.3644
Epoch 8/10
46/46 [==============================] - 10s 220ms/step - g_loss: 4.5060 - d_loss: 0.1635 - real_acc: 0.9421 - gen_acc: 0.9594 - aug_p: 0.0392 - val_kid: 3.1381
Epoch 9/10
46/46 [==============================] - 10s 219ms/step - g_loss: 3.8264 - d_loss: 0.1667 - real_acc: 0.9383 - gen_acc: 0.9484 - aug_p: 0.0433 - val_kid: 2.9423
Epoch 10/10
46/46 [==============================] - 10s 219ms/step - g_loss: 3.4063 - d_loss: 0.1757 - real_acc: 0.9314 - gen_acc: 0.9475 - aug_p: 0.0473 - val_kid: 2.9112

png

<keras.callbacks.History at 0x7fefcc2cb9d0>

推理

# load the best model and generate images
model.load_weights(checkpoint_path)
model.plot_images()

png


结果

通过运行 400 个 epoch 的训练(在 Colab 笔记本中大约需要 2-3 小时),您可以使用此代码示例获得高质量的图像生成。

随机图像批次在 400 个 epoch 训练过程中的演变(ema=0.999 用于动画平滑):鸟类演变gif

选定图像批次之间的潜在空间插值:鸟类插值gif

我还建议尝试在其他数据集上进行训练,例如 CelebA。根据我的经验,无需更改任何超参数即可获得良好的结果(尽管可能不需要鉴别器增强)。


GAN 提示和技巧

我通过此示例的目标是在 GAN 的易实现性和生成质量之间找到良好的平衡。在准备过程中,我使用 此存储库 运行了大量消融实验。

在本节中,我将根据主观重要性顺序列出经验教训和建议。

我建议查看 DCGAN 论文NeurIPS 演讲大型 GAN 研究,了解其他人对该主题的看法。

架构提示

  • 分辨率:在更高分辨率下训练 GAN 往往更困难,我建议最初尝试 32x32 或 64x64 分辨率。
  • 初始化:如果在训练初期看到强烈的彩色图案,则初始化可能是问题所在。将层的 kernel_initializer 参数设置为 随机正态分布,并降低标准差(推荐值:0.02,遵循 DCGAN),直到问题消失。
  • 上采样:生成器中有两种主要的上采样方法。 转置卷积 速度更快,但可能导致 棋盘格伪影,可以通过使用可被步长整除的内核大小来减少(对于步长为 2,推荐的内核大小为 4)。 上采样 + 标准卷积 的质量可能略低,但棋盘格伪影不是问题。我建议为此使用最近邻插值而不是双线性插值。
  • 鉴别器中的批归一化:有时影响很大,我建议尝试两种方法。
  • 谱归一化:一种流行的训练 GAN 技术,可以帮助提高稳定性。我建议同时禁用批归一化的可学习缩放参数。
  • 残差连接:虽然残差鉴别器表现相似,但根据我的经验,残差生成器更难训练。然而,它们对于训练大型和深度架构是必要的。我建议从非残差架构开始。
  • dropout:根据我的经验,在鉴别器的最后一层之前使用 dropout 可以提高生成质量。推荐的 dropout 率低于 0.5。
  • Leaky ReLU:在鉴别器中使用 Leaky ReLU 激活函数,使它的梯度不那么稀疏。遵循 DCGAN,推荐的斜率/alpha 为 0.2。

算法提示

  • 损失函数:多年来,人们提出了许多用于训练 GAN 的损失函数,承诺可以提高性能和稳定性。我在 此存储库 中实现了其中的 5 个,我的经验与 此 GAN 研究 一致:似乎没有哪个损失函数能够始终优于默认的非饱和 GAN 损失。我建议将其用作默认值。
  • Adam 的 beta_1 参数:Adam 中的 beta_1 参数可以解释为平均梯度估计的动量。DCGAN 中建议使用 0.5 甚至 0.0 而不是默认值 0.9,这一点很重要。此示例在使用其默认值时将无法正常工作。
  • 为生成图像和真实图像分别进行批归一化:鉴别器的正向传播应该针对生成图像和真实图像分别进行。否则会导致伪影(在我的例子中是 45 度条纹)并降低性能。
  • 生成器权重的指数移动平均值:这有助于减少 KID 度量的方差,并有助于在训练期间平均出快速的颜色调色板变化。
  • 生成器和鉴别器的不同学习率:如果有资源,可以帮助分别调整这两个网络的学习率。类似的想法是,对于另一个网络的每次更新,多次更新其中一个网络(通常是鉴别器)的权重。我建议遵循 DCGAN,对这两个网络使用相同的 2e-4 学习率(Adam),并且默认情况下只更新它们一次。
  • 标签噪声单侧标签平滑(对真实标签使用小于 1.0 的值)或向标签添加噪声可以规范鉴别器,使其不变得过于自信,但在我的情况下,它们并没有提高性能。
  • 自适应数据增强:因为它为训练过程添加了另一个动态组件,因此默认情况下将其禁用,并且仅在其他组件已正常工作时才启用它。

其他与 GAN 相关的 Keras 代码示例

现代 GAN 架构路线

关于鉴别器数据增强的并发论文:123

关于 GAN 的近期文献概述:演讲