-
Notifications
You must be signed in to change notification settings - Fork 2
/
finetuning_training.py
251 lines (207 loc) · 7.6 KB
/
finetuning_training.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
import torch
from torch.utils.data import DataLoader
import torch.optim as optim
import numpy as np
from datetime import datetime
from matplotlib import pyplot as plt
from pathlib import Path
from dataset.dataset_class import FineTuningImagesDataset, FineTuningVideoDataset
from network.model import Generator, Discriminator
from loss.loss_discriminator import LossDSCreal, LossDSCfake
from loss.loss_generator import LossGF
from network.utils import calculate_fid
from config import (
device,
cpu,
path_to_chkpt,
path_to_video,
path_to_images,
path_to_save,
path_to_e_hat_video,
VGGFace_body_path,
VGGFace_weight_path,
)
"""Create dataset and net"""
choice = ''
while choice != '0' and choice != '1':
choice = input(
'What source to finetune on?\n0: Video\n1: Images\n\nEnter number\n>>'
)
if choice == '0': # video
dataset = FineTuningVideoDataset(path_to_video, device)
else: # Images
dataset = FineTuningImagesDataset(path_to_images, device)
dataLoader = DataLoader(dataset, batch_size=2, shuffle=False)
# 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)
e_hat = torch.load(path_to_e_hat_video, map_location=cpu)
e_hat = e_hat['e_hat']
G = Generator(256, finetuning=True, e_finetuning=e_hat)
D = Discriminator(dataset.__len__(), finetuning=True, e_finetuning=e_hat)
G.train()
D.train()
optimizerG = optim.Adam(params=G.parameters(), lr=5e-5)
optimizerD = optim.Adam(params=D.parameters(), lr=2e-4)
"""Criterion"""
criterionG = LossGF(
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
num_epochs = 40
# Warning if checkpoint inexistant
if not Path(path_to_chkpt).is_file():
print('ERROR: cannot find checkpoint')
"""Loading from past checkpoint"""
checkpoint = torch.load(path_to_chkpt, map_location=cpu)
checkpoint['D_state_dict']['W_i'] = torch.rand(
512, dataset.__len__()
) # change W_i for finetuning
G.load_state_dict(checkpoint['G_state_dict'])
D.load_state_dict(checkpoint['D_state_dict'])
"""Change to finetuning mode"""
G.finetuning_init()
D.finetuning_init()
G.to(device)
D.to(device)
"""Training"""
batch_start = datetime.now()
for epoch in range(num_epochs):
for i_batch, (x, g_y) in enumerate(dataLoader):
with torch.autograd.enable_grad():
# zero the parameter gradients
optimizerG.zero_grad()
optimizerD.zero_grad()
# forward
# train G and D
x_hat = G(g_y, e_hat)
r_hat, D_hat_res_list = D(x_hat, g_y, i=0)
r, D_res_list = D(x, g_y, i=0)
lossG = criterionG(x, x_hat, r_hat, D_res_list, D_hat_res_list)
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=0)
r, D_res_list = D(x, g_y, i=0)
lossDfake = criterionDfake(r_hat)
lossDreal = criterionDreal(r)
lossD = lossDreal + lossDfake
lossD.backward(retain_graph=False)
optimizerD.step()
# Output training stats
if epoch % 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()
lossesD.append(lossD.item())
lossesG.append(lossG.item())
writer.close()
plt.clf()
plt.plot(lossesG) # blue
plt.plot(lossesD) # orange
plt.show()
print('Saving finetuned model...')
torch.save(
{
'epoch': epoch,
'lossesG': lossesG,
'lossesD': lossesD,
'G_state_dict': G.state_dict(),
'D_state_dict': D.state_dict(),
'optimizerG_state_dict': optimizerG.state_dict(),
'optimizerD_state_dict': optimizerD.state_dict(),
},
path_to_save,
)
print('...Done saving latest')