0% found this document useful (1 vote)
206 views7 pages

Prak. Robotika Cerdas Tugas 2

1. The document describes building a generative adversarial network (GAN) to generate handwritten digit images similar to MNIST data. 2. It imports Keras packages, defines variables for the neural networks, and builds a generator and discriminator sequentially. 3. The generator and discriminator are connected to form the GAN, which is trained on real MNIST images and generated fake images to improve the generator.

Uploaded by

Rifqi Ramadhan
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 (1 vote)
206 views7 pages

Prak. Robotika Cerdas Tugas 2

1. The document describes building a generative adversarial network (GAN) to generate handwritten digit images similar to MNIST data. 2. It imports Keras packages, defines variables for the neural networks, and builds a generator and discriminator sequentially. 3. The generator and discriminator are connected to form the GAN, which is trained on real MNIST images and generated fake images to improve the generator.

Uploaded by

Rifqi Ramadhan
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/ 7

3/14/22, 6:14 PM NumberGAN.

ipynb - Colaboratory

1) Importing Python Packages for GAN

from keras.datasets import mnist

from keras.models import Sequential
from keras.layers import BatchNormalization
from keras.layers import Dense, Reshape, Flatten
from keras.layers.advanced_activations import LeakyReLU
from tensorflow.keras.optimizers import Adam

import numpy as np
!mkdir generated_images

2) Variables for Neural Networks & Data

img_width = 28
img_height = 28
channels = 1
img_shape = (img_width, img_height, channels)
latent_dim = 100
adam = Adam(lr=0.0001)

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

3) Building Generator

def build_generator():

  model = Sequential()

  model.add(Dense(256, input_dim=latent_dim))

  model.add(LeakyReLU(alpha=0.2))

  model.add(BatchNormalization(momentum=0.8))

  model.add(Dense(256))

  model.add(LeakyReLU(alpha=0.2))

  model.add(BatchNormalization(momentum=0.8))

  model.add(Dense(256))

  model.add(LeakyReLU(alpha=0.2))

  model.add(BatchNormalization(momentum=0.8))

  model.add(Dense(np.prod(img_shape), activation='tanh'))

  model.add(Reshape(img_shape))

  

https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 1/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory

  model.summary()

  return model

generator = build_generator()

Model: "sequential"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

dense (Dense) (None, 256) 25856

leaky_re_lu (LeakyReLU) (None, 256) 0

batch_normalization (BatchN (None, 256) 1024

ormalization)

dense_1 (Dense) (None, 256) 65792

leaky_re_lu_1 (LeakyReLU) (None, 256) 0

batch_normalization_1 (Batc (None, 256) 1024

hNormalization)

dense_2 (Dense) (None, 256) 65792

leaky_re_lu_2 (LeakyReLU) (None, 256) 0

batch_normalization_2 (Batc (None, 256) 1024

hNormalization)

dense_3 (Dense) (None, 784) 201488

reshape (Reshape) (None, 28, 28, 1) 0

=================================================================

Total params: 362,000

Trainable params: 360,464

Non-trainable params: 1,536

_________________________________________________________________

4) Building Discriminator

def build_discriminator():

  model = Sequential()

  model.add(Flatten(input_shape=img_shape))

  model.add(Dense(512))

  model.add(LeakyReLU(alpha=0.2))

  model.add(Dense(256))

  model.add(Dense(1, activation='sigmoid'))

  model.summary()

  return model

https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 2/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory

discriminator = build_discriminator()

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

Model: "sequential_1"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

flatten (Flatten) (None, 784) 0

dense_4 (Dense) (None, 512) 401920

leaky_re_lu_3 (LeakyReLU) (None, 512) 0

dense_5 (Dense) (None, 256) 131328

dense_6 (Dense) (None, 1) 257

=================================================================

Total params: 533,505

Trainable params: 533,505

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()

Model: "sequential_2"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

sequential (Sequential) (None, 28, 28, 1) 362000

sequential_1 (Sequential) (None, 1) 533505

=================================================================

Total params: 895,505

Trainable params: 360,464

Non-trainable params: 535,041

_________________________________________________________________

6) Outputting Images

#@title

## **7) Outputting Images**

import matplotlib.pyplot as plt

https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 3/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory

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

    gen_imgs = 0.5 * gen_imgs + 0.5

    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, :,:,0], cmap='gray')

            # axs[i,j].imshow(gen_imgs[cnt])

            axs[i,j].axis('off')

            cnt += 1

    fig.savefig("generated_images/%.8f.png" % save_name)

    print('saved')

    plt.close()

7) Training GAN

def train(epochs, batch_size=64, save_interval=200):

  (X_train, _), (_, _) = mnist.load_data()

  # print(X_train.shape)

  #Rescale data between -1 and 1

  X_train = X_train / 127.5 -1.

  # X_train = np.expand_dims(X_train, axis=3)

  # print(X_train.shape)

  #Create our Y for our Neural Networks

  valid = np.ones((batch_size, 1))

  fakes = np.zeros((batch_size, 1))

  for epoch in range(epochs):

    #Get Random Batch

    idx = np.random.randint(0, X_train.shape[0], batch_size)

    imgs = X_train[idx]

    #Generate Fake Images

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

https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 4/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory

    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 loss: %f, acc: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100* d_

    if(epoch % save_interval) == 0:

      save_imgs(epoch)

  # print(valid)

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

******* 29942 [D loss: 0.559609, acc: 70.31%] [G loss: 1.274176]

******* 29943 [D loss: 0.562384, acc: 71.09%] [G loss: 1.345835]

******* 29944 [D loss: 0.468171, acc: 78.91%] [G loss: 1.376247]

