SlideShare a Scribd company logo
Rajiv Gandhi University of Knowledge Technologies, Basar
Department of Computer Science and Engineering
A BRIEF PRESENTATION
ON
TITLE OF THE PROJECT:
Generation of Deep Fake images using GAN and LSGAN
1 Generation of deep fake images using GAN and LSGAN
By
Gugulothu Divya [B171326]
MD Sanakowsar [B171741]
Eslavath Swathi [B171909]
Under the Guidance and Supervision of
Mr.K.RAVIKANTH
Assistant Proffessor,CSE,RGUKT Basar
ABSTRACT:
Generative Adversarial Networks have undoubtedly proven how
powerful the deep neural networks and they have created a new
milestone in the field of machine learning. GAN’s or Generative models
which are trained with the samples to generate the new data using the
data provided. In this project developed a generative adversarial network
model and Least Squares model and trained it’s sub models one for
generating fake images and another model for image classification. This
project use the work of Ian Good fellow. The main idea behind
developing this project is to provide a system where the users can learn
about GAN model and LSGAN model. The GAN and LSGAN model
was developed using python keras ,torch, torchvision.
Generation of deep fake images using GAN and LSGAN
STAGE 1:
• Generation of images from generator by learning from
discriminator.
• Discriminator discriminates the image whether it is real or fake.
STAGE 2:
• We are planning to implement a model which can detect
whether generated image is real or fake by using Convulational
neural networks techniques.
Generation of deep fake images using GAN and LSGAN
LITERATURE SURVEY:
Generation of deep fake images using GAN and LSGAN
SUMMARY OF SURVEY PAPERS:
1. A survey of face manipulation and fake detection:
Different ways of manipulation are there like entire face synthesis, Attribute manipulation ,
identity swap, expression swap.
2. A style based architecture of GAN:
Compared with traditional GAN the StyleGAN can produce more realistic image by adding
the styles. The new architecture leads to an automatically learned, unsupervised separation of
high-level attributes (e.g., pose and identity when trained on human faces) and stochastic variation
in the generated images (e.g., freckles, hair), and it enables intuitive, scale-specific control of the
synthesis.
Generation of deep fake images using GAN and LSGAN
SUMMARY OF SURVEY PAPERS:
3.Least Squares Generative Adversarial Networks:
Least Squares generative adversarial network (LSGANs) adopts the least squares loss
function for the discriminator. This will minimizing the Pearson divergence. First, LSGANs are
able to generate higher quality images than regular GANs. Second, LSGANs perform more stable
during the learning process.
2. Gradient Descent GAN optimization is locally stable:
Traditional GAN implemented using gradient descent optimizer. Presented a theoretical
analysis of the local asymptotic stability of GAN optimization under proper conditions. Further
showed that the recently proposed WGAN is not asymptotically stable under the same conditions,
but we introduced a gradient-based regularizer which stabilizes both traditional GANs and the
WGANs, and can improve convergence speed in practice.
Generation of deep fake images using GAN and LSGAN
Begin by downloading the dataset
Normalize the image De normalize the image
Created a data loader to load the
images in batches
Device Configuration
Discriminator Network
Move discriminator model to chosen
device
Generator Network
Move Generator to chosen device
Discriminator Training Generator Training
Training both discriminator and
generator model
Output:
Generated images
For plotting images
Work Flow of GAN MODEL:
Generation of deep fake images using GAN and LSGAN
Work Flow of LS GAN MODEL:
Generation of deep fake images using GAN and LSGAN
PROBLEM STATEMENT:
There are many improvements of GAN model. By which one can make
the image more realistic. This Project shows the difference between
generated images by Traditional GAN and Least Squares GAN. This
can happen just by changing the Loss function from Binary Cross
Entropy to Mean Square Error. This Project also shows the difference
between GAN and LSGAN which will solve the problems of GAN like
mode collapse, Image Quality , vanishing Gradient problems and
stability during learning process.
Generation of deep fake images using GAN and LSGAN
CALCULATIONS:
GAN MODEL:
Discriminator Formula:
max log D(x) + log(1 – D(G(z)))
Generator Formula:
min log(1 – D(G(z)))
Training of both discriminator and generator:
Generation of deep fake images using GAN and LSGAN
LSGAN MODEL:
Discriminator Formula:
Generator Formula:
Training of both discriminator and generator:
Generation of deep fake images using GAN and LSGAN
DISCIMINATOR MODEL
image_size = 784
hidden_size = 256
hidden_size1=128
import torch.nn as nn
D = nn.Sequential(
nn.Linear(image_size, hidden_size),
nn.LeakyReLU(0.2),
nn.Linear(hidden_size, hidden_size1),
nn.LeakyReLU(0.2),
nn.Linear(hidden_size1, hidden_size1),
nn.LeakyReLU(0.2),
nn.Linear(hidden_size1, 1),
nn.Sigmoid())
CODE SNIPPETS OF GAN:
GENERATOR MODEL
latent_size = 64
G = nn.Sequential(
nn.Linear(latent_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size1)
,
nn.ReLU(),
nn.Linear(hidden_size1, hidden_size1
),
nn.ReLU(),
nn.Linear(hidden_size1, image_size),
nn.Tanh())
TRAIN DISCRIMINATOR MODEL
def reset_grad():
d_optimizer.zero_grad()
g_optimizer.zero_grad()
def train_discriminator(images):
# Create the labels which are later used as in
put for the BCE loss
real_labels = torch.ones(batch_size, 1).to(dev
ice)
fake_labels = torch.zeros(batch_size, 1).to(de
vice)
# Loss for real images
outputs = D(images)
d_loss_real = criterion(outputs, real_labels)
real_score = outputs
# Loss for fake images
z = torch.randn(batch_size, latent_size).to(de
vice)
fake_images = G(z)
outputs = D(fake_images)
d_loss_fake = criterion(outputs, fake_labels)
fake_score = outputs
# Combine losses
d_loss = d_loss_real + d_loss_fake
# Reset gradients
reset_grad()
# Compute gradients
d_loss.backward()
# Adjust the parameters using backprop
d_optimizer.step()
return d_loss, real_score, fake_score
Generation of deep fake images using GAN and LSGAN
TRAIN GENERATOR:
def train_generator():
# Generate fake images and calculate loss
z = torch.randn(batch_size, latent_size).to(devi
ce)
fake_images = G(z)
labels = torch.ones(batch_size, 1).to(device)
g_loss = criterion(D(fake_images), labels)
# Backprop and optimize
reset_grad()
g_loss.backward()
g_optimizer.step()
return g_loss, fake_images
TRAIN BOTH GENERATOR AND DISCRIMINATOR
%%time
num_epochs = 100
total_step = len(data_loader)
d_losses, g_losses, real_scores, fake_scores = [], [], [],
[]
for epoch in range(num_epochs):
for i, (images, _) in enumerate(data_loader):
# Load a batch & transform to vectors
images = images.reshape(batch_size, -1).to(device)
# Train the discriminator and generator
d_loss, real_score, fake_score = train_discriminato
r(images)
g_loss, fake_images = train_generator()
# Inspect the losses
if (i+1) % 200 == 0:
d_losses.append(d_loss.item())
g_losses.append(g_loss.item())
real_scores.append(real_score.mean().item())
fake_scores.append(fake_score.mean().item())
print('Epoch [{}/{}], Step [{}/{}], d_loss: {:.
4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}'
.format(epoch, num_epochs, i+1, total_ste
p, d_loss.item(), g_loss.item(),
real_score.mean().item(), fake_sc
ore.mean().item()))
# Sample and save images
save_fake_images(epoch+1)
Generation of deep fake images using GAN and LSGAN
GAN MODEL OUTPUT
FIRST EPOCH: 50TH EPOCH 100TH EPOCH
Generation of deep fake images using GAN and LSGAN
CODE SNIPPETS OF LSGAN:
DISCRIMINATOR MODEL
# define the standalone discriminator model
def define_discriminator(in_shape=(28,28,1)):
# weight initialization
init = RandomNormal(stddev=0.02)
# define model
model = Sequential()
# downsample to 14x14
model.add(Conv2D(64, (4,4), strides=(2,2), padd
ing='same', kernel_initializer=init, input_shape=
in_shape))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha=0.2))
# downsample to 7x7
model.add(Conv2D(128, (4,4), strides=(2,2), pad
ding='same', kernel_initializer=init))
model.add(BatchNormalization())
model.add(LeakyReLU(alpha=0.2))
# classifier
model.add(Flatten())
model.add(Dense(1, activation='linear', kernel_
initializer=init))
# compile model with L2 loss
model.compile(loss='mse', optimizer=Adam(lr=0.0
002, beta_1=0.5))
return model
GENERATOR MODEL
# define the standalone generator model
def define_generator(latent_dim):
# weight initialization
init = RandomNormal(stddev=0.02)
# define model
model = Sequential()
# foundation for 7x7 image
n_nodes = 256 * 7 * 7
model.add(Dense(n_nodes, kernel_initializer=init,
input_dim=latent_dim))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Reshape((7, 7, 256)))
# upsample to 14x14
model.add(Conv2DTranspose(128, (4,4), strides=(2,
2), padding='same', kernel_initializer=init))
model.add(BatchNormalization())
model.add(Activation('relu'))
# upsample to 28x28
model.add(Conv2DTranspose(64, (4,4), strides=(2,2
), padding='same', kernel_initializer=init))
model.add(BatchNormalization())
model.add(Activation('relu'))
# output 28x28x1
model.add(Conv2D(1, (7,7), padding='same', kernel
_initializer=init))
model.add(Activation('tanh'))
return model
Generation of deep fake images using GAN and LSGAN
TRAIN BOTH GENERATOR AND GENERATOR
# train the generator and discriminator
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=20, n_batch=
128):
# calculate the number of batches per training epoch
bat_per_epo = int(dataset.shape[0] / n_batch)
# calculate the number of training iterations
n_steps = bat_per_epo * n_epochs
# calculate the size of half a batch of samples
half_batch = int(n_batch / 2)
# lists for storing loss, for plotting later
d1_hist, d2_hist, g_hist = list(), list(), list()
# manually enumerate epochs
for i in range(n_steps):
# prepare real and fake samples
X_real, y_real = generate_real_samples(dataset, half_batch)
X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator model
d_loss1 = d_model.train_on_batch(X_real, y_real)
d_loss2 = d_model.train_on_batch(X_fake, y_fake)
# update the generator via the discriminator's error
z_input = generate_latent_points(latent_dim, n_batch)
y_real2 = ones((n_batch, 1))
g_loss = gan_model.train_on_batch(z_input, y_real2)
# summarize loss on this batch
print('>%d, d1=%.3f, d2=%.3f g=%.3f' % (i+1, d_loss1, d_loss2, g_loss))
# record history
d1_hist.append(d_loss1)
d2_hist.append(d_loss2)
g_hist.append(g_loss)
# evaluate the model performance every 'epoch'
if (i+1) % (bat_per_epo * 1) == 0:
summarize_performance(i, g_model, latent_dim)
# create line plot of training history
plot_history(d1_hist, d2_hist, g_hist)
Generation of deep fake images using GAN and LSGAN
LSGAN MODEL OUTPUT:
FIRST EPOCH 10TH EPOCH 20TH EPOCH
Generation of deep fake images using GAN and LSGAN
CONCEPTS / MODULES:
1.Keras:
Keras is used in this project because it is powerful open source library, Which used for developing
and evaluating deep learning models. Written in python programming language . It is capable of
running above the tensor-flow and other numerical computation libraries. Keras allows to train and
develop neural network models with fewer lines of code.
2.PYTORCH:
PYTORCH is a defined as an open source machine learning library for python .It is used for
application such as natural language processing .It is initially developed by face book artificial
intelligence research group. Pytorch redesigns and implements torch in python while sharing the
same course c libraries for the backend code.
3.TORCH VISION:
Torch vision library is a part of pytorch. Pytorch is an open source machine learning frame
work. The torch vision package consist of popular datasets , model architectures and common
image transformations for computer editions
Generation of deep fake images using GAN and LSGAN
CONCLUSION AND FUTURE SCOPE
Conclusion:
This project had successfully demonstrated Generative Adversarial Networks and Least Squares GAN.
This project had also demonstrated to test the trained GAN model which classifies whether the image is
real or fake. The whole system with all the tools was less than 50 MB. This model can be used for other
image dataset training and classification.
This project also demonstrated Least Squares GAN which also gives more realistic image than the
traditional GAN by using Least Squares error loss function.
Future Work:
Since the scope of this project is limited to one trained model that which is trained with the single
dataset. This can be improvised by adding more number of hidden layers. That can also improved using
different loss function and optimizer. In future one can make application to identify whether the image is
real or fake. One can also design an application to identify the accuracy whether the written digits
really matching with generated or not.
Generation of deep fake images using GAN and LSGAN
20
REFERENCES:
•https://www.researchgate.net/publication/343691176_Applicatio
n_to_generate_fake_images_and_image_classification_using_
Generative_Adversarial_Networks
•https://www.youtube.com/watch?v=6RTJbbAD1uw&feature=sh
are&utm_source=EJGixIgBCJiu2KjB4oSJEQ
•https://in.video.search.yahoo.com/search/video?fr=mcafee&ei=
UTF-
8&p=style+gan&type=E211IN826G0#id=1&vid=438d41a6077d5
e6f8ea0dbda82f93943&action=click
Generation of deep fake images using GAN and LSGAN
21
Thank you
Generation of deep fake images using GAN and LSGAN

