使用 Gan 网络生成名人照片

Celebrity Face Generation using GANs 使用 Gan 网络生成名人照片 Generative adversarial networks (GANs) are one of the hottest topics in deep learning. (GANs) are a class of artificial algorithms used in unsupervised learning algorithm, implemented by a system of two neural networksGeneratorDiscriminatorBoth networks are contesting with each other in the Zero-Sum Game Framework. Generative Adversarial Networks(GANs) are a set of models that basically learn to create synthetic data that is similar to input data it’s given.The discriminator has the task of determining whether a given image looks natural (i.e, is an image from the dataset) or looks like it has been artificially created. The task of the generator is to create natural looking images that are similar to the original data distribution, images that look natural enough to fool the discriminator network. Firstly a random noise is given to the Generator, using this it creates the Fake images and then these Fake images are along with origi

A person who loves writing, loves novels, and loves life.Seeking objective truth, hoping for world peace, and wishing for a world without wars.
Celebrity Face Generation using GANs 使用 Gan 网络生成名人照片

Generative adversarial networks (GANs) are one of the hottest topics in deep learning. (GANs) are a class of artificial algorithms used in unsupervised learning algorithm, implemented by a system of two neural networks

  1. Generator

  2. Discriminator

Both networks are contesting with each other in the Zero-Sum Game Framework. Generative Adversarial Networks(GANs) are a set of models that basically learn to create synthetic data that is similar to input data it’s given.

The discriminator has the task of determining whether a given image looks natural (i.e, is an image from the dataset) or looks like it has been artificially created. The task of the generator is to create natural looking images that are similar to the original data distribution, images that look natural enough to fool the discriminator network. Firstly a random noise is given to the Generator, using this it creates the Fake images and then these Fake images are along with original images sent to the Discriminator.

The discriminative model has the task of determining whether a given image looks natural (an image from the dataset) or looks like it has been artificially created. This is basically a binary classifier that will take the form of a normal convolutional neural network (CNN). The task of the generator is to create natural looking images that are similar to the original data distribution.

The generator is trying to fool the discriminator while the discriminator is trying to not get fooled by the generator. As the models train through alternating optimization, both methods are improved until a point where the “Fake images are indistinguishable from the dataset images”.

Mathematical Equation of Generative Adversarial Network:

生成式对抗网络(GANs)是深度学习中最热门的话题之一。 生成式对抗网络是一类用于无监督学习算法的人工算法,由两个神经网络系统实现

生成器

鉴别器

两个神经网络都在零和游戏框架中相互竞争。 生成式对抗网络(GANs)是一组模型,他们基本上学习创建与其给出的输入数据类似的合成数据。

鉴别器的任务是确定给定图像是否看起来自然(即,是来自数据集的图像)还是看起来它是人工创建的。 生成器的任务是创建与原始数据分布类似的自然外观图像,这些图像看起来足够自然以欺骗鉴别器网络。 首先给发生器一个随机噪声,使用它产生假图像,然后这些假图像与原始图像一起发送到鉴别器。

鉴别模型的任务是确定给定图像看起来是自然的(来自数据集的图像)还是人工创建的。 这基本上是一个二元分类器,它采用普通卷积神经网络(CNN)的形式。 生成器的任务是创建与原始数据分布类似的自然外观图像。

生成器试图蒙骗鉴别器,而鉴别器试图不被生成器蒙骗。当模型通过交替优化训练时,两种方法都被改进到“假图像与数据集图像无法区分”的点。

生成对抗式网络的数学方程


We can consider this equation composed of two parts, first part is the data sampled from the original data distribution and second part is the data sampled from the data distribution of noise.

First Part-

Discriminator always wants to maximize its probability of classifying an image correctly as real or fake. Here, the images are sampled from the original data distribution, which is the real data itself. we know that D(x) shows the probability that the image is real so Discriminator always wants to maximize D(x) , so log(D(x)) should be maximized and First part has to be maximized.

Second Part-

‘z’ is the random noise sample and G(z) is the generated image using a noise sample. The explanation for this term is quite similar. Generator always wants maximize the probability that the discriminator getting fooled by the generated images. Which means, the generator should want to maximize D(G(z)), so it should minimize 1- D(G(z)) and hence log(1- D(G(z))).

Celebrity Image Generation using GANs

我们可以认为这个方程由两部分组成,第一部分是从原始数据分布中采样的数据,第二部分是从噪声数据分布中采样的数据。

第一部分-

鉴别者总是希望最大化其将图像正确分类为真实或假的概率。 这里的图像是从原始数据分布中采样的,原始数据分布是真实数据本身。 D(x)是显示图像是真实的概率,所以鉴别器总是想要最大化D(x),因此log(D(x))应该最大化并且第一部分必须最大化。

第二部分-

'z'是随机噪声样本,G(z)是使用噪声样本生成的图像。 这个术语的解释和之前很相似。 生成器总是希望最大化鉴别器被生成的图像蒙骗的概率。 这意味着,生成器想要最大化D(G(z)),因此它应该最小化1-D(G(z))并因此最小化(1-D(G(z)))。

使用GANs生成名人图像

Celebrity Image Dataset:

CelebA dataset is the collection of over 200,000 celebrity faces with annotations. Since in this blog, I am just going to generate the faces so I am not taking annotations into consideration.

1).Getting the Data:-