******* 29945 [D loss: 0.539233, acc: 71.09%] [G loss: 1.593389]

******* 29946 [D loss: 0.609176, acc: 68.75%] [G loss: 1.719936]

******* 29947 [D loss: 0.447626, acc: 80.47%] [G loss: 1.609250]

******* 29948 [D loss: 0.570068, acc: 71.88%] [G loss: 1.363200]

******* 29949 [D loss: 0.519664, acc: 71.88%] [G loss: 1.314612]

******* 29950 [D loss: 0.536247, acc: 71.88%] [G loss: 1.293153]

******* 29951 [D loss: 0.601880, acc: 66.41%] [G loss: 1.475299]

******* 29952 [D loss: 0.522597, acc: 72.66%] [G loss: 1.409748]

******* 29953 [D loss: 0.495711, acc: 79.69%] [G loss: 1.676774]

******* 29954 [D loss: 0.462763, acc: 82.03%] [G loss: 1.689979]

******* 29955 [D loss: 0.574412, acc: 70.31%] [G loss: 1.281035]

******* 29956 [D loss: 0.477904, acc: 78.91%] [G loss: 1.460865]

******* 29957 [D loss: 0.460951, acc: 79.69%] [G loss: 1.607989]

******* 29958 [D loss: 0.573022, acc: 69.53%] [G loss: 1.745356]

******* 29959 [D loss: 0.518633, acc: 69.53%] [G loss: 1.448777]

******* 29960 [D loss: 0.628339, acc: 71.09%] [G loss: 1.390859]

******* 29961 [D loss: 0.569294, acc: 72.66%] [G loss: 1.289187]

******* 29962 [D loss: 0.524241, acc: 75.00%] [G loss: 1.568775]

******* 29963 [D loss: 0.568505, acc: 70.31%] [G loss: 1.456203]

******* 29964 [D loss: 0.561265, acc: 75.00%] [G loss: 1.549096]

******* 29965 [D loss: 0.595656, acc: 67.97%] [G loss: 1.352225]

******* 29966 [D loss: 0.598945, acc: 68.75%] [G loss: 1.322804]

******* 29967 [D loss: 0.545177, acc: 68.75%] [G loss: 1.389703]

******* 29968 [D loss: 0.560595, acc: 74.22%] [G loss: 1.494714]

******* 29969 [D loss: 0.613210, acc: 68.75%] [G loss: 1.534424]

******* 29970 [D loss: 0.508624, acc: 78.12%] [G loss: 1.334824]

******* 29971 [D loss: 0.522550, acc: 71.09%] [G loss: 1.595869]

******* 29972 [D loss: 0.496946, acc: 75.78%] [G loss: 1.426164]

******* 29973 [D loss: 0.666016, acc: 69.53%] [G loss: 1.390148]

******* 29974 [D loss: 0.567081, acc: 70.31%] [G loss: 1.159011]

******* 29975 [D loss: 0.492483, acc: 74.22%] [G loss: 1.361443]

******* 29976 [D loss: 0.514459, acc: 76.56%] [G loss: 1.619536]

******* 29977 [D loss: 0.522412, acc: 74.22%] [G loss: 1.687408]

******* 29978 [D loss: 0.558854, acc: 73.44%] [G loss: 1.327003]


https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 5/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory
29978 [D loss: 0.558854, acc: 73.44%] [G loss: 1.327003]

******* 29979 [D loss: 0.562206, acc: 70.31%] [G loss: 1.694671]

******* 29980 [D loss: 0.525805, acc: 70.31%] [G loss: 1.349991]

******* 29981 [D loss: 0.527241, acc: 73.44%] [G loss: 1.485349]

******* 29982 [D loss: 0.484577, acc: 77.34%] [G loss: 1.573783]

******* 29983 [D loss: 0.487552, acc: 79.69%] [G loss: 1.571594]

******* 29984 [D loss: 0.624213, acc: 69.53%] [G loss: 1.419726]

******* 29985 [D loss: 0.632475, acc: 66.41%] [G loss: 1.326593]

******* 29986 [D loss: 0.488182, acc: 73.44%] [G loss: 1.428414]

******* 29987 [D loss: 0.591298, acc: 68.75%] [G loss: 1.369640]

******* 29988 [D loss: 0.459753, acc: 78.12%] [G loss: 1.578751]

******* 29989 [D loss: 0.509766, acc: 78.91%] [G loss: 1.669813]

******* 29990 [D loss: 0.491158, acc: 77.34%] [G loss: 1.348950]

******* 29991 [D loss: 0.625076, acc: 66.41%] [G loss: 1.167850]

******* 29992 [D loss: 0.632827, acc: 67.19%] [G loss: 1.197102]

******* 29993 [D loss: 0.500343, acc: 78.12%] [G loss: 1.425504]

******* 29994 [D loss: 0.520639, acc: 78.91%] [G loss: 1.719311]

******* 29995 [D loss: 0.424208, acc: 81.25%] [G loss: 1.839022]

******* 29996 [D loss: 0.471873, acc: 76.56%] [G loss: 1.578932]

******* 29997 [D loss: 0.498148, acc: 73.44%] [G loss: 1.425658]

******* 29998 [D loss: 0.583471, acc: 72.66%] [G loss: 1.398373]

******* 29999 [D loss: 0.541282, acc: 71.09%] [G loss: 1.406367]

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/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 6/7
3/14/22, 6:14 PM NumberGAN.ipynb - Colaboratory

check 2s completed at 6:13 PM

https://colab.research.google.com/drive/1IXODAITOAo5BGLfViCY_ExK-8UHBQFNY#scrollTo=XPShgQpg1EMy 7/7

You might also like