0% found this document useful (0 votes)
231 views8 pages

GAN Tutorial for Python Users

This document describes the process of building a generative adversarial network (GAN) in Python to generate images. It includes: 1) Importing necessary packages for building the GAN model 2) Defining parameters for the neural networks and loading the CIFAR10 dataset 3) Building the generator model as a sequential model with dense, reshape, transpose convolutional and convolutional layers 4) Building the discriminator model as a sequential model with convolutional, leaky ReLU and dense layers 5) Connecting the generator and discriminator to form the full GAN model 6) Defining functions to output and save generated images at intervals during training 7) Defining a training function to train the GAN by alternatingly training the discriminator and

Uploaded by

Hilman Ovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
231 views8 pages

GAN Tutorial for Python Users

This document describes the process of building a generative adversarial network (GAN) in Python to generate images. It includes: 1) Importing necessary packages for building the GAN model 2) Defining parameters for the neural networks and loading the CIFAR10 dataset 3) Building the generator model as a sequential model with dense, reshape, transpose convolutional and convolutional layers 4) Building the discriminator model as a sequential model with convolutional, leaky ReLU and dense layers 5) Connecting the generator and discriminator to form the full GAN model 6) Defining functions to output and save generated images at intervals during training 7) Defining a training function to train the GAN by alternatingly training the discriminator and

Uploaded by

Hilman Ovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/4/22, 12:13 PM Praktikum M5.

ipynb - Colaboratory

1) Importing Python Packages for GAN

from keras.datasets import cifar10, mnist
from keras.models import Sequential
from keras.layers import Reshape
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import Conv2DTranspose
from keras.layers import Dropout
from keras.layers.advanced_activations import LeakyReLU
from tensorflow.keras.optimizers import Adam
import numpy as np
!mkdir generated_images

2) Parameters for Neural Networks & Data

img_width = 32
img_height = 32
channels = 3
img_shape = (img_width, img_height, channels)
latent_dim = 100
adam = Adam(lr=0.0002)

/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning:
super(Adam, self).__init__(name, **kwargs)

3) Building Generator

def build_generator():
    model = Sequential()

    # Create first layer, to receive the input 
    model.add(Dense(256 * 4 * 4, input_dim = latent_dim))
    # 256 * 8 * 8; for upscaling the layers, 
    # initial shape to construct into final shape

    # Create default activation function
    model.add(LeakyReLU(alpha = 0.2))

    # Create reshape layer
    model.add(Reshape((4, 4,256)))
https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 1/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory

    # 8,8,256 ; reffers to first layer

    # Adding more layers for neurons and better result
    model.add(Conv2DTranspose(128, (4,4), strides = (2,2), padding = 'same'))
    model.add(LeakyReLU(alpha= 0.2))
    model.add(Conv2DTranspose(128, (4,4), strides = (2,2), padding = 'same'))
    model.add(LeakyReLU(alpha= 0.2))
    model.add(Conv2DTranspose(128, (4,4), strides = (2,2), padding = 'same'))
    model.add(LeakyReLU(alpha= 0.2))
    # (4,4) >> filter size
    # strides = (2,2) >> Convolutional layers, that how NN understand images

    # Create Final output layer and forming image shape
    # the shape (3, (3,3)) reffers to image shape :
    #    >>>  img_shape = (img_width, img_height, channels)
    model.add(Conv2D(3, (3,3), activation= 'tanh', padding = 'same'))

    #
    model.summary()
    return model

generator = build_generator()

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 4096) 413696

leaky_re_lu (LeakyReLU) (None, 4096) 0

reshape (Reshape) (None, 4, 4, 256) 0

conv2d_transpose (Conv2DTra (None, 8, 8, 128) 524416


nspose)

leaky_re_lu_1 (LeakyReLU) (None, 8, 8, 128) 0

conv2d_transpose_1 (Conv2DT (None, 16, 16, 128) 262272


ranspose)

leaky_re_lu_2 (LeakyReLU) (None, 16, 16, 128) 0

conv2d_transpose_2 (Conv2DT (None, 32, 32, 128) 262272


ranspose)

leaky_re_lu_3 (LeakyReLU) (None, 32, 32, 128) 0

conv2d (Conv2D) (None, 32, 32, 3) 3459

=================================================================
Total params: 1,466,115
Trainable params: 1,466,115

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 2/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory
Non-trainable params: 0
_________________________________________________________________

4) Building Discriminator

def build_discriminator():
    model = Sequential()

    # Create input layer and filter and stride layer. That makes NN understand image
    model.add(Conv2D(64, (3,3), padding = 'same', input_shape = img_shape))

    # Adding activation function
    model.add(LeakyReLU(alpha = 0.2))
    model.add(Conv2D(128, (3,3), padding = 'same'))
    model.add(LeakyReLU(alpha = 0.2))
    model.add(Conv2D(128, (3,3), padding = 'same'))
    model.add(LeakyReLU(alpha = 0.2))
    model.add(Conv2D(256, (3,3), padding = 'same'))
    model.add(LeakyReLU(alpha = 0.2))
    model.add(Flatten())

    model.add(Dropout(0.4))

    # Create output layer
    model.add(Dense(1, activation = 'sigmoid'))

    model.summary()
    return model

