-
Notifications
You must be signed in to change notification settings - Fork 167
/
main.py
62 lines (47 loc) · 2.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''TensorFlow implementation of http://arxiv.org/pdf/1312.6114v10.pdf'''
from __future__ import absolute_import, division, print_function
import math
import os
import numpy as np
import scipy.misc
import tensorflow as tf
from tensorflow.contrib import layers
from tensorflow.contrib import losses
from tensorflow.contrib.framework import arg_scope
from scipy.misc import imsave
from tensorflow.examples.tutorials.mnist import input_data
from progressbar import ETA, Bar, Percentage, ProgressBar
from vae import VAE
from gan import GAN
flags = tf.flags
logging = tf.logging
flags.DEFINE_integer("batch_size", 128, "batch size")
flags.DEFINE_integer("updates_per_epoch", 1000, "number of updates per epoch")
flags.DEFINE_integer("max_epoch", 100, "max epoch")
flags.DEFINE_float("learning_rate", 1e-2, "learning rate")
flags.DEFINE_string("working_directory", "", "")
flags.DEFINE_integer("hidden_size", 128, "size of the hidden VAE unit")
flags.DEFINE_string("model", "gan", "gan or vae")
FLAGS = flags.FLAGS
if __name__ == "__main__":
data_directory = os.path.join(FLAGS.working_directory, "MNIST")
if not os.path.exists(data_directory):
os.makedirs(data_directory)
mnist = input_data.read_data_sets(data_directory, one_hot=True)
assert FLAGS.model in ['vae', 'gan']
if FLAGS.model == 'vae':
model = VAE(FLAGS.hidden_size, FLAGS.batch_size, FLAGS.learning_rate)
elif FLAGS.model == 'gan':
model = GAN(FLAGS.hidden_size, FLAGS.batch_size, FLAGS.learning_rate)
for epoch in range(FLAGS.max_epoch):
training_loss = 0.0
pbar = ProgressBar()
for i in pbar(range(FLAGS.updates_per_epoch)):
images, _ = mnist.train.next_batch(FLAGS.batch_size)
loss_value = model.update_params(images)
training_loss += loss_value
training_loss = training_loss / \
(FLAGS.updates_per_epoch * FLAGS.batch_size)
print("Loss %f" % training_loss)
model.generate_and_save_images(
FLAGS.batch_size, FLAGS.working_directory)