Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python uses GAN to generate anime avatars


Jun 01, 2021 Article blog



This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn

References

Using deep convolution generation against the network for unsuperviged representation learning;

GenerativeAdversarialNets。

Gan paper summary:

https://github.com/zhangqianhui/AdversarialNetsPapers


statement

All source code and material provided in this tutorial are for learning exchange purposes only and are prohibited from commercial/illegal use.


Lead

Found that the previous two articles about GAN effect is more general, want to try to optimize, train a decent model, at least not too lost GAN's face, so there is this article.

OK, let's start happily.


Related documents

Baidu web download link: https://pan.baidu.com/s/1t-d5wq3TeBWcVzTOoraPtQ

Password: 84ky


Develop tools

Version in Python: 3.6.4

Related modules:

pytorch module;

torchvision module;

PIL module;

and some of the modules that come with Python.

PyTorch version:

0.3.0


Environment construction

The installed Python is added to the environment variable, and the PIP installation requires the relevant modules.

Additional notes:

PyTorch 0.3.0 does not support direct PIP installation (Windows) in .

There are two options:

(1) after the installation of anaconda3 installed in the environment of anaconda3 (direct PIP installation can be);

2 Use the compiled WHL file installation, download link as:

https://pan.baidu.com/s/1dF6ayLr#list/path=%2Fpytorch

Introduction to the principle

For the core idea of generating a network against the network, refer to the previous article:

In Python, Gan is used to generate the MNIST dataset.

By the way, add the mathematical language description of Gan's training objectives:

 Python uses GAN to generate anime avatars1

The formula is explained as follows:

X: Real pictures;

Z: input ģ the noise of the network;

G(Z): A picture generated by the G-network;

d (X): Probability of whether the real picture is real;

d(G(X): The probability that the picture generated by the G-network is true.

As mentioned in the previous article, the training goal of generating network ģ is to generate as many real pictures as possible to deceive the identification network d; T he training goal of judging network d is to distinguish as far as possible between the pictures generated ģ generated by the network and the real pictures, that is, the training process is a dynamic "game process".

Therefore, the ģ network in the formula wants d(G(Z) to be as large as possible; The d network wants d(x) to be as large as possible, and d (G(X) to be as small as possible and the training objective of the formula is :

 Python uses GAN to generate anime avatars2

More about Gan's principles and application can be found in the "References" section.

The specific model

Unlike the network structure used in the ARTICLE "Python" uses GAN Pokemon, this article uses a full convolutional network structure (i.e., no longer joins the full-connection layer FC). A t the same time, this paper increases the amount of training data, using about 50,000 anime avatars as training data.

Specifically, the generator structure is:

 Python uses GAN to generate anime avatars3

The structure of the judge is:

 Python uses GAN to generate anime avatars4

See the source code in the relevant file for details of the implementation.


Model training

One. Train the dataset

About 50,000 anime avatars were used as training data sets, sources:

https://zhuanlan.zhihu.com/p/24767059。

Second, model training

Modify the training dataset path in the config.json file:

 Python uses GAN to generate anime avatars5

Run the display train.py file in the CMD display window.

Training screenshots:

 Python uses GAN to generate anime avatars6

Effect display

Epoch0:

 Python uses GAN to generate anime avatars7

Epoch5:

 Python uses GAN to generate anime avatars8

Epoch10:

 Python uses GAN to generate anime avatars9

Epoch15:

 Python uses GAN to generate anime avatars10

Epoch20:

 Python uses GAN to generate anime avatars11

Epoch25:

 Python uses GAN to generate anime avatars12

Epoch29:

 Python uses GAN to generate anime avatars13

more

Code as of July 4, 2018 The test is correct.

The relevant file provides a trained model and a simple script to call the model, running the "test.py" file directly in the cmd window to generate an anime avatar:

 Python uses GAN to generate anime avatars14