Generative Adversarial Networks (GAN)


By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

Table of Contents

0. Video Lectures

In [4]:
%%html
<center><iframe src="https://www.youtube.com/embed/d3DGaIoRRNo?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
In [3]:
%%html
<center><iframe src="https://www.youtube.com/embed/9IItTSYOfqM?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
In [2]:
%%html
<center><iframe src="https://www.youtube.com/embed/YzZpWwJlesE?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
In [5]:
%%html
<center><iframe src="https://www.youtube.com/embed/H_F28cy2wNs?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>

1. Discriminative Model v.s. Generative Model

  • Discriminative model




  • Cenerative model



2. Density Function Estimation

  • Probability density function estimation problem
  • If $P_{\text{model}}(x)$ can be estimated as close to $P_{\text{data}}(x)$, then data can be generated by sampling from $P_{\text{model}}(x)$.

    • Note: Kullback–Leibler Divergence is a kind of distance measure between two distributions
  • Learn determinstic transformation via a neural network
    • Start by sampling the code vector $z$ from a simple, fixed distribution such as a uniform distribution or a standard Gaussian $\mathcal{N}(0,I)$
    • Then this code vector is passed as input to a deterministic generator network $G$, which produces an output sample $x=G(z)$
    • This is how a neural network plays in a generative model (as a nonlinear mapping to a target probability density function)



- An example of a generator network which encodes a univariate distribution with two different modes

  • Generative model of high dimensional space

3. Generative Adversarial Networks (GAN)

  • In generative modeling, we'd like to train a network that models a distribution, such as a distribution over images.

  • GANs do not work with any explicit density function !

  • Instead, take game-theoretic approach

  • One way to judge the quality of the model is to sample from it.

  • Model to produce samples which are indistinguishable from the real data, as judged by a discriminator network whose job is to tell real from fake






- The idea behind Generative Adversarial Networks (GANs): train two different networks
- Discriminator network: try to distinguish between real and fake data

- Generator network: try to produce realistic-looking samples to fool the discriminator network

4. GAN with MNIST

4.1. GAN Implementation

In [1]:
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
In [2]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
mnist = tf.keras.datasets.mnist

(train_x, train_y), (test_x, test_y) = mnist.load_data()

train_x, test_x = train_x/255.0, test_x/255.0
train_x = train_x.reshape(-1, 784)
test_x = test_x.reshape(-1, 784)

print('train_x: ', train_x.shape)
print('test_x: ', test_x.shape)
train_x:  (60000, 784)
test_x:  (10000, 784)
In [4]:
generator = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units = 256, input_dim = 100, activation = 'relu'),
    tf.keras.layers.Dense(units = 784, activation = 'sigmoid')    
])
In [5]:
discriminator = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units = 256, input_dim = 784, activation = 'relu'),
    tf.keras.layers.Dense(units = 1, activation = 'sigmoid'),
])
In [6]:
discriminator.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001), 
                      loss = 'binary_crossentropy')
In [7]:
combined_input = tf.keras.layers.Input(shape = (100,))
generated = generator(combined_input)
discriminator.trainable = False
combined_output = discriminator(generated)

combined = tf.keras.models.Model(inputs = combined_input, outputs = combined_output)
In [8]:
combined.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0002), 
                 loss = 'binary_crossentropy')
In [9]:
def make_noise(samples):
    return np.random.normal(0, 1, [samples, 100])
In [10]:
def plot_generated_images(generator, samples = 3):
    
    noise = make_noise(samples)
    
    generated_images = generator.predict(noise)
    generated_images = generated_images.reshape(samples, 28, 28)
    
    for i in range(samples):
        plt.subplot(1, samples, i+1)
        plt.imshow(generated_images[i], 'gray', interpolation = 'nearest')
        plt.axis('off')
        plt.tight_layout()
    plt.show()
In [11]:
n_iter = 20000
batch_size = 100

fake = np.zeros(batch_size)
real = np.ones(batch_size)

