Giter Site home page Giter Site logo

gan's Introduction

I organized this reposity mainly for learning GANs, so all codes about classical GANs were implemented with simple network structure and tested by MNIST dataset.
Just know about how mathmatical analysis (in network structure and loss function) works in actual codes, learn how others implement the GANs, and finally, enjoy the magic of GANs :-D

For more theoretical details and pratical codes about GANs, please go to GAN_Theories and GAN_Applications, thanks!


All have been tested with python2.7+ and tensorflow1.0+ in linux.

  • Datas: save training data.
  • Samples: save generated data, each folder contains several figs to show the results.
  • utils: contains 2 files
    • data.py: prepreocessing data.
    • nets.py: Generator and Discriminator are saved here.

Note:

The final layer can be sigmoid(data: [0,1]) or tanh(data:[-1,1]), my codes all use sigmoid.
Using weights_initializer=tf.random_normal_initializer(0, 0.02) will converge faster.

DCGAN

  • conv

Conditional GAN

  • condition + mlp D:G = 1:1
  • condition + conv(dcgan) D:G=1:1 faster than mlp
  • dcgan + classifier D:G:C = 1:1:1 very fast
  • wgan + classifier D:G:C = 5:1:1 fast

Note:

a. The step ratio of G and D is important and it takes some time to reach the balance. Condition+mlp with D:G = 1:1 works better than 2:1.
b. Adding a classfier to trained with conditions and constraint G works faster and better than appending conditions to images for D training.

Wasserstein GAN

  • wgan + mlp D:G = 5: 1 not good, need to *modify*
  • wgan + conv(dcgan) D:G = 5:1 clip = 0.01

infoGAN

  • infogan + mlp + D and Q not share Q loss to update G(as feedback) not good
  • infogan + mlp + D and Q share not good. lacking numbers
  • infogan + conv + D and Q not share clear and have 10 number
  • infogan + conv + D and Q share the same with not share, not faster?
  • infogan + wgan + D and Q not share to be done

Results are shown in the end of this page.

Adversarial Nets

✨ GAN

The beginning.
The first paper.

Two main research directions:

  1. stabilize the training
  2. apply GAN

paper

[Generative Adversarial Nets]

  • Loss :
    GAN loss

blog

[openai/generative-models] (Motivation, Game Theory)
[wiseodd/gan-tensorflow] (Introduction, Implementation)


✨DCGAN

stabilize the training with some architectural constraints.
GAN is hard to train.
Stabilize Generative Adversarial networks with some architectural constraints.
Popular used in cv. Most used architecture.

paper

[Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks]

Architecture guidelines for stable Deep Convolutional GANs

  • Replace any pooling layers with strided convolutions (discriminator) and fractional-strided convolutions (generator).
  • Use batchnorm in both the generator and the discriminator
  • Remove fully connected hidden layers for deeper architectures. Just use average pooling at the end.
  • Use ReLU activation in generator for all layers except for the output, which uses Tanh.
  • Use LeakyReLU activation in the discriminator for all layers.

blog

[bamos/deep-completion] (Introduction, Implementation)

code

[carpedm20/DCGAN-tensorflow]( star 1.6k+, many files, easy to run, hard to read and modify)

G: fc-->reshape--> deconv bn relu (4) --> tanh
D: conv bn lrelu[leak=0.2] (4) --> reshape--> fc(opn=1)-->sigmoid
G Loss:

	tf.reduce_mean(sigmoid_cross_entropy_with_logits(self.D_logits_,tf.ones_like(self.D_)))

D Loss:

	tf.reduce_mean(sigmoid_cross_entropy_with_logits(self.D_logits, tf.ones_like(self.D))) + tf.reduce_mean(sigmoid_cross_entropy_with_logits(self.D_logits_, tf.zeros_like(self.D_)))

Solver: Adam lr=0.0002 stepG:stepD=1:1
Data: mnist celebA normalized to [-1,1] for tanh and [0,1] for sigmoid

[sugyan/tf-dcgan]( star 20, easy to read, just 1 dcgan file. dcgan, generator, discriminator all class. not tf.contrib.layers. )

G: fc-->reshape--> deconv bn relu (4) --> tanh
D: conv bn lrelu[leak=0.2] (4) --> reshape--> fc(opn=2 one-hot?)
Losses: softmax_cross_entropy_with_logits
Solver: Adam lr=0.0002 stepG:stepD=1:1


✨Conditional GAN

