Skip to content

Commit f8e4b0b

Browse files
committed
Merge branch 'develop' of github.com:baidu/Paddle into feature/mnist_train_api
2 parents 9b41b08 + 87170a7 commit f8e4b0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+416
-421
lines changed

WORKSPACE

Lines changed: 0 additions & 31 deletions
This file was deleted.

doc/getstarted/build_and_install/docker_install_en.rst

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,13 @@ The general development workflow with Docker and Bazel is as follows:
6565
--name paddle \
6666
-p 2022:22 \
6767
-v $PWD:/paddle \
68-
-v $HOME/.cache/bazel:/root/.cache/bazel \
6968
paddle:dev
7069
7170
where :code:`-d` makes the container running in background,
7271
:code:`--name paddle` allows us to run a nginx container to serve
7372
documents in this container, :code:`-p 2022:22` allows us to SSH
7473
into this container, :code:`-v $PWD:/paddle` shares the source code
75-
on the host with the container, :code:`-v
76-
$HOME/.cache/bazel:/root/.cache/bazel` shares Bazel cache on the
77-
host with the container.
74+
on the host with the container.
7875

7976
4. SSH into the container:
8077

@@ -94,13 +91,6 @@ The general development workflow with Docker and Bazel is as follows:
9491
make -j `nproc`
9592
CTEST_OUTPUT_ON_FAILURE=1 ctest
9693
97-
or Bazel in the container:
98-
99-
.. code-block:: bash
100-
101-
cd /paddle
102-
bazel test ...
103-
10494
10595
CPU-only and GPU Images
10696
-----------------------

doc/tutorials/gan/gan.png

32.5 KB
Loading