for i in range(n_iter):
        
    # Train Discriminator
    noise = make_noise(batch_size)
    generated_images = generator.predict(noise)

    idx = np.random.randint(0, train_x.shape[0], batch_size)
    real_images = train_x[idx]
    
    D_loss_real = discriminator.train_on_batch(real_images, real)
    D_loss_fake = discriminator.train_on_batch(generated_images, fake)
    D_loss = D_loss_real + D_loss_fake
    
    # Train Generator
    noise = make_noise(batch_size)    
    G_loss = combined.train_on_batch(noise, real)
    
    if i % 5000 == 0:
        
        print('Discriminator Loss: ', D_loss)
        print('Generator Loss: ', G_loss)

        plot_generated_images(generator)
Discriminator Loss:  1.5352138876914978
Generator Loss:  0.7901448011398315
Discriminator Loss:  0.11274127289652824
Generator Loss:  3.2274858951568604
Discriminator Loss:  0.4444339722394943
Generator Loss:  2.0490427017211914
Discriminator Loss:  0.34972238540649414
Generator Loss:  2.3174216747283936

4.2. After Training

  • After training, use the generator network to generate new data


In [12]:
plot_generated_images(generator)

5. GAN with Vibration Signal

In [13]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from scipy.fftpack import fft, fftshift
from scipy.signal import spectrogram
$$x = 4 \sin(2\pi 2 t) + 2 \sin(2\pi 5 t) + \varepsilon$$
In [14]:
# Gen. & plot signal
Fs = 24
T = 1/Fs

N = 48

t = np.arange(0, N)*T

k = np.arange(0, N)
f = (Fs/N)*k      

x1 = 4*np.sin(2*np.pi*2*t) + 0.5*np.random.randn(N)
x2 = 2*np.sin(2*np.pi*5*t) + 0.5*np.random.randn(N)
x = x1 + x2

xt = fft(x)/N
xtss = xt[0:int(N/2)+1]      # 0:N/2
xtss[1:-1] = 2*xtss[1:-1]

fss = f[0:int(N/2)+1]

plt.figure(figsize=(10,10))
plt.subplot(3,1,1)
plt.plot(t, x1)
plt.ylim([-5,5])
plt.grid()
plt.title('Vibration 1')
plt.subplot(3,1,2)
plt.plot(t, x2)
plt.ylim([-5,5])
plt.grid()
plt.title('Vibration 2')
plt.subplot(3,1,3)
plt.plot(t, x)
plt.ylim([-5,5])
plt.grid()
plt.title('Vibration 1 + Vibration 2')
plt.show()

plt.figure(figsize = (10,3))
plt.plot(fss, np.abs(xtss))
plt.grid()
plt.xlim([np.min(fss), np.max(fss)])
plt.xlabel('f')
plt.ylabel('|X(f)|', fontsize = 15)
plt.ylim([0, 4.1])
plt.title('Single-sided FFT')
plt.show()
In [15]:
def data_gen(f1,f2,A1,A2):
    Fs = 24
    T = 1/Fs

    N = 48

    t = np.arange(0, N)*T

    k = np.arange(0, N)
    f = (Fs/N)*k      

    x1 = A1*np.sin(2*np.pi*f1*t)
    x2 = A2*np.sin(2*np.pi*f2*t)
    x = x1 + x2
    
    return x
In [16]:
x = data_gen(2,5,2,4)

plt.figure(figsize = (10,3))
plt.plot(t, x)
plt.grid()
plt.show()
In [17]:
def batch_maker(n_batch):
    
    signals = []    
    for _ in range(n_batch):
                        
        A1 = 0.4 + 0.1*np.random.random()   # uniform [0.4,0.5]
        A2 = 0.1 + 0.2*np.random.random()   # uniform [0.1,0.3]
        x = data_gen(2, 5, A1, A2)        
        signals.append(x)
        
    return np.array(signals)
In [18]:
x = batch_maker(1)