Apply GAN by adding condition(supervised)
Add conditions to GAN by feeding y to G.
G(z)-->G(z,y) D(X)-->D(X,y)

  • z: the same as in GAN. unconstrained noise to generate a image.
  • y: the condition to constraint the network. supervised.
    A very important structure that used in image applications (data augmentation, image transfer, etc.)
    Make GAN useful.

paper

[Conditional Generative Adversarial Nets]

  • Loss :
    CGAN loss

blog

[wiseodd/conditional-gan-tensorflow] (Fomulation, Architecture, Implementation)

code

[wiseodd/conditional_gan](star 500+, very simple, 1 file, easy to read and run, not conv, inconvinient to extend)

G: concat(z,y)-->fc-->sigmoid
D: concat(z,y)-->fc-->sigmoid loss
Solver: Adam lr=0.001 stepG:stepD=1:1
Data: mnist [0,1]

[zhangqianhui/Conditional-Gans](star 16, easy to extend)

G: concat(z,y)-->fc-->conv-->sigmoid
D: conv_concat(x,y)-->conv-->fc-->sigmoid loss
Solver: Adam lr=0.0002 stepG:stepD=2:1
Data: mnist [0,1]

[fairytale0011/Conditional-WassersteinGAN] (star 6. use wgan to train GAN, use separate classifier to enforce the condition. very clear, easy to read and modify)

G: concat(z,y)-->fc-->conv-->tanh
D: X-->conv-->fc-->sigmoid loss
classifier: X-->conv-->fc-->softmax loss (real label to train classifier, fake label to train G)
clip D var
Solver: RMS lr=0.0002 stepG:stepD:stepC_real:stepC_fake=1:10:0.5:0.5
Data: mnist [-1,1]


✨Wasserstein GAN

stabilize the training by using Wasserstein-1 distance
GAN before using JS divergence has the problem of non-overlapping, leading to mode collapse and convergence difficulty.
Use EM distance or Wasserstein-1 distance, so GAN solve the two problems above without particular architecture (like dcgan).

paper

[Wasserstein GAN]

Algorithm guidelines for stable GANs

  • No log in the loss. The output of D is no longer a probability, hence we do not apply sigmoid at the output of D
  • Clip the weight of D (0.01)
  • Train D more than G (5:1)
  • Use RMSProp instead of ADAM
  • Lower learning rate (0.00005)

blog

[AidenN/WassersteinGAN] (Theory)
[wiseodd/wasserstein-gan] (Introduction, Implementation)
[zhihu/Wassertein GAN] (Introduction, Analysis)

code

[wiseodd/wgan_tensorflow](very simple, use mlp)

G: fc-->sigmoid
D: fc clip D
G Loss:

	G_loss = -tf.reduce_mean(D_fake)

D Loss:

	D_loss = tf.reduce_mean(D_fake) - tf.reduce_mean(D_real) 

Solver: RMSProp lr=0.0001 stepG:stepD=1:5


✨InfoGAN

Apply GAN by learning conditions(unsupervised)
Attempt to make conditional learned automatically. Find and control some useful information in the images.

  • z: the same as in GAN. unconstrainted noise to generate a image.
  • c: like c in conditional GAN, but learned by Q instead of given what that is, unsupervised.

paper

[InfoGAN: Interpretable Representation Learning by Information Maximizing Generative Adversarial Nets]

  • Loss :

infoGAN loss 1

Define: Q(c|x) to approximate P(c|x)(which is the conditional distribution)

infoGAN loss 2

blog

[wiseodd/infogan] (Introduction Implementation)

code

[openai/infogan]( star 300+, hard to read and modify, too much files)

G: fc(1024 bn relu)-->fc (bn relu) reshape--> deconv bn relu --> deconv flatten--> activate
D and Q:
shared: reshape-->conv lrelu --> conv bn lrelu --> fc bn lrelu
D: fc(1) --> activate
Q: fc(128) bn lrelu --> fc (c_dim) --> activate
activate: softmax(Categorical) / mean stddev:sqrt(e^x) (Gaussian) / sigmoid(Bernoulli)
Losses:
D and G: sigmoid as DCGAN
Q: cross entropy loss (softmax for discrete) ** add Q loss to D and G loss **
Solver:
Adam beta1=0.5 stepG:stepD=1:1

my understanding:

Adding Q loss to D and G loss, then updating D var and G var by 1:1 equal to Q loss to update Q(D) var and G var by Q:D:G=2:1:1

[wiseodd/infogan-tensorflow](also simple, use mlp)

