forked from diegoalejogm/gans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DC-GAN-TensorFlow.py
312 lines (203 loc) · 6.66 KB
/
DC-GAN-TensorFlow.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# get_ipython().run_line_magic('load_ext', 'autoreload')
# get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
# get_ipython().run_line_magic('autoreload', '2')
from IPython import display
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
from utils import Logger
import tensorflow as tf
from tensorflow import nn, layers
from tensorflow.contrib import layers as clayers
import numpy as np
# In[3]:
DATA_FOLDER = './tf_data/DCGAN/CIFAR'
# ## Load Data
# In[4]:
def cifar_data():
compose = transforms.Compose(
[
transforms.Resize(64),
transforms.ToTensor(),
transforms.Normalize((.5, .5, .5), (.5, .5, .5)),
])
out_dir = '{}/dataset'.format(DATA_FOLDER)
return datasets.CIFAR10(root=out_dir, train=True, transform=compose, download=True)
# In[5]:
dataset = cifar_data()
batch_size = 100
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
num_batches = len(dataloader)
# In[6]:
IMAGES_SHAPE = (64, 64, 3)
NOISE_SIZE = 100
# ## Models
# ##### Ops
# In[7]:
def default_conv2d(inputs, filters):
return layers.conv2d(
inputs,
filters=filters,
kernel_size=4,
strides=(2, 2),
padding='same',
data_format='channels_last',
use_bias=False,
)
def default_conv2d_transpose(inputs, filters):
return layers.conv2d_transpose(
inputs,
filters=filters,
kernel_size=4,
strides=(2, 2),
padding='same',
data_format='channels_last',
use_bias=False,
)
def noise(n_rows, n_cols):
return np.random.normal(size=(n_rows, n_cols))
# #### Discriminator
# In[8]:
def discriminator(x):
with tf.variable_scope("discriminator", reuse=tf.AUTO_REUSE):
with tf.variable_scope("conv1"):
conv1 = default_conv2d(x, 128)
conv1 = nn.leaky_relu(conv1,alpha=0.2)
with tf.variable_scope("conv2"):
conv2 = default_conv2d(conv1, 256)
conv2 = layers.batch_normalization(conv2)
conv2 = nn.leaky_relu(conv2,alpha=0.2)
with tf.variable_scope("conv3"):
conv3 = default_conv2d(conv2, 512)
conv3 = layers.batch_normalization(conv3)
conv3 = nn.leaky_relu(conv3,alpha=0.2)
with tf.variable_scope("conv4"):
conv4 = default_conv2d(conv3, 1024)
conv4 = layers.batch_normalization(conv3)
conv4 = nn.leaky_relu(conv3,alpha=0.2)
with tf.variable_scope("linear"):
linear = clayers.flatten(conv4)
linear = clayers.fully_connected(linear, 1)
with tf.variable_scope("out"):
out = nn.sigmoid(linear)
return out
# #### Generator
# In[9]:
def generator(z):
with tf.variable_scope("generator", reuse=tf.AUTO_REUSE):
with tf.variable_scope("linear"):
linear = clayers.fully_connected(z, 1024 * 4 * 4)
with tf.variable_scope("conv1_transp"):
# Reshape as 4x4 images
conv1 = tf.reshape(linear, (-1, 4, 4, 1024))
conv1 = default_conv2d_transpose(conv1, 512)
conv1 = layers.batch_normalization(conv1)
conv1 = nn.relu(conv1)
with tf.variable_scope("conv2_transp"):
conv2 = default_conv2d_transpose(conv1, 256)
conv2 = layers.batch_normalization(conv2)
conv2 = nn.relu(conv2)
with tf.variable_scope("conv3_transp"):
conv3 = default_conv2d_transpose(conv2, 128)
conv3 = layers.batch_normalization(conv3)
conv3 = nn.relu(conv3)
with tf.variable_scope("conv4_transp"):
conv4 = default_conv2d_transpose(conv3, 3)
with tf.variable_scope("out"):
out = tf.tanh(conv4)
return out
# ## Graph Optimization
# #### Inputs
# In[10]:
## Real Input
X = tf.placeholder(tf.float32, shape=(None, )+IMAGES_SHAPE)
## Latent Variables / Noise
Z = tf.placeholder(tf.float32, shape=(None, NOISE_SIZE))
# #### Outputs
# In[11]:
# Generator
G_sample = generator(Z)
# Discriminator
D_real = discriminator(X)
D_fake = discriminator(G_sample)
# #### Losses
# In[12]:
# Generator
G_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_fake, labels=tf.ones_like(D_fake)
)
)
# Discriminator
D_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_real, labels=tf.ones_like(D_real)
)
)
D_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=D_fake, labels=tf.zeros_like(D_fake)
)
)
D_loss = D_loss_real + D_loss_fake
# #### Optimizers
# In[13]:
# Obtain trainable variables for both networks
train_vars = tf.trainable_variables()
G_vars = [var for var in train_vars if 'generator' in var.name]
D_vars = [var for var in train_vars if 'discriminator' in var.name]
num_epochs = 200
# In[14]:
G_opt = tf.train.AdamOptimizer(2e-4).minimize(G_loss, var_list=G_vars,)
D_opt = tf.train.AdamOptimizer(2e-4).minimize(D_loss, var_list=D_vars,)
# ## Training
# #### Testing
# In[15]:
num_test_samples = 16
test_noise = noise(num_test_samples, NOISE_SIZE)
# #### Inits
# In[ ]:
BATCH_SIZE = 100
NUM_EPOCHS = 200
# Start interactive session
session = tf.InteractiveSession()
# Init Variables
tf.global_variables_initializer().run()
# Init Logger
logger = Logger(model_name='DCGAN1', data_name='CIFAR10')
# #### Train
# In[ ]:
# Start interactive session
session = tf.InteractiveSession()
# Init Variables
tf.global_variables_initializer().run()
# Iterate through epochs
for epoch in range(NUM_EPOCHS):
for n_batch, (batch,_) in enumerate(dataloader):
# 1. Train Discriminator
X_batch = batch.permute(0, 2, 3, 1).numpy()
feed_dict = {X: X_batch, Z: noise(BATCH_SIZE, NOISE_SIZE)}
_, d_error, d_pred_real, d_pred_fake = session.run(
[D_opt, D_loss, D_real, D_fake], feed_dict=feed_dict
)
# 2. Train Generator
feed_dict = {Z: noise(BATCH_SIZE, NOISE_SIZE)}
_, g_error = session.run(
[G_opt, G_loss], feed_dict=feed_dict
)
if n_batch % 100 == 0:
display.clear_output(True)
# Generate images from test noise
test_images = session.run(
G_sample, feed_dict={Z: test_noise}
)
# Log Images
logger.log_images(test_images, num_test_samples, epoch, n_batch, num_batches, format='NHWC');
# Log Status
logger.display_status(
epoch, num_epochs, n_batch, num_batches,
d_error, g_error, d_pred_real, d_pred_fake
)