kaggle競賽報告:Generative Dog Images

這個競賽的任務是根據給定的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的訓練其實是一個零和問題,天生的難以訓練。

但是可以參考這裏。,也許會有幫助。

教程目錄

主要由兩部分組成:

  1. 啓發式文件GANs的訓練方法
  2. 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]
  3. 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())

API

論文參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章