Introduction to TensorFlow: Generative Adversarial Networks

Currently, in the field of deep learning, Generative Adversarial Networks (GANs) are very popular, bringing us an incredible direction in this field. Today, I will share how to use GANs to generate images (Mnist and cartoon faces).

1. How GANs Came to Be

It is said in academia that the founder of GANs, Ian Goodfellow, had a moment of inspiration while slightly drunk in a bar discussing academic issues with colleagues. At that time, he proposed the initial idea of GANs, but it was not recognized by his colleagues. After returning home from the bar and finding his girlfriend already asleep, he stayed up all night coding and found it actually worked. After some research, GANs were born. (https://blog.csdn.net/qq_25737169/article/details/78857724)

Introduction to TensorFlow: Generative Adversarial Networks

2. The Principle of GANs

“Generative Adversarial Networks” is the seminal work on GANs, authored by Ian Goodfellow and published in 2014 (https://arxiv.org/abs/1406.2661). In this paper, Ian Goodfellow succinctly described GANs in just one paragraph.

Introduction to TensorFlow: Generative Adversarial Networks

In simple terms, GANs consist of the following three points:

(1) Construct two networks: one generator network (G) and one discriminator network (D). The structure of both networks can be arbitrary.

(2) Training method. The loss for the G network is: log(1-D(G(z))), while the loss for D is: -(log(D(x)) + log(1-D(G(z)))), note that this is not the commonly used cross-entropy function.

(3) Data input. The input for the G network is noise, while the input for D is a mixture of the G’s output data and sample data.

The GAN proposed by Goodfellow et al. is a new framework for estimating generative models through adversarial processes. In this framework, two models must be trained simultaneously: a generative model G that captures the data distribution and a discriminative model D that estimates the probability that the data comes from real samples. The training process of the generator G maximizes the probability of the discriminator making mistakes, i.e., the discriminator mistakenly believes that the data is real samples rather than fake samples generated by the generator. The training process of the discriminator D maximizes the separation between real samples and fake samples generated by the generator. Therefore, this framework corresponds to a minimax game between two participants. Among all possible functions G and D, a unique equilibrium solution can be obtained, in which G can generate a distribution identical to the training samples, while the probability judged by D is always 1/2.

3. Implementing GAN with TensorFlow

I have implemented the initial version of GAN and Wasserstein GAN using TensorFlow (this is an improved version of GAN, where the initial version of GAN is very unstable and difficult to train, while WGAN is relatively stable, converging quickly to generate realistic images).

4. Generated Image Results

I have tested the training on Mnist and cartoon faces, and the results are as follows.

Result after 0 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 50 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 1000 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 10000 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 5000 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 0 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 50 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 500 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 10000 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

Result after 49999 iterations:

Introduction to TensorFlow: Generative Adversarial Networks

From the above results, it can be seen that as the network continues to train and iterate, the generated images have become very similar to real images, achieving a level of realism that is indistinguishable. I will further study how to apply the generative adversarial network model to medical imaging.

To facilitate everyone’s quick learning of GANs, I have shared the corresponding training data and model on Baidu Cloud: https://pan.baidu.com/s/1SQZTuThtMSJvS8G2TGRBGg Password: hfwz

Corresponding code address: https://github.com/junqiangchen/GAN.

Friends interested can see the detailed process on GitHub. If you think this project is good, I hope you can give it a Star and Fork, which can help more people learn. If you encounter any problems, feel free to leave a message, and I will try to respond.

Leave a Comment