-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
261 lines (187 loc) · 7.67 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
import torch
import torch.nn as nn
from model import *
from tqdm import tqdm
from dataset import *
from json import dumps
import os
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def train_eval(vgg, unet, loss_f=F_loss, lam=0.05):
epochs = 100
lr = 1e-4
wd = 1e-4
bs = 2 #batch size
train_logs = []
val_logs = []
train_ds, test_ds = create_dataset() #path is already default
train_dl = torch.utils.data.DataLoader(train_ds,batch_size=bs, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_ds,batch_size=1, shuffle=True)
optimizer = torch.optim.Adam(unet.parameters(), lr=lr, weight_decay=wd)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer=optimizer, T_max=epochs, eta_min=0, verbose=True)
max_psnr = -float('inf')
for epoch in range(epochs):
torch.cuda.empty_cache()
train_logs.append(train_epoch(vgg, unet, train_dl, optimizer, loss_f, epoch, epochs, lam).clone().detach().to('cpu'))
torch.cuda.empty_cache()
t = valid_epoch(vgg, unet, test_dl, loss_f, epoch, epochs, None)
val_logs.append(t.clone().detach().to('cpu'))
#torch.cuda.empty_cache()
#val_logs.append(valid_epoch(vgg, unet, test_dl, loss_f, epoch, epochs, None))
scheduler.step()
#loss = min_loss #TODO: remove this line
psnr = val_logs[-1]
if (psnr >= max_psnr):
torch.save({
'epoch': epoch,
'model_state_dict': unet.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'psnr': psnr,
'loss': train_logs[-1],
}, './checkpoint.pt')
max_psnr = psnr
return train_logs, val_logs
'''
Returns the validation loss for each epoch (no accuracy measure, but can add PSNR measure)
'''
def valid_epoch(vgg, unet, test_dl, loss_func, epoch, epochs, optimizer):
total_loss = 0
total_psnr = 0
total_samples = 0
bar = tqdm.tqdm(test_dl)
for x1, x2, gt in bar:
torch.cuda.empty_cache()
unet.train()
x1, gt = x1.to(device), gt.to(device)
n = x1.shape[0]
total_samples += n
y1 = unet(x1)
total_loss += optimize(vgg, y1, gt, optimizer, loss_func)
total_psnr += psnr(y1, gt)
# Add evaluation for PSNR and FSNR and return these values
bar.set_description(f'Validation:[{epoch+1}/{epochs}] psnr:{total_psnr/ total_samples}', refresh=True)
return (total_psnr/ total_samples).to('cpu')
def eval(model, im1, im2, loss_func):
res1 = model(im1)
res2 = model(im2)
loss = loss_func(res1, res2)
return loss
def train_epoch(vgg, unet, train_dl, optimizer, loss_func, epoch, epochs, lam):
total_loss = 0
total_samples = 0
bar = tqdm.tqdm(train_dl)
for x1, x2, gt in bar:
torch.cuda.empty_cache()
unet.train()
optimizer.zero_grad()
x1, x2, gt = x1.to(device), x2.to(device), gt.to(device)
n = x1.shape[0]
total_samples += n
y1 = unet(x1)
y2 = unet(x2)
t1 = vgg(y1)
t2 = vgg(y2)
t3 = vgg(gt)
loss = loss_func(t3, t1) + loss_func(t3, t2) + lam * loss_func(t1, t2)
loss.backward()
optimizer.step()
total_loss += loss
#total_loss += optimize(vgg, y1, gt, optimizer, loss_func)
#total_loss += optimize(vgg, y2, gt, optimizer, loss_func)
#total_loss += optimize(vgg, y1, y2, optimizer, loss_func)
#loss.backward()
#optimizer.step()
#total_loss += loss
bar.set_description(f'TEpoch:[{epoch+1}/{epochs}] loss:{total_loss/ total_samples}', refresh=True)
return (total_loss / total_samples).to('cpu')
def psnr(im1, im2):
#max is to be changed
max = torch.max(im1.max(), im2.max())
if max < 0:
max = torch.max(im1.abs().max(), im2.abs().max())
return 20 * torch.log10(max / (torch.sqrt(torch.mean((im1 - im2) ** 2))))
def optimize(model, im1, im2, optimizer, loss_func):
res1 = model(im1)
res2 = model(im2)
loss = loss_func(res1, res2)
loss.backward()
if optimizer:
optimizer.step()
return loss
def train_epoch_m(vgg, unet, unet_m, train_dl, optimizer, loss_func, epoch, epochs, m, lam):
total_loss = 0
total_samples = 0
bar = tqdm.tqdm(train_dl)
for x1, x2, gt in bar:
torch.cuda.empty_cache()
unet.train()
optimizer.zero_grad()
x1, x2, gt = x1.to(device), x2.to(device), gt.to(device)
n = x1.shape[0]
total_samples += n
y1 = unet(x1)
y2 = unet_m(x2)
t1 = vgg(y1)
t2 = vgg(y2)
t3 = vgg(gt)
loss = loss_func(t3, t1) + loss_func(t3, t2) + lam * loss_func(t1, t2)
loss.backward()
optimizer.step()
total_loss += loss
bar.set_description(f'TEpoch:[{epoch+1}/{epochs}] loss:{total_loss/ total_samples}', refresh=True)
#update momentum model params
enc_params = zip(unet.parameters(), unet_m.parameters())
for q_parameters, k_parameters in enc_params:
k_parameters.data = k_parameters.data * m + q_parameters.data * (1. - m)
return (total_loss/total_samples).to('cpu')
def momentum_train(vgg, unet, lam=0.05, loss_f=F_loss):
epochs = 100
lr = 1e-4
wd = 1e-4
bs = 2 #batch size
momentum = 0.999
import copy
momentum_encoder = copy.deepcopy(unet).to(device)
for params in momentum_encoder.parameters(): #Momentum model does not require gradient
params.requires_grad = False
train_ds, test_ds = create_dataset() #path is already default
train_dl = torch.utils.data.DataLoader(train_ds,batch_size=bs, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_ds,batch_size=1, shuffle=True)
optimizer = torch.optim.Adam(unet.parameters(), lr=lr, weight_decay=wd)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer=optimizer, T_max=epochs, eta_min=0, verbose=True)
max_psnr = -float('inf')
train_logs = []
val_logs = []
for epoch in range(epochs):
torch.cuda.empty_cache()
train_logs.append(train_epoch_m(vgg, unet, momentum_encoder, train_dl, optimizer, loss_f, epoch, epochs, momentum, lam).clone().detach().to('cpu'))
torch.cuda.empty_cache()
t = valid_epoch(vgg, unet, test_dl, loss_f, epoch, epochs, None)
val_logs.append(t.clone().detach().to('cpu'))
scheduler.step()
if (val_logs[-1] >= max_psnr):
torch.save({
'psnr' : max_psnr,
'model_state_dict': unet.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),'epoch' : epoch,
}, './checkpoint.pt')
max_psnr = val_logs[-1]
return train_logs, val_logs
if __name__ == "__main__":
os.chdir("/home/shai.kimhi/advancedDeep/DIP/code/")
serial = str(len(os.listdir("./logs")))
print(device)
vgg = Vgg19().to(device)
#unet = ResUnet().to(device)
#logs = train_eval(vgg, unet, lam=0.2)
#train with momentum method (add logs save)
#logs = momentum_train(vgg, unet, lam=0.2)
#train without special loss (add logs save)
#logs = train_eval(nn.Identity(), unet, compute_error) #compute error is MSE difference between two images
#train with transfer learning from COCO (add logs save)
fcn_net = Fcn_resent50().to(device)
logs = train_eval(vgg, fcn_net)
#logs = train_eval(nn.Identity(), fcn_net, compute_error)
logs = torch.tensor(logs).tolist()
file = open(f"logs/transfer-2-l0.2-{serial}.txt","w")
file.write(dumps(logs))
file.close()