More Related Content

PDF
Project_Final_Review.pdf
PPTX
Module4_GAN.pptxgdgdijehejejjejejejhehjdd
PDF
Unpaired Image Translations Using GANs: A Review
PDF
Creating Objects for Metaverse using GANs and Autoencoders
PDF
Decomposing image generation into layout priction and conditional synthesis
PPTX
Let's paint a Picasso - A Look at Generative Adversarial Networks (GAN) and i...
PDF
Learning with Relative Attributes
PDF
Performance analysis of transformation and bogdonov chaotic substitution base...
Project_Final_Review.pdf
Module4_GAN.pptxgdgdijehejejjejejejhehjdd
Unpaired Image Translations Using GANs: A Review
Creating Objects for Metaverse using GANs and Autoencoders
Decomposing image generation into layout priction and conditional synthesis
Let's paint a Picasso - A Look at Generative Adversarial Networks (GAN) and i...
Learning with Relative Attributes
Performance analysis of transformation and bogdonov chaotic substitution base...

Similar to Generation of Deepfake images using GAN and Least squares GAN.ppt (20)

DOC
Implementing Neural Style Transfer
PDF
Image De-Noising Using Deep Neural Network
PDF
Image De-Noising Using Deep Neural Network
PDF
Image Classification using Deep Learning
PDF
IRJET- Generating 3D Models Using 3D Generative Adversarial Network
PDF
A Novel Approach to Image Denoising and Image in Painting
PDF
Deep Generative Models - Kevin McGuinness - UPC Barcelona 2018
PDF
An Intelligent approach to Pic to Cartoon Conversion using White-box-cartooni...
PDF
AIML4 CNN lab256 1hr (111-1).pdf
PPTX
Introduction to Deep Learning and Tensorflow
PDF
ADVANCED SINGLE IMAGE RESOLUTION UPSURGING USING A GENERATIVE ADVERSARIAL NET...
PDF
Advanced Single Image Resolution Upsurging Using A Generative Adversarial Net...
PPTX
Generative Adversarial Networks and Their Applications in Medical Imaging
PDF
IMAGE DE-NOISING USING DEEP NEURAL NETWORK
PDF
Image De-Noising Using Deep Neural Network
PDF
IRJET- Design and Implementation of ATM Security System using Vibration Senso...
PDF
IRJET- Real Time Implementation of Bi-Histogram Equalization Method on Androi...
PDF
IMAGE GENERATION FROM CAPTION
PDF
Image Generation from Caption
PDF
[AI07] Revolutionizing Image Processing with Cognitive Toolkit
Implementing Neural Style Transfer
Image De-Noising Using Deep Neural Network
Image De-Noising Using Deep Neural Network
Image Classification using Deep Learning
IRJET- Generating 3D Models Using 3D Generative Adversarial Network
A Novel Approach to Image Denoising and Image in Painting
Deep Generative Models - Kevin McGuinness - UPC Barcelona 2018
An Intelligent approach to Pic to Cartoon Conversion using White-box-cartooni...
AIML4 CNN lab256 1hr (111-1).pdf
Introduction to Deep Learning and Tensorflow
ADVANCED SINGLE IMAGE RESOLUTION UPSURGING USING A GENERATIVE ADVERSARIAL NET...
Advanced Single Image Resolution Upsurging Using A Generative Adversarial Net...
Generative Adversarial Networks and Their Applications in Medical Imaging
IMAGE DE-NOISING USING DEEP NEURAL NETWORK
Image De-Noising Using Deep Neural Network
IRJET- Design and Implementation of ATM Security System using Vibration Senso...
IRJET- Real Time Implementation of Bi-Histogram Equalization Method on Androi...
IMAGE GENERATION FROM CAPTION
Image Generation from Caption
[AI07] Revolutionizing Image Processing with Cognitive Toolkit
Ad

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
web development for engineering and engineering
PPTX
Sustainable Sites - Green Building Construction
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CH1 Production IntroductoryConcepts.pptx
OOP with Java - Java Introduction (Basics)
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
web development for engineering and engineering
Sustainable Sites - Green Building Construction
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
UNIT 4 Total Quality Management .pptx
additive manufacturing of ss316l using mig welding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