doc/tutorials/gan/index_en.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Generative Adversarial Networks (GAN)
2+
3+
This demo implements GAN training described in the original [GAN paper](https://arxiv.org/abs/1406.2661) and deep convolutional generative adversarial networks [DCGAN paper](https://arxiv.org/abs/1511.06434).
4+
5+
The high-level structure of GAN is shown in Figure. 1 below. It is composed of two major parts: a generator and a discriminator, both of which are based on neural networks. The generator takes in some kind of noise with a known distribution and transforms it into an image. The discriminator takes in an image and determines whether it is artificially generated by the generator or a real image. So the generator and the discriminator are in a competitive game in which generator is trying to generate image to look as real as possible to fool the discriminator, while the discriminator is trying to distinguish between real and fake images.
6+
7+
<p align="center">
8+
<img src="./gan.png" width="500" height="300">
9+
</p>
10+
<p align="center">
11+
Figure 1. GAN-Model-Structure
12+
<a href="https://ishmaelbelghazi.github.io/ALI/">figure credit</a>
13+
</p>
14+
15+
The generator and discriminator take turn to be trained using SGD. The objective function of the generator is for its generated images being classified as real by the discriminator, and the objective function of the discriminator is to correctly classify real and fake images. When the GAN model is trained to converge to the equilibrium state, the generator will transform the given noise distribution to the distribution of real images, and the discriminator will not be able to distinguish between real and fake images at all.
16+
17+
## Implementation of GAN Model Structure
18+
Since GAN model involves multiple neural networks, it requires to use paddle python API. So the code walk-through below can also partially serve as an introduction to the usage of Paddle Python API.
19+
20+
There are three networks defined in gan_conf.py, namely **generator_training**, **discriminator_training** and **generator**. The relationship to the model structure we defined above is that **discriminator_training** is the discriminator, **generator** is the generator, and the **generator_training** combined the generator and discriminator since training generator would require the discriminator to provide loss function. This relationship is described in the following code:
21+
```python
22+
if is_generator_training:
23+
noise = data_layer(name="noise", size=noise_dim)
24+
sample = generator(noise)
25+
26+
if is_discriminator_training:
27+
sample = data_layer(name="sample", size=sample_dim)
28+
29+
if is_generator_training or is_discriminator_training:
30+
label = data_layer(name="label", size=1)
31+
prob = discriminator(sample)
32+
cost = cross_entropy(input=prob, label=label)
33+
classification_error_evaluator(
34+
input=prob, label=label, name=mode + '_error')
35+
outputs(cost)
36+
37+
if is_generator:
38+
noise = data_layer(name="noise", size=noise_dim)
39+
outputs(generator(noise))
40+
```
41+
42+
In order to train the networks defined in gan_conf.py, one first needs to initialize a Paddle environment, parse the config, create GradientMachine from the config and create trainer from GradientMachine as done in the code chunk below:
43+
```python
44+
import py_paddle.swig_paddle as api
45+
# init paddle environment
46+
api.initPaddle('--use_gpu=' + use_gpu, '--dot_period=10',
47+
'--log_period=100', '--gpu_id=' + args.gpu_id,
48+
'--save_dir=' + "./%s_params/" % data_source)
49+
50+
# Parse config
51+
gen_conf = parse_config(conf, "mode=generator_training,data=" + data_source)
52+
dis_conf = parse_config(conf, "mode=discriminator_training,data=" + data_source)
53+
generator_conf = parse_config(conf, "mode=generator,data=" + data_source)
54+
55+
# Create GradientMachine
56+
dis_training_machine = api.GradientMachine.createFromConfigProto(
57+
dis_conf.model_config)
58+
gen_training_machine = api.GradientMachine.createFromConfigProto(
59+
gen_conf.model_config)
60+
generator_machine = api.GradientMachine.createFromConfigProto(
61+
generator_conf.model_config)
62+
63+
# Create trainer
64+
dis_trainer = api.Trainer.create(dis_conf, dis_training_machine)
65+
gen_trainer = api.Trainer.create(gen_conf, gen_training_machine)
66+
```
67+
68+
In order to balance the strength between generator and discriminator, we schedule to train whichever one is performing worse by comparing their loss function value. The loss function value can be calculated by a forward pass through the GradientMachine.
69+
```python
70+
def get_training_loss(training_machine, inputs):
71+
outputs = api.Arguments.createArguments(0)
72+
training_machine.forward(inputs, outputs, api.PASS_TEST)
73+
loss = outputs.getSlotValue(0).copyToNumpyMat()
74+
return numpy.mean(loss)
75+
```
76+
77+
After training one network, one needs to sync the new parameters to the other networks. The code below demonstrates one example of such use case:
78+
```python
79+
# Train the gen_training
80+
gen_trainer.trainOneDataBatch(batch_size, data_batch_gen)
81+
82+
# Copy the parameters from gen_training to dis_training and generator
83+
copy_shared_parameters(gen_training_machine,
84+
dis_training_machine)
85+
copy_shared_parameters(gen_training_machine, generator_machine)
86+
```
87+
88+
89+
## A Toy Example
90+
With the infrastructure explained above, we can now walk you through a toy example of generating two dimensional uniform distribution using 10 dimensional Gaussian noise.
91+
92+
The Gaussian noises are generated using the code below:
93+
```python
94+
def get_noise(batch_size, noise_dim):
95+
return numpy.random.normal(size=(batch_size, noise_dim)).astype('float32')
96+
```
97+
98+
The real samples (2-D uniform) are generated using the code below:
99+
```python
100+
# synthesize 2-D uniform data in gan_trainer.py:114
101+
def load_uniform_data():
102+
data = numpy.random.rand(1000000, 2).astype('float32')
103+
return data
104+
```
105+
106+
The generator and discriminator network are built using fully-connected layer and batch_norm layer, and are defined in gan_conf.py.
107+
108+
To train the GAN model, one can use the command below. The flag -d specifies the training data (cifar, mnist or uniform) and flag --useGpu specifies whether to use gpu for training (0 is cpu, 1 is gpu).
109+
```bash
110+
$python gan_trainer.py -d uniform --useGpu 1
111+
```
112+
The generated samples can be found in ./uniform_samples/ and one example is shown below as Figure 2. One can see that it roughly recovers the 2D uniform distribution.
113+
114+
<p align="center">
115+
<img src="./uniform_sample.png" width="300" height="300">
116+
</p>
117+
<p align="center">
118+
Figure 2. Uniform Sample
119+
</p>
120+
121+
## MNIST Example
122+
### Data preparation
123+
To download the MNIST data, one can use the following commands:
124+
```bash
125+
$cd data/
126+
$./get_mnist_data.sh
127+
```
128+
129+
### Model description
130+
Following the DC-Gan paper (https://arxiv.org/abs/1511.06434), we use convolution/convolution-transpose layer in the discriminator/generator network to better deal with images. The details of the network structures are defined in gan_conf_image.py.
131+
132+
### Training the model
133+
To train the GAN model on mnist data, one can use the following command:
134+
```bash
135+
$python gan_trainer.py -d mnist --useGpu 1
136+
```
137+
The generated sample images can be found at ./mnist_samples/ and one example is shown below as Figure 3.
138+
<p align="center">
139+
<img src="./mnist_sample.png" width="300" height="300">
140+
</p>
141+
<p align="center">
142+
Figure 3. MNIST Sample
143+
</p>

doc/tutorials/gan/mnist_sample.png

28 KB
Loading

doc/tutorials/gan/uniform_sample.png

20.1 KB
Loading

0 commit comments

Comments
 (0)