-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanilla_gan.py
More file actions
277 lines (201 loc) · 8.64 KB
/
vanilla_gan.py
File metadata and controls
277 lines (201 loc) · 8.64 KB
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
# CSC 321, Assignment 4
#
# This is the main training file for the vanilla GAN part of the assignment.
#
# Usage:
# ======
# To train with the default hyperparamters (saves results to checkpoints_vanilla/ and samples_vanilla/):
# python vanilla_gan.py
import os
import pdb
import pickle
import argparse
import warnings
warnings.filterwarnings("ignore")
# Numpy & Scipy imports
import numpy as np
import scipy
import scipy.misc
# Torch imports
import torch
import torch.nn as nn
import torch.optim as optim
# Local imports
import utils
from data_loader import get_emoji_loader
from models import DCGenerator, 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, D):
"""Prints model information for the generators and discriminators.
"""
print(" G ")
print("---------------------------------------")
print(G)
print("---------------------------------------")
print(" D ")
print("---------------------------------------")
print(D)
print("---------------------------------------")
def create_model(opts):
"""Builds the generators and discriminators.
"""
G = DCGenerator(noise_size=opts.noise_size, conv_dim=opts.conv_dim)
D = DCDiscriminator(conv_dim=opts.conv_dim)
print_models(G, D)
if torch.cuda.is_available():
G.cuda()
D.cuda()
print('Models moved to GPU.')
return G, D
def checkpoint(iteration, G, D, opts):
"""Saves the parameters of the generator G and discriminator D.
"""
G_path = os.path.join(opts.checkpoint_dir, 'G.pkl')
D_path = os.path.join(opts.checkpoint_dir, 'D.pkl')
torch.save(G.state_dict(), G_path)
torch.save(D.state_dict(), D_path)
def create_image_grid(array, ncols=None):
"""
"""
num_images, channels, cell_h, cell_w = array.shape
if not ncols:
ncols = int(np.sqrt(num_images))
nrows = int(np.math.floor(num_images / float(ncols)))
result = np.zeros((cell_h*nrows, cell_w*ncols, channels), dtype=array.dtype)
for i in range(0, nrows):
for j in range(0, ncols):
result[i*cell_h:(i+1)*cell_h, j*cell_w:(j+1)*cell_w, :] = array[i*ncols+j].transpose(1, 2, 0)
if channels == 1:
result = result.squeeze()
return result
def save_samples(G, fixed_noise, iteration, opts):
generated_images = G(fixed_noise)
generated_images = utils.to_data(generated_images)
grid = create_image_grid(generated_images)
# merged = merge_images(X, fake_Y, opts)
path = os.path.join(opts.sample_dir, 'sample-{:06d}.png'.format(iteration))
scipy.misc.imsave(path, grid)
print('Saved {}'.format(path))
def sample_noise(dim):
"""
Generate a PyTorch Variable of uniform random noise.
Input:
- batch_size: Integer giving the batch size of noise to generate.
- dim: Integer giving the dimension of noise to generate.
Output:
- A PyTorch Variable of shape (batch_size, dim, 1, 1) containing uniform
random noise in the range (-1, 1).
"""
return utils.to_var(torch.rand(batch_size, dim) * 2 - 1).unsqueeze(2).unsqueeze(3)
def training_loop(train_dataloader, opts):
"""Runs the training loop.
* Saves checkpoints every opts.checkpoint_every iterations
* Saves generated samples every opts.sample_every iterations
"""
# Create generators and discriminators
G, D = create_model(opts)
# Create optimizers for the generators and discriminators
g_optimizer = optim.Adam(G.parameters(), opts.lr, [opts.beta1, opts.beta2])
d_optimizer = optim.Adam(D.parameters(), opts.lr, [opts.beta1, opts.beta2])
# Generate fixed noise for sampling from the generator
fixed_noise = sample_noise(opts.noise_size) # batch_size x noise_size x 1 x 1
iteration = 1
total_train_iters = opts.num_epochs * len(train_dataloader)
for epoch in range(opts.num_epochs):
for batch in train_dataloader:
real_images, labels = batch
real_images, labels = utils.to_var(real_images), utils.to_var(labels).long().squeeze()
################################################
### TRAIN THE DISCRIMINATOR ####
################################################
d_optimizer.zero_grad()
# FILL THIS IN
# 1. Compute the MSE loss on real images
output = D(real_images)
D_real_loss = nn.MSELoss()
# 2. Sample noise
# dimension is 1 as this is the dimension of the input of the generator
noise = sample_noise(1)
# 3. Generate fake images from the noise
fake_images = G(noise)
# 4. Compute the discriminator loss on the fake images
output = D(fake_images)
#We use a string of zeros to represent the target labels
zeros_label = torch.zeros(batch.size(0))
D_fake_loss = nn.MSELoss(output, zeros_label)
# 5. Compute the total discriminator loss
D_total_loss = D_real_loss + D_fake_loss
D_total_loss.backward()
d_optimizer.step()
###########################################
### TRAIN THE GENERATOR ###
###########################################
g_optimizer.zero_grad()
# FILL THIS IN
# 1. Sample noise
#dimension is again 1
noise = sample_noise(1)
# 2. Generate fake images from the noise
fake_images = G(noise)
# 3. Compute the generator loss
D_out = D(fake_images)
#We use a string of ones to represent the target labels
ones_label = torch.ones(batch.size(0))
G_loss = nn.MSELoss(D_out, ones_label)
G_loss.backward()
g_optimizer.step()
# Print the log info
if iteration % opts.log_step == 0:
print('Iteration [{:4d}/{:4d}] | D_real_loss: {:6.4f} | D_fake_loss: {:6.4f} | G_loss: {:6.4f}'.format(
iteration, total_train_iters, D_real_loss.data[0], D_fake_loss.data[0], G_loss.data[0]))
# Save the generated samples
if iteration % opts.sample_every == 0:
save_samples(G, fixed_noise, iteration, opts)
# Save the model parameters
if iteration % opts.checkpoint_every == 0:
checkpoint(iteration, G, D, opts)
iteration += 1
def main(opts):
"""Loads the data, creates checkpoint and sample directories, and starts the training loop.
"""
# Create a dataloader for the training images
train_dataloader, _ = get_emoji_loader(opts.emoji, opts)
# Create checkpoint and sample directories
utils.create_dir(opts.checkpoint_dir)
utils.create_dir(opts.sample_dir)
training_loop(train_dataloader, opts)
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('--conv_dim', type=int, default=32)
parser.add_argument('--noise_size', type=int, default=100)
# Training hyper-parameters
parser.add_argument('--num_epochs', type=int, default=40)
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('--emoji', type=str, default='Apple', choices=['Apple', 'Facebook', 'Windows'], help='Choose the type of emojis to generate.')
# Directories and checkpoint/sample iterations
parser.add_argument('--checkpoint_dir', type=str, default='./checkpoints_vanilla')
parser.add_argument('--sample_dir', type=str, default='./samples_vanilla')
parser.add_argument('--log_step', type=int , default=10)
parser.add_argument('--sample_every', type=int , default=200)
parser.add_argument('--checkpoint_every', type=int , default=400)
return parser
if __name__ == '__main__':
parser = create_parser()
opts = parser.parse_args()
batch_size = opts.batch_size
print(opts)
main(opts)