-
Notifications
You must be signed in to change notification settings - Fork 69
/
train_IMDN.py
193 lines (164 loc) · 7.08 KB
/
train_IMDN.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
import argparse, os
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from model import architecture
from data import DIV2K, Set5_val
import utils
import skimage.color as sc
import random
from collections import OrderedDict
# os.environ["CUDA_VISIBLE_DEVICES"] = '0'
# Training settings
parser = argparse.ArgumentParser(description="IMDN")
parser.add_argument("--batch_size", type=int, default=16,
help="training batch size")
parser.add_argument("--testBatchSize", type=int, default=1,
help="testing batch size")
parser.add_argument("-nEpochs", type=int, default=1000,
help="number of epochs to train")
parser.add_argument("--lr", type=float, default=2e-4,
help="Learning Rate. Default=2e-4")
parser.add_argument("--step_size", type=int, default=200,
help="learning rate decay per N epochs")
parser.add_argument("--gamma", type=int, default=0.5,
help="learning rate decay factor for step decay")
parser.add_argument("--cuda", action="store_true", default=True,
help="use cuda")
parser.add_argument("--resume", default="", type=str,
help="path to checkpoint")
parser.add_argument("--start-epoch", default=1, type=int,
help="manual epoch number")
parser.add_argument("--threads", type=int, default=8,
help="number of threads for data loading")
parser.add_argument("--root", type=str, default="training_data/",
help='dataset directory')
parser.add_argument("--n_train", type=int, default=800,
help="number of training set")
parser.add_argument("--n_val", type=int, default=1,
help="number of validation set")
parser.add_argument("--test_every", type=int, default=1000)
parser.add_argument("--scale", type=int, default=2,
help="super-resolution scale")
parser.add_argument("--patch_size", type=int, default=192,
help="output patch size")
parser.add_argument("--rgb_range", type=int, default=1,
help="maxium value of RGB")
parser.add_argument("--n_colors", type=int, default=3,
help="number of color channels to use")
parser.add_argument("--pretrained", default="", type=str,
help="path to pretrained models")
parser.add_argument("--seed", type=int, default=1)
parser.add_argument("--isY", action="store_true", default=True)
parser.add_argument("--ext", type=str, default='.npy')
parser.add_argument("--phase", type=str, default='train')
args = parser.parse_args()
print(args)
torch.backends.cudnn.benchmark = True
# random seed
seed = args.seed
if seed is None:
seed = random.randint(1, 10000)
print("Ramdom Seed: ", seed)
random.seed(seed)
torch.manual_seed(seed)
cuda = args.cuda
device = torch.device('cuda' if cuda else 'cpu')
print("===> Loading datasets")
trainset = DIV2K.div2k(args)
testset = Set5_val.DatasetFromFolderVal("Test_Datasets/Set5/",
"Test_Datasets/Set5_LR/x{}/".format(args.scale),
args.scale)
training_data_loader = DataLoader(dataset=trainset, num_workers=args.threads, batch_size=args.batch_size, shuffle=True, pin_memory=True, drop_last=True)
testing_data_loader = DataLoader(dataset=testset, num_workers=args.threads, batch_size=args.testBatchSize,
shuffle=False)
print("===> Building models")
args.is_train = True
model = architecture.IMDN(upscale=args.scale)
l1_criterion = nn.L1Loss()
print("===> Setting GPU")
if cuda:
model = model.to(device)
l1_criterion = l1_criterion.to(device)
if args.pretrained:
if os.path.isfile(args.pretrained):
print("===> loading models '{}'".format(args.pretrained))
checkpoint = torch.load(args.pretrained)
new_state_dcit = OrderedDict()
for k, v in checkpoint.items():
if 'module' in k:
name = k[7:]
else:
name = k
new_state_dcit[name] = v
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in new_state_dcit.items() if k in model_dict}
for k, v in model_dict.items():
if k not in pretrained_dict:
print(k)
model.load_state_dict(pretrained_dict, strict=True)
else:
print("===> no models found at '{}'".format(args.pretrained))
print("===> Setting Optimizer")
optimizer = optim.Adam(model.parameters(), lr=args.lr)
def train(epoch):
model.train()
utils.adjust_learning_rate(optimizer, epoch, args.step_size, args.lr, args.gamma)
print('epoch =', epoch, 'lr = ', optimizer.param_groups[0]['lr'])
for iteration, (lr_tensor, hr_tensor) in enumerate(training_data_loader, 1):
if args.cuda:
lr_tensor = lr_tensor.to(device) # ranges from [0, 1]
hr_tensor = hr_tensor.to(device) # ranges from [0, 1]
optimizer.zero_grad()
sr_tensor = model(lr_tensor)
loss_l1 = l1_criterion(sr_tensor, hr_tensor)
loss_sr = loss_l1
loss_sr.backward()
optimizer.step()
if iteration % 100 == 0:
print("===> Epoch[{}]({}/{}): Loss_l1: {:.5f}".format(epoch, iteration, len(training_data_loader),
loss_l1.item()))
def valid():
model.eval()
avg_psnr, avg_ssim = 0, 0
for batch in testing_data_loader:
lr_tensor, hr_tensor = batch[0], batch[1]
if args.cuda:
lr_tensor = lr_tensor.to(device)
hr_tensor = hr_tensor.to(device)
with torch.no_grad():
pre = model(lr_tensor)
sr_img = utils.tensor2np(pre.detach()[0])
gt_img = utils.tensor2np(hr_tensor.detach()[0])
crop_size = args.scale
cropped_sr_img = utils.shave(sr_img, crop_size)
cropped_gt_img = utils.shave(gt_img, crop_size)
if args.isY is True:
im_label = utils.quantize(sc.rgb2ycbcr(cropped_gt_img)[:, :, 0])
im_pre = utils.quantize(sc.rgb2ycbcr(cropped_sr_img)[:, :, 0])
else:
im_label = cropped_gt_img
im_pre = cropped_sr_img
avg_psnr += utils.compute_psnr(im_pre, im_label)
avg_ssim += utils.compute_ssim(im_pre, im_label)
print("===> Valid. psnr: {:.4f}, ssim: {:.4f}".format(avg_psnr / len(testing_data_loader), avg_ssim / len(testing_data_loader)))
def save_checkpoint(epoch):
model_folder = "checkpoint_x{}/".format(args.scale)
model_out_path = model_folder + "epoch_{}.pth".format(epoch)
if not os.path.exists(model_folder):
os.makedirs(model_folder)
torch.save(model.state_dict(), model_out_path)
print("===> Checkpoint saved to {}".format(model_out_path))
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
print("===> Training")
print_network(model)
for epoch in range(args.start_epoch, args.nEpochs + 1):
valid()
train(epoch)
save_checkpoint(epoch)