GAN(Advance DL concept)

GAN(Advance DL concept)

In this blog I will explain GAN(one of the advance DL/ML concept) & it’s code from scratch with an example.

In single line, Core of GAN is “Train generator with the output of discriminator”

Lets move to very first question which might pop up in your mind

What is GAN??

G = GENERATIVE

A = ADVERSARIAL

N = NETWORKS

GANs was proposed by Ian Goodfellow, by applying game theory, he founded a way in which machine can learn by teaching itself.

In very simple words if you wants to be better at Table tennis then what you should do? Probably compete with opponent better than you & finally learn what you did wrong & what he/she did right i.e. learn from your mistakes.

In similar way in GAN, untrained model learns from trained model.

Let’s start with example itself & then as an when things are require I will explain them.

No alt text provided for this image

Problem Statement:-

Our target is to generate Image using DL model. Meaning of generating image from model is, we will be providing a vector(array) for random numbers & DL model will convert it to that vector into an image?

Let’s see what to I mean by generating images:

No alt text provided for this image

What is “Generator ” & “Discriminator” ??

We need to create one model which will accept an vector & create image out of it. But how do we know whether that image looks like real image or not???

For this we need to have 2nd ?model who checks/investigate whether image generated by first model looks real or not.?But there exist one requirement which is to be fulfilled for successfully establishing the architecture which we have design. 2nd model should be able to classify which image is looks real & which image is looks fake.

So now lets label these 2 models; 1st model which will generate image from vector is known as “generator” & 2nd model which will investigate the image produced by 1st one is known as “discriminator”.

So now let’s begin with the code. First we need to create discriminator model as it is the primary requirement for our architecture

No alt text provided for this image

Output of this model will be between 0 & 1?i.e. more nearer towards 0 means image looks fake & more nearer towards 1 means real. Thus we used "sigmoid" as activation function. Now we need to provide dataset to discriminator model which contains real as well as fake images. We can get real images from mnist dataset, but from where we can get fake images ???

The answer is generator model, yes this is the “paradox”, where model which is suppose to direct other, is learning from for it.

Now let’s see code for generator

No alt text provided for this image

Generator model is taking vector of certain size as input and providing vector of 784 as output i.e. image of?size 28 * 28. But ?in vector of 784 all the values will be between 1 & 0 , hence we used "sigmoid" as activation function. Thus it will produce an array of 784 elements, where value of each element of array ranges between (0,1)?

Dataset for discriminator:-

Our both model are ready. Now we need to provide dataset for training models. So very next question is how our dataset will look like??

Discriminator dataset will have both, real images(mnist dataset) & fake images(by generator model). So all the real images would be tagged with “1” & all fake images would be?tagged “0”. Hence after training discriminator would be able to differentiate between real & fake images.

Now before understanding dataset of generator it’s important to know how generator & discriminator will work?? Because it will give answer of “what” & “why” about generator dataset!!!

Generator & Discriminator would work together as one model known as “GAN”. So our target is to train GAN model. We will see that workflow below.

Workflow:

So we would training discriminator first, thus it will find out correct weight and bias which can differentiate between real & fake image. Then we will make discriminator non trainable i.e. we will freeze weight & bias which it has find out.

Now question arises why??

See when next time we provide any image, say it’s fake, so we don’t want discriminator to learn that image(learning means : adjust weight & bias according to image), rather we want, discriminator to uses it pre-found weight & bias and predict whether image is real or fake. Hence it’s important to make discriminator non trainable.

Next step is to train GAN model(Generator model + Discriminator Model). So it’s dataset would be random vector of particular size. Generator model would take that vector & finally produce a vector of size 784 or 28 * 28 image; which would be further provided to discriminator model(whose weights are freeze ).

Now here is very interesting concept, all the image provided to generator would be label 1 i.e. we will purposely label that all image are real.

Now again question arises why??

Simple crisp answer is it will create error/loss. Let’s take a case where generator model takes vector & create fake image & it’s is label 1 i.e. real image. So now when that image would be provided to discriminator model, it(discriminator model) will produce output 0 i.e. fake image because discriminator is non trainable & has pre found weight & bias which can easily predict whether image is real or fake.

It will finally create a huge error/loss as final output 1(real image) but output of our architecture is 0(fake image), thus with the concept of back propagation entire architecture will try to reduce error. For reducing error we will adjust weight & bias. But we can only adjust weights of generator model as weights of discriminator model are already freeze. This is the reason why we made discriminator non trainable.

Thus at end of training,?generator will acquire such weights which will help to generate vector of size 784(28 * 28 image), which will look real. I request to read above concept again for better understanding & try to logically conclude it in your mind. In real world we repeat above concept of training discriminator & generator, multiple times for certain number of image say 50. We achieve this with help of loop.

