DV Dis

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

Member 2 – code - Generator Model

# Creating the Generator Model

KI = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)

input_dim = 300

def Generator_Model():

Generator = Sequential()

Generator.add(Dense(8 * 8 * 512, input_dim=input_dim))

Generator.add(ReLU())

Generator.add(Reshape((8, 8, 512)))

Generator.add(Conv2DTranspose(256, (4, 4), strides=(2, 2), padding='same', kernel_initializer=KI,


activation='ReLU'))

Generator.add(Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', kernel_initializer=KI,


activation='ReLU'))
Generator.add(Conv2DTranspose(64, (4, 4), strides=(2, 2), padding='same', kernel_initializer=KI,
activation='ReLU'))

Generator.add(Conv2D(3, (4, 4), padding='same', activation='sigmoid'))

return Generator

generator = Generator_Model()

generator.summary()

keras.utils.plot_model(generator, show_shapes=True)

…………………………………………………………………………………………………………………………………………………………

Member 3 code - Discriminator Model

# Creating the discriminator Model

def Discriminator_Model():

input_shape = (64, 64, 3)

discriminator = Sequential()

discriminator.add(Conv2D(64, kernel_size=(3, 3), activation='LeakyReLU',


input_shape=input_shape))

discriminator.add(MaxPooling2D(pool_size=(2, 2)))

discriminator.add(Conv2D(128, kernel_size=(3, 3), activation='LeakyReLU'))

discriminator.add(MaxPooling2D(pool_size=(2, 2)))

discriminator.add(Conv2D(256, kernel_size=(3, 3), activation='LeakyReLU'))

discriminator.add(MaxPooling2D(pool_size=(2, 2)))

discriminator.add(Flatten())

discriminator.add(Dense(256, activation='LeakyReLU'))

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

return discriminator

discriminator = Discriminator_Model()

discriminator.summary()

keras.utils.plot_model(discriminator, show_shapes=True)

……………………………………………………………………………………………………………………………………………………………
Member 4th - Training Loop and Model Evaluation

# DCGAN Model Training Step with Discriminator and Generator

class DCGAN(keras.Model):

# ...

# Definition of DCGAN class goes here

class DCGANMonitor(keras.callbacks.Callback):

# ...

# Definition of DCGANMonitor class goes here

# Training DCGAN on Image Dataset for 30 Epochs

epochs = 30

lr_g = 0.0003

lr_d = 0.0001

beta = 0.5

latent_dim = 300

dcgan = DCGAN(generator=generator, discriminator=discriminator, latent_dim=latent_dim)

dcgan.compile(g_optimizer=Adam(learning_rate=lr_g, beta_1=beta),
d_optimizer=Adam(learning_rate=lr_g, beta_1=beta), loss_fn=BinaryCrossentropy())

history = dcgan.fit(train_generator, epochs=epochs, callbacks=[DCGANMonitor()])

plt.figure(figsize=(40, 40))

# Generate and plot random images here

# Plot loss curves

def create_loss_figure(d_loss_values, g_loss_values):

# Function definition here

d_loss_values = history.history['d_loss']
g_loss_values = history.history['g_loss']

create_loss_figure(d_loss_values, g_loss_values)

…………………………………………………………………………………………………………………………………………………………

Member 1 – data handing and preprocessing

# Importing required libraries and tools

from tensorflow import keras

import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

import seaborn as sns

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Conv2DTranspose,


Reshape, BatchNormalization, Dropout, Input, ReLU, LeakyReLU

from keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.losses import BinaryCrossentropy

from PIL import Image

import warnings

warnings.filterwarnings('ignore')

# Loading and Preparing Anime Face Images Dataset using Keras Image Data Generator

img_width, img_height = 256, 256

batchsize = 32

train = keras.utils.image_dataset_from_directory(

directory='/kaggle/input/anime-faces/data',

batch_size=batchsize,

image_size=(img_width, img_height))

# Visualizing a Batch of Anime Face Images


data_iterator = train.as_numpy_iterator()

batch = data_iterator.next()

fig, ax = plt.subplots(ncols=4, figsize=(10, 10))

for idx, img in enumerate(batch[0][:4]):

ax[idx].imshow(img.astype(int))

ax[idx].title.set_text(batch[1][idx])

# Generating Augmented Batches of Anime Face Images using ImageDataGenerator

DIR = '/kaggle/input/anime-faces/data' # path

train_datagen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)

train_generator = train_datagen.flow_from_directory(

DIR,

target_size=(64, 64),

batch_size=batchsize,

class_mode=None)

You might also like