-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle_gan.py
404 lines (303 loc) · 14.2 KB
/
cycle_gan.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# CSC 321, Assignment 4
#
# This is the main training file for the CycleGAN part of the assignment.
#
# Usage:
# ======
# To train with the default hyperparamters (saves results to samples_cyclegan/):
# python cycle_gan.py
#
# To train with cycle consistency loss (saves results to samples_cyclegan_cycle/):
# python cycle_gan.py --use_cycle_consistency_loss
#
#
# For optional experimentation:
# -----------------------------
# If you have a powerful computer (ideally with a GPU), then you can obtain better results by
# increasing the number of filters used in the generator and/or discriminator, as follows:
# python cycle_gan.py --g_conv_dim=64 --d_conv_dim=64
import os
import pdb
import pickle
import argparse
import warnings
warnings.filterwarnings("ignore")
# Torch imports
import torch
import torch.nn as nn
import torch.optim as optim
# Numpy & Scipy imports
import numpy as np
import scipy
import scipy.misc
# Local imports
import utils
from data_loader import get_emoji_loader
from models import CycleGenerator, DCDiscriminator
SEED = 11
# Set the random seed manually for reproducibility.
np.random.seed(SEED)
torch.manual_seed(SEED)
if torch.cuda.is_available():
torch.cuda.manual_seed(SEED)
def print_models(G_XtoY, G_YtoX, D_X, D_Y):
"""Prints model information for the generators and discriminators.
"""
print(" G_XtoY ")
print("---------------------------------------")
print(G_XtoY)
print("---------------------------------------")
print(" G_YtoX ")
print("---------------------------------------")
print(G_YtoX)
print("---------------------------------------")
print(" D_X ")
print("---------------------------------------")
print(D_X)
print("---------------------------------------")
print(" D_Y ")
print("---------------------------------------")
print(D_Y)
print("---------------------------------------")
def create_model(opts):
"""Builds the generators and discriminators.
"""
G_XtoY = CycleGenerator(conv_dim=opts.g_conv_dim, init_zero_weights=opts.init_zero_weights)
G_YtoX = CycleGenerator(conv_dim=opts.g_conv_dim, init_zero_weights=opts.init_zero_weights)
D_X = DCDiscriminator(conv_dim=opts.d_conv_dim)
D_Y = DCDiscriminator(conv_dim=opts.d_conv_dim)
print_models(G_XtoY, G_YtoX, D_X, D_Y)
if torch.cuda.is_available():
G_XtoY.cuda()
G_YtoX.cuda()
D_X.cuda()
D_Y.cuda()
print('Models moved to GPU.')
return G_XtoY, G_YtoX, D_X, D_Y
def checkpoint(iteration, G_XtoY, G_YtoX, D_X, D_Y, opts):
"""Saves the parameters of both generators G_YtoX, G_XtoY and discriminators D_X, D_Y.
"""
G_XtoY_path = os.path.join(opts.checkpoint_dir, 'G_XtoY.pkl')
G_YtoX_path = os.path.join(opts.checkpoint_dir, 'G_YtoX.pkl')
D_X_path = os.path.join(opts.checkpoint_dir, 'D_X.pkl')
D_Y_path = os.path.join(opts.checkpoint_dir, 'D_Y.pkl')
torch.save(G_XtoY.state_dict(), G_XtoY_path)
torch.save(G_YtoX.state_dict(), G_YtoX_path)
torch.save(D_X.state_dict(), D_X_path)
torch.save(D_Y.state_dict(), D_Y_path)
def load_checkpoint(opts):
"""Loads the generator and discriminator models from checkpoints.
"""
G_XtoY_path = os.path.join(opts.load, 'G_XtoY.pkl')
G_YtoX_path = os.path.join(opts.load, 'G_YtoX.pkl')
D_X_path = os.path.join(opts.load, 'D_X.pkl')
D_Y_path = os.path.join(opts.load, 'D_Y.pkl')
G_XtoY = CycleGenerator(conv_dim=opts.g_conv_dim, init_zero_weights=opts.init_zero_weights)
G_YtoX = CycleGenerator(conv_dim=opts.g_conv_dim, init_zero_weights=opts.init_zero_weights)
D_X = DCDiscriminator(conv_dim=opts.d_conv_dim)
D_Y = DCDiscriminator(conv_dim=opts.d_conv_dim)
G_XtoY.load_state_dict(torch.load(G_XtoY_path, map_location=lambda storage, loc: storage))
G_YtoX.load_state_dict(torch.load(G_YtoX_path, map_location=lambda storage, loc: storage))
D_X.load_state_dict(torch.load(D_X_path, map_location=lambda storage, loc: storage))
D_Y.load_state_dict(torch.load(D_Y_path, map_location=lambda storage, loc: storage))
if torch.cuda.is_available():
G_XtoY.cuda()
G_YtoX.cuda()
D_X.cuda()
D_Y.cuda()
print('Models moved to GPU.')
return G_XtoY, G_YtoX, D_X, D_Y
def merge_images(sources, targets, opts, k=10):
"""Creates a grid consisting of pairs of columns, where the first column in
each pair contains images source images and the second column in each pair
contains images generated by the CycleGAN from the corresponding images in
the first column.
"""
_, _, h, w = sources.shape
row = int(np.sqrt(opts.batch_size))
merged = np.zeros([3, row*h, row*w*2])
for idx, (s, t) in enumerate(zip(sources, targets)):
i = idx // row
j = idx % row
merged[:, i*h:(i+1)*h, (j*2)*h:(j*2+1)*h] = s
merged[:, i*h:(i+1)*h, (j*2+1)*h:(j*2+2)*h] = t
return merged.transpose(1, 2, 0)
def save_samples(iteration, fixed_Y, fixed_X, G_YtoX, G_XtoY, opts):
"""Saves samples from both generators X->Y and Y->X.
"""
fake_X = G_YtoX(fixed_Y)
fake_Y = G_XtoY(fixed_X)
X, fake_X = utils.to_data(fixed_X), utils.to_data(fake_X)
Y, fake_Y = utils.to_data(fixed_Y), utils.to_data(fake_Y)
merged = merge_images(X, fake_Y, opts)
path = os.path.join(opts.sample_dir, 'sample-{:06d}-X-Y.png'.format(iteration))
scipy.misc.imsave(path, merged)
print('Saved {}'.format(path))
merged = merge_images(Y, fake_X, opts)
path = os.path.join(opts.sample_dir, 'sample-{:06d}-Y-X.png'.format(iteration))
scipy.misc.imsave(path, merged)
print('Saved {}'.format(path))
def training_loop(dataloader_X, dataloader_Y, test_dataloader_X, test_dataloader_Y, opts):
"""Runs the training loop.
* Saves checkpoint every opts.checkpoint_every iterations
* Saves generated samples every opts.sample_every iterations
"""
# Create generators and discriminators
if opts.load:
G_XtoY, G_YtoX, D_X, D_Y = load_checkpoint(opts)
else:
G_XtoY, G_YtoX, D_X, D_Y = create_model(opts)
g_params = list(G_XtoY.parameters()) + list(G_YtoX.parameters()) # Get generator parameters
d_params = list(D_X.parameters()) + list(D_Y.parameters()) # Get discriminator parameters
# Create optimizers for the generators and discriminators
g_optimizer = optim.Adam(g_params, opts.lr, [opts.beta1, opts.beta2])
d_optimizer = optim.Adam(d_params, opts.lr, [opts.beta1, opts.beta2])
iter_X = iter(dataloader_X)
iter_Y = iter(dataloader_Y)
test_iter_X = iter(test_dataloader_X)
test_iter_Y = iter(test_dataloader_Y)
# Get some fixed data from domains X and Y for sampling. These are images that are held
# constant throughout training, that allow us to inspect the model's performance.
fixed_X = utils.to_var(test_iter_X.next()[0])
fixed_Y = utils.to_var(test_iter_Y.next()[0])
iter_per_epoch = min(len(iter_X), len(iter_Y))
for iteration in range(1, opts.train_iters+1):
# Reset data_iter for each epoch
if iteration % iter_per_epoch == 0:
iter_X = iter(dataloader_X)
iter_Y = iter(dataloader_Y)
images_X, labels_X = iter_X.next()
images_X, labels_X = utils.to_var(images_X), utils.to_var(labels_X).long().squeeze()
images_Y, labels_Y = iter_Y.next()
images_Y, labels_Y = utils.to_var(images_Y), utils.to_var(labels_Y).long().squeeze()
# ============================================
# TRAIN THE DISCRIMINATORS
# ============================================
#########################################
## FILL THIS IN ##
#########################################
# Train with real images
d_optimizer.zero_grad()
# 1. Compute the discriminator losses on real images
# D_X_loss = ...
# D_Y_loss = ...
d_real_loss = D_X_loss + D_Y_loss
d_real_loss.backward()
d_optimizer.step()
# Train with fake images
d_optimizer.zero_grad()
# 2. Generate fake images that look like domain X based on real images in domain Y
# fake_X = ...
# 3. Compute the loss for D_X
# D_X_loss = ...
# 4. Generate fake images that look like domain Y based on real images in domain X
# fake_Y = ...
# 5. Compute the loss for D_Y
# D_Y_loss = ...
d_fake_loss = D_X_loss + D_Y_loss
d_fake_loss.backward()
d_optimizer.step()
# =========================================
# TRAIN THE GENERATORS
# =========================================
#########################################
## FILL THIS IN: Y--X-->Y CYCLE ##
#########################################
g_optimizer.zero_grad()
# 1. Generate fake images that look like domain X based on real images in domain Y
# fake_X = ...
# 2. Compute the generator loss based on domain X
# g_loss = ...
if opts.use_cycle_consistency_loss:
reconstructed_Y = G_XtoY(fake_X)
# 3. Compute the cycle consistency loss (the reconstruction loss)
# cycle_consistency_loss = ...
g_loss += cycle_consistency_loss
g_loss.backward()
g_optimizer.step()
#########################################
## FILL THIS IN: X--Y-->X CYCLE ##
#########################################
g_optimizer.zero_grad()
# 1. Generate fake images that look like domain Y based on real images in domain X
# fake_Y = ...
# 2. Compute the generator loss based on domain Y
# g_loss = ...
if opts.use_cycle_consistency_loss:
reconstructed_X = G_YtoX(fake_Y)
# 3. Compute the cycle consistency loss (the reconstruction loss)
# cycle_consistency_loss = ...
g_loss += cycle_consistency_loss
g_loss.backward()
g_optimizer.step()
# Print the log info
if iteration % opts.log_step == 0:
print('Iteration [{:5d}/{:5d}] | d_real_loss: {:6.4f} | d_Y_loss: {:6.4f} | d_X_loss: {:6.4f} | '
'd_fake_loss: {:6.4f} | g_loss: {:6.4f}'.format(
iteration, opts.train_iters, d_real_loss.data[0], D_Y_loss.data[0],
D_X_loss.data[0], d_fake_loss.data[0], g_loss.data[0]))
# Save the generated samples
if iteration % opts.sample_every == 0:
save_samples(iteration, fixed_Y, fixed_X, G_YtoX, G_XtoY, opts)
# Save the model parameters
if iteration % opts.checkpoint_every == 0:
checkpoint(iteration, G_XtoY, G_YtoX, D_X, D_Y, opts)
def main(opts):
"""Loads the data, creates checkpoint and sample directories, and starts the training loop.
"""
# Create train and test dataloaders for images from the two domains X and Y
dataloader_X, test_dataloader_X = get_emoji_loader(emoji_type=opts.X, opts=opts)
dataloader_Y, test_dataloader_Y = get_emoji_loader(emoji_type=opts.Y, opts=opts)
# Create checkpoint and sample directories
utils.create_dir(opts.checkpoint_dir)
utils.create_dir(opts.sample_dir)
# Start training
training_loop(dataloader_X, dataloader_Y, test_dataloader_X, test_dataloader_Y, opts)
def print_opts(opts):
"""Prints the values of all command-line arguments.
"""
print('=' * 80)
print('Opts'.center(80))
print('-' * 80)
for key in opts.__dict__:
if opts.__dict__[key]:
print('{:>30}: {:<30}'.format(key, opts.__dict__[key]).center(80))
print('=' * 80)
def create_parser():
"""Creates a parser for command-line arguments.
"""
parser = argparse.ArgumentParser()
# Model hyper-parameters
parser.add_argument('--image_size', type=int, default=32, help='The side length N to convert images to NxN.')
parser.add_argument('--g_conv_dim', type=int, default=32)
parser.add_argument('--d_conv_dim', type=int, default=32)
parser.add_argument('--use_cycle_consistency_loss', action='store_true', default=False, help='Choose whether to include the cycle consistency term in the loss.')
parser.add_argument('--init_zero_weights', action='store_true', default=False, help='Choose whether to initialize the generator conv weights to 0 (implements the identity function).')
# Training hyper-parameters
parser.add_argument('--train_iters', type=int, default=600, help='The number of training iterations to run (you can Ctrl-C out earlier if you want).')
parser.add_argument('--batch_size', type=int, default=16, help='The number of images in a batch.')
parser.add_argument('--num_workers', type=int, default=0, help='The number of threads to use for the DataLoader.')
parser.add_argument('--lr', type=float, default=0.0003, help='The learning rate (default 0.0003)')
parser.add_argument('--beta1', type=float, default=0.5)
parser.add_argument('--beta2', type=float, default=0.999)
# Data sources
parser.add_argument('--X', type=str, default='Apple', choices=['Apple', 'Windows'], help='Choose the type of images for domain X.')
parser.add_argument('--Y', type=str, default='Windows', choices=['Apple', 'Windows'], help='Choose the type of images for domain Y.')
# Saving directories and checkpoint/sample iterations
parser.add_argument('--checkpoint_dir', type=str, default='checkpoints_cyclegan')
parser.add_argument('--sample_dir', type=str, default='samples_cyclegan')
parser.add_argument('--load', type=str, default=None)
parser.add_argument('--log_step', type=int , default=10)
parser.add_argument('--sample_every', type=int , default=100)
parser.add_argument('--checkpoint_every', type=int , default=800)
return parser
if __name__ == '__main__':
parser = create_parser()
opts = parser.parse_args()
if opts.use_cycle_consistency_loss:
opts.sample_dir = 'samples_cyclegan_cycle'
if opts.load:
opts.sample_dir = '{}_pretrained'.format(opts.sample_dir)
opts.sample_every = 20
print_opts(opts)
main(opts)