Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__pycache__
GANs/*.pyc
checkpoint
logs
data/minist

results
4 changes: 2 additions & 2 deletions ACGAN.py → GANs/ACGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class ACGAN(object):
model_name = "ACGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions BEGAN.py → GANs/BEGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class BEGAN(object):
model_name = "BEGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions CGAN.py → GANs/CGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class CGAN(object):
model_name = "CGAN" # name for checkpoint
Expand Down
6 changes: 3 additions & 3 deletions CVAE.py → GANs/CVAE.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

import prior_factory as prior
import GANs.prior_factory as prior

class CVAE(object):
model_name = "CVAE" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions DRAGAN.py → GANs/DRAGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class DRAGAN(object):
model_name = "DRAGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions EBGAN.py → GANs/EBGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class EBGAN(object):
model_name = "EBGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions GAN.py → GANs/GAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class GAN(object):
model_name = "GAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions LSGAN.py → GANs/LSGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class LSGAN(object):
model_name = "LSGAN" # name for checkpoint
Expand Down
10 changes: 5 additions & 5 deletions VAE.py → GANs/VAE.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

import prior_factory as prior
import GANs.prior_factory as prior

class VAE(object):
model_name = "VAE" # name for checkpoint
Expand Down Expand Up @@ -96,7 +96,7 @@ def build_model(self):

""" Loss Function """
# encoding
self.mu, sigma = self.encoder(self.inputs, is_training=True, reuse=False)
self.mu, sigma = self.encoder(self.inputs, is_training=True, reuse=False)

# sampling by re-parameterization technique
z = self.mu + sigma * tf.random_normal(tf.shape(self.mu), 0, 1, dtype=tf.float32)
Expand Down Expand Up @@ -241,7 +241,7 @@ def visualize_results(self, epoch):
else:
z_tot = np.concatenate((z_tot, z), axis=0)
id_tot = np.concatenate((id_tot, batch_labels), axis=0)

# in conda, py2.7 and matpltlib version not match
save_scattered_image(z_tot, id_tot, -4, 4, name=check_folder(
self.result_dir + '/' + self.model_dir) + '/' + self.model_name + '_epoch%03d' % epoch + '_learned_manifold.png')

Expand Down
4 changes: 2 additions & 2 deletions WGAN.py → GANs/WGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class WGAN(object):
model_name = "WGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions WGAN_GP.py → GANs/WGAN_GP.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class WGAN_GP(object):
model_name = "WGAN_GP" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions infoGAN.py → GANs/infoGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import tensorflow as tf
import numpy as np

from ops import *
from utils import *
from GANs.ops import *
from GANs.utils import *

class infoGAN(object):
model_name = "infoGAN" # name for checkpoint
Expand Down
4 changes: 2 additions & 2 deletions ops.py → GANs/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Most codes from https://github.com/carpedm20/DCGAN-tensorflow
"""
import math
import numpy as np
import numpy as np
import tensorflow as tf

from tensorflow.python.framework import ops

from utils import *
from GANs.utils import *

if "concat_v2" in dir(tf):
def concat(tensors, axis, *args, **kwargs):
Expand Down
File renamed without changes.
53 changes: 29 additions & 24 deletions utils.py → GANs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
from time import gmtime, strftime
from six.moves import xrange
import matplotlib.pyplot as plt
#import matplotlib.pyplot as plt
import os, gzip

import tensorflow as tf
Expand All @@ -19,6 +19,7 @@ def load_mnist(dataset_name):
data_dir = os.path.join("./data", dataset_name)

def extract_data(filename, num_data, head_size, data_size):
print('reading gz data {}'.format(filename))
with gzip.open(filename) as bytestream:
bytestream.read(head_size)
buf = bytestream.read(data_size * num_data)
Expand Down Expand Up @@ -123,27 +124,31 @@ def inverse_transform(images):
return (images+1.)/2.

""" Drawing Tools """
# in conda, py2.7 and matpltlib version not match, del

# borrowed from https://github.com/ykwon0407/variational_autoencoder/blob/master/variational_bayes.ipynb
def save_scattered_image(z, id, z_range_x, z_range_y, name='scattered_image.jpg'):
N = 10
plt.figure(figsize=(8, 6))
plt.scatter(z[:, 0], z[:, 1], c=np.argmax(id, 1), marker='o', edgecolor='none', cmap=discrete_cmap(N, 'jet'))
plt.colorbar(ticks=range(N))
axes = plt.gca()
axes.set_xlim([-z_range_x, z_range_x])
axes.set_ylim([-z_range_y, z_range_y])
plt.grid(True)
plt.savefig(name)

# borrowed from https://gist.github.com/jakevdp/91077b0cae40f8f8244a
def discrete_cmap(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map"""

# Note that if base_cmap is a string or None, you can simply do
# return plt.cm.get_cmap(base_cmap, N)
# The following works for string, None, or a colormap instance:

base = plt.cm.get_cmap(base_cmap)
color_list = base(np.linspace(0, 1, N))
cmap_name = base.name + str(N)
return base.from_list(cmap_name, color_list, N)
# def save_scattered_image(z, id, z_range_x, z_range_y, name='scattered_image.jpg'):
# N = 10
# plt.figure(figsize=(8, 6))
# plt.scatter(z[:, 0], z[:, 1], c=np.argmax(id, 1), marker='o', edgecolor='none', cmap=discrete_cmap(N, 'jet'))
# plt.colorbar(ticks=range(N))
# axes = plt.gca()
# axes.set_xlim([-z_range_x, z_range_x])
# axes.set_ylim([-z_range_y, z_range_y])
# plt.grid(True)
# plt.savefig(name)

# # borrowed from https://gist.github.com/jakevdp/91077b0cae40f8f8244a
# def discrete_cmap(N, base_cmap=None):
# """Create an N-bin discrete colormap from the specified input map"""

# # Note that if base_cmap is a string or None, you can simply do
# # return plt.cm.get_cmap(base_cmap, N)
# # The following works for string, None, or a colormap instance:

# base = plt.cm.get_cmap(base_cmap)
# color_list = base(np.linspace(0, 1, N))
# cmap_name = base.name + str(N)
# return base.from_list(cmap_name, color_list, N)


35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# tensorflow-generative-model-collections
Tensorflow implementation of various GANs and VAEs.


## Related Repositories
### Pytorch version
Pytorch version of this repository is availabel at https://github.com/znxlwm/pytorch-generative-model-collections

### "Are GANs Created Equal? A Large-Scale Study" Paper
https://github.com/google/compare_gan is the code that was used in [the paper](https://arxiv.org/abs/1711.10337).
It provides IS/FID and rich experimental results for all gan-variants.
### "Are GANs Created Equal? A Large-Scale Study" Paper
https://github.com/google/compare_gan is the code that was used in [the paper](https://arxiv.org/abs/1711.10337).
It provides IS/FID and rich experimental results for all gan-variants.

## Generative Adversarial Networks (GANs)
### Lists
### Lists

*Name* | *Paper Link* | *Value Function*
:---: | :---: | :--- |
Expand All @@ -23,16 +24,16 @@ It provides IS/FID and rich experimental results for all gan-variants.
**infoGAN**| [Arxiv](https://arxiv.org/abs/1606.03657) | <img src = 'assets/equations/infoGAN.png' height = '70px'>
**ACGAN**| [Arxiv](https://arxiv.org/abs/1610.09585) | <img src = 'assets/equations/ACGAN.png' height = '70px'>
**EBGAN**| [Arxiv](https://arxiv.org/abs/1609.03126) | <img src = 'assets/equations/EBGAN.png' height = '70px'>
**BEGAN**| [Arxiv](https://arxiv.org/abs/1702.08431) | <img src = 'assets/equations/BEGAN.png' height = '105px'>
**BEGAN**| [Arxiv](https://arxiv.org/abs/1702.08431) | <img src = 'assets/equations/BEGAN.png' height = '105px'>

#### Variants of GAN structure
<img src = 'assets/etc/GAN_structure.png' height = '600px'>

### Results for mnist
Network architecture of generator and discriminator is the exaclty sames as in [infoGAN paper](https://arxiv.org/abs/1606.03657).
Network architecture of generator and discriminator is the exaclty sames as in [infoGAN paper](https://arxiv.org/abs/1606.03657).
For fair comparison of core ideas in all gan variants, all implementations for network architecture are kept same except EBGAN and BEGAN. Small modification is made for EBGAN/BEGAN, since those adopt auto-encoder strucutre for discriminator. But I tried to keep the capacity of discirminator.

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset mnist --gan_type <TYPE> --epoch 25 --batch_size 64
```
Expand Down Expand Up @@ -68,10 +69,10 @@ infoGAN | <img src = 'assets/mnist_results/conditional_generation/infoGAN_epoch0
</table>

### Results for fashion-mnist
Comments on network architecture in mnist are also applied to here.
Comments on network architecture in mnist are also applied to here.
[Fashion-mnist](https://github.com/zalandoresearch/fashion-mnist) is a recently proposed dataset consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. (T-shirt/top, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot)

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset fashion-mnist --gan_type <TYPE> --epoch 40 --batch_size 64
```
Expand All @@ -98,8 +99,8 @@ CGAN | <img src = 'assets/fashion_mnist_results/conditional_generation/CGAN_epoc
ACGAN | <img src = 'assets/fashion_mnist_results/conditional_generation/ACGAN_epoch000_test_all_classes_style_by_style.png' height = '230px'> | <img src = 'assets/fashion_mnist_results/conditional_generation/ACGAN_epoch019_test_all_classes_style_by_style.png' height = '230px'> | <img src = 'assets/fashion_mnist_results/conditional_generation/ACGAN_epoch039_test_all_classes_style_by_style.png' height = '230px'>
infoGAN | <img src = 'assets/fashion_mnist_results/conditional_generation/infoGAN_epoch000_test_all_classes_style_by_style.png' height = '230px'> | <img src = 'assets/fashion_mnist_results/conditional_generation/infoGAN_epoch019_test_all_classes_style_by_style.png' height = '230px'> | <img src = 'assets/fashion_mnist_results/conditional_generation/infoGAN_epoch039_test_all_classes_style_by_style.png' height = '230px'>

Without hyper-parameter tuning from mnist-version, ACGAN/infoGAN does not work well as compared with CGAN.
ACGAN tends to fall into mode-collapse.
Without hyper-parameter tuning from mnist-version, ACGAN/infoGAN does not work well as compared with CGAN.
ACGAN tends to fall into mode-collapse.
infoGAN tends to ignore noise-vector. It results in that various style within the same class can not be represented.

#### InfoGAN : Manipulating two continous codes
Expand All @@ -122,15 +123,15 @@ infoGAN tends to ignore noise-vector. It results in that various style within th
**VAE**| [Arxiv](https://arxiv.org/abs/1312.6114) | <img src = 'assets/equations/VAE.png' height = '35px'>
**CVAE**| [Arxiv](https://arxiv.org/abs/1406.5298) | <img src = 'assets/equations/CVAE.png' height = '35px'>
**DVAE**| [Arxiv](https://arxiv.org/abs/1511.06406) | (to be added)
**AAE**| [Arxiv](https://arxiv.org/abs/1511.05644) | (to be added)
**AAE**| [Arxiv](https://arxiv.org/abs/1511.05644) | (to be added)

#### Variants of VAE structure
<img src = 'assets/etc/VAE_structure.png' height = '280px'>

### Results for mnist
Network architecture of decoder(generator) and encoder(discriminator) is the exaclty sames as in [infoGAN paper](https://arxiv.org/abs/1606.0365). The number of output nodes in encoder is different. (2x z_dim for VAE, 1 for GAN)

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset mnist --gan_type <TYPE> --epoch 25 --batch_size 64
```
Expand Down Expand Up @@ -158,7 +159,7 @@ Results of CGAN is also given to compare images generated from CVAE and CGAN.

#### Learned manifold

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset mnist --gan_type VAE --epoch 25 --batch_size 64 --dim_z 2
```
Expand All @@ -169,9 +170,9 @@ Please notice that dimension of noise-vector z is 2.
VAE | <img src = 'assets/mnist_results/learned_manifold/VAE_epoch000_learned_manifold.png' height = '230px'> | <img src = 'assets/mnist_results/learned_manifold/VAE_epoch009_learned_manifold.png' height = '230px'> | <img src = 'assets/mnist_results/learned_manifold/VAE_epoch024_learned_manifold.png' height = '230px'>

### Results for fashion-mnist
Comments on network architecture in mnist are also applied to here.
Comments on network architecture in mnist are also applied to here.

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset fashion-mnist --gan_type <TYPE> --epoch 40 --batch_size 64
```
Expand All @@ -198,7 +199,7 @@ Results of CGAN is also given to compare images generated from CVAE and CGAN.

#### Learned manifold

The following results can be reproduced with command:
The following results can be reproduced with command:
```
python main.py --dataset fashion-mnist --gan_type VAE --epoch 25 --batch_size 64 --dim_z 2
```
Expand Down
6 changes: 6 additions & 0 deletions data/get_mnist_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
import tensorflow as tf

# run code in gan_env
mnist = read_data_sets("mnist", one_hot=True)
print(mnist)
Binary file added data/mnist/t10k-images-idx3-ubyte.gz
Binary file not shown.
Binary file added data/mnist/t10k-labels-idx1-ubyte.gz
Binary file not shown.
Binary file added data/mnist/train-images-idx3-ubyte.gz
Binary file not shown.
Binary file added data/mnist/train-labels-idx1-ubyte.gz
Binary file not shown.
Loading