-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan_utils.py
168 lines (126 loc) · 6.15 KB
/
gan_utils.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
import time
from matplotlib import pyplot as plt
import torch
import torch.nn.functional as F
import torchvision
import torch.autograd
import numpy as np
def train_gan(num_epochs, model, optimizer_gen, optimizer_discr,
latent_dim, device, train_loader, loss_fn=None,
logging_interval=100,
save_model=None):
log_dict = {'train_generator_loss_per_batch': [],
'train_discriminator_loss_per_batch': [],
'train_discriminator_real_acc_per_batch': [],
'train_discriminator_fake_acc_per_batch': [],
'images_from_noise_per_epoch': []}
if loss_fn is None:
loss_fn = F.binary_cross_entropy_with_logits
# Batch of latent (noise) vectors for
# evaluating / visualizing the training progress
# of the generator
fixed_noise = torch.randn(64, latent_dim, 1, 1, device=device) # format NCHW
start_time = time.time()
for epoch in range(num_epochs):
model.train()
for batch_idx ,features in enumerate(train_loader):
batch_size = features.size(0)
# real images
real_images = features.to(device)
real_labels = torch.ones(batch_size, device=device) # real label = 1
# generated (fake) images
noise = torch.randn(batch_size, latent_dim, 1, 1, device=device) # format NCHW
fake_images = model.gen_forward(noise)
fake_labels = torch.zeros(batch_size, device=device) # fake label = 0
flipped_fake_labels = real_labels # here, fake label = 1
# --------------------------
# Train Discriminator
# --------------------------
optimizer_discr.zero_grad()
# get discriminator loss on real images
discr_pred_real = model.disc_forward(real_images).view(-1) # Nx1 -> N
real_loss = loss_fn(discr_pred_real, real_labels)
# real_loss.backward()
# get discriminator loss on fake images
discr_pred_fake = model.disc_forward(fake_images.detach()).view(-1)
fake_loss = loss_fn(discr_pred_fake, fake_labels)
# fake_loss.backward()
# combined loss
discr_loss = 0.5*(real_loss + fake_loss)
discr_loss.backward()
optimizer_discr.step()
# --------------------------
# Train Generator
# --------------------------
optimizer_gen.zero_grad()
# get discriminator loss on fake images with flipped labels
discr_pred_fake = model.disc_forward(fake_images).view(-1)
gener_loss = loss_fn(discr_pred_fake, flipped_fake_labels)
gener_loss.backward()
optimizer_gen.step()
# --------------------------
# Logging
# --------------------------
log_dict['train_generator_loss_per_batch'].append(gener_loss.item())
log_dict['train_discriminator_loss_per_batch'].append(discr_loss.item())
predicted_labels_real = torch.where(discr_pred_real.detach() > 0., 1., 0.)
predicted_labels_fake = torch.where(discr_pred_fake.detach() > 0., 1., 0.)
acc_real = (predicted_labels_real == real_labels).float().mean()*100.
acc_fake = (predicted_labels_fake == fake_labels).float().mean()*100.
log_dict['train_discriminator_real_acc_per_batch'].append(acc_real.item())
log_dict['train_discriminator_fake_acc_per_batch'].append(acc_fake.item())
if not batch_idx % logging_interval:
print('Epoch: %03d/%03d | Batch %03d/%03d | Gen/Dis Loss: %.4f/%.4f'
% (epoch+1, num_epochs, batch_idx,
len(train_loader), gener_loss.item(), discr_loss.item()))
### Save images for evaluation
with torch.no_grad():
fake_images = model.gen_forward(fixed_noise).detach().cpu()
log_dict['images_from_noise_per_epoch'].append(
torchvision.utils.make_grid(fake_images, padding=2, normalize=True))
print('Time elapsed: %.2f min' % ((time.time() - start_time)/60))
print('Total Training Time: %.2f min' % ((time.time() - start_time)/60))
if save_model is not None:
torch.save(model.state_dict(), save_model)
return log_dict
def plot_multiple_training_losses(losses_list, num_epochs,
averaging_iterations=100, custom_labels_list=None):
for i,_ in enumerate(losses_list):
if not len(losses_list[i]) == len(losses_list[0]):
raise ValueError('All loss tensors need to have the same number of elements.')
if custom_labels_list is None:
custom_labels_list = [str(i) for i,_ in enumerate(custom_labels_list)]
iter_per_epoch = len(losses_list[0]) // num_epochs
plt.figure()
ax1 = plt.subplot(1, 1, 1)
for i, minibatch_loss_tensor in enumerate(losses_list):
ax1.plot(range(len(minibatch_loss_tensor)),
(minibatch_loss_tensor),
label=f'Minibatch Loss{custom_labels_list[i]}')
ax1.set_xlabel('Iterations')
ax1.set_ylabel('Loss')
ax1.plot(np.convolve(minibatch_loss_tensor,
np.ones(averaging_iterations,)/averaging_iterations,
mode='valid'),
color='black')
if len(losses_list[0]) < 1000:
num_losses = len(losses_list[0]) // 2
else:
num_losses = 1000
maxes = [np.max(losses_list[i][num_losses:]) for i,_ in enumerate(losses_list)]
ax1.set_ylim([0, np.max(maxes)*1.5])
ax1.legend()
###################
# Set scond x-axis
ax2 = ax1.twiny()
newlabel = list(range(num_epochs+1))
newpos = [e*iter_per_epoch for e in newlabel]
ax2.set_xticks(newpos[::10])
ax2.set_xticklabels(newlabel[::10])
ax2.xaxis.set_ticks_position('bottom')
ax2.xaxis.set_label_position('bottom')
ax2.spines['bottom'].set_position(('outward', 45))
ax2.set_xlabel('Epochs')
ax2.set_xlim(ax1.get_xlim())
###################
plt.tight_layout()