-
Notifications
You must be signed in to change notification settings - Fork 0
/
acgan_train.py
375 lines (294 loc) · 14.1 KB
/
acgan_train.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
import os
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
from torchvision import datasets
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
from shutil import rmtree
import args
import util
from models import acgan
from eval import fid_score
def set_random_seed(seed=23):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
def main():
set_random_seed()
# Arguments
opt = args.get_setup_args()
#cuda = True if torch.cuda.is_available() else False
device, gpu_ids = util.get_available_devices()
num_classes = opt.num_classes
noise_dim = opt.latent_dim + opt.num_classes
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
train_images_path = os.path.join(opt.data_path, "train")
val_images_path = os.path.join(opt.data_path, "val")
output_model_path = os.path.join(opt.output_path, opt.version)
output_train_images_path = os.path.join(opt.output_path, opt.version, "train")
output_sample_images_path = os.path.join(opt.output_path, opt.version, "sample")
os.makedirs(output_train_images_path, exist_ok=True)
os.makedirs(output_sample_images_path, exist_ok=True)
train_set = datasets.ImageFolder(root=train_images_path,
transform=transforms.Compose([
transforms.Resize((opt.img_size, opt.img_size)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))
dataloader = torch.utils.data.DataLoader(train_set,
batch_size=opt.batch_size,
shuffle=True,
num_workers=opt.num_workers)
gen = acgan.Generator(noise_dim).to(device)
disc = acgan.Discriminator(num_classes).to(device)
gen.apply(weights_init)
disc.apply(weights_init)
optimG = optim.Adam(gen.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))
optimD = optim.Adam(disc.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))
adversarial_loss = torch.nn.BCELoss()
auxiliary_loss = torch.nn.CrossEntropyLoss()
real_label_val = 1
real_label_low = 0.75
real_label_high = 1.0
fake_label_val = 0
# Probability of adding label noise during discriminator training
label_noise_prob = 0.05
# Keep track of losses, accuracy, FID
G_losses = []
D_losses = []
D_acc = []
FIDs = []
val_epochs = []
def print_labels():
for class_name in train_set.classes:
print("{} -> {}".format(class_name, train_set.class_to_idx[class_name]))
def eval_fid(gen_images_path, eval_images_path):
print("Calculating FID...")
fid = fid_score.calculate_fid_given_paths((gen_images_path, eval_images_path), opt.batch_size, device)
return fid
def validate(keep_images=True):
# Put G in eval mode
gen.eval()
val_set = datasets.ImageFolder(root=val_images_path,
transform=transforms.Compose([
transforms.Resize((opt.img_size, opt.img_size)),
transforms.ToTensor()
]))
val_loader = torch.utils.data.DataLoader(val_set,
batch_size=opt.batch_size,
shuffle=True,
num_workers=opt.num_workers)
output_images_path = os.path.join(opt.output_path, opt.version, "val")
os.makedirs(output_images_path, exist_ok=True)
output_source_images_path = val_images_path + "_" + str(opt.img_size)
source_images_available = True
if (not os.path.exists(output_source_images_path)):
os.makedirs(output_source_images_path)
source_images_available = False
images_done = 0
for _, data in enumerate(val_loader, 0):
images, labels = data
batch_size = images.size(0)
noise = torch.randn((batch_size, opt.latent_dim)).to(device)
labels = torch.randint(0, num_classes, (batch_size,)).to(device)
labels_onehot = F.one_hot(labels, num_classes)
noise = torch.cat((noise, labels_onehot.to(dtype=torch.float)), 1)
gen_images = gen(noise)
for i in range(images_done, images_done + batch_size):
vutils.save_image(gen_images[i - images_done, :, :, :], "{}/{}.jpg".format(output_images_path, i), normalize=True)
if (not source_images_available):
vutils.save_image(images[i - images_done, :, :, :], "{}/{}.jpg".format(output_source_images_path, i), normalize=True)
images_done += batch_size
# Put G back in train mode
gen.train()
fid = eval_fid(output_images_path, output_source_images_path)
if (not keep_images):
print("Deleting images generated for validation...")
rmtree(output_images_path)
return fid
def sample_images(num_images, batches_done):
# Sample noise
z = torch.randn((num_classes * num_images, opt.latent_dim)).to(device)
# Get labels ranging from 0 to n_classes for n rows
labels = torch.zeros((num_classes * num_images,), dtype=torch.long).to(device)
for i in range(num_classes):
for j in range(num_images):
labels[i*num_images + j] = i
labels_onehot = F.one_hot(labels, num_classes)
z = torch.cat((z, labels_onehot.to(dtype=torch.float)), 1)
sample_imgs = gen(z)
vutils.save_image(sample_imgs.data, "{}/{}.png".format(output_sample_images_path, batches_done), nrow=num_images, padding=2, normalize=True)
def save_loss_plot(path):
plt.figure(figsize=(10,5))
plt.title("Generator and Discriminator Loss During Training")
plt.plot(G_losses,label="G")
plt.plot(D_losses,label="D")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
plt.savefig(path)
plt.close()
def save_acc_plot(path):
plt.figure(figsize=(10,5))
plt.title("Discriminator Accuracy")
plt.plot(D_acc)
plt.xlabel("iterations")
plt.ylabel("accuracy")
plt.savefig(path)
plt.close()
def save_fid_plot(FIDs, epochs, path):
#N = len(FIDs)
plt.figure(figsize=(10,5))
plt.title("FID on Validation Set")
plt.plot(epochs, FIDs)
plt.xlabel("epochs")
plt.ylabel("FID")
#plt.xticks([i * 49 for i in range(1, N+1)])
plt.savefig(path)
plt.close()
print("Label to class mapping:")
print_labels()
for epoch in range(1, opt.num_epochs + 1):
for i, data in enumerate(dataloader, 0):
images, class_labels = data
images = images.to(device)
class_labels = class_labels.to(device)
batch_size = images.size(0)
real_label_smooth = (real_label_low - real_label_high) * torch.rand((batch_size,), device=device) + real_label_high
real_label = torch.full((batch_size,), real_label_val, device=device)
fake_label = torch.full((batch_size,), fake_label_val, device=device)
############################
# Train Discriminator
###########################
## Train with all-real batch
optimD.zero_grad()
real_pred, real_aux = disc(images)
mask = torch.rand((batch_size,), device=device) <= label_noise_prob
mask = mask.type(torch.float)
noisy_label = torch.mul(1-mask, real_label_smooth) + torch.mul(mask, fake_label)
d_real_loss = (adversarial_loss(real_pred, noisy_label) + auxiliary_loss(real_aux, class_labels)) / 2
# Train with fake batch
noise = torch.randn((batch_size, opt.latent_dim)).to(device)
gen_class_labels = torch.randint(0, num_classes, (batch_size,)).to(device)
gen_class_labels_onehot = F.one_hot(gen_class_labels, num_classes)
noise = torch.cat((noise, gen_class_labels_onehot.to(dtype=torch.float)), 1)
gen_images = gen(noise)
fake_pred, fake_aux = disc(gen_images.detach())
mask = torch.rand((batch_size,), device=device) <= label_noise_prob
mask = mask.type(torch.float)
noisy_label = torch.mul(1-mask, fake_label) + torch.mul(mask, real_label_smooth)
d_fake_loss = (adversarial_loss(fake_pred, noisy_label) + auxiliary_loss(fake_aux, gen_class_labels)) / 2
# Total discriminator loss
d_loss = (d_real_loss + d_fake_loss) / 2
# Calculate discriminator accuracy
pred = np.concatenate([real_aux.data.cpu().numpy(), fake_aux.data.cpu().numpy()], axis=0)
gt = np.concatenate([class_labels.data.cpu().numpy(), gen_class_labels.data.cpu().numpy()], axis=0)
d_acc = np.mean(np.argmax(pred, axis=1) == gt)
d_loss.backward()
optimD.step()
############################
# Train Generator
###########################
optimG.zero_grad()
validity, aux_scores = disc(gen_images)
g_loss = 0.5 * (adversarial_loss(validity, real_label) + auxiliary_loss(aux_scores, gen_class_labels))
g_loss.backward()
optimG.step()
# Save losses and accuracy for plotting
G_losses.append(g_loss.item())
D_losses.append(d_loss.item())
D_acc.append(d_acc)
# Output training stats
if i % opt.print_every == 0:
print("[Epoch %d/%d] [Batch %d/%d] [D loss: %.4f, acc: %d%%] [G loss: %.4f]"
% (epoch, opt.num_epochs, i, len(dataloader), d_loss.item(), 100 * d_acc, g_loss.item())
)
batches_done = epoch * len(dataloader) + i
# Generate and save sample images
if (batches_done % opt.sample_interval == 0) or ((epoch == opt.num_epochs-1) and (i == len(dataloader)-1)):
# Put G in eval mode
gen.eval()
with torch.no_grad():
sample_images(opt.num_sample_images, batches_done)
vutils.save_image(gen_images.data[:36], "{}/{}.png".format(output_train_images_path, batches_done), nrow=6, padding=2, normalize=True)
# Put G back in train mode
gen.train()
# Save model checkpoint
if (epoch != opt.num_epochs and epoch % opt.checkpoint_epochs == 0):
print("Checkpoint at epoch {}".format(epoch))
print("Saving G & D loss plot...")
save_loss_plot(os.path.join(opt.output_path, opt.version, "loss_plot_{}.png".format(epoch)))
print("Saving D accuracy plot...")
save_acc_plot(os.path.join(opt.output_path, opt.version, "accuracy_plot_{}.png".format(epoch)))
print("Validating model...")
with torch.no_grad():
fid = validate(keep_images=False)
print("Validation FID: {}".format(fid))
with open(os.path.join(opt.output_path, opt.version, "FIDs.txt"), "a") as f:
f.write("Epoch: {}, FID: {}\n".format(epoch, fid))
FIDs.append(fid)
val_epochs.append(epoch)
print("Saving FID plot...")
save_fid_plot(FIDs, val_epochs, os.path.join(opt.output_path, opt.version, "fid_plot_{}.png".format(epoch)))
print("Saving model checkpoint...")
torch.save({
'epoch': epoch,
'g_state_dict': gen.state_dict(),
'd_state_dict': disc.state_dict(),
'g_optimizer_state_dict': optimG.state_dict(),
'd_optimizer_state_dict': optimD.state_dict(),
'g_loss': g_loss.item(),
'd_loss': d_loss.item(),
'd_accuracy': d_acc,
'val_fid': fid
}, os.path.join(output_model_path, "model_checkpoint_{}.tar".format(epoch)))
print("Saving final G & D loss plot...")
save_loss_plot(os.path.join(opt.output_path, opt.version, "loss_plot.png"))
print("Done!")
print("Saving final D accuracy plot...")
save_acc_plot(os.path.join(opt.output_path, opt.version, "accuracy_plot.png"))
print("Done!")
print("Validating final model...")
gen.eval()
with torch.no_grad():
fid = validate()
print("Final Validation FID: {}".format(fid))
with open(os.path.join(opt.output_path, opt.version, "FIDs.txt"), "a") as f:
f.write("Epoch: {}, FID: {}\n".format(epoch, fid))
FIDs.append(fid)
val_epochs.append(epoch)
print("Saving final FID plot...")
save_fid_plot(FIDs, val_epochs, os.path.join(opt.output_path, opt.version, "fid_plot"))
print("Done!")
print("Saving final model...")
torch.save({
'epoch': epoch,
'g_state_dict': gen.state_dict(),
'd_state_dict': disc.state_dict(),
'g_optimizer_state_dict': optimG.state_dict(),
'd_optimizer_state_dict': optimD.state_dict(),
'g_loss': g_loss.item(),
'd_loss': d_loss.item(),
'd_accuracy': d_acc,
'val_fid': fid
}, os.path.join(output_model_path, "model.tar"))
print("Done!")
if __name__ == '__main__':
main()