-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRResNet.py
More file actions
564 lines (450 loc) · 23 KB
/
Copy pathSRResNet.py
File metadata and controls
564 lines (450 loc) · 23 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import torch
import os
from torch import nn
import json
import torchvision
import math
import random
from PIL import Image
from torchvision import transforms as FT
from torch.utils.data import Dataset, DataLoader
import time
class ConvolutionalBlock(nn.Module):
"""
A convolutional block, comprising convolutional, BN, activation layers.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1, batch_norm=False, activation=None):
"""
:param in_channels: number of input channels
:param out_channels: number of output channe;s
:param kernel_size: kernel size
:param stride: stride
:param batch_norm: include a BN layer?
:param activation: Type of activation; None if none
"""
super(ConvolutionalBlock, self).__init__()
if activation is not None:
activation = activation.lower()
assert activation in {'prelu', 'leakyrelu', 'tanh'}
# A container that will hold the layers in this convolutional block
layers = list()
# A convolutional layer
layers.append(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride,
padding=kernel_size // 2))
# A batch normalization (BN) layer, if wanted
if batch_norm is True:
layers.append(nn.BatchNorm2d(num_features=out_channels))
# An activation layer, if wanted
if activation == 'prelu':
layers.append(nn.PReLU())
elif activation == 'leakyrelu':
layers.append(nn.LeakyReLU(0.2))
elif activation == 'tanh':
layers.append(nn.Tanh())
# Put together the convolutional block as a sequence of the layers in this container
self.conv_block = nn.Sequential(*layers)
def forward(self, input):
"""
Forward propagation.
:param input: input images, a tensor of size (N, in_channels, w, h)
:return: output images, a tensor of size (N, out_channels, w, h)
"""
output = self.conv_block(input) # (N, out_channels, w, h)
return output
class SubPixelConvolutionalBlock(nn.Module):
"""
A subpixel convolutional block, comprising convolutional, pixel-shuffle, and PReLU activation layers.
"""
def __init__(self, kernel_size=3, n_channels=64, scaling_factor=2):
"""
:param kernel_size: kernel size of the convolution
:param n_channels: number of input and output channels
:param scaling_factor: factor to scale input images by (along both dimensions)
"""
super(SubPixelConvolutionalBlock, self).__init__()
# A convolutional layer that increases the number of channels by scaling factor^2, followed by pixel shuffle and PReLU
self.conv = nn.Conv2d(in_channels=n_channels, out_channels=n_channels * (scaling_factor ** 2),
kernel_size=kernel_size, padding=kernel_size // 2)
# These additional channels are shuffled to form additional pixels, upscaling each dimension by the scaling factor
self.pixel_shuffle = nn.PixelShuffle(upscale_factor=scaling_factor)
self.prelu = nn.PReLU()
def forward(self, input):
"""
Forward propagation.
:param input: input images, a tensor of size (N, n_channels, w, h)
:return: scaled output images, a tensor of size (N, n_channels, w * scaling factor, h * scaling factor)
"""
output = self.conv(input) # (N, n_channels * scaling factor^2, w, h)
output = self.pixel_shuffle(output) # (N, n_channels, w * scaling factor, h * scaling factor)
output = self.prelu(output) # (N, n_channels, w * scaling factor, h * scaling factor)
return output
class ResidualBlock(nn.Module):
"""
A residual block, comprising two convolutional blocks with a residual connection across them.
"""
def __init__(self, kernel_size=3, n_channels=64):
"""
:param kernel_size: kernel size
:param n_channels: number of input and output channels (same because the input must be added to the output)
"""
super(ResidualBlock, self).__init__()
# The first convolutional block
self.conv_block1 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels, kernel_size=kernel_size,
batch_norm=True, activation='PReLu')
# The second convolutional block
self.conv_block2 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels, kernel_size=kernel_size,
batch_norm=True, activation=None)
def forward(self, input):
"""
Forward propagation.
:param input: input images, a tensor of size (N, n_channels, w, h)
:return: output images, a tensor of size (N, n_channels, w, h)
"""
residual = input # (N, n_channels, w, h)
output = self.conv_block1(input) # (N, n_channels, w, h)
output = self.conv_block2(output) # (N, n_channels, w, h)
output = output + residual # (N, n_channels, w, h)
return output
class SRResNet(nn.Module):
"""
The SRResNet, as defined in the paper.
"""
def __init__(self, large_kernel_size=9, small_kernel_size=3, n_channels=64, n_blocks=16, scaling_factor=4):
"""
:param large_kernel_size: kernel size of the first and last convolutions which transform the inputs and outputs
:param small_kernel_size: kernel size of all convolutions in-between, i.e. those in the residual and subpixel convolutional blocks
:param n_channels: number of channels in-between, i.e. the input and output channels for the residual and subpixel convolutional blocks
:param n_blocks: number of residual blocks
:param scaling_factor: factor to scale input images by (along both dimensions) in the subpixel convolutional block
"""
super(SRResNet, self).__init__()
# Scaling factor must be 2, 4, or 8
scaling_factor = int(scaling_factor)
assert scaling_factor in {2, 4, 8}, "The scaling factor must be 2, 4, or 8!"
# The first convolutional block
self.conv_block1 = ConvolutionalBlock(in_channels=3, out_channels=n_channels, kernel_size=large_kernel_size,
batch_norm=False, activation='PReLu')
# A sequence of n_blocks residual blocks, each containing a skip-connection across the block
self.residual_blocks = nn.Sequential(
*[ResidualBlock(kernel_size=small_kernel_size, n_channels=n_channels) for i in range(n_blocks)])
# Another convolutional block
self.conv_block2 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels,
kernel_size=small_kernel_size,
batch_norm=True, activation=None)
# Upscaling is done by sub-pixel convolution, with each such block upscaling by a factor of 2
n_subpixel_convolution_blocks = int(math.log2(scaling_factor))
self.subpixel_convolutional_blocks = nn.Sequential(
*[SubPixelConvolutionalBlock(kernel_size=small_kernel_size, n_channels=n_channels, scaling_factor=2) for i
in range(n_subpixel_convolution_blocks)])
# The last convolutional block
self.conv_block3 = ConvolutionalBlock(in_channels=n_channels, out_channels=3, kernel_size=large_kernel_size,
batch_norm=False, activation='Tanh')
def forward(self, lr_imgs):
"""
Forward prop.
:param lr_imgs: low-resolution input images, a tensor of size (N, 3, w, h)
:return: super-resolution output images, a tensor of size (N, 3, w * scaling factor, h * scaling factor)
"""
output = self.conv_block1(lr_imgs) # (N, 3, w, h)
residual = output # (N, n_channels, w, h)
output = self.residual_blocks(output) # (N, n_channels, w, h)
output = self.conv_block2(output) # (N, n_channels, w, h)
output = output + residual # (N, n_channels, w, h)
output = self.subpixel_convolutional_blocks(output) # (N, n_channels, w * scaling factor, h * scaling factor)
sr_imgs = self.conv_block3(output) # (N, 3, w * scaling factor, h * scaling factor)
return sr_imgs
class SRDataset(Dataset):
"""
A PyTorch Dataset to be used by a PyTorch DataLoader.
"""
def __init__(self, data_folder, split, crop_size, scaling_factor, lr_img_type, hr_img_type, sample_fraction=1.0, test_data_name=None):
"""
:param data_folder: # folder with JSON data files
:param split: one of 'train' or 'test'
:param crop_size: crop size of target HR images
:param scaling_factor: the input LR images will be downsampled from the target HR images by this factor; the scaling done in the super-resolution
:param lr_img_type: the format for the LR image supplied to the model; see convert_image() in utils.py for available formats
:param hr_img_type: the format for the HR image supplied to the model; see convert_image() in utils.py for available formats
:param sample_fraction: fraction of the dataset to use (e.g., 0.02 for 2%)
:param test_data_name: if this is the 'test' split, which test dataset? (for example, "Set14")
"""
self.data_folder = data_folder
self.split = split.lower()
self.crop_size = int(crop_size)
self.scaling_factor = int(scaling_factor)
self.lr_img_type = lr_img_type
self.hr_img_type = hr_img_type
self.test_data_name = test_data_name
self.sample_fraction = sample_fraction
assert self.split in {'train', 'test'}
if self.split == 'test' and self.test_data_name is None:
raise ValueError("Please provide the name of the test dataset!")
assert lr_img_type in {'[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm'}
assert hr_img_type in {'[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm'}
if self.split == 'train':
assert self.crop_size % self.scaling_factor == 0, "Crop dimensions are not perfectly divisible by scaling factor! This will lead to a mismatch in the dimensions of the original HR patches and their super-resolved (SR) versions!"
# Read list of image-paths
if self.split == 'train':
with open(os.path.join(data_folder, 'train_images.json'), 'r') as j:
self.images = json.load(j)
else:
with open(os.path.join(data_folder, self.test_data_name + '_test_images.json'), 'r') as j:
self.images = json.load(j)
# Sample a fraction of the images if specified
if self.sample_fraction < 1.0:
self.images = random.sample(self.images, int(len(self.images) * self.sample_fraction))
# Select the correct set of transforms
self.transform = ImageTransforms(split=self.split,
crop_size=self.crop_size,
scaling_factor=self.scaling_factor,
lr_img_type=self.lr_img_type,
hr_img_type=self.hr_img_type)
def __getitem__(self, i):
"""
This method is required to be defined for use in the PyTorch DataLoader.
:param i: index to retrieve
:return: the 'i'th pair LR and HR images to be fed into the model
"""
# Read image
img = Image.open(self.images[i], mode='r')
img = img.convert('RGB')
if img.width <= 96 or img.height <= 96:
print(self.images[i], img.width, img.height)
lr_img, hr_img = self.transform(img)
return lr_img, hr_img
def __len__(self):
"""
This method is required to be defined for use in the PyTorch DataLoader.
:return: size of this data (in number of images)
"""
return len(self.images)
class ImageTransforms(object):
"""
Image transformation pipeline.
"""
def __init__(self, split, crop_size, scaling_factor, lr_img_type, hr_img_type):
"""
:param split: one of 'train' or 'test'
:param crop_size: crop size of HR images
:param scaling_factor: LR images will be downsampled from the HR images by this factor
:param lr_img_type: the target format for the LR image; see convert_image() above for available formats
:param hr_img_type: the target format for the HR image; see convert_image() above for available formats
"""
self.split = split.lower()
self.crop_size = crop_size
self.scaling_factor = scaling_factor
self.lr_img_type = lr_img_type
self.hr_img_type = hr_img_type
assert self.split in {'train', 'test'}
def __call__(self, img):
"""
:param img: a PIL source image from which the HR image will be cropped, and then downsampled to create the LR image
:return: LR and HR images in the specified format
"""
# Crop
if self.split == 'train':
# Take a random fixed-size crop of the image, which will serve as the high-resolution (HR) image
left = random.randint(1, img.width - self.crop_size)
top = random.randint(1, img.height - self.crop_size)
right = left + self.crop_size
bottom = top + self.crop_size
hr_img = img.crop((left, top, right, bottom))
else:
# Take the largest possible center-crop of it such that its dimensions are perfectly divisible by the scaling factor
x_remainder = img.width % self.scaling_factor
y_remainder = img.height % self.scaling_factor
left = x_remainder // 2
top = y_remainder // 2
right = left + (img.width - x_remainder)
bottom = top + (img.height - y_remainder)
hr_img = img.crop((left, top, right, bottom))
# Downsize this crop to obtain a low-resolution version of it
lr_img = hr_img.resize((int(hr_img.width / self.scaling_factor), int(hr_img.height / self.scaling_factor)),
Image.BICUBIC)
# Sanity check
assert hr_img.width == lr_img.width * self.scaling_factor and hr_img.height == lr_img.height * self.scaling_factor
# Convert the LR and HR image to the required type
lr_img = convert_image(lr_img, source='pil', target=self.lr_img_type)
hr_img = convert_image(hr_img, source='pil', target=self.hr_img_type)
return lr_img, hr_img
class AverageMeter(object):
"""
Keeps track of most recent, average, sum, and count of a metric.
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def clip_gradient(optimizer, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def save_checkpoint(state, filename):
"""
Save model checkpoint.
:param state: checkpoint contents
"""
torch.save(state, filename)
def adjust_learning_rate(optimizer, shrink_factor):
"""
Shrinks learning rate by a specified factor.
:param optimizer: optimizer whose learning rate must be shrunk.
:param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
"""
print("\nDECAYING learning rate.")
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * shrink_factor
print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
rgb_weights = torch.FloatTensor([65.481, 128.553, 24.966]).to(device)
imagenet_mean = torch.FloatTensor([0.485, 0.456, 0.406]).unsqueeze(1).unsqueeze(2)
imagenet_std = torch.FloatTensor([0.229, 0.224, 0.225]).unsqueeze(1).unsqueeze(2)
imagenet_mean_cuda = torch.FloatTensor([0.485, 0.456, 0.406]).to(device).unsqueeze(0).unsqueeze(2).unsqueeze(3)
imagenet_std_cuda = torch.FloatTensor([0.229, 0.224, 0.225]).to(device).unsqueeze(0).unsqueeze(2).unsqueeze(3)
def convert_image(img, source, target):
"""
Convert an image from a source format to a target format.
:param img: image
:param source: source format, one of 'pil' (PIL image), '[0, 1]' or '[-1, 1]' (pixel value ranges)
:param target: target format, one of 'pil' (PIL image), '[0, 255]', '[0, 1]', '[-1, 1]' (pixel value ranges),
'imagenet-norm' (pixel values standardized by imagenet mean and std.),
'y-channel' (luminance channel Y in the YCbCr color format, used to calculate PSNR and SSIM)
:return: converted image
"""
assert source in {'pil', '[0, 1]', '[-1, 1]'}, "Cannot convert from source format %s!" % source
assert target in {'pil', '[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm',
'y-channel'}, "Cannot convert to target format %s!" % target
# Convert from source to [0, 1]
if source == 'pil':
img = FT.to_tensor(img)
elif source == '[0, 1]':
pass # already in [0, 1]
elif source == '[-1, 1]':
img = (img + 1.) / 2.
# Convert from [0, 1] to target
if target == 'pil':
img = FT.to_pil_image(img)
elif target == '[0, 255]':
img = 255. * img
elif target == '[0, 1]':
pass # already in [0, 1]
elif target == '[-1, 1]':
img = 2. * img - 1.
elif target == 'imagenet-norm':
if img.ndimension() == 3:
img = (img - imagenet_mean) / imagenet_std
elif img.ndimension() == 4:
img = (img - imagenet_mean_cuda) / imagenet_std_cuda
elif target == 'y-channel':
# Based on definitions at https://github.com/xinntao/BasicSR/wiki/Color-conversion-in-SR
# torch.dot() does not work the same way as numpy.dot()
# So, use torch.matmul() to find the dot product between the last dimension of an 4-D tensor and a 1-D tensor
img = torch.matmul(255. * img.permute(0, 2, 3, 1)[:, 4:-4, 4:-4, :], rgb_weights) / 255. + 16.
return img
grad_clip = None # clip if gradients are exploding
print_freq = 500 # print training status once every __ batches
def train(train_loader, model, criterion, optimizer, epoch):
"""
One epoch's training.
:param train_loader: DataLoader for training data
:param model: model
:param criterion: content loss function (Mean Squared-Error loss)
:param optimizer: optimizer
:param epoch: epoch number
"""
model.train() # training mode enables batch normalization
batch_time = AverageMeter() # forward prop. + back prop. time
data_time = AverageMeter() # data loading time
losses = AverageMeter() # loss
start = time.time()
# Batches
for i, (lr_imgs, hr_imgs) in enumerate(train_loader):
data_time.update(time.time() - start)
# Move to default device
lr_imgs = lr_imgs.to(device) # (batch_size (N), 3, 24, 24), imagenet-normed
hr_imgs = hr_imgs.to(device) # (batch_size (N), 3, 96, 96), in [-1, 1]
# Forward prop.
sr_imgs = model(lr_imgs) # (N, 3, 96, 96), in [-1, 1]
# Loss
loss = criterion(sr_imgs, hr_imgs) # scalar
# Backward prop.
optimizer.zero_grad()
loss.backward()
# Clip gradients, if necessary
if grad_clip is not None:
clip_gradient(optimizer, grad_clip)
# Update model
optimizer.step()
# Keep track of loss
losses.update(loss.item(), lr_imgs.size(0))
# Keep track of batch time
batch_time.update(time.time() - start)
# Reset start time
start = time.time()
# Print status
if i % print_freq == 0:
print('Epoch: [{0}][{1}/{2}]----'
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})----'
'Data Time {data_time.val:.3f} ({data_time.avg:.3f})----'
'Loss {loss.val:.4f} ({loss.avg:.4f})'.format(epoch, i, len(train_loader),
batch_time=batch_time,
data_time=data_time, loss=losses))
del lr_imgs, hr_imgs, sr_imgs # free some memory since their histories may be stored
# Data parameters
data_folder = './' # folder with JSON data files
crop_size = 96 # crop size of target HR images
scaling_factor = 4 # the scaling factor for the generator; the input LR images will be downsampled from the target HR images by this factor
# Model parameters
large_kernel_size = 9 # kernel size of the first and last convolutions which transform the inputs and outputs
small_kernel_size = 3 # kernel size of all convolutions in-between, i.e. those in the residual and subpixel convolutional blocks
n_channels = 64 # number of channels in-between, i.e. the input and output channels for the residual and subpixel convolutional blocks
n_blocks = 16 # number of residual blocks
# Learning parameters
checkpoint = None # path to model checkpoint, None if none
batch_size = 16 # batch size
start_epoch = 0 # start at this epoch
iterations = 1e6 # number of training iterations
workers = 4 # number of workers for loading data in the DataLoader
lr = 1e-4 # learning rate
def main():
"""
Training.
"""
global start_epoch, epoch, checkpoint
# Initialize model or load checkpoint
if checkpoint is None:
model = SRResNet(large_kernel_size=large_kernel_size, small_kernel_size=small_kernel_size,
n_channels=n_channels, n_blocks=n_blocks, scaling_factor=scaling_factor)
optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=lr)
else:
checkpoint = torch.load(checkpoint)
start_epoch = checkpoint['epoch'] + 1
model = checkpoint['model']
optimizer = checkpoint['optimizer']
model = model.to(device)
criterion = nn.MSELoss().to(device)
# Custom dataloaders
train_dataset = SRDataset(data_folder, split='train', crop_size=crop_size, scaling_factor=scaling_factor,
lr_img_type='imagenet-norm', hr_img_type='[-1, 1]', sample_fraction=0.02)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=workers, pin_memory=True)
epochs = int(iterations // len(train_loader) + 1)
for epoch in range(start_epoch, epochs):
train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch)
torch.save({'epoch': epoch, 'model': model, 'optimizer': optimizer}, 'checkpoint_srresnet.pth.tar')
if __name__ == '__main__':
main()