-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_wgan_cifar10.py
361 lines (248 loc) · 11.1 KB
/
train_wgan_cifar10.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
import os
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchsummary import summary
import torchvision
import torchvision.transforms as transforms
from torchvision.utils import save_image
from sklearn.model_selection import train_test_split
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import DataLoader
import copy
import time
import numpy as np
import pickle
from optparse import OptionParser
from gan_model import Generator, Discriminator
INPUT_LATENT = 64 # This overfits substantially; you're probably better off with 64
LAMBDA = 10 # Gradient penalty lambda hyperparameter
CRITIC_ITERS = 5 # How many critic iterations per generator iteration
ITERS = 100000 # How many generator iterations to train for
OUTPUT_DIM = 3072 # Number of pixels in CIFAR10 (3*32*32)
batch_size = 64
inital_epoch = 0
learning_rate = 1e-4
display_steps = 10
in_channel = 3
height = 32
width = 32
def get_args():
parser = OptionParser()
parser.add_option('--data_path', dest='data_path',type='string',
default='data/', help='data path')
parser.add_option('--iterations', dest='ITERS', default=100000, type='int',
help='number of critic iterations per generator iteration')
parser.add_option('--channels', dest='channels', default=3, type='int',
help='number of channels')
parser.add_option('--width', dest='width', default=32, type='int',
help='image width')
parser.add_option('--height', dest='height', default=32, type='int',
help='image height')
parser.add_option('--deviceD', dest='deviceD', default=0, type='int',
help='discriminator device number')
parser.add_option('--deviceG', dest='deviceG', default=0, type='int',
help='generator device number')
(options, args) = parser.parse_args()
return options
def adjust_lr(optimizer, iteration, init_lr = 1e-4, total_iteration = 200000):
gradient = (float(-init_lr) / total_iteration)
lr = gradient * iteration + init_lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
"""
To Calculate the gradient penalty loss for WGAN GP
"""
def compute_gradient_penalty(D, real_samples, fake_samples, device):
# Random weight term for interpolation between real and fake samples
alpha = torch.Tensor(np.random.random((real_samples.size(0), 1, 1, 1)))
alpha = alpha.expand(real_samples.size(0), real_samples.size(1), real_samples.size(2), real_samples.size(3))
alpha = alpha.to(device)
# Get random interpolation between real and fake samples
interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples)).requires_grad_(True)
d_interpolates = D(interpolates)
fake = autograd.Variable(torch.Tensor(real_samples.shape[0], 1).fill_(1.0), requires_grad=False)
fake = fake.to(device)
# Get gradient w.r.t. interpolates
gradients = autograd.grad(
outputs=d_interpolates,
inputs=interpolates,
grad_outputs=fake,
create_graph=True,
retain_graph=True,
only_inputs=True,
)[0]
gradients = gradients.view(gradients.size(0), -1)
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
def train_GAN(netD, netG, inital_epoch, args):
# set parameters
ITERS = args.ITERS
in_channel = args.channels
height = args.height
width = args.width
# set optimizer for generator and discriminator
optimizerD = optim.Adam(netD.parameters(), lr=learning_rate, betas=(0.5, 0.9))
optimizerG = optim.Adam(netG.parameters(), lr=learning_rate, betas=(0.5, 0.9))
# load dataset
transform_train = transforms.Compose([
transforms.ToTensor(),
])
trainset = torchvision.datasets.CIFAR10(
root=args.data_path, train=True, download=True, transform=transform_train)
train_indices, val_indices = train_test_split(np.arange(len(trainset)), test_size=0.2)
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
train_loader = DataLoader(
trainset,
batch_size=batch_size,
num_workers=4,
sampler=train_sampler
)
val_loader = DataLoader(
trainset,
batch_size=batch_size,
num_workers=4,
sampler=valid_sampler
)
print('train_loader : {}, val_loader : {}'.format(len(train_loader), len(val_loader)))
save_losses = []
dev_disc_costs = []
if os.path.exists('./defensive_models/cifar10_losses_gp.pickle'):
with open ('./defensive_models/cifar10_losses_gp.pickle', 'rb') as fp:
save_losses = pickle.load(fp)
if os.path.exists('./defensive_models/dev_disc_costs.pickle'):
with open ('./defensive_models/dev_disc_costs.pickle', 'rb') as fp:
dev_disc_costs = pickle.load(fp)
one = torch.FloatTensor([1])
mone = one * -1
one = one.to(device_D)
mone = mone.to(device_D)
# Training
print('training start')
for iteration in range(inital_epoch, ITERS, 1):
start_time = time.time()
adjust_lr(optimizerD, iteration, init_lr = learning_rate, total_iteration = ITERS)
adjust_lr(optimizerG, iteration, init_lr = learning_rate, total_iteration = ITERS)
d_loss_real = 0
d_loss_fake = 0
#for iter_d in range(CRITIC_ITERS):
for i, (imgs, _) in enumerate(train_loader):
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update
real_imgs = autograd.Variable(imgs.to(device_D))
optimizerD.zero_grad()
# Sample noise as generator input
z = autograd.Variable(torch.randn(imgs.size(0), INPUT_LATENT))
z = z.to(device_G)
# Generate a batch of images
fake_imgs = netG(z).cpu()
fake_imgs = fake_imgs.to(device_D)
# Real images
real_validity = netD(real_imgs)
d_loss_real = real_validity.mean()
d_loss_real.backward(mone)
# Fake images
fake_validity = netD(fake_imgs)
d_loss_fake = fake_validity.mean()
d_loss_fake.backward(one)
# Gradient penalty
gradient_penalty = compute_gradient_penalty(netD, real_imgs.data, fake_imgs.data, device_D)
gradient_penalty.backward()
# Adversarial loss
loss_D = d_loss_fake - d_loss_real + LAMBDA * gradient_penalty
#loss_D.backward()
optimizerD.step()
optimizerG.zero_grad()
del real_validity, fake_validity, fake_imgs, gradient_penalty, real_imgs
# Train the generator every n_critic iterations
if (i + 1)% CRITIC_ITERS == 0 or (i + 1) == len(train_loader):
############################
# (2) Update G network
###########################
for p in netD.parameters():
p.requires_grad = False # to avoid computation
# Generate a batch of images
fake_imgs = netG(z).cpu()
fake_imgs = fake_imgs.to(device_D)
# Loss measures generator's ability to fool the discriminator
# Train on fake images
fake_validity = netD(fake_imgs)
g_loss = fake_validity.mean()
g_loss.backward(mone)
loss_G = -g_loss
#loss_G.backward()
optimizerG.step()
del fake_validity
save_losses.append([loss_D.item(), loss_G.item()])
if (iteration + 1) % display_steps == 0 or (iteration + 1) == ITERS:
print('batch {:>3}/{:>3}, D_cost {:.4f}, G_cost {:.4f}\r'\
.format(iteration + 1, ITERS,loss_D.item(), loss_G.item()))
with open('./defensive_models/cifar10_losses_gp.pickle', 'wb') as fp:
pickle.dump(save_losses, fp)
# snapshots for model
modelG_copy = copy.deepcopy(netG)
modelG_copy = modelG_copy.cpu()
modelG_state_dict = modelG_copy.state_dict()
modelD_copy = copy.deepcopy(netD)
modelD_copy = modelD_copy.cpu()
modelD_state_dict = modelD_copy.state_dict()
torch.save({
'netG_state_dict': modelG_state_dict,
'netD_state_dict': modelD_state_dict,
'epoch': iteration
}, check_point_path)
del modelG_copy, modelG_state_dict, modelD_copy, modelD_state_dict
# save generator model after certain iteration
if (iteration + 1) % 100 == 0 :
g_path = './defensive_models/gen_cifar10_gp_' + str(iteration) + '.pth'
model_copy = copy.deepcopy(netG)
model_copy = model_copy.cpu()
model_state_dict = model_copy.state_dict()
torch.save(model_state_dict, g_path)
del model_copy
# save CIFAR 10 generated images by generator model every 1000 time
if (iteration + 1) % 1000 == 0 :
save_image(fake_imgs.data, './CIFAR10/samples_{}.png'.format(iteration), nrow=5,normalize=True)
costs_avg = 0.0
disc_count = 0
# validate GAN model
with torch.no_grad():
for images,_ in val_loader:
imgs = images.to(device_D)
D = netD(imgs)
costs_avg += -D.mean().cpu().data.numpy()
disc_count += 1
del images, imgs
costs_avg = costs_avg / disc_count
dev_disc_costs.append(costs_avg)
with open('./defensive_models/dev_disc_costs.pickle', 'wb') as fp:
pickle.dump(dev_disc_costs, fp)
print('batch {:>3}/{:>3}, validation disc cost : {:.4f}'.format(iteration, ITERS, costs_avg))
if __name__ == "__main__":
args = get_args()
device_D = torch.device(args.deviceD)
device_G = torch.device(args.deviceG)
# load generator and discriminator model
netG = Generator()
summary(netG, input_size = (INPUT_LATENT,), device = 'cpu')
netD = Discriminator()
summary(netD, input_size = (3, 32, 32), device = 'cpu')
# set folder to save model checkpoints
model_folder = os.path.abspath('./defensive_models')
if not os.path.exists(model_folder):
os.mkdir(model_folder)
check_point_path = './defensive_models/snapshots.pth'
if os.path.exists(check_point_path):
checkpoint = torch.load(check_point_path)
inital_epoch = checkpoint['epoch']
netG.load_state_dict(checkpoint['netG_state_dict'])
netD.load_state_dict(checkpoint['netD_state_dict'])
netG = netG.to(device_G)
netD = netD.to(device_D)
train_GAN(netD, netG, inital_epoch, args)