-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcgan_1.py
194 lines (171 loc) · 7.97 KB
/
dcgan_1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
#%%
import tensorflow as tf
import util
from glob import glob
import os
import numpy as np
#%%
class Generator:
def __init__(self, depths=[1024, 512, 256, 128], s_size=4):
self.depths = depths + [3]
self.s_size = s_size
self.reuse = False
def __call__(self, inputs, training=False):
inputs = tf.convert_to_tensor(inputs)
with tf.variable_scope('g', reuse=self.reuse):
# reshape from inputs
with tf.variable_scope('reshape'):
outputs = tf.layers.dense(inputs, self.depths[0] * self.s_size * self.s_size)
outputs = tf.reshape(outputs, [-1, self.s_size, self.s_size, self.depths[0]])
outputs = tf.nn.relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
# deconvolution (transpose of convolution) x 4
with tf.variable_scope('deconv1'):
outputs = tf.layers.conv2d_transpose(outputs, self.depths[1], [5, 5], strides=(2, 2), padding='SAME')
outputs = tf.nn.relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('deconv2'):
outputs = tf.layers.conv2d_transpose(outputs, self.depths[2], [5, 5], strides=(2, 2), padding='SAME')
outputs = tf.nn.relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('deconv3'):
outputs = tf.layers.conv2d_transpose(outputs, self.depths[3], [5, 5], strides=(2, 2), padding='SAME')
outputs = tf.nn.relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('deconv4'):
outputs = tf.layers.conv2d_transpose(outputs, self.depths[4], [5, 5], strides=(2, 2), padding='SAME')
# output images
with tf.variable_scope('tanh'):
outputs = tf.tanh(outputs, name='outputs')
self.reuse = True
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='g')
return outputs
class Discriminator:
def __init__(self, depths=[64, 128, 256, 512]):
self.depths = [3] + depths
self.reuse = False
def __call__(self, inputs, training=False, name=''):
def leaky_relu(x, leak=0.2, name=''):
return tf.maximum(x, x * leak, name=name)
outputs = tf.convert_to_tensor(inputs)
with tf.name_scope('d' + name), tf.variable_scope('d', reuse=self.reuse):
# convolution x 4
with tf.variable_scope('conv1'):
outputs = tf.layers.conv2d(outputs, self.depths[1], [5, 5], strides=(2, 2), padding='SAME')
outputs = leaky_relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('conv2'):
outputs = tf.layers.conv2d(outputs, self.depths[2], [5, 5], strides=(2, 2), padding='SAME')
outputs = leaky_relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('conv3'):
outputs = tf.layers.conv2d(outputs, self.depths[3], [5, 5], strides=(2, 2), padding='SAME')
outputs = leaky_relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('conv4'):
outputs = tf.layers.conv2d(outputs, self.depths[4], [5, 5], strides=(2, 2), padding='SAME')
outputs = leaky_relu(tf.layers.batch_normalization(outputs, training=training), name='outputs')
with tf.variable_scope('classify'):
batch_size = outputs.get_shape()[0].value
reshape = tf.reshape(outputs, [batch_size, -1])
outputs = tf.layers.dense(reshape, 2, name='outputs')
self.reuse = True
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='d')
return outputs
class DCGAN:
def __init__(self,
batch_size=128, s_size=4, z_dim=100,
g_depths=[1024, 512, 256, 128],
d_depths=[64, 128, 256, 512]):
self.batch_size = batch_size
self.s_size = s_size
self.z_dim = z_dim
self.g = Generator(depths=g_depths, s_size=self.s_size)
self.d = Discriminator(depths=d_depths)
self.z = tf.random_uniform([self.batch_size, self.z_dim], minval=-1.0, maxval=1.0)
def loss(self, traindata):
"""build models, calculate losses.
Args:
traindata: 4-D Tensor of shape `[batch, height, width, channels]`.
Returns:
dict of each models' losses.
"""
generated = self.g(self.z, training=True)
g_outputs = self.d(generated, training=True, name='g')
t_outputs = self.d(traindata, training=True, name='t')
# add each losses to collection
tf.add_to_collection(
'g_losses',
tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.ones([self.batch_size], dtype=tf.int64),
logits=g_outputs)))
tf.add_to_collection(
'd_losses',
tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.ones([self.batch_size], dtype=tf.int64),
logits=t_outputs)))
tf.add_to_collection(
'd_losses',
tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.zeros([self.batch_size], dtype=tf.int64),
logits=g_outputs)))
return {
self.g: tf.add_n(tf.get_collection('g_losses'), name='total_g_loss'),
self.d: tf.add_n(tf.get_collection('d_losses'), name='total_d_loss'),
}
def train(self, losses, learning_rate=0.0002, beta1=0.5):
"""
Args:
losses dict.
Returns:
train op.
"""
g_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
d_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
g_opt_op = g_opt.minimize(losses[self.g], var_list=self.g.variables)
d_opt_op = d_opt.minimize(losses[self.d], var_list=self.d.variables)
with tf.control_dependencies([g_opt_op, d_opt_op]):
return tf.no_op(name='train')
def sample_images(self, row=8, col=8, inputs=None):
if inputs is None:
inputs = self.z
images = self.g(inputs, training=True)
images = tf.image.convert_image_dtype(tf.div(tf.add(images, 1.0), 2.0), tf.uint8)
images = [image for image in tf.split(images, self.batch_size, axis=0)]
rows = []
for i in range(row):
rows.append(tf.concat(images[col * i + 0:col * i + col], 2))
image = tf.concat(rows, 1)
return tf.image.decode_jpeg(tf.squeeze(image,[0]))
#%%
##ttaining
train_dir="/home/yejg/tensorflow/DCGAN/data/train/"
train_files=[]
for d,_,_ in os.walk(train_dir):
train_files.append(glob(os.path.join(d,"*.jpg*")))
train_files=train_files[0]
np.random.shuffle(train_files)
#%%
batch_size=128
batch_images=train_files[:batch_size]
batch_images = map(lambda x: util.crop_resize(x),batch_images)
batch_images = np.array(batch_images).astype(np.float32)
#%%
dcgan=DCGAN(batch_size)
losses=dcgan.loss(batch_images)
train_op=dcgan.train(losses)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(200):
_, g_loss_value, d_loss_value = sess.run([train_op, losses[dcgan.g], losses[dcgan.d]])
if step%50==0:
print "g_loss: ------ %f"%g_loss_value
print "d_loss: -------%f"%d_loss_value
#%%
### generate
#dcgan = DCGAN()
#images = dcgan.sample_images()
#
#with tf.Session() as sess:
# # restore trained variables
#
# generated = sess.run(images)
# with open('<filename>', 'wb') as f:
# f.write(generated)