Hendra Bayu - 12419795 - Robot M3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

10/15/22, 2:41 PM TEKREK M3 - Jupyter Notebook

1) Importing Python Packages for GAN


In [1]: 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

mkdir: cannot create directory ‘generated_images’: Permission denied

2) Parameters for Neural Networks & Data


In [2]: img_width = 32
img_height = 32
channels = 3
img_shape = (img_width, img_height, channels)
latent_dim = 100
adam = Adam(learning_rate=0.0002)

3) Building Generator

https://hypercomputation-hub.gunadarma.ac.id/fti/user/13419274/notebooks/Praktikum-Tingkat-4-Tekrek/TEKREK M3.ipynb 1/5


10/15/22, 2:41 PM TEKREK M3 - Jupyter Notebook

In [3]: 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()

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 (Conv2DTran (None, 8, 8, 128) 524416

_________________________________________________________________

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

_________________________________________________________________

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

_________________________________________________________________

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

_________________________________________________________________

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

_________________________________________________________________

https://hypercomputation-hub.gunadarma.ac.id/fti/user/13419274/notebooks/Praktikum-Tingkat-4-Tekrek/TEKREK M3.ipynb 2/5


10/15/22, 2:41 PM TEKREK M3 - Jupyter Notebook

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

Non-trainable params: 0

_________________________________________________________________

4) Building Discriminator

https://hypercomputation-hub.gunadarma.ac.id/fti/user/13419274/notebooks/Praktikum-Tingkat-4-Tekrek/TEKREK M3.ipynb 3/5


10/15/22, 2:41 PM TEKREK M3 - Jupyter Notebook

In [4]: def build_discriminator():


model = Sequential()

# Create input layer and filter and stride layer. That makes NN understand im
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=['accur

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

_________________________________________________________________

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

_________________________________________________________________

https://hypercomputation-hub.gunadarma.ac.id/fti/user/13419274/notebooks/Praktikum-Tingkat-4-Tekrek/TEKREK M3.ipynb 4/5


10/15/22, 2:41 PM TEKREK M3 - Jupyter Notebook

5) Connecting Neural Networks to build GAN


In [5]: GAN = Sequential()
discriminator.trainable = False
GAN.add(generator)
GAN.add(discriminator)

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

In [6]: #GAN.summary()

6) Outputting Images
In [ ]: ​

7) Training GAN
In [ ]: ​

In [ ]: ​

In [ ]: ​

8) Making GIF

In [ ]: ​

https://hypercomputation-hub.gunadarma.ac.id/fti/user/13419274/notebooks/Praktikum-Tingkat-4-Tekrek/TEKREK M3.ipynb 5/5

You might also like