-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
242 lines (174 loc) · 9.37 KB
/
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
import os
import argparse
import numpy as np
from itertools import cycle
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets
from torchvision import transforms
from torch.autograd import Variable
import torch.nn.functional as F
from utils import weights_init, matrix_log_density_gaussian
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from mpl_toolkits.mplot3d import Axes3D
from torch.utils.data import DataLoader
from networks import *
from utils import imshow_grid, mse_loss, reparameterize, transform_config, augment_batch, mix_latents, get_augmentations_and_mask
from flags import FLAGS
from sklearn import manifold
from torchvision.utils import save_image
import random
def compute_center_loss(centers, embeddings, labels, num_classes):
latent_labels = torch.LongTensor(labels)
if(FLAGS.cuda):
latent_labels = latent_labels.cuda()
embeddings = embeddings.cuda()
distmat = torch.pow(embeddings, 2).sum(dim=1, keepdim=True).expand(FLAGS.z_num_chunks*FLAGS.batch_size, num_classes) + torch.pow(centers, 2).sum(dim=1, keepdim=True).expand(num_classes, FLAGS.z_num_chunks*FLAGS.batch_size).t()
distmat.addmm_(1, -2, embeddings, centers.t())
classes = torch.arange(num_classes).long()
if(FLAGS.cuda):
classes = classes.cuda()
distmat = distmat.cuda()
labels = latent_labels.unsqueeze(1).expand(FLAGS.batch_size*FLAGS.z_num_chunks, num_classes)
mask = labels.eq(classes.expand(FLAGS.batch_size*FLAGS.z_num_chunks, num_classes))
dist = distmat * mask.float()
loss = dist.clamp(min=1e-12, max=1e+12).sum() / FLAGS.batch_size*FLAGS.z_num_chunks
return loss
if __name__ == '__main__':
torch.autograd.set_detect_anomaly(True)
encoder = Encoder()
encoder.apply(weights_init)
decoder = Decoder()
decoder.apply(weights_init)
cv_network = ContextVector()
cv_network.apply(weights_init)
if FLAGS.load_saved:
encoder.load_state_dict(torch.load(os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.encoder_save)))
decoder.load_state_dict(torch.load(os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.decoder_save)))
cv_network.load_state_dict(torch.load(os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.context_vector_save)))
combine_optimizer = optim.Adam(
list(encoder.parameters()) + list(decoder.parameters()) + list(cv_network.parameters()),
lr = FLAGS.lrate,
betas = (FLAGS.beta_1, FLAGS.beta_2)
)
if torch.cuda.is_available() and not FLAGS.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
if not os.path.exists('checkpoints_'+FLAGS.folder_save):
os.makedirs('checkpoints_'+FLAGS.folder_save)
if not os.path.exists('reconstructed_images_'+FLAGS.folder_save):
os.makedirs('reconstructed_images_'+FLAGS.folder_save)
if not os.path.exists('predicted_images_'+FLAGS.folder_save):
os.makedirs('predicted_images_'+FLAGS.folder_save)
if not os.path.exists('visualizations_'+FLAGS.folder_save):
os.makedirs('visualizations_'+FLAGS.folder_save)
if not os.path.exists('context_vectors_'+FLAGS.folder_save):
os.makedirs('context_vectors_'+FLAGS.folder_save)
if not FLAGS.load_saved:
with open(FLAGS.log_file, 'w') as log:
log.write('Epoch\tIteration\tReconstruction_loss\tKL_divergence_loss\t')
log.write('Generator_loss\tDiscriminator_loss\tDiscriminator_accuracy\n')
print('Loading Sprites Dataset...')
data_dir = '../../2D_Sprites/data/sprites_train/'
transform = transforms.Compose([transforms.Resize((64, 64)), transforms.ToTensor()])
dset = datasets.ImageFolder(data_dir, transform = transform)
sprites = torch.utils.data.DataLoader(dset, batch_size=FLAGS.batch_size, shuffle=True, drop_last=True)
X1 = torch.zeros(FLAGS.batch_size, FLAGS.num_channels, FLAGS.image_size, FLAGS.image_size)
X2 = torch.zeros(FLAGS.batch_size, FLAGS.num_channels, FLAGS.image_size, FLAGS.image_size)
cv_full_view = Variable(torch.zeros((FLAGS.z_num_chunks, FLAGS.c_num_chunks*FLAGS.c_chunk_size)), requires_grad=True)
if FLAGS.cuda:
encoder.cuda()
decoder.cuda()
cv_network.cuda()
X1 = X1.cuda()
X2 = X2.cuda()
cv_full_view = cv_full_view.cuda()
for epoch in range(FLAGS.start_epoch, FLAGS.end_epoch):
print('Epoch #' + str(epoch) + '..........................................................................')
for iteration in range(len(sprites)):
combine_optimizer.zero_grad()
image_batch_1 = next(iter(sprites))[0]
X1.copy_(image_batch_1)
# augmented_batch = augment_batch(X1)
augmented_batch, mask = get_augmentations_and_mask(X1)
encoder_outputs = encoder(X1)
specified_latents, unspecified_variational_latent, mu, logvar = encoder_outputs[0], encoder_outputs[1], encoder_outputs[2], encoder_outputs[3]
augmented_encoder_outputs = encoder(augmented_batch)
aug_specified_latents, aug_unspecified_variational_latent, aug_mu, aug_logvar = augmented_encoder_outputs[0], augmented_encoder_outputs[1], augmented_encoder_outputs[2], augmented_encoder_outputs[3]
# kl loss
kl_loss = FLAGS.kl_divergence_coef * (-0.5 * (torch.sum(1 + logvar - mu.pow(2) - logvar.exp())))
kl_loss /= FLAGS.batch_size * FLAGS.num_channels * FLAGS.image_size * FLAGS.image_size
# reconstruction loss batch
image_batch_recon = decoder(specified_latents, unspecified_variational_latent)
recon_loss = mse_loss(image_batch_recon, X1)
gen_loss = recon_loss + kl_loss
# center loss
cv, cv_full_view_ = cv_network(specified_latents)
transformed_chunks = torch.zeros(FLAGS.batch_size*FLAGS.z_num_chunks, FLAGS.c_num_chunks*FLAGS.c_chunk_size)
with torch.no_grad():
for i in range(FLAGS.batch_size):
transformed_temp_chunks = []
for j in range(FLAGS.z_num_chunks):
curr_tensor = specified_latents[j][i]
curr_tensor = curr_tensor.repeat(FLAGS.batch_size).view(FLAGS.batch_size, FLAGS.z_chunk_size)
transformed_temp_chunks.append(curr_tensor)
curr_cv, curr_cv_full_view = cv_network(transformed_temp_chunks)
transformed_chunks[i*FLAGS.z_num_chunks : (i+1)*FLAGS.z_num_chunks] = curr_cv_full_view
lab_list = [i for i in range(FLAGS.z_num_chunks)]
transformed_chunks_labels = lab_list*FLAGS.batch_size
if(cv_full_view.sum().item()==0):
loc_cv_update = Variable(cv_full_view_.clone(), requires_grad=True)
else:
loc_cv_update = Variable(cv_full_view.clone(), requires_grad=True)
center_loss = compute_center_loss(loc_cv_update, transformed_chunks, transformed_chunks_labels, num_classes=FLAGS.z_num_chunks)
# masked augmentation loss
aug_loss = 0
for i, val in enumerate(mask[:-1]):
val = 1-val
aug_loss+=val*mse_loss(aug_specified_latents[i], specified_latents[i])
aug_loss+=mse_loss(aug_unspecified_variational_latent, unspecified_variational_latent)
# total losses, backprop and optimization
loss = gen_loss + center_loss + aug_loss
if(cv_full_view.sum().item()!=0):
# SUBSEQUENT UPDATES AFTER FIRST UPDATE
loc_cv_update.retain_grad() # retain gradients of the CV for CV update
center_loss.backward(retain_graph=True)
cv_gradient = loc_cv_update.grad.clone() # ensure gradient is not None, save it before we detach from computation graph
cv_full_view_ = cv_full_view_.detach() # detach CV var from computation graph so as to not interfere with other gradients
cv_full_view = cv_full_view.detach() # detach CV var from computation graph so as to not interfere with other gradients
cv_full_view = cv_full_view - FLAGS.center_loss_lrate*cv_gradient # CV update
loc_cv_update = loc_cv_update.detach() # detach local CV var from computation graph so as to not interfere with other gradients (just in case issues occur)
else:
# FIRST UPDATE
cv_full_view = cv_full_view_
# detach all vars from computation graph so as to not interfere with other gradients
cv_full_view = cv_full_view.detach()
cv_full_view_ = cv_full_view_.detach()
loss.backward()
combine_optimizer.step()
if (iteration + 1) % 50 == 0:
print('')
print('----------------------------------------------------------------------')
print('Epoch #' + str(epoch))
print('Iteration #' + str(iteration))
print('')
print('Reconstruction loss: ' + str(recon_loss.data.storage().tolist()[0]))
print('KL-Divergence loss: ' + str(kl_loss.data.storage().tolist()[0]))
print('Center loss: '+str(center_loss.item()))
print('Mask Augmentation loss: '+str(aug_loss.item()))
print()
print('Generator loss: ' + str(gen_loss.data.storage().tolist()[0]))
print('----------------------------------------------------------------------')
# save model after every 5 epochs
if (epoch + 1) % 5 == 0 or (epoch + 1) == FLAGS.end_epoch:
torch.save(encoder.state_dict(), os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.encoder_save))
torch.save(decoder.state_dict(), os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.decoder_save))
torch.save(cv_network.state_dict(), os.path.join('checkpoints_'+FLAGS.folder_save, FLAGS.context_vector_save))
image_batch_1 = next(iter(sprites))[0]
X1.copy_(image_batch_1)
latent = encoder(X1)
dec = decoder(latent[0], latent[1])
save_image(torch.cat([X1[:8, :, :, :], dec[:8, :, :, :]]), './reconstructed_images_'+FLAGS.folder_save+'/'+str(epoch)+'.png', nrow=4)