-
Notifications
You must be signed in to change notification settings - Fork 36
/
train.py
147 lines (108 loc) · 4.91 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
import os
import argparse
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.cuda.amp import autocast, GradScaler
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
from tqdm import tqdm
from utils import AverageMeter
from datasets.loader import PairLoader
from models import *
parser = argparse.ArgumentParser()
parser.add_argument('--model', default='dehazeformer-s', type=str, help='model name')
parser.add_argument('--num_workers', default=16, type=int, help='number of workers')
parser.add_argument('--no_autocast', action='store_false', default=True, help='disable autocast')
parser.add_argument('--save_dir', default='./saved_models/', type=str, help='path to models saving')
parser.add_argument('--data_dir', default='./data/', type=str, help='path to dataset')
parser.add_argument('--log_dir', default='./logs/', type=str, help='path to logs')
parser.add_argument('--dataset', default='RESIDE-IN', type=str, help='dataset name')
parser.add_argument('--exp', default='indoor', type=str, help='experiment setting')
parser.add_argument('--gpu', default='0,1,2,3', type=str, help='GPUs used for training')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
def train(train_loader, network, criterion, optimizer, scaler):
losses = AverageMeter()
torch.cuda.empty_cache()
network.train()
for batch in train_loader:
source_img = batch['source'].cuda()
target_img = batch['target'].cuda()
with autocast(args.no_autocast):
output = network(source_img)
loss = criterion(output, target_img)
losses.update(loss.item())
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
return losses.avg
def valid(val_loader, network):
PSNR = AverageMeter()
torch.cuda.empty_cache()
network.eval()
for batch in val_loader:
source_img = batch['source'].cuda()
target_img = batch['target'].cuda()
with torch.no_grad(): # torch.no_grad() may cause warning
output = network(source_img).clamp_(-1, 1)
mse_loss = F.mse_loss(output * 0.5 + 0.5, target_img * 0.5 + 0.5, reduction='none').mean((1, 2, 3))
psnr = 10 * torch.log10(1 / mse_loss).mean()
PSNR.update(psnr.item(), source_img.size(0))
return PSNR.avg
if __name__ == '__main__':
setting_filename = os.path.join('configs', args.exp, args.model+'.json')
if not os.path.exists(setting_filename):
setting_filename = os.path.join('configs', args.exp, 'default.json')
with open(setting_filename, 'r') as f:
setting = json.load(f)
network = eval(args.model.replace('-', '_'))()
network = nn.DataParallel(network).cuda()
criterion = nn.L1Loss()
if setting['optimizer'] == 'adam':
optimizer = torch.optim.Adam(network.parameters(), lr=setting['lr'])
elif setting['optimizer'] == 'adamw':
optimizer = torch.optim.AdamW(network.parameters(), lr=setting['lr'])
else:
raise Exception("ERROR: unsupported optimizer")
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=setting['epochs'], eta_min=setting['lr'] * 1e-2)
scaler = GradScaler()
dataset_dir = os.path.join(args.data_dir, args.dataset)
train_dataset = PairLoader(dataset_dir, 'train', 'train',
setting['patch_size'], setting['edge_decay'], setting['only_h_flip'])
train_loader = DataLoader(train_dataset,
batch_size=setting['batch_size'],
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
drop_last=True)
val_dataset = PairLoader(dataset_dir, 'test', setting['valid_mode'],
setting['patch_size'])
val_loader = DataLoader(val_dataset,
batch_size=setting['batch_size'],
num_workers=args.num_workers,
pin_memory=True)
save_dir = os.path.join(args.save_dir, args.exp)
os.makedirs(save_dir, exist_ok=True)
if not os.path.exists(os.path.join(save_dir, args.model+'.pth')):
print('==> Start training, current model name: ' + args.model)
# print(network)
writer = SummaryWriter(log_dir=os.path.join(args.log_dir, args.exp, args.model))
best_psnr = 0
for epoch in tqdm(range(setting['epochs'] + 1)):
loss = train(train_loader, network, criterion, optimizer, scaler)
writer.add_scalar('train_loss', loss, epoch)
scheduler.step()
if epoch % setting['eval_freq'] == 0:
avg_psnr = valid(val_loader, network)
writer.add_scalar('valid_psnr', avg_psnr, epoch)
if avg_psnr > best_psnr:
best_psnr = avg_psnr
torch.save({'state_dict': network.state_dict()},
os.path.join(save_dir, args.model+'.pth'))
writer.add_scalar('best_psnr', best_psnr, epoch)
else:
print('==> Existing trained model')
exit(1)