这个竞赛的任务是根据给定的ImageNet中的120种狗狗图片,生成这个图片。
1 参考模型
1.1 模型1
DCGAN
1.2 模型2
WGAN
1.3 模型3
WGAN-GP(WGAN2)
1.4 模型4
WGAN-C
2 我的提交模型和LB结果
2.1 生成模型(fork from the job by Chris Detto)
在Chris的生成模型基础上做一些改进。
- 模型改进
- 生成模型的学习算法改进
- 数据增广(采用类似于mixup的方法)
2.1 DCGAN
2.2 WGAN
2.3 CGAN
3 最终LB结果
4 GAN训练的tricks
在keras中如何稳健的训练GAN模型
原文作者Jason Brownlee
Generative Adversarial Networks, 或者多个GANs存在训练困难,对于这个比赛尤其困难,因为只有一个P100的GPU和9个小时的训练时间。
言归正传,GAN的训练其实是一个零和问题,天生的难以训练。
但是可以参考这里。,也许会有帮助。
教程目录
主要由两部分组成:
- 启发式文件GANs的训练方法
- DCGAN的最优实践
2.1. 使用Stride卷积进行下采样
2.2. 使用Stride反卷积进行上采样
2.3. 使用LeakyReLU
2.4. 使用Batch Normalization
2.5. 使用Gaussian Weight Initialization
2.6. 使用Adam Stochastic Gradient Descent
2.7. 将图像归一化到[-1,1] - Soumith Chintala的GAN技巧
3.1. 使用Gaussian隐变量空间
3.2. 批量分开真假图像
3.3. 使用Label Smoothing
3.4. 使用噪声Labels
1 启发式文件GANs的训练方法
2 DCGAN的最优实践
2.1. 使用Stride卷积进行下采样
# example of downsampling with strided convolutions
from keras.models import Sequential
from keras.layers import Conv2D
# define model
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), strides=(2,2), padding='same', input_shape=(64,64,3)))
# summarize model
model.summary()
2.2. 使用Stride反卷积进行上采样
# example of upsampling with strided convolutions
from keras.models import Sequential
from keras.layers import Conv2DTranspose
# define model
model = Sequential()
model.add(Conv2DTranspose(64, kernel_size=(4,4), strides=(2,2), padding='same', input_shape=(64,64,3)))
# summarize model
model.summary()
2.3. 使用LeakyReLU
# example of using leakyrelu in a discriminator model
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import BatchNormalization
from keras.layers import LeakyReLU
# define model
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), strides=(2,2), padding='same', input_shape=(64,64,3)))
model.add(LeakyReLU(0.2))
# summarize model
model.summary()
2.4. 使用Batch Normalization
# example of using batch norm in a discriminator model
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import BatchNormalization
from keras.layers import LeakyReLU
# define model
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), strides=(2,2), padding='same', input_shape=(64,64,3)))
model.add(BatchNormalization())
model.add(LeakyReLU(0.2))
# summarize model
model.summary()
2.5. 使用Gaussian Weight Initialization
# example of gaussian weight initialization in a generator model
from keras.models import Sequential
from keras.layers import Conv2DTranspose
from keras.initializers import RandomNormal
# define model
model = Sequential()
init = RandomNormal(mean=0.0, stddev=0.02)
model.add(Conv2DTranspose(64, kernel_size=(4,4), strides=(2,2), padding='same', kernel_initializer=init, input_shape=(64,64,3)))
2.6. 使用Adam Stochastic Gradient Descent
# example of using adam when training a discriminator model
from keras.models import Sequential
from keras.layers import Conv2D
from keras.optimizers import Adam
# define model
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), strides=(2,2), padding='same', input_shape=(64,64,3)))
# compile model
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
2.7. 将图像归一化到[-1,1]
# example of a function for scaling images
# scale image data from [0,255] to [-1,1]
def scale_images(images):
# convert from unit8 to float32
images = images.astype('float32')
# scale from [0,255] to [-1,1]
images = (images - 127.5) / 127.5
return images
3. Soumith Chintala的GAN技巧
3.1 使用Gaussian隐变量空间
# example of sampling from a gaussian latent space
from numpy.random import randn
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = randn(latent_dim * n_samples)
# reshape into a batch of inputs for the network
x_input = x_input.reshape((n_samples, latent_dim))
return x_input
# size of latent space
n_dim = 100
# number of samples to generate
n_samples = 500
# generate samples
samples = generate_latent_points(n_dim, n_samples)
# summarize
print(samples.shape, samples.mean(), samples.std())
3.2. 批量分开真假图像
# get randomly selected 'real' samples
X_real, y_real = ...
# update discriminator model weights
discriminator.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = ...
# update discriminator model weights
discriminator.train_on_batch(X_fake, y_fake)
3.3. 使用Label Smoothing
# example of positive label smoothing
from numpy import ones
from numpy.random import random
# example of smoothing class=1 to [0.7, 1.2]
def smooth_positive_labels(y):
return y - 0.3 + (random(y.shape) * 0.5)
# generate 'real' class labels (1)
n_samples = 1000
y = ones((n_samples, 1))
# smooth labels
y = smooth_positive_labels(y)
# summarize smooth labels
print(y.shape, y.min(), y.max())
# example of negative label smoothing
from numpy import zeros
from numpy.random import random
# example of smoothing class=0 to [0.0, 0.3]
def smooth_negative_labels(y):
return y + random(y.shape) * 0.3
# generate 'fake' class labels (0)
n_samples = 1000
y = zeros((n_samples, 1))
# smooth labels
y = smooth_negative_labels(y)
# summarize smooth labels
print(y.shape, y.min(), y.max())
3.4. 使用噪声Labels
# example of noisy labels
from numpy import ones
from numpy import zeros
from numpy.random import choice
# randomly flip some labels
def noisy_labels(y, p_flip):
# determine the number of labels to flip
n_select = int(p_flip * y.shape[0])
# choose labels to flip
flip_ix = choice([i for i in range(y.shape[0])], size=n_select)
# invert the labels in place
y[flip_ix] = 1 - y[flip_ix]
return y
# generate 'real' class labels (1)
n_samples = 1000
y = ones((n_samples, 1))
# flip labels with 5% probability
y = noisy_labels(y, 0.05)
# summarize labels
print(y.sum())
# generate 'fake' class labels (0)
y = zeros((n_samples, 1))
# flip labels with 5% probability
y = noisy_labels(y, 0.05)
# summarize labels
print(y.sum())
- Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks, 2015.
- Tutorial: Generative Adversarial Networks, NIPS, 2016.
- Improved Techniques for Training GANs, 2016.
API
- Keras API.
- NumPy Random sampling (numpy.random) API
- NumPy Array manipulation routines
论文参考
- ganhacks: How to Train a GAN? Tips and tricks to make GANs work
- Ian Goodfellow, Introduction to GANs, NIPS 2016.
- Soumith Chintala, How to train a GAN, NIPS 2016 Workshop on Adversarial Training.