GAN(Advance DL concept)
Ayush Ganatra
DevOps Engineer @ The Good Glamm Group | AWS Solutions Architect Professional | CKS | CKA | Azure Administrator
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.
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:
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
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
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:-
For better understating see the below 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.
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. ????
Senior Associate, Infrastructure Specialist
3 年Congratulations Ayush
Senior SDET at QASource | Data Analyst Enthusiast
3 年Informative + useful
World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC
3 年Good work
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 ???