Generation of Deepfake images using GAN and Least squares GAN.ppt

  • 1. Rajiv Gandhi University of Knowledge Technologies, Basar Department of Computer Science and Engineering A BRIEF PRESENTATION ON TITLE OF THE PROJECT: Generation of Deep Fake images using GAN and LSGAN 1 Generation of deep fake images using GAN and LSGAN By Gugulothu Divya [B171326] MD Sanakowsar [B171741] Eslavath Swathi [B171909] Under the Guidance and Supervision of Mr.K.RAVIKANTH Assistant Proffessor,CSE,RGUKT Basar
  • 2. ABSTRACT: Generative Adversarial Networks have undoubtedly proven how powerful the deep neural networks and they have created a new milestone in the field of machine learning. GAN’s or Generative models which are trained with the samples to generate the new data using the data provided. In this project developed a generative adversarial network model and Least Squares model and trained it’s sub models one for generating fake images and another model for image classification. This project use the work of Ian Good fellow. The main idea behind developing this project is to provide a system where the users can learn about GAN model and LSGAN model. The GAN and LSGAN model was developed using python keras ,torch, torchvision. Generation of deep fake images using GAN and LSGAN
  • 3. STAGE 1: • Generation of images from generator by learning from discriminator. • Discriminator discriminates the image whether it is real or fake. STAGE 2: • We are planning to implement a model which can detect whether generated image is real or fake by using Convulational neural networks techniques. Generation of deep fake images using GAN and LSGAN
  • 4. LITERATURE SURVEY: Generation of deep fake images using GAN and LSGAN
  • 5. SUMMARY OF SURVEY PAPERS: 1. A survey of face manipulation and fake detection: Different ways of manipulation are there like entire face synthesis, Attribute manipulation , identity swap, expression swap. 2. A style based architecture of GAN: Compared with traditional GAN the StyleGAN can produce more realistic image by adding the styles. The new architecture leads to an automatically learned, unsupervised separation of high-level attributes (e.g., pose and identity when trained on human faces) and stochastic variation in the generated images (e.g., freckles, hair), and it enables intuitive, scale-specific control of the synthesis. Generation of deep fake images using GAN and LSGAN
  • 6. SUMMARY OF SURVEY PAPERS: 3.Least Squares Generative Adversarial Networks: Least Squares generative adversarial network (LSGANs) adopts the least squares loss function for the discriminator. This will minimizing the Pearson divergence. First, LSGANs are able to generate higher quality images than regular GANs. Second, LSGANs perform more stable during the learning process. 2. Gradient Descent GAN optimization is locally stable: Traditional GAN implemented using gradient descent optimizer. Presented a theoretical analysis of the local asymptotic stability of GAN optimization under proper conditions. Further showed that the recently proposed WGAN is not asymptotically stable under the same conditions, but we introduced a gradient-based regularizer which stabilizes both traditional GANs and the WGANs, and can improve convergence speed in practice. Generation of deep fake images using GAN and LSGAN
  • 7. Begin by downloading the dataset Normalize the image De normalize the image Created a data loader to load the images in batches Device Configuration Discriminator Network Move discriminator model to chosen device Generator Network Move Generator to chosen device Discriminator Training Generator Training Training both discriminator and generator model Output: Generated images For plotting images Work Flow of GAN MODEL: Generation of deep fake images using GAN and LSGAN
  • 8. Work Flow of LS GAN MODEL: Generation of deep fake images using GAN and LSGAN
  • 9. PROBLEM STATEMENT: There are many improvements of GAN model. By which one can make the image more realistic. This Project shows the difference between generated images by Traditional GAN and Least Squares GAN. This can happen just by changing the Loss function from Binary Cross Entropy to Mean Square Error. This Project also shows the difference between GAN and LSGAN which will solve the problems of GAN like mode collapse, Image Quality , vanishing Gradient problems and stability during learning process. Generation of deep fake images using GAN and LSGAN
  • 10. CALCULATIONS: GAN MODEL: Discriminator Formula: max log D(x) + log(1 – D(G(z))) Generator Formula: min log(1 – D(G(z))) Training of both discriminator and generator: Generation of deep fake images using GAN and LSGAN
  • 11. LSGAN MODEL: Discriminator Formula: Generator Formula: Training of both discriminator and generator: Generation of deep fake images using GAN and LSGAN
  • 12. DISCIMINATOR MODEL image_size = 784 hidden_size = 256 hidden_size1=128 import torch.nn as nn D = nn.Sequential( nn.Linear(image_size, hidden_size), nn.LeakyReLU(0.2), nn.Linear(hidden_size, hidden_size1), nn.LeakyReLU(0.2), nn.Linear(hidden_size1, hidden_size1), nn.LeakyReLU(0.2), nn.Linear(hidden_size1, 1), nn.Sigmoid()) CODE SNIPPETS OF GAN: GENERATOR MODEL latent_size = 64 G = nn.Sequential( nn.Linear(latent_size, hidden_size), nn.ReLU(), nn.Linear(hidden_size, hidden_size1) , nn.ReLU(), nn.Linear(hidden_size1, hidden_size1 ), nn.ReLU(), nn.Linear(hidden_size1, image_size), nn.Tanh()) TRAIN DISCRIMINATOR MODEL def reset_grad(): d_optimizer.zero_grad() g_optimizer.zero_grad() def train_discriminator(images): # Create the labels which are later used as in put for the BCE loss real_labels = torch.ones(batch_size, 1).to(dev ice) fake_labels = torch.zeros(batch_size, 1).to(de vice) # Loss for real images outputs = D(images) d_loss_real = criterion(outputs, real_labels) real_score = outputs # Loss for fake images z = torch.randn(batch_size, latent_size).to(de vice) fake_images = G(z) outputs = D(fake_images) d_loss_fake = criterion(outputs, fake_labels) fake_score = outputs # Combine losses d_loss = d_loss_real + d_loss_fake # Reset gradients reset_grad() # Compute gradients d_loss.backward() # Adjust the parameters using backprop d_optimizer.step() return d_loss, real_score, fake_score Generation of deep fake images using GAN and LSGAN
  • 13. TRAIN GENERATOR: def train_generator(): # Generate fake images and calculate loss z = torch.randn(batch_size, latent_size).to(devi ce) fake_images = G(z) labels = torch.ones(batch_size, 1).to(device) g_loss = criterion(D(fake_images), labels) # Backprop and optimize reset_grad() g_loss.backward() g_optimizer.step() return g_loss, fake_images TRAIN BOTH GENERATOR AND DISCRIMINATOR %%time num_epochs = 100 total_step = len(data_loader) d_losses, g_losses, real_scores, fake_scores = [], [], [], [] for epoch in range(num_epochs): for i, (images, _) in enumerate(data_loader): # Load a batch & transform to vectors images = images.reshape(batch_size, -1).to(device) # Train the discriminator and generator d_loss, real_score, fake_score = train_discriminato r(images) g_loss, fake_images = train_generator() # Inspect the losses if (i+1) % 200 == 0: d_losses.append(d_loss.item()) g_losses.append(g_loss.item()) real_scores.append(real_score.mean().item()) fake_scores.append(fake_score.mean().item()) print('Epoch [{}/{}], Step [{}/{}], d_loss: {:. 4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}' .format(epoch, num_epochs, i+1, total_ste p, d_loss.item(), g_loss.item(), real_score.mean().item(), fake_sc ore.mean().item())) # Sample and save images save_fake_images(epoch+1) Generation of deep fake images using GAN and LSGAN
  • 14. GAN MODEL OUTPUT FIRST EPOCH: 50TH EPOCH 100TH EPOCH Generation of deep fake images using GAN and LSGAN
  • 15. CODE SNIPPETS OF LSGAN: DISCRIMINATOR MODEL # define the standalone discriminator model def define_discriminator(in_shape=(28,28,1)): # weight initialization init = RandomNormal(stddev=0.02) # define model model = Sequential() # downsample to 14x14 model.add(Conv2D(64, (4,4), strides=(2,2), padd ing='same', kernel_initializer=init, input_shape= in_shape)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.2)) # downsample to 7x7 model.add(Conv2D(128, (4,4), strides=(2,2), pad ding='same', kernel_initializer=init)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.2)) # classifier model.add(Flatten()) model.add(Dense(1, activation='linear', kernel_ initializer=init)) # compile model with L2 loss model.compile(loss='mse', optimizer=Adam(lr=0.0 002, beta_1=0.5)) return model GENERATOR MODEL # define the standalone generator model def define_generator(latent_dim): # weight initialization init = RandomNormal(stddev=0.02) # define model model = Sequential() # foundation for 7x7 image n_nodes = 256 * 7 * 7 model.add(Dense(n_nodes, kernel_initializer=init, input_dim=latent_dim)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Reshape((7, 7, 256))) # upsample to 14x14 model.add(Conv2DTranspose(128, (4,4), strides=(2, 2), padding='same', kernel_initializer=init)) model.add(BatchNormalization()) model.add(Activation('relu')) # upsample to 28x28 model.add(Conv2DTranspose(64, (4,4), strides=(2,2 ), padding='same', kernel_initializer=init)) model.add(BatchNormalization()) model.add(Activation('relu')) # output 28x28x1 model.add(Conv2D(1, (7,7), padding='same', kernel _initializer=init)) model.add(Activation('tanh')) return model Generation of deep fake images using GAN and LSGAN
  • 16. TRAIN BOTH GENERATOR AND GENERATOR # train the generator and discriminator def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=20, n_batch= 128): # calculate the number of batches per training epoch bat_per_epo = int(dataset.shape[0] / n_batch) # calculate the number of training iterations n_steps = bat_per_epo * n_epochs # calculate the size of half a batch of samples half_batch = int(n_batch / 2) # lists for storing loss, for plotting later d1_hist, d2_hist, g_hist = list(), list(), list() # manually enumerate epochs for i in range(n_steps): # prepare real and fake samples X_real, y_real = generate_real_samples(dataset, half_batch) X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch) # update discriminator model d_loss1 = d_model.train_on_batch(X_real, y_real) d_loss2 = d_model.train_on_batch(X_fake, y_fake) # update the generator via the discriminator's error z_input = generate_latent_points(latent_dim, n_batch) y_real2 = ones((n_batch, 1)) g_loss = gan_model.train_on_batch(z_input, y_real2) # summarize loss on this batch print('>%d, d1=%.3f, d2=%.3f g=%.3f' % (i+1, d_loss1, d_loss2, g_loss)) # record history d1_hist.append(d_loss1) d2_hist.append(d_loss2) g_hist.append(g_loss) # evaluate the model performance every 'epoch' if (i+1) % (bat_per_epo * 1) == 0: summarize_performance(i, g_model, latent_dim) # create line plot of training history plot_history(d1_hist, d2_hist, g_hist) Generation of deep fake images using GAN and LSGAN
  • 17. LSGAN MODEL OUTPUT: FIRST EPOCH 10TH EPOCH 20TH EPOCH Generation of deep fake images using GAN and LSGAN
  • 18. CONCEPTS / MODULES: 1.Keras: Keras is used in this project because it is powerful open source library, Which used for developing and evaluating deep learning models. Written in python programming language . It is capable of running above the tensor-flow and other numerical computation libraries. Keras allows to train and develop neural network models with fewer lines of code. 2.PYTORCH: PYTORCH is a defined as an open source machine learning library for python .It is used for application such as natural language processing .It is initially developed by face book artificial intelligence research group. Pytorch redesigns and implements torch in python while sharing the same course c libraries for the backend code. 3.TORCH VISION: Torch vision library is a part of pytorch. Pytorch is an open source machine learning frame work. The torch vision package consist of popular datasets , model architectures and common image transformations for computer editions Generation of deep fake images using GAN and LSGAN
  • 19. CONCLUSION AND FUTURE SCOPE Conclusion: This project had successfully demonstrated Generative Adversarial Networks and Least Squares GAN. This project had also demonstrated to test the trained GAN model which classifies whether the image is real or fake. The whole system with all the tools was less than 50 MB. This model can be used for other image dataset training and classification. This project also demonstrated Least Squares GAN which also gives more realistic image than the traditional GAN by using Least Squares error loss function. Future Work: Since the scope of this project is limited to one trained model that which is trained with the single dataset. This can be improvised by adding more number of hidden layers. That can also improved using different loss function and optimizer. In future one can make application to identify whether the image is real or fake. One can also design an application to identify the accuracy whether the written digits really matching with generated or not. Generation of deep fake images using GAN and LSGAN
  • 21. 21 Thank you Generation of deep fake images using GAN and LSGAN