這個(gè)競(jìng)賽的任務(wù)是根據(jù)給定的ImageNet中的120種狗狗圖片,生成這個(gè)圖片淤刃。
1 參考模型
1.1 模型1
DCGAN
1.2 模型2
WGAN
1.3 模型3
WGAN-GP(WGAN2)
1.4 模型4
WGAN-C
2 我的提交模型和LB結(jié)果
2.1 生成模型(fork from the job by Chris Detto)
在Chris的生成模型基礎(chǔ)上做一些改進(jìn)。
- 模型改進(jìn)
- 生成模型的學(xué)習(xí)算法改進(jìn)
- 數(shù)據(jù)增廣(采用類似于mixup的方法)
2.1 DCGAN
2.2 WGAN
2.3 CGAN
3 最終LB結(jié)果
4 GAN訓(xùn)練的tricks
在keras中如何穩(wěn)健的訓(xùn)練GAN模型
原文作者Jason Brownlee
Generative Adversarial Networks, 或者多個(gè)GANs存在訓(xùn)練困難,對(duì)于這個(gè)比賽尤其困難,因?yàn)橹挥幸粋€(gè)P100的GPU和9個(gè)小時(shí)的訓(xùn)練時(shí)間拓哺。
言歸正傳,GAN的訓(xùn)練其實(shí)是一個(gè)零和問(wèn)題脖母,天生的難以訓(xùn)練士鸥。
但是可以參考這里。谆级,也許會(huì)有幫助烤礁。
教程目錄
主要由兩部分組成:
- 啟發(fā)式文件GANs的訓(xùn)練方法
- DCGAN的最優(yōu)實(shí)踐
2.1. 使用Stride卷積進(jìn)行下采樣
2.2. 使用Stride反卷積進(jìn)行上采樣
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. 批量分開(kāi)真假圖像
3.3. 使用Label Smoothing
3.4. 使用噪聲Labels
1 啟發(fā)式文件GANs的訓(xùn)練方法
2 DCGAN的最優(yōu)實(shí)踐
2.1. 使用Stride卷積進(jìn)行下采樣
# 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反卷積進(jìn)行上采樣
# 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. 批量分開(kāi)真假圖像
# 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
論文參考