discriminator = build_discriminator()
discriminator.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])

Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 32, 32, 64) 1792

leaky_re_lu_4 (LeakyReLU) (None, 32, 32, 64) 0

conv2d_2 (Conv2D) (None, 32, 32, 128) 73856

leaky_re_lu_5 (LeakyReLU) (None, 32, 32, 128) 0

conv2d_3 (Conv2D) (None, 32, 32, 128) 147584

leaky_re_lu_6 (LeakyReLU) (None, 32, 32, 128) 0

conv2d_4 (Conv2D) (None, 32, 32, 256) 295168

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 3/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory

leaky_re_lu_7 (LeakyReLU) (None, 32, 32, 256) 0

flatten (Flatten) (None, 262144) 0

dropout (Dropout) (None, 262144) 0

dense_1 (Dense) (None, 1) 262145

=================================================================
Total params: 780,545
Trainable params: 780,545
Non-trainable params: 0
_________________________________________________________________

5) Connecting Neural Networks to build GAN

GAN = Sequential()
discriminator.trainable = False
GAN.add(generator)
GAN.add(discriminator)

GAN.compile(loss='binary_crossentropy', optimizer=adam)

#GAN.summary()

6) Outputting Images

#@title
## **7) Outputting Images**
import matplotlib.pyplot as plt
import glob
import imageio
import PIL

save_name = 0.00000000

def save_imgs(epoch):
    r, c = 5, 5
    noise = np.random.normal(0, 1, (r * c, latent_dim))
    gen_imgs = generator.predict(noise)
    global save_name
    save_name += 0.00000001
    # print("%.8f" % save_name)

    # Rescale images 0 - 1

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 4/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory

    # gen_imgs = 0.5 * gen_imgs + 0.5
    gen_imgs = (gen_imgs + 1) / 2.0
    # gen_imgs = gen_imgs * 255

    fig, axs = plt.subplots(r, c)
    cnt = 0
    for i in range(r):
        for j in range(c):
            axs[i,j].imshow(gen_imgs[cnt])
            axs[i,j].axis('off')
            cnt += 1
    fig.savefig("generated_images/%.8f.png" % save_name)
    plt.close()

7) Training GAN

