Skip to content
Merged
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
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# DCGAN in TensorLayer

TensorLayer implementation of [Deep Convolutional Generative Adversarial Networks](http://arxiv.org/abs/1511.06434).

This is the TensorLayer implementation of [Deep Convolutional Generative Adversarial Networks](http://arxiv.org/abs/1511.06434).
Looking for Text to Image Synthesis ? [click here](https://github.com/zsdonghao/text-to-image)

![alt tag](img/DCGAN.png)


- 🆕 🔥 2019 May: We just update this project to support TF2 and TL2. Enjoy!
- 🆕 🔥 2019 May: This project is chosen as the default template of TL projects.


## Prerequisites

- Python3
- TensorFlow==1.13
- TensorLayer (self-contained)
- Python3.5 3.6
- TensorFlow==2.0.0a0 `pip3 install tensorflow-gpu==2.0.0a0`
- TensorLayer=2.0.0 `pip3 install tensorlayer==2.0.0`

## Usage

First, download the aligned face images from [google](https://drive.google.com/open?id=0B7EVK8r0v71pWEZsZE9oNnFzTm8) or [baidu](https://pan.baidu.com/s/1eSNpdRG#list/path=%2F) to a `data` folder.

Second, train the GAN:

$ python main_eager_mode.py # single GPU for TF>=1.13
$ python main_graph_mode.py # single GPU for TF<=1.13
$ python main_eager_mode_horovod.py # multiple GPU (TODO)
$ python main_eager_mode_tlmagic.py # multiple GPU (TODO)

$ python train.py

## Result on celebA


Expand Down
57 changes: 57 additions & 0 deletions data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import numpy as np
import tensorflow as tf
import tensorlayer as tl
## enable debug logging
tl.logging.set_verbosity(tl.logging.DEBUG)

class FLAGS(object):
def __init__(self):
self.n_epoch = 25 # "Epoch to train [25]"
self.z_dim = 100 # "Num of noise value]"
self.learning_rate = 0.0002 # "Learning rate of for adam [0.0002]")
self.beta1 = 0.5 # "Momentum term of adam [0.5]")
self.batch_size = 64 # "The number of batch images [64]")
self.output_size = 64 # "The size of the output images to produce [64]")
self.sample_size = 64 # "The number of sample images [64]")
self.c_dim = 3 # "Number of image channels. [3]")
self.save_step = 500 # "The interval of saveing checkpoints. [500]")
# self.dataset = "celebA" # "The name of dataset [celebA, mnist, lsun]")
self.checkpoint_dir = "checkpoint" # "Directory name to save the checkpoints [checkpoint]")
self.sample_dir = "samples" # "Directory name to save the image samples [samples]")
assert np.sqrt(self.sample_size) % 1 == 0., 'Flag `sample_size` needs to be a perfect square'
flags = FLAGS()

tl.files.exists_or_mkdir(flags.checkpoint_dir) # save model
tl.files.exists_or_mkdir(flags.sample_dir) # save generated image

def get_celebA(output_size, n_epoch, batch_size):
# dataset API and augmentation
images_path = tl.files.load_file_list(path='data', regx='.*.jpg', keep_prefix=True, printable=False)
def generator_train():
for image_path in images_path:
yield image_path.encode('utf-8')
def _map_fn(image_path):
image = tf.io.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=3) # get RGB with 0~1
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
# image = tf.image.crop_central(image, [FLAGS.output_size, FLAGS.output_size, FLAGS.c_dim])
# image = tf.image.resize_images(image, FLAGS.output_size])
image = image[45:173, 25:153, :] # central crop
image = tf.image.resize([image], (output_size, output_size))[0]
# image = tf.image.crop_and_resize(image, boxes=[[]], crop_size=[64, 64])
# image = tf.image.resize_image_with_crop_or_pad(image, FLAGS.output_size, FLAGS.output_size) # central crop
image = tf.image.random_flip_left_right(image)
image = image * 2 - 1
return image
train_ds = tf.data.Dataset.from_generator(generator_train, output_types=tf.string)
ds = train_ds.shuffle(buffer_size=4096)
# ds = ds.shard(num_shards=hvd.size(), index=hvd.rank())
ds = ds.repeat(n_epoch)
ds = ds.map(_map_fn, num_parallel_calls=4)
ds = ds.batch(batch_size)
ds = ds.prefetch(buffer_size=2)
return ds, images_path
# for batch_images in train_ds:
# print(batch_images.shape)
# value = ds.make_one_shot_iterator().get_next()
84 changes: 0 additions & 84 deletions main_eager_mode2.py

This file was deleted.

89 changes: 0 additions & 89 deletions main_eager_mode_horovod.py

This file was deleted.

66 changes: 0 additions & 66 deletions main_graph_mode.py

This file was deleted.

9 changes: 4 additions & 5 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import tensorlayer as tl
from tensorlayer.layers import Input, Dense, DeConv2d, Reshape, BatchNorm2d, Conv2d, Flatten, BatchNorm

flags = tf.app.flags
FLAGS = flags.FLAGS

def get_generator(shape, gf_dim=64): # Dimension of gen filters in first conv layer. [64]
image_size = 64
s16 = image_size // 16
w_init = tf.glorot_normal_initializer()
# w_init = tf.glorot_normal_initializer()
w_init = tf.random_normal_initializer(stddev=0.02)
gamma_init = tf.random_normal_initializer(1., 0.02)

ni = Input(shape)
Expand All @@ -26,7 +24,8 @@ def get_generator(shape, gf_dim=64): # Dimension of gen filters in first conv la
return tl.models.Model(inputs=ni, outputs=nn, name='generator')

def get_discriminator(shape, df_dim=64): # Dimension of discrim filters in first conv layer. [64]
w_init = tf.glorot_normal_initializer()
# w_init = tf.glorot_normal_initializer()
w_init = tf.random_normal_initializer(stddev=0.02)
gamma_init = tf.random_normal_initializer(1., 0.02)
lrelu = lambda x : tf.nn.leaky_relu(x, 0.2)

Expand Down
Loading