-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
376 lines (315 loc) · 15.4 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import argparse
import math
import random
import sys
import time
from collections import defaultdict
from typing import List
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import transforms
from models.ldmic import *
from lib.utils import get_output_folder, AverageMeter, save_checkpoint, StereoImageDataset
import numpy as np
import yaml
import wandb
import os
from tqdm import tqdm
from pytorch_msssim import ms_ssim
os.environ["WANDB_API_KEY"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # write your own wandb id
def compute_aux_loss(aux_list: List, backward=False):
aux_loss_sum = 0
for aux_loss in aux_list:
aux_loss_sum += aux_loss
if backward is True:
aux_loss.backward()
return aux_loss_sum
def configure_optimizers(net, args):
"""Separate parameters for the main optimizer and the auxiliary optimizer.
Return two optimizers"""
parameters = {
n
for n, p in net.named_parameters()
if not n.endswith(".quantiles") and p.requires_grad
}
aux_parameters = {
n
for n, p in net.named_parameters()
if n.endswith(".quantiles") and p.requires_grad
}
# Make sure we don't have an intersection of parameters
params_dict = dict(p for p in net.named_parameters() if p[1].requires_grad)
inter_params = parameters & aux_parameters
union_params = parameters | aux_parameters
assert len(inter_params) == 0
assert len(union_params) - len(params_dict.keys()) == 0
optimizer = optim.Adam(
(params_dict[n] for n in sorted(parameters)),
lr=args.learning_rate,
)
aux_optimizer = optim.Adam(
(params_dict[n] for n in sorted(aux_parameters)),
lr=args.learning_rate,
)
return optimizer, aux_optimizer
def train_one_epoch(model, criterion, train_dataloader, optimizer, aux_optimizer, epoch, clip_max_norm, args):
model.train()
device = next(model.parameters()).device
if args.metric == "mse":
metric_dB_name, left_db_name, right_db_name = 'psnr', "left_PSNR", "right_PSNR"
metric_name = "mse_loss"
else:
metric_dB_name, left_db_name, right_db_name = "ms_db", "left_ms_db", "right_ms_db"
metric_name = "ms_ssim_loss"
metric_dB = AverageMeter(metric_dB_name, ':.4e')
metric_loss = AverageMeter(args.metric, ':.4e')
left_db, right_db = AverageMeter(left_db_name, ':.4e'), AverageMeter(right_db_name, ':.4e')
metric0, metric1 = args.metric+"0", args.metric+"1"
loss = AverageMeter('Loss', ':.4e')
bpp_loss = AverageMeter('BppLoss', ':.4e')
aux_loss = AverageMeter('AuxLoss', ':.4e')
left_bpp, right_bpp = AverageMeter('LBpp', ':.4e'), AverageMeter('RBpp', ':.4e')
train_dataloader = tqdm(train_dataloader)
print('Train epoch:', epoch)
for i, batch in enumerate(train_dataloader):
d = [frame.to(device) for frame in batch]
optimizer.zero_grad()
if aux_optimizer is not None:
aux_optimizer.zero_grad()
#aux_optimizer.zero_grad()
out_net = model(d)
out_criterion = criterion(out_net, d, args.lmbda)
out_criterion["loss"].backward()
if clip_max_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), clip_max_norm)
optimizer.step()
if aux_optimizer is not None:
out_aux_loss = compute_aux_loss(model.aux_loss(), backward=True)
aux_optimizer.step()
else:
out_aux_loss = compute_aux_loss(model.aux_loss(), backward=False)
#out_aux_loss = compute_aux_loss(model.aux_loss(), backward=True)
#aux_optimizer.step()
loss.update(out_criterion["loss"].item())
bpp_loss.update((out_criterion["bpp_loss"]).item())
aux_loss.update(out_aux_loss.item())
metric_loss.update(out_criterion[metric_name].item())
left_bpp.update(out_criterion["bpp0"].item())
right_bpp.update(out_criterion["bpp1"].item())
if out_criterion[metric0] > 0 and out_criterion[metric1] > 0:
left_metric = 10 * (torch.log10(1 / out_criterion[metric0])).mean().item()
right_metric = 10 * (torch.log10(1 / out_criterion[metric1])).mean().item()
left_db.update(left_metric)
right_db.update(right_metric)
metric_dB.update((left_metric+right_metric)/2)
train_dataloader.set_description('[{}/{}]'.format(i, len(train_dataloader)))
train_dataloader.set_postfix({"Loss":loss.avg, 'Bpp':bpp_loss.avg, args.metric: metric_loss.avg, 'Aux':aux_loss.avg,
metric_dB_name:metric_dB.avg})
out = {"loss": loss.avg, metric_name: metric_loss.avg, "bpp_loss": bpp_loss.avg,
"aux_loss":aux_loss.avg, metric_dB_name: metric_dB.avg, "left_bpp": left_bpp.avg, "right_bpp": right_bpp.avg,
left_db_name:left_db.avg, right_db_name: right_db.avg,}
return out
def test_epoch(epoch, val_dataloader, model, criterion, args):
model.eval()
device = next(model.parameters()).device
if args.metric == "mse":
metric_dB_name, left_db_name, right_db_name = 'psnr', "left_PSNR", "right_PSNR"
metric_name = "mse_loss"
else:
metric_dB_name, left_db_name, right_db_name = "ms_db", "left_ms_db", "right_ms_db"
metric_name = "ms_ssim_loss"
metric_dB = AverageMeter(metric_dB_name, ':.4e')
metric_loss = AverageMeter(args.metric, ':.4e')
left_db, right_db = AverageMeter(left_db_name, ':.4e'), AverageMeter(right_db_name, ':.4e')
metric0, metric1 = args.metric+"0", args.metric+"1"
loss = AverageMeter('Loss', ':.4e')
bpp_loss = AverageMeter('BppLoss', ':.4e')
aux_loss = AverageMeter('AuxLoss', ':.4e')
left_bpp, right_bpp = AverageMeter('LBpp', ':.4e'), AverageMeter('RBpp', ':.4e')
loop = tqdm(val_dataloader)
with torch.no_grad():
for i, batch in enumerate(loop):
d = [frame.to(device) for frame in batch]
out_net = model(d)
out_criterion = criterion(out_net, d, args.lmbda)
out_aux_loss = compute_aux_loss(model.aux_loss(), backward=False)
loss.update(out_criterion["loss"].item())
bpp_loss.update((out_criterion["bpp_loss"]).item())
aux_loss.update(out_aux_loss.item())
metric_loss.update(out_criterion[metric_name].item())
left_bpp.update(out_criterion["bpp0"].item())
right_bpp.update(out_criterion["bpp1"].item())
if out_criterion[metric0] > 0 and out_criterion[metric1] > 0:
left_metric = 10 * (torch.log10(1 / out_criterion[metric0])).mean().item()
right_metric = 10 * (torch.log10(1 / out_criterion[metric1])).mean().item()
left_db.update(left_metric)
right_db.update(right_metric)
metric_dB.update((left_metric+right_metric)/2)
loop.set_description('[{}/{}]'.format(i, len(val_dataloader)))
loop.set_postfix({"Loss":loss.avg, 'Bpp':bpp_loss.avg, args.metric: metric_loss.avg, 'Aux':aux_loss.avg,
metric_dB_name:metric_dB.avg})
out = {"loss": loss.avg, metric_name: metric_loss.avg, "bpp_loss": bpp_loss.avg,
"aux_loss":aux_loss.avg, metric_dB_name: metric_dB.avg, "left_bpp": left_bpp.avg, "right_bpp": right_bpp.avg,
left_db_name:left_db.avg, right_db_name: right_db.avg,}
return out
def parse_args(argv):
parser = argparse.ArgumentParser(description="Example training script.")
parser.add_argument(
"-d", "--dataset", type=str, default='./datasets/Instereo2K/', help="Training dataset"
)
parser.add_argument(
"--data-name", type=str, default='instereo2K', help="Training dataset"
)
parser.add_argument(
"--model-name", type=str, default='LDMIC', help="Training dataset"
)
parser.add_argument(
"-n",
"--num-workers",
type=int,
default=2,
help="Dataloaders threads (default: %(default)s)",
)
parser.add_argument(
"--lambda",
dest="lmbda",
type=float,
default=2048,
help="Bit-rate distortion parameter (default: %(default)s)",
)
parser.add_argument(
"--batch-size", type=int, default=16, help="Batch size (default: %(default)s)"
)
parser.add_argument(
"--epochs", type=int, default=400, help="number of training epochs (default: %(default)s)"
)
parser.add_argument(
"--test-batch-size",
type=int,
default=64,
help="Test batch size (default: %(default)s)",
)
parser.add_argument(
"--patch-size",
type=int,
nargs=2,
default=(256, 256),
help="Size of the patches to be cropped (default: %(default)s)",
)
parser.add_argument("--cuda", action="store_true", help="Use cuda")
parser.add_argument(
"--save", action="store_true", help="Save model to disk"
)
parser.add_argument(
"--resize", action="store_true", help="training use resize or randomcrop"
)
parser.add_argument(
"--seed", type=float, default=1, help="Set random seed for reproducibility"
)
parser.add_argument(
"--clip_max_norm",
default=1.0,
type=float,
help="gradient clipping max norm (default: %(default)s",
)
parser.add_argument("--i_model_path", type=str, help="Path to a checkpoint")
parser.add_argument("--metric", type=str, default="mse", help="metric: mse, ms-ssim")
parser.add_argument(
"--learning-rate",
type=float,
default=1e-4,
help="Learning rate (default: %(default)s)",
)
args = parser.parse_args(argv)
return args
def main(argv):
args = parse_args(argv)
# Cache the args as a text string to save them in the output dir later
args_text = yaml.safe_dump(args.__dict__, default_flow_style=False)
if args.seed is not None:
torch.manual_seed(args.seed)
random.seed(args.seed)
# Warning, the order of the transform composition should be kept.
train_dataset = StereoImageDataset(ds_type='train', ds_name=args.data_name, root=args.dataset, crop_size=args.patch_size, resize=args.resize)
test_dataset = StereoImageDataset(ds_type='test', ds_name=args.data_name, root=args.dataset, crop_size=args.patch_size, resize=args.resize)
device = "cuda" if args.cuda and torch.cuda.is_available() else "cpu"
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, num_workers=args.num_workers,
shuffle=True, pin_memory=(device == "cuda"))
test_dataloader = DataLoader(test_dataset, batch_size=args.test_batch_size, num_workers=args.num_workers, shuffle=False, pin_memory=(device == "cuda"))
if args.model_name == "LDMIC":
net = LDMIC(N=192, M=192, decode_atten=JointContextTransfer)
elif args.model_name == "LDMIC_checkboard":
net = LDMIC_checkboard(N=192, M=192, decode_atten=JointContextTransfer)
net = net.to(device)
optimizer, aux_optimizer = configure_optimizers(net, args)
lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [100, 200, 300, 400], 0.5) #optim.lr_scheduler.ReduceLROnPlateau(optimizer, "min", patience=10, factor=0.2)
if args.metric == "mse":
criterion = MSE_Loss() #MSE_Loss(lmbda=args.lmbda)
else:
criterion = MS_SSIM_Loss(device) #(device, lmbda=args.lmbda)
last_epoch = 0
best_loss = float("inf")
if args.i_model_path: #load from previous checkpoint
print("Loading model: ", args.i_model_path)
checkpoint = torch.load(args.i_model_path, map_location=device)
net.load_state_dict(checkpoint["state_dict"])
last_epoch = checkpoint["epoch"] + 1
optimizer.load_state_dict(checkpoint["optimizer"])
aux_optimizer.load_state_dict(checkpoint["aux_optimizer"])
lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])
best_b_model_path = os.path.join(os.path.split(args.i_model_path)[0], 'ckpt.best.pth.tar')
best_loss = torch.load(best_b_model_path)["loss"]
log_dir, experiment_id = get_output_folder('./checkpoints/{}/{}/{}/lamda{}/'.format(args.data_name, args.metric, args.model_name, int(args.lmbda)), 'train')
display_name = "{}_{}_lmbda{}".format(args.model_name, args.metric, int(args.lmbda))
tags = "lmbda{}".format(args.lmbda)
with open(os.path.join(log_dir, 'args.yaml'), 'w') as f:
f.write(args_text)
project_name = "DSIC_" + args.data_name
wandb.init(project=project_name, name=display_name, tags=[tags],) #notes="lmbda{}".format(args.lmbda))
wandb.watch_called = False # Re-run the model without restarting the runtime, unnecessary after our next release
wandb.config.update(args) # config is a variable that holds and saves hyper parameters and inputs
if args.metric == "mse":
metric_dB_name, left_db_name, right_db_name = 'psnr', "left_PSNR", "right_PSNR"
metric_name = "mse_loss"
else:
metric_dB_name, left_db_name, right_db_name = "ms_db", "left_ms_db", "right_ms_db"
metric_name = "ms_ssim_loss"
#val_loss = test_epoch(0, test_dataloader, net, criterion, args)
for epoch in range(last_epoch, args.epochs):
#adjust_learning_rate(optimizer, aux_optimizer, epoch, args)
print(f"Learning rate: {optimizer.param_groups[0]['lr']}")
train_loss = train_one_epoch(net, criterion, train_dataloader, optimizer, aux_optimizer, epoch, args.clip_max_norm, args)
lr_scheduler.step()
wandb.log({"train": {"loss": train_loss["loss"], metric_name: train_loss[metric_name], "bpp_loss": train_loss["bpp_loss"],
"aux_loss": train_loss["aux_loss"], metric_dB_name: train_loss[metric_dB_name], "left_bpp": train_loss["left_bpp"], "right_bpp": train_loss["right_bpp"],
left_db_name:train_loss[left_db_name], right_db_name: train_loss[right_db_name]}, }
)
if epoch%10==0:
val_loss = test_epoch(epoch, test_dataloader, net, criterion, args)
wandb.log({
"test": {"loss": val_loss["loss"], metric_name: val_loss[metric_name], "bpp_loss": val_loss["bpp_loss"],
"aux_loss": val_loss["aux_loss"], metric_dB_name: val_loss[metric_dB_name], "left_bpp": val_loss["left_bpp"], "right_bpp": val_loss["right_bpp"],
left_db_name:val_loss[left_db_name], right_db_name: val_loss[right_db_name],}
})
loss = val_loss["loss"]
is_best = loss < best_loss
best_loss = min(loss, best_loss)
else:
loss = best_loss
is_best = False
if args.save:
save_checkpoint(
{
"epoch": epoch,
"state_dict": net.state_dict(),
"loss": loss,
"optimizer": optimizer.state_dict(),
"aux_optimizer": aux_optimizer.state_dict() if aux_optimizer is not None else None,
'lr_scheduler': lr_scheduler.state_dict(),
},
is_best, log_dir
)
if __name__ == "__main__":
main(sys.argv[1:])