def train(epochs, batch_size = 64, save_interval = 200):
  (X_train, _), (_, _) = cifar10.load_data()

  # Rescaling the data
  X_train = X_train / 127.5 -1.

  bat_per_epo = int(X_train.shape[0] / batch_size)

  # Create Y label for NN
  valid = np.ones((batch_size,1))
  fakes = np.zeros((batch_size, 1))

  for epoch in range (epochs) :
    for j in range(bat_per_epo) :
      #Get Random Batch
      idx = np.random.randint(0, X_train.shape[0], batch_size)
      imgs = X_train[idx]

      # Generate Fakes Images
      noise = np.random.normal(0, 1, (batch_size, latent_dim))
      gen_imgs = generator.predict(noise)

      # Train Discriminator
      d_loss_real = discriminator.train_on_batch(imgs, valid)
      d_loss_fake = discriminator.train_on_batch(gen_imgs, fakes)
      d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

      noise = np.random.normal(0, 1, (batch_size, latent_dim))

      # Inverse Y label
      g_loss = GAN.train_on_batch(noise, valid)

      #print('******* %d %d [D loss: %f, acc: %.2f%%] [G loss: %f]' % (epoch, d_loss[0],10

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 5/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory

      print("******* %d [D loss: %f, acc: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100* 
    save_imgs(epoch)

train(30, batch_size = 64, save_interval = 200)

Output streaming akan dipotong hingga 5000 baris terakhir.


******* 23 [D loss: 0.237705, acc: 91.41%] [G loss: 2.605383]
******* 23 [D loss: 0.226395, acc: 91.41%] [G loss: 3.003776]
******* 23 [D loss: 0.291051, acc: 87.50%] [G loss: 3.033010]
******* 23 [D loss: 0.233918, acc: 91.41%] [G loss: 2.888760]
******* 23 [D loss: 0.296427, acc: 86.72%] [G loss: 2.438142]
******* 23 [D loss: 0.182580, acc: 92.19%] [G loss: 2.538202]
******* 23 [D loss: 0.247833, acc: 89.06%] [G loss: 2.869013]
******* 23 [D loss: 0.199248, acc: 89.84%] [G loss: 3.006717]
******* 23 [D loss: 0.288649, acc: 87.50%] [G loss: 2.768791]
******* 23 [D loss: 0.284107, acc: 86.72%] [G loss: 3.141478]
******* 23 [D loss: 0.215450, acc: 92.19%] [G loss: 2.565929]
******* 23 [D loss: 0.271300, acc: 86.72%] [G loss: 2.979988]
******* 23 [D loss: 0.230862, acc: 91.41%] [G loss: 3.043058]
******* 23 [D loss: 0.250167, acc: 90.62%] [G loss: 2.582546]
******* 23 [D loss: 0.163865, acc: 94.53%] [G loss: 2.685328]
******* 23 [D loss: 0.183212, acc: 92.97%] [G loss: 2.352496]
******* 23 [D loss: 0.264949, acc: 91.41%] [G loss: 2.393627]
******* 23 [D loss: 0.210630, acc: 91.41%] [G loss: 3.298650]
******* 23 [D loss: 0.179395, acc: 92.97%] [G loss: 3.330546]
******* 23 [D loss: 0.270062, acc: 89.06%] [G loss: 3.541386]
******* 23 [D loss: 0.236943, acc: 89.06%] [G loss: 2.586155]
******* 23 [D loss: 0.246239, acc: 89.84%] [G loss: 2.442355]
******* 23 [D loss: 0.210629, acc: 92.97%] [G loss: 2.698267]
******* 23 [D loss: 0.205155, acc: 92.19%] [G loss: 2.841053]
******* 23 [D loss: 0.224557, acc: 90.62%] [G loss: 3.257985]
******* 23 [D loss: 0.263853, acc: 89.84%] [G loss: 2.327201]
******* 23 [D loss: 0.235647, acc: 89.84%] [G loss: 2.499351]
******* 23 [D loss: 0.294156, acc: 85.94%] [G loss: 2.291996]
******* 23 [D loss: 0.299462, acc: 85.94%] [G loss: 3.056570]
******* 23 [D loss: 0.195242, acc: 91.41%] [G loss: 3.168653]
******* 23 [D loss: 0.296736, acc: 84.38%] [G loss: 2.874703]
******* 23 [D loss: 0.331240, acc: 85.16%] [G loss: 2.368404]
******* 23 [D loss: 0.335965, acc: 82.03%] [G loss: 1.992947]
******* 23 [D loss: 0.280721, acc: 86.72%] [G loss: 2.606233]
******* 23 [D loss: 0.171700, acc: 94.53%] [G loss: 3.000512]
******* 23 [D loss: 0.278180, acc: 91.41%] [G loss: 3.339077]
******* 23 [D loss: 0.293942, acc: 85.94%] [G loss: 2.718234]
******* 23 [D loss: 0.413896, acc: 83.59%] [G loss: 2.257464]
******* 23 [D loss: 0.306837, acc: 86.72%] [G loss: 2.507373]
******* 23 [D loss: 0.434177, acc: 82.03%] [G loss: 2.377747]
******* 23 [D loss: 0.395241, acc: 82.81%] [G loss: 2.876856]
******* 23 [D loss: 0.306901, acc: 89.06%] [G loss: 2.846602]
******* 23 [D loss: 0.278472, acc: 85.94%] [G loss: 2.912837]
******* 23 [D loss: 0.463771, acc: 78.12%] [G loss: 2.115741]
******* 23 [D loss: 0.388522, acc: 80.47%] [G loss: 2.195743]
******* 23 [D loss: 0.345006, acc: 82.81%] [G loss: 1.875096]
******* 23 [D loss: 0.177174, acc: 92.19%] [G loss: 2.861898]
******* 23 [D loss: 0.226428, acc: 91.41%] [G loss: 3.435443]
******* 23 [D loss: 0.369448, acc: 86.72%] [G loss: 2.722461]
******* 23 [D loss: 0.399197, acc: 85.16%] [G loss: 2.325563]
https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 6/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory
******* 23 [D loss: 0.306209, acc: 82.03%] [G loss: 1.891291]
******* 23 [D loss: 0.276683, acc: 84.38%] [G loss: 2.449034]
******* 23 [D loss: 0.204344, acc: 94.53%] [G loss: 3.205822]
******* 23 [D loss: 0.241797, acc: 89.06%] [G loss: 3.016014]
******* 23 [D loss: 0.264371, acc: 87.50%] [G loss: 2.879304]
******* 23 [D loss: 0.321563, acc: 82.81%] [G loss: 2.635684]
******* 23 [D loss: 0.237498, acc: 90.62%] [G loss: 2.933164]

noise = np.random.normal(0, 1, (1,latent_dim))
gen_imgs = generator.predict(noise)

gen_imgs = (gen_imgs + 1) / 2.0
plt.imshow(gen_imgs)

8) Making GIF

# Display a single image using the epoch number
# def display_image(epoch_no):
#   return PIL.Image.open('generated_images/%.8f.png'.format(epoch_no))

anim_file = 'dcgan.gif'

with imageio.get_writer(anim_file, mode='I') as writer:
  filenames = glob.glob('generated_images/*.png')
  filenames = sorted(filenames)
  for filename in filenames:
    image = imageio.imread(filename)
    writer.append_data(image)
  image = imageio.imread(filename)
  writer.append_data(image)

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 7/8
4/4/22, 12:13 PM Praktikum M5.ipynb - Colaboratory

https://colab.research.google.com/drive/1j0yVXuctqNpakW_a2yGcRdbNMRTsGR9W#printMode=true 8/8

You might also like