forked from ltkong218/IFRNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_gopro.py
199 lines (156 loc) · 7.99 KB
/
train_gopro.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
import os
import math
import time
import random
import argparse
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
import torch.distributed as dist
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel as DDP
from datasets import GoPro_Train_Dataset, GoPro_Test_Dataset
from metric import calculate_psnr, calculate_ssim
from utils import AverageMeter
import logging
def get_lr(args, iters):
ratio = 0.5 * (1.0 + np.cos(iters / (args.epochs * args.iters_per_epoch) * math.pi))
lr = (args.lr_start - args.lr_end) * ratio + args.lr_end
return lr
def set_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def train(args, ddp_model):
local_rank = args.local_rank
print('Distributed Data Parallel Training IFRNet on Rank {}'.format(local_rank))
if local_rank == 0:
os.makedirs(args.log_path, exist_ok=True)
log_path = os.path.join(args.log_path, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
os.makedirs(log_path, exist_ok=True)
logger = logging.getLogger()
logger.setLevel('INFO')
BASIC_FORMAT = '%(asctime)s:%(levelname)s:%(message)s'
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(BASIC_FORMAT, DATE_FORMAT)
chlr = logging.StreamHandler()
chlr.setFormatter(formatter)
chlr.setLevel('INFO')
fhlr = logging.FileHandler(os.path.join(log_path, 'train.log'))
fhlr.setFormatter(formatter)
logger.addHandler(chlr)
logger.addHandler(fhlr)
logger.info(args)
dataset_train = GoPro_Train_Dataset(dataset_dir='/home/ltkong/Datasets/GOPRO', augment=True)
sampler = DistributedSampler(dataset_train)
dataloader_train = DataLoader(dataset_train, batch_size=args.batch_size, num_workers=args.num_workers, pin_memory=True, drop_last=True, sampler=sampler)
args.iters_per_epoch = dataloader_train.__len__()
iters = args.resume_epoch * args.iters_per_epoch
dataset_val = GoPro_Test_Dataset(dataset_dir='/home/ltkong/Datasets/GOPRO')
dataloader_val = DataLoader(dataset_val, batch_size=2, num_workers=4, pin_memory=True, shuffle=False, drop_last=True)
optimizer = optim.AdamW(ddp_model.parameters(), lr=args.lr_start, weight_decay=0)
time_stamp = time.time()
avg_rec = AverageMeter()
avg_geo = AverageMeter()
avg_dis = AverageMeter()
best_psnr = 0.0
for epoch in range(args.resume_epoch, args.epochs):
sampler.set_epoch(epoch)
for i, data in enumerate(dataloader_train):
for l in range(len(data)):
data[l] = data[l].to(args.device)
img0, img1, img2, img3, img4, img5, img6, img7, img8, emb1, emb2, emb3, emb4, emb5, emb6, emb7 = data
img0 = torch.cat([img0, img0, img0, img0, img0, img0, img0], 0)
img8 = torch.cat([img8, img8, img8, img8, img8, img8, img8], 0)
imgt = torch.cat([img1, img2, img3, img4, img5, img6, img7], 0)
embt = torch.cat([emb1, emb2, emb3, emb4, emb5, emb6, emb7], 0)
data_time_interval = time.time() - time_stamp
time_stamp = time.time()
lr = get_lr(args, iters)
set_lr(optimizer, lr)
optimizer.zero_grad()
imgt_pred, loss_rec, loss_geo, loss_dis = ddp_model(img0, img8, embt, imgt, None)
loss = loss_rec + loss_geo + loss_dis
loss.backward()
optimizer.step()
avg_rec.update(loss_rec.cpu().data)
avg_geo.update(loss_geo.cpu().data)
avg_dis.update(loss_dis.cpu().data)
train_time_interval = time.time() - time_stamp
if (iters+1) % 50 == 0 and local_rank == 0:
logger.info('epoch:{}/{} iter:{}/{} time:{:.2f}+{:.2f} lr:{:.5e} loss_rec:{:.4e} loss_geo:{:.4e} loss_dis:{:.4e}'.format(epoch+1, args.epochs, iters+1, args.epochs * args.iters_per_epoch, data_time_interval, train_time_interval, lr, avg_rec.avg, avg_geo.avg, avg_dis.avg))
avg_rec.reset()
avg_geo.reset()
avg_dis.reset()
iters += 1
time_stamp = time.time()
if (epoch+1) % args.eval_interval == 0 and local_rank == 0:
psnr = evaluate(args, ddp_model, dataloader_val, epoch, logger)
if psnr > best_psnr:
best_psnr = psnr
torch.save(ddp_model.module.state_dict(), '{}/{}_{}.pth'.format(log_path, args.model_name, 'best'))
torch.save(ddp_model.module.state_dict(), '{}/{}_{}.pth'.format(log_path, args.model_name, 'latest'))
dist.barrier()
def evaluate(args, ddp_model, dataloader_val, epoch, logger):
loss_rec_list = []
loss_geo_list = []
loss_dis_list = []
psnr_list = []
time_stamp = time.time()
for i, data in enumerate(dataloader_val):
for l in range(len(data)):
data[l] = data[l].to(args.device)
img0, img1, img2, img3, img4, img5, img6, img7, img8, emb1, emb2, emb3, emb4, emb5, emb6, emb7 = data
img0 = torch.cat([img0, img0, img0, img0, img0, img0, img0], 0)
img8 = torch.cat([img8, img8, img8, img8, img8, img8, img8], 0)
imgt = torch.cat([img1, img2, img3, img4, img5, img6, img7], 0)
embt = torch.cat([emb1, emb2, emb3, emb4, emb5, emb6, emb7], 0)
with torch.no_grad():
imgt_pred, loss_rec, loss_geo, loss_dis = ddp_model(img0, img8, embt, imgt, None)
loss_rec_list.append(loss_rec.cpu().numpy())
loss_geo_list.append(loss_geo.cpu().numpy())
loss_dis_list.append(loss_dis.cpu().numpy())
for j in range(img0.shape[0]):
psnr = calculate_psnr(imgt_pred[j].unsqueeze(0), imgt[j].unsqueeze(0)).cpu().data
psnr_list.append(psnr)
eval_time_interval = time.time() - time_stamp
logger.info('eval epoch:{}/{} time:{:.2f} loss_rec:{:.4e} loss_geo:{:.4e} loss_dis:{:.4e} psnr:{:.3f}'.format(epoch+1, args.epochs, eval_time_interval, np.array(loss_rec_list).mean(), np.array(loss_geo_list).mean(), np.array(loss_dis_list).mean(), np.array(psnr_list).mean()))
return np.array(psnr_list).mean()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='IFRNet')
parser.add_argument('--model_name', default='IFRNet', type=str, help='IFRNet, IFRNet_L, IFRNet_S')
parser.add_argument('--local_rank', default=-1, type=int)
parser.add_argument('--world_size', default=4, type=int)
parser.add_argument('--epochs', default=600, type=int)
parser.add_argument('--eval_interval', default=8, type=int)
parser.add_argument('--batch_size', default=2, type=int)
parser.add_argument('--lr_start', default=1e-4, type=float)
parser.add_argument('--lr_end', default=1e-5, type=float)
parser.add_argument('--log_path', default='checkpoint', type=str)
parser.add_argument('--resume_epoch', default=0, type=int)
parser.add_argument('--resume_path', default=None, type=str)
args = parser.parse_args()
dist.init_process_group(backend='nccl', world_size=args.world_size)
torch.cuda.set_device(args.local_rank)
args.device = torch.device('cuda', args.local_rank)
seed = 1234
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = True
if args.model_name == 'IFRNet':
from models.IFRNet import Model
elif args.model_name == 'IFRNet_L':
from models.IFRNet_L import Model
elif args.model_name == 'IFRNet_S':
from models.IFRNet_S import Model
args.log_path = args.log_path + '/' + args.model_name
args.num_workers = args.batch_size * 4
model = Model().to(args.device)
if args.resume_epoch != 0:
model.load_state_dict(torch.load(args.resume_path, map_location='cpu'))
ddp_model = DDP(model, device_ids=[args.local_rank], output_device=args.local_rank)
train(args, ddp_model)
dist.destroy_process_group()