0% found this document useful (0 votes)
6 views4 pages

M6 - Aliyah Muthi Lathifah - 10521097 - 4PA21

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)
6 views4 pages

M6 - Aliyah Muthi Lathifah - 10521097 - 4PA21

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/ 4

10/30/24, 8:33 PM 4PA21_Aliyah Muthi Lathifah_10521097 - Colab

keyboard_arrow_down 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 import LeakyReLU
from tensorflow.keras.optimizers import Adam
import numpy as np
!mkdir generated_images

keyboard_arrow_down 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(learning_rate=0.0002)

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

keyboard_arrow_down 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

https://colab.research.google.com/drive/1vnJnWO16rcVPpV5yEacL0XFQ6styQ-_A#printMode=true 1/4
10/30/24, 8:33 PM 4PA21_Aliyah Muthi Lathifah_10521097 - Colab
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'])

keyboard_arrow_down 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, 32, 32, 3) │ 1,466,115 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ sequential_1 (Sequential) │ (None, 1) │ 780,545 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 2,246,660 (8.57 MB)
Trainable params: 1,466,115 (5.59 MB)
Non-trainable params: 780,545 (2.98 MB)

keyboard_arrow_down 6) 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
# 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()

https://colab.research.google.com/drive/1vnJnWO16rcVPpV5yEacL0XFQ6styQ-_A#printMode=true 2/4
10/30/24, 8:33 PM 4PA21_Aliyah Muthi Lathifah_10521097 - Colab

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

# Assuming d_loss and g_loss are lists containing loss and accuracy:
# print('******* %d %d [D loss: %f, acc: %.2f%%] [G loss: %f]' % (epoch, d_loss[0],100*d_loss[1], g_loss))
print("******* %d [D loss: %f, acc: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100* d_loss[1], g_loss[0])) # Access the first elem
save_imgs(epoch)

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

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


gen_imgs = generator.predict(noise)

gen_imgs = (gen_imgs + 1) / 2.0


gen_imgs = gen_imgs.squeeze(0)
plt.imshow(gen_imgs)

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

#The if statement was not indented at the correct level


if filenames: # Check if filenames list is not empty
last_filename = filenames[-1]
image = imageio.imread(last_filename)
writer.append_data(image)

https://colab.research.google.com/drive/1vnJnWO16rcVPpV5yEacL0XFQ6styQ-_A#printMode=true 3/4
10/30/24, 8:33 PM 4PA21_Aliyah Muthi Lathifah_10521097 - Colab

https://colab.research.google.com/drive/1vnJnWO16rcVPpV5yEacL0XFQ6styQ-_A#printMode=true 4/4

You might also like