plt.figure(figsize=(10,3))
plt.plot(t, x[0])
plt.grid()
plt.show()
In [19]:
generator = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units = 32, input_dim = 10, activation = 'relu'),
    tf.keras.layers.Dense(units = 32, activation = 'relu'),
    tf.keras.layers.Dense(units = 48, activation = 'relu'),
    tf.keras.layers.Dense(units = 48, activation = 'tanh'),
])
In [20]:
discriminator = tf.keras.models.Sequential([
    tf.keras.layers.Dense(units = 50, input_dim = 48, activation = 'relu'),
    tf.keras.layers.Dense(units = 32, activation = 'relu'),
    tf.keras.layers.Dense(units = 32, activation = 'relu'),
    tf.keras.layers.Dense(units = 1, activation = 'sigmoid'),
])
In [21]:
discriminator.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 2e-5), 
                      loss = 'binary_crossentropy')
In [22]:
combined_input = tf.keras.layers.Input(shape = (10,))
generated = generator(combined_input)
discriminator.trainable = False
combined_output = discriminator(generated)

combined = tf.keras.models.Model(inputs = combined_input, outputs = combined_output)
In [23]:
combined.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 2e-5), 
                 loss = 'binary_crossentropy')
In [24]:
def make_noise(samples):
    return np.random.normal(0, 1, [samples, 10])
In [25]:
def plot_generated_images(generator, samples = 2):
    
    noise = make_noise(samples)
    
    generated_images = generator.predict(noise)
    
    plt.figure(figsize = (10,3))
    for i in range(samples):
        plt.subplot(1, samples, i+1)
        plt.plot(generated_images[i])
        plt.grid()
        plt.tight_layout()
    plt.show()
In [26]:
n_iter = 5000
batch_size = 100

fake = np.zeros(batch_size)
real = np.ones(batch_size)

for i in range(n_iter):
        
    # Train Discriminator
    noise = make_noise(batch_size)
    generated_signals = generator.predict(noise)

    real_signals = batch_maker(batch_size)
    
    D_loss_real = discriminator.train_on_batch(real_signals, real)
    D_loss_fake = discriminator.train_on_batch(generated_signals, fake)
    D_loss = D_loss_real + D_loss_fake
    
    # Train Generator
    noise = make_noise(batch_size)    
    G_loss = combined.train_on_batch(noise, real)
    
    if i % 1000 == 0:
        
        print('Discriminator Loss: ', D_loss)
        print('Generator Loss: ', G_loss)

        plot_generated_images(generator)
Discriminator Loss:  1.3505698442459106
Generator Loss:  0.6310455799102783
Discriminator Loss:  0.947221115231514
Generator Loss:  0.6556361317634583
Discriminator Loss:  1.1822659075260162
Generator Loss:  0.5793311595916748
Discriminator Loss:  1.302720546722412
Generator Loss:  0.7551210522651672
Discriminator Loss:  1.306977927684784
Generator Loss:  0.9063000679016113
In [27]:
Fs = 24
T = 1/Fs

N = 48

t = np.arange(0, N)*T

k = np.arange(0, N)
f = (Fs/N)*k 

noise = make_noise(1)
generated_images = generator.predict(noise)
x = generated_images[0]

xt = fft(x)/N
xtss = xt[0:int(N/2)+1]      # 0:N/2
xtss[1:-1] = 2*xtss[1:-1]

fss = f[0:int(N/2)+1]
In [28]:
plt.figure(figsize = (10,7))
plt.subplot(2,1,1)
plt.plot(t, x)
plt.ylim([-1, 1])
plt.grid()
plt.title('Generated Vibration', fontsize = 15)

plt.subplot(2,1,2)
plt.plot(fss, np.abs(xtss))
plt.grid()
plt.xlim([np.min(fss), np.max(fss)])
plt.xlabel('f')
plt.ylabel('|X(f)|', fontsize = 15)
plt.ylim([0, 1.1])
plt.title('Single-sided FFT')
plt.show()
In [29]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')