Let’s see how one iteration of loop looks like:-

  • Make discriminator trainable.
  • Get 50 real images from mnist & 50 fake images from generator model.
  • Label real images with “1” & fake images with “0”.
  • Train discriminator with those images.
  • Make discriminator non trainable.
  • Now generate random vector & label each of them with “1”.
  • Provide it to generator .
  • Give Image generated by generator to discriminator.
  • If loss is more then with help of back propagation re adjust the weights of generator as weight of discriminator are freeze.

For better understating see the below image:-

No alt text provided for this image
No alt text provided for this image


Final Solution:-

Now I would like to show the real solution of out problem statement. Basically you already know it, it’s “GENERATOR MODEL”. As at end generator model will acquire/find such weights which will help to generate vector of size 784(28 * 28 image), which will look real, which is eventually our problem statement!!!

But for creating generator model GAN model or rather GAN concept is important or I can say it’s required.

Real Life use case:-

Security:-

Artificial intelligence has proved to be a boon to many industries but it is also surrounded by the problem of Cyber threats. GANs are proved to be a great help to handle the adversarial attacks. The adversarial attacks use a variety of techniques to fool deep learning architectures. By creating fake examples and training the model to identify them we counter these attacks.

Generating Data using GANs:-

Data is the most important key for any deep learning algorithm. In general, more is the data, better is the performance of any deep learning algorithm. But in many cases such as health diagnostics, the amount of data is restricted, in such cases, there is a need to generate good quality data. For which GANs are being used.

Privacy Perseverance:-

There are many cases when our data needs to be kept confidential. This is especially useful in defence and military applications. We have many data encryption schemes but each has its own limitations, in such a case GANs can be useful. Recently, in 2016, Google opened a new research path on using GAN competitive framework for encryption problem, where two networks had to compete in creating the code and cracking it.

No alt text provided for this image

Thankyou for Reading !!!

Bhavesh Sonewale

3x RedHat Certified | ISVG | ISIM | Ansible | Openshift | Podman | Docker | Kubernetes | RHEL-8 | AWS | ML | Python

3 年

Ayush, well explained bro. Really a very good and detailed information about GAN. ????

Mashfooq Ahmed

Senior Associate, Infrastructure Specialist

3 年

Congratulations Ayush

Sarfraj Alam

Senior SDET at QASource | Data Analyst Enthusiast

3 年

Informative + useful

Vimal Daga

World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC

3 年

Good work

Shrishti Kapoor

Accenture | 4 x RedHat Certified | Aviatrix Certified Engineer | AI-900 | Technical Trainer | IIEC-DOT Volunteer | Technical Research Writer Enthusiast..!! ??DevOps ??Cloud-AWS, Azure ??ML/AI/DL

3 年

Great Work ???

要查看或添加评论,请登录

Ayush Ganatra的更多文章

  • Privilege Escalation: Lets know the Hidden Danger.

    Privilege Escalation: Lets know the Hidden Danger.

    Introduction: Containers have revolutionized the way we build, ship, and run applications. They provide a lightweight…

  • Authentication ~ A custom Usecase & a different approch

    Authentication ~ A custom Usecase & a different approch

    hase 1 :The Usecase Hey folks, Recently I came across a very unique & interesting AWS architectural usecase & trust me…

    4 条评论
  • Dynamic Rendering ~ A BOON!!!

    Dynamic Rendering ~ A BOON!!!

    The SEO Score: - SEO has potential to make a major impact on company’s most important goals, like sales & leads. Let…

    4 条评论
  • OpenSCAP ~ Let me check

    OpenSCAP ~ Let me check

    “Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young.

    4 条评论
  • What’s inside the Pod??

    What’s inside the Pod??

    Container has brought the revolutions in technical area. Seriously one of the most stunning yet simplest technology…

    12 条评论
  • Kafka Message Queue in a Nutshell

    Kafka Message Queue in a Nutshell

    In this blog we will see: What is Kafka ? Kafka as Message Queue Installation Demo(with cli & python script) Industry…

    2 条评论
  • Image segmentation based on K-means algorithm

    Image segmentation based on K-means algorithm

    What is K-Means ?? Kmeans algorithm is an iterative algorithm that tries to partition the dataset into Kpre-defined…

  • OSPF Protocol implementing Dijkstra’s Algorithm

    OSPF Protocol implementing Dijkstra’s Algorithm

    Router works on third layer of our OSI model. OSPF (Open Shortest Path First) is a routing protocol.

    2 条评论
  • USE-CASE OF JavaScript

    USE-CASE OF JavaScript

    Walmart Strives to be an Online Retail Leader with Node.js Walmart needed to improve its business.

    6 条评论
  • Confusion matrix in Cyber crime

    Confusion matrix in Cyber crime

    Task Description ?? ?? Cyber crime cases where they talk about confusion matrix or its two types of error. WHAT IS…

    4 条评论

社区洞察

其他会员也浏览了