Q: fc --> softmax with c (not share with D) update vars: G and Q
Solver: Adam G:D:Q = 1:1:1

Results

All images shown here are from ./Samples, for more information about results, please go to the folder.
Then first number of image name is the trianing epoches.
For better observing, I keep generated images in the beginning, middle and final training process for each algorithm.

Conditional GAN

  • condition + mlp each pic has the same condition

cgan

  • condition + conv(dcgan) each row has the same condition: [0 0 0 0; 1 1 1 1; 2 2 2 2; 3 3 3 3]

cgan

  • dcgan + classifier condition: [0 1 2 3; 4 5 6 7; 0 1 2 3; 4 5 6 7]

cgan

  • wgan + classifier condition: [0 1 2 3; 4 5 6 7; 0 1 2 3; 4 5 6 7]

cgan

InfoGAN
The generated images with the same condition belong to the same category.

  • infogan + conv + D and Q not share condition: [0 1 2 3; 4 5 6 7; 8 9 0 1; 2 3 4 5]

cgan

  • infogan + conv + D and Q share condition: [0 1 2 3; 4 5 6 7; 8 9 0 1; 2 3 4 5]

cgan

DCGAN

  • 3D face result (dcgan)
    3d face

Others

Tensorflow style: https://www.tensorflow.org/community/style_guide
tf.concat(1,[x,y]) in tf 0.12- ---> tf.concat([x,y],1) in tf 1.0+.
use tf.get_Variable or tf.contrib.layers to reuse variables.

A good website to convert latex equation to img(then insert into README): http://www.sciweavers.org/free-online-latex-equation-editor

gan's People

Contributors

yfeng95 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gan's Issues

Papers

In the readMe the links to the research papers are missing...

Some puzzles about the hidden variable C

Thank you for writing such an excellent and convenient code!But I don't understand the meaning of C in the paper.How should c be valued for different problems? Why can C be continuous or discrete?
Looking forward to your answer~

wgan_conv runs error

The code in the file's wgan_conv has little error:
the class G_conv generate [-1, 32, 32, 1] shape of inputs,
but class D_conv's input is [-1, 28, 28, 1],the dimension not equal

A small question

Why does the first param of the functions from line 28 to line 30 in nets.py is z rather than g?

encounter ValueError when I "python dcgan.py"

ValueError: Variable G_conv/Conv/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

I wonder if I am the only one who meet this problem and I have been confused for days. I hope I can get some help.

here is the information
Traceback (most recent call last):
File "dcgan.py", line 107, in
dcgan = DCGAN(generator, discriminator, data)
File "dcgan.py", line 34, in init
self.D_real, _ = self.discriminator(self.X)
File "/home/baiqiujian/github/GAN-master/nets.py", line 146, in call
stride=2, activation_fn=lrelu)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 947, in convolution
outputs = layer.apply(inputs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 492, in apply
return self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 434, in call
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/convolutional.py", line 137, in build
dtype=self.dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 374, in add_variable
trainable=trainable and self.trainable)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 360, in get_variable
validate_shape=validate_shape, use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1370, in layer_variable_getter
return _model_variable_getter(getter, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1362, in _model_variable_getter
custom_getter=getter, use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 261, in model_variable
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
return func(*args, **current_args)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 216, in variable
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
use_resource=use_resource)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 682, in _get_single_variable
"VarScope?" % name)
ValueError: Variable G_conv/Conv/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

After training , cannot test the cgan if i define a variable and use cgan.generate(example) always wrong

Variable G_conv_mnist/fully_connected/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

File "C:\Users\tc\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 1718, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
File "C:\Users\tc\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
op_def=op_def)
File "C:\Users\tc\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)

ValueError: Cannot feed value of shape (0,) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)'

When I try to run dcgan.py it returns me such error info:
Traceback (most recent call last):
File "dcgan.py", line 108, in
dcgan.train(sample_dir)
File "dcgan.py", line 58, in train
feed_dict={self.X: X_b, self.z: sample_z(batch_size, self.z_dim)}
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1096, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (0,) for Tensor u'Placeholder:0', which has shape '(?, 64, 64, 3)'

Any suggestion?
My environment is: centos 7, tensorflow 1.4, cuda 8.0, cudnn 6.0.

关于GAN 的训练

如果GAN训练收敛了, 之后固定G,再接着训练D,那么D还可以判断出来是否是G生成的图片吗?

batch norm problem

I've recently been using batch norm in my code. And I discover that batch norm in your code is not used correctly. Hope to see this problem fixed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.