-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
298 lines (253 loc) · 9.69 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
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
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from datetime import datetime
from matplotlib import pyplot as plt
import pytorch_ssim
from pathlib import Path
import numpy as np
from dataset.dataset_class import VidDataSet, MyNewDataset
from loss.loss_discriminator import LossDSCreal, LossDSCfake
from loss.loss_generator import LossG
from network.model import Generator, Embedder, Discriminator, PartialInceptionNetwork
from network.utils import calculate_fid
from config import (
device,
cpu,
path_to_chkpt,
path_to_backup,
num_epochs,
VGGFace_body_path,
VGGFace_weight_path,
)
"""Create dataset and net"""
dataset = MyNewDataset(path_to_images='clean_dataset', device=device)
dataLoader = DataLoader(dataset, batch_size=2, shuffle=True)
# Initialize SummaryWriter for tensorboard
RUN_NAME = datetime.now().strftime(format='%b%d_%H-%M-%S')
writer = SummaryWriter(log_dir=f'runs/{RUN_NAME}')
# Initialize PartialInceptionNetwork for calculating FID score
inception = PartialInceptionNetwork().eval()
# Initialize Cosine similarity for calculating CSIM score
cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
G = Generator(224).to(device)
E = Embedder(224).to(device)
D = Discriminator(dataset.__len__()).to(device)
G.train()
E.train()
D.train()
optimizerG = optim.Adam(params=list(E.parameters()) + list(G.parameters()), lr=5e-5)
optimizerD = optim.Adam(params=D.parameters(), lr=2e-4)
"""Criterion"""
criterionG = LossG(
VGGFace_body_path=VGGFace_body_path,
VGGFace_weight_path=VGGFace_weight_path,
device=device,
)
criterionDreal = LossDSCreal()
criterionDfake = LossDSCfake()
"""Training init"""
epochCurrent = epoch = i_batch = 0
lossesG = []
lossesD = []
i_batch_current = 0
# initiate checkpoint if inexistant
if not Path(path_to_chkpt).is_file():
print('Initiating new checkpoint...')
torch.save(
{
'epoch': epoch,
'lossesG': lossesG,
'lossesD': lossesD,
'E_state_dict': E.state_dict(),
'G_state_dict': G.state_dict(),
'D_state_dict': D.state_dict(),
'optimizerG_state_dict': optimizerG.state_dict(),
'optimizerD_state_dict': optimizerD.state_dict(),
'num_vid': dataset.__len__(),
'i_batch': i_batch,
},
path_to_chkpt,
)
print('...Done')
"""Loading from past checkpoint"""
checkpoint = torch.load(path_to_chkpt, map_location=cpu)
E.load_state_dict(checkpoint['E_state_dict'])
G.load_state_dict(checkpoint['G_state_dict'])
D.load_state_dict(checkpoint['D_state_dict'])
optimizerG.load_state_dict(checkpoint['optimizerG_state_dict'])
optimizerD.load_state_dict(checkpoint['optimizerD_state_dict'])
epochCurrent = checkpoint['epoch']
lossesG = checkpoint['lossesG']
lossesD = checkpoint['lossesD']
num_vid = checkpoint['num_vid']
i_batch_current = checkpoint['i_batch'] + 1
G.train()
E.train()
D.train()
"""Training"""
batch_start = datetime.now()
for epoch in range(epochCurrent, num_epochs):
for i_batch, (f_lm, x, g_y, i) in enumerate(dataLoader, start=i_batch_current):
if i_batch > len(dataLoader):
i_batch_current = 0
break
with torch.autograd.enable_grad():
# zero the parameter gradients
optimizerG.zero_grad()
optimizerD.zero_grad()
# forward
# Calculate average encoding vector for video
f_lm_compact = f_lm.view(
-1, f_lm.shape[-4], f_lm.shape[-3], f_lm.shape[-2], f_lm.shape[-1]
) # BxK,2,3,224,224
e_vectors = E(
f_lm_compact[:, 0, :, :, :], f_lm_compact[:, 1, :, :, :]
) # BxK,512,1
e_vectors = e_vectors.view(-1, f_lm.shape[1], 512, 1) # B,K,512,1
e_hat = e_vectors.mean(dim=1)
# train G and D
x_hat = G(g_y, e_hat)
r_hat, D_hat_res_list = D(x_hat, g_y, i)
r, D_res_list = D(x, g_y, i)
lossG = criterionG(
x, x_hat, r_hat, D_res_list, D_hat_res_list, e_vectors, D.W_i, i
)
lossDfake = criterionDfake(r_hat)
lossDreal = criterionDreal(r)
loss = lossDreal + lossDfake + lossG
loss.backward(retain_graph=False)
optimizerG.step()
optimizerD.step()
# train D again
optimizerG.zero_grad()
optimizerD.zero_grad()
x_hat.detach_()
r_hat, D_hat_res_list = D(x_hat, g_y, i)
r, D_res_list = D(x, g_y, i)
lossDfake = criterionDfake(r_hat)
lossDreal = criterionDreal(r)
lossD = lossDreal + lossDfake
lossD.backward(retain_graph=False)
optimizerD.step()
# Output training stats
if i_batch % 10 == 0:
batch_end = datetime.now()
avg_time = (batch_end - batch_start) / 10
print('\n\navg batch time for batch size of', x.shape[0], ':', avg_time)
batch_start = datetime.now()
print(
'[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(y)): %.4f'
% (
epoch,
num_epochs,
i_batch,
len(dataLoader),
lossD.item(),
lossG.item(),
r.mean(),
r_hat.mean(),
)
)
# Initialize empty lists for storing metrics
ssim_vals = []
csim_vals = []
fid_vals = []
# Calculate the metrics
fid = calculate_fid(x, x_hat, inception)
fid_vals.append(fid)
for img_no in range(x_hat.shape[0]):
x_temp = x.transpose(1, 3)[img_no].unsqueeze(0)
x_hat_temp = x_hat.transpose(1, 3)[img_no].unsqueeze(0)
# calcule the batch avg SSIM score
ssim_val = pytorch_ssim.ssim(x_temp, x_hat_temp)
ssim_vals.append(ssim_val)
# calcule the batch avg CSIM score
csim_val = cos(x_temp, x_hat_temp)
csim_vals.append(csim_val)
# Calculate the mean of metrics stored in each list
ssim_score = torch.mean(torch.stack(ssim_vals))
csim_score = torch.cat((csim_vals[0], csim_vals[1]), dim=1)
fid_score = torch.mean(torch.stack(fid_vals))
# Log metrics and losses in tensorboard
writer.add_scalar('SSIM', ssim_score, i_batch)
writer.add_scalar('CSIM', ssim_score, i_batch)
writer.add_scalar('FID', ssim_score, i_batch)
writer.add_scalar('LossD', lossD.item(), i_batch)
writer.add_scalar('LossG', lossG.item(), i_batch)
plt.clf()
out = x_hat.transpose(1, 3)[0]
for img_no in range(1, x_hat.shape[0]):
out = torch.cat((out, x_hat.transpose(1, 3)[img_no]), dim=1)
out = out.type(torch.int32).to(cpu).numpy()
writer.add_image(
'test_x_hat_image',
np.transpose(out.astype("uint8"), (2, 0, 1)),
i_batch,
)
plt.imshow(out)
plt.show()
plt.clf()
out = x.transpose(1, 3)[0]
for img_no in range(1, x.shape[0]):
out = torch.cat((out, x.transpose(1, 3)[img_no]), dim=1)
out = out.type(torch.int32).to(cpu).numpy()
writer.add_image(
'test_x_image', np.transpose(out.astype("uint8"), (2, 0, 1)), i_batch
)
plt.imshow(out)
plt.show()
plt.clf()
out = g_y.transpose(1, 3)[0]
for img_no in range(1, g_y.shape[0]):
out = torch.cat((out, g_y.transpose(1, 3)[img_no]), dim=1)
out = out.type(torch.int32).to(cpu).numpy()
writer.add_image(
'test_g_y_image', np.transpose(out.astype("uint8"), (2, 0, 1)), i_batch
)
plt.imshow(out)
plt.show()
if i_batch % 100 == 99:
lossesD.append(lossD.item())
lossesG.append(lossG.item())
plt.clf()
plt.plot(lossesG) # blue
plt.plot(lossesD) # orange
plt.show()
print('Saving latest...')
torch.save(
{
'epoch': epoch,
'lossesG': lossesG,
'lossesD': lossesD,
'E_state_dict': E.state_dict(),
'G_state_dict': G.state_dict(),
'D_state_dict': D.state_dict(),
'optimizerG_state_dict': optimizerG.state_dict(),
'optimizerD_state_dict': optimizerD.state_dict(),
'num_vid': dataset.__len__(),
'i_batch': i_batch,
},
path_to_chkpt,
)
print('...Done saving latest')
if i_batch % 500 == 499:
print('Saving latest...')
torch.save(
{
'epoch': epoch,
'lossesG': lossesG,
'lossesD': lossesD,
'E_state_dict': E.state_dict(),
'G_state_dict': G.state_dict(),
'D_state_dict': D.state_dict(),
'optimizerG_state_dict': optimizerG.state_dict(),
'optimizerD_state_dict': optimizerD.state_dict(),
'num_vid': dataset.__len__(),
'i_batch': i_batch,
},
path_to_backup,
)
print('...Done saving latest')
writer.close()