I have created the helper.py file through which you can download the CelebA dataset images. While running this code snippet, it will download the CelebA dataset .(Source code link is given below).

2).Preprocessing the images:-

Since I am working only on faces so I have resized it down to 28*28 in order to get the good results. I have cropped the portion of image which not includes the image portion.

Since Generative Adversarial Network are very hard to train.(you can checkout this link in order to know why training of Generative Adversarial Network is so hard?).

In order to get the accurate results we should have a good GPU(4GB or above than this), by running this code snippet you can find whether tensorflow is installed with GPU or not.

3).Model Inputs and Network Architecture-

I am taking image width, image height ,image channel and noise parameter as model inputs which is further taken by the generator for generating the fake images.

Architecture of Generator :-

名人图片数据集:

CelebA数据集是超过200,000个带注释的名人面部图像的集合。 因为在这个博客中,我只是想生成面孔所以我没有考虑注释。

1)获取数据: 

我创建了helper.py文件,你可以通过该文件下载CelebA数据集图像。 在运行此代码片段时,它将下载CelebA数据集。(源代码链接如下)。

2)预处理图像:

由于我仅在面部图像上工作,为获得良好的效果所以我将其调整到28 * 28。 我裁剪了图像中不包含图像部分的部分。


由于生成式对抗网络很难训练(你可以查看此链接,以了解为什么生成式对抗网络的训练如此困难?)


为了获得准确的结果,我们应该拥有一个良好的GPU(4GB或更高版本),通过运行此代码片段,你可以了解是否使用自己的GPU安装了tensorflow。

The generator architecture has first dense layer and fully connected layer after that deconvolutional layer (every layer contains batch_normalization, leaky relu and dropout layer except output layer). generator takes a random noise vector z after that it is reshaped to the 4D shape and pass it to the series of upsampling layers. each upsampling layer reprsents transpose convolution operation i.e Deconvolution operation.

All transpose convolutions with depths reducing from 1024 all the way down to 3 —which represents an RGB color image. The final layer outputs a 28x28x3 tensor through the Hyperbolic Tangent (tanh) function.

Architecture of Discriminator:-

The work of Discriminator is to identify which image is real and which is fake. discriminator is also the 4 layer CNN with batch normalization and leaky relu layer(except in input layer). the discriminator receives the output image (which is of size 28*28*3) and perform convolutions on it. at last discriminator shows the output probabilities for showing whether image is real or fake using Logistic Sigmoid Function .

When discriminator sees the differences in the image it sends the gradient signal to the Generator and this signal is flows from discriminator to the generator.

4).Generator Loss and Discriminator Loss:-

Discriminator is receiving the images from both i.e training images and generator ,so while calculating discriminator’s loss we have to add loss due of real images and also due of fake images both networks are trained simultaneously so we need two optimizers for both generator and discriminator both. we want from discriminator to output the probabilities close to 1 if the images are real and close to 0 if the images are fake.

Training and Results:-

when the training process is going on generator produces the set of images and after every epoch it gets better and bettter so that the discriminator couldn’t identify whether it is real image or fake image. results produces are as follows-

Images Generated:-

and so on the new faces are continously generated….

I have also got the pre-trained network from here and if you want to run the GAN using this pre-trained networks then use this python file which i am providing here.

after running this this will generate the set of 10 fake images. some of them are-


These are the fake images generated from the pretrained network given.

This Completes all the amazing stuff related to Generative Adversarial Network.


在解卷积层之后,生成器结构具有致密层和全连接层(除输出层外每一层都有批量标准化,leaky ReLu和dropout)。生成器将随机噪声向量z,之后把它重塑为4D形状并把它传递给一系列上采样层。每个上采样层都代表一个转置卷积运算,即反卷积运算


所有转置卷积的深度从1024一直减少到3 ,它表示RGB彩色图像。 最后一层通过双曲正切(tanh)函数输出28x28x3张量。

鉴别器结构

鉴别器的工作是识别哪个图像是真实的,哪个是假的。鉴别器也是具有批量归一化、lekeay Relu的4层 CNN(输入层除外)。鉴别器接收输出图像(大小为28 * 28 * 3)并对其进行卷积。 最后,鉴别器使用Logistic Sigmoid函数显示用于表示图像是真或假的输出概率。

当鉴别器看到图像中的差异时,它将梯度信号发送到生成器,此信号从鉴别器流向生成器。

4)生成器损失和鉴别器损失:  

鉴别器从训练图像和生成器两者接收图像,因此在计算鉴别器的损失时,我们必须增加被同时训练的两个网络由于真实图像和假图像造成的损失,,因此我们需要用在生成器和鉴别器的两个优化。 如果图像是真实的,我们希望从鉴别器输出接近1的概率,如果图像是假的,则输出接近0的概率。

训练和结果

当训练过程正在进行时,生成器产生一组图像,并且在每个时期之后,它变得越来越好,使得鉴别器不能识别它是真实图像还是假图像。 结果生成如下 

生成的图像: 
新面部图像不断地产生

我也从这里获得了预先训练好的网络,如果你想使用这个预先训练好的网络运行生成式对抗网络,那么请使用我在这里提供的这个python文件。

运行后,这将生成10个假图像的集合。 他们之中有一些是

这些是从给定的预训练网络生成的假图像。

到这里就完成了与生成式对抗网络相关的所有惊人的东西。

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow