-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
447 lines (389 loc) · 14.9 KB
/
utils.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
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
"""
Different utilities such as orthogonalization of weights, initialization of
loggers, etc
Copyright (C) 2019, Matias Tassano <matias.tassano@parisdescartes.fr>
This program is free software: you can use, modify and/or
redistribute it under the terms of the GNU General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later
version. You should have received a copy of this license along
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import subprocess
import glob
import logging
from random import choices # requires Python >= 3.6
import numpy as np
import cv2
import torch
from PIL import Image
from io import BytesIO
import torchvision
from skimage.metrics import peak_signal_noise_ratio, structural_similarity as ssim
from tensorboardX import SummaryWriter
IMAGETYPES = ('*.bmp', '*.png', '*.jpg', '*.jpeg', '*.tif') # Supported image types
def normalize_augment(datain, ctrl_fr_idx):
'''Normalizes and augments an input patch of dim [N, num_frames, C. H, W] in [0., 255.] to \
[N, num_frames*C. H, W] in [0., 1.]. It also returns the central frame of the temporal \
patch as a ground truth.
'''
def transform(sample):
# define transformations
do_nothing = lambda x: x
do_nothing.__name__ = 'do_nothing'
flipud = lambda x: torch.flip(x, dims=[2])
flipud.__name__ = 'flipup'
rot90 = lambda x: torch.rot90(x, k=1, dims=[2, 3])
rot90.__name__ = 'rot90'
rot90_flipud = lambda x: torch.flip(torch.rot90(x, k=1, dims=[2, 3]), dims=[2])
rot90_flipud.__name__ = 'rot90_flipud'
rot180 = lambda x: torch.rot90(x, k=2, dims=[2, 3])
rot180.__name__ = 'rot180'
rot180_flipud = lambda x: torch.flip(torch.rot90(x, k=2, dims=[2, 3]), dims=[2])
rot180_flipud.__name__ = 'rot180_flipud'
rot270 = lambda x: torch.rot90(x, k=3, dims=[2, 3])
rot270.__name__ = 'rot270'
rot270_flipud = lambda x: torch.flip(torch.rot90(x, k=3, dims=[2, 3]), dims=[2])
rot270_flipud.__name__ = 'rot270_flipud'
add_csnt = lambda x: x + torch.normal(mean=torch.zeros(x.size()[0], 1, 1, 1), \
std=(5/255.)).expand_as(x).to(x.device)
add_csnt.__name__ = 'add_csnt'
# define transformations and their frequency, then pick one.
aug_list = [do_nothing, flipud, rot90, rot90_flipud, \
rot180, rot180_flipud, rot270, rot270_flipud, add_csnt]
w_aug = [32, 12, 12, 12, 12, 12, 12, 12, 12] # one fourth chances to do_nothing
transf = choices(aug_list, w_aug)
# transform all images in array
return transf[0](sample)
img_train = datain
# convert to [N, num_frames*C. H, W] in [0., 1.] from [N, num_frames, C. H, W] in [0., 255.]
img_train = img_train.view(img_train.size()[0], -1, \
img_train.size()[-2], img_train.size()[-1]) / 255.
#augment
img_train = transform(img_train)
# extract ground truth (central frame)
gt_train = img_train[:, 3*ctrl_fr_idx:3*ctrl_fr_idx+3, :, :]
return img_train, gt_train
def init_logging(argdict):
"""Initilizes the logging and the SummaryWriter modules
"""
if not os.path.exists(argdict['log_dir']):
os.makedirs(argdict['log_dir'])
writer = SummaryWriter(argdict['log_dir'])
logger = init_logger(argdict['log_dir'], argdict)
return writer, logger
def get_imagenames(seq_dir, pattern=None):
""" Get ordered list of filenames
"""
files = []
for typ in IMAGETYPES:
files.extend(glob.glob(os.path.join(seq_dir, typ)))
# filter filenames
if not pattern is None:
ffiltered = []
ffiltered = [f for f in files if pattern in os.path.split(f)[-1]]
files = ffiltered
del ffiltered
# sort filenames alphabetically
files.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
return files
def open_sequence(seq_dir, gray_mode, expand_if_needed=False, max_num_fr=100):
r""" Opens a sequence of images and expands it to even sizes if necesary
Args:
fpath: string, path to image sequence
gray_mode: boolean, True indicating if images is to be open are in grayscale mode
expand_if_needed: if True, the spatial dimensions will be expanded if
size is odd
expand_axis0: if True, output will have a fourth dimension
max_num_fr: maximum number of frames to load
Returns:
seq: array of dims [num_frames, C, H, W], C=1 grayscale or C=3 RGB, H and W are even.
The image gets normalized gets normalized to the range [0, 1].
expanded_h: True if original dim H was odd and image got expanded in this dimension.
expanded_w: True if original dim W was odd and image got expanded in this dimension.
"""
# Get ordered list of filenames
files = get_imagenames(seq_dir)
seq_list = []
print("\tOpen sequence in folder: ", seq_dir)
for fpath in files[0:max_num_fr]:
img, expanded_h, expanded_w = open_image(fpath,\
gray_mode=gray_mode,\
expand_if_needed=expand_if_needed,\
expand_axis0=False)
seq_list.append(img)
seq = np.stack(seq_list, axis=0)
return seq, expanded_h, expanded_w
def open_image(fpath, gray_mode, expand_if_needed=False, expand_axis0=True, normalize_data=True):
r""" Opens an image and expands it if necesary
Args:
fpath: string, path of image file
gray_mode: boolean, True indicating if image is to be open
in grayscale mode
expand_if_needed: if True, the spatial dimensions will be expanded if
size is odd
expand_axis0: if True, output will have a fourth dimension
Returns:
img: image of dims NxCxHxW, N=1, C=1 grayscale or C=3 RGB, H and W are even.
if expand_axis0=False, the output will have a shape CxHxW.
The image gets normalized gets normalized to the range [0, 1].
expanded_h: True if original dim H was odd and image got expanded in this dimension.
expanded_w: True if original dim W was odd and image got expanded in this dimension.
"""
if not gray_mode:
# Open image as a CxHxW torch.Tensor
img = cv2.imread(fpath)
# from HxWxC to CxHxW, RGB image
img = (cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).transpose(2, 0, 1)
else:
# from HxWxC to CxHxW grayscale image (C=1)
img = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE)
if expand_axis0:
img = np.expand_dims(img, 0)
# Handle odd sizes
expanded_h = False
expanded_w = False
sh_im = img.shape
if expand_if_needed:
if sh_im[-2]%2 == 1:
expanded_h = True
if expand_axis0:
img = np.concatenate((img, \
img[:, :, -1, :][:, :, np.newaxis, :]), axis=2)
else:
img = np.concatenate((img, \
img[:, -1, :][:, np.newaxis, :]), axis=1)
if sh_im[-1]%2 == 1:
expanded_w = True
if expand_axis0:
img = np.concatenate((img, \
img[:, :, :, -1][:, :, :, np.newaxis]), axis=3)
else:
img = np.concatenate((img, \
img[:, :, -1][:, :, np.newaxis]), axis=2)
if normalize_data:
img = normalize(img)
return img, expanded_h, expanded_w
def rgb2ycbcr(im_rgb):
im_rgb = im_rgb.astype(np.float32)
im_ycrcb = cv2.cvtColor(im_rgb, cv2.COLOR_RGB2YCrCb)
im_ycbcr = im_ycrcb[:,:,(0,2,1)].astype(np.float32)
im_ycbcr[:,:,0] = (im_ycbcr[:,:,0]*(235-16)+16)/255.0 #to [16/255, 235/255]
im_ycbcr[:,:,1:] = (im_ycbcr[:,:,1:]*(240-16)+16)/255.0 #to [16/255, 240/255]
return im_ycbcr
def batch_psnr_ycbcr(img, imclean, data_range):
r"""
Computes the PSNR along the batch dimension (not pixel-wise)
Args:
img: a `torch.Tensor` containing the restored image
imclean: a `torch.Tensor` containing the reference image
data_range: The data range of the input image (distance between
minimum and maximum possible values). By default, this is estimated
from the image data-type.
"""
img_cpu = img.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32).copy() * 255.
imgclean = imclean.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32).copy() * 255.
psnr = 0
for i in range(img_cpu.shape[0]):
im1 = rgb2ycbcr(imgclean[i, :, :, :])
im2 = rgb2ycbcr(img_cpu[i, :, :, :])
psnr += peak_signal_noise_ratio(im1[:, :, 0], im2[:, :, 0], \
data_range=255.)
return psnr/img_cpu.shape[0]
def batch_ssim_ycbcr(img, imclean, data_range=None):
r"""
Computes the SSIM along the batch dimension (not pixel-wise)
Args:
img: a `torch.Tensor` containing the restored image
imclean: a `torch.Tensor` containing the reference image
data_range: The data range of the input image (distance between
minimum and maximum possible values). By default, this is estimated
from the image data-type.
"""
img_cpu = img.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32) * 255.
imgclean = imclean.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32) * 255.
ssim_ = 0
for i in range(img_cpu.shape[0]):
im1 = rgb2ycbcr(imgclean[i, :, :, :])
im2 = rgb2ycbcr(img_cpu[i, :, :, :])
ssim_ += ssim(im1[:, :, 0], im2[:, :, 0], \
data_range=255., multichannel=False)
return ssim_/img_cpu.shape[0]
def batch_psnr(img, imclean, data_range):
r"""
Computes the PSNR along the batch dimension (not pixel-wise)
Args:
img: a `torch.Tensor` containing the restored image
imclean: a `torch.Tensor` containing the reference image
data_range: The data range of the input image (distance between
minimum and maximum possible values). By default, this is estimated
from the image data-type.
"""
img_cpu = img.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32)
imgclean = imclean.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32)
psnr = 0
for i in range(img_cpu.shape[0]):
im1 = imgclean[i, :, :, :]
im2 = img_cpu[i, :, :, :]
psnr += peak_signal_noise_ratio(im1, im2, \
data_range=data_range)
return psnr/img_cpu.shape[0]
def batch_ssim(img, imclean, data_range=None):
r"""
Computes the SSIM along the batch dimension (not pixel-wise)
Args:
img: a `torch.Tensor` containing the restored image
imclean: a `torch.Tensor` containing the reference image
data_range: The data range of the input image (distance between
minimum and maximum possible values). By default, this is estimated
from the image data-type.
"""
img_cpu = img.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32)
imgclean = imclean.data.cpu().clone().detach().permute(0, 2, 3, 1).numpy().astype(np.float32)
ssim_ = 0
for i in range(img_cpu.shape[0]):
im1 = imgclean[i, :, :, :]
im2 = img_cpu[i, :, :, :]
ssim_ += ssim(im1, im2, \
data_range=data_range, multichannel=True)
return ssim_/img_cpu.shape[0]
def variable_to_cv2_image(invar, conv_rgb_to_bgr=True):
r"""Converts a torch.autograd.Variable to an OpenCV image
Args:
invar: a torch.autograd.Variable
conv_rgb_to_bgr: boolean. If True, convert output image from RGB to BGR color space
Returns:
a HxWxC uint8 image
"""
assert torch.max(invar) <= 1.0
size4 = len(invar.size()) == 4
if size4:
nchannels = invar.size()[1]
else:
nchannels = invar.size()[0]
if nchannels == 1:
if size4:
res = invar.data.cpu().numpy()[0, 0, :]
else:
res = invar.data.cpu().numpy()[0, :]
res = (res*255.).clip(0, 255).astype(np.uint8)
elif nchannels == 3:
if size4:
res = invar.data.cpu().numpy()[0]
else:
res = invar.data.cpu().numpy()
res = res.transpose(1, 2, 0)
res = (res*255.).clip(0, 255).astype(np.uint8)
if conv_rgb_to_bgr:
res = cv2.cvtColor(res, cv2.COLOR_RGB2BGR)
else:
raise Exception('Number of color channels not supported')
return res
def get_git_revision_short_hash():
r"""Returns the current Git commit.
"""
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
def init_logger(log_dir, argdict):
r"""Initializes a logging.Logger to save all the running parameters to a
log file
Args:
log_dir: path in which to save log.txt
argdict: dictionary of parameters to be logged
"""
from os.path import join
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
fh = logging.FileHandler(join(log_dir, 'log.txt'), mode='w+')
formatter = logging.Formatter('%(asctime)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
try:
logger.info("Commit: {}".format(get_git_revision_short_hash()))
except Exception as e:
logger.error("Couldn't get commit number: {}".format(e))
logger.info("Arguments: ")
for k in argdict.keys():
logger.info("\t{}: {}".format(k, argdict[k]))
return logger
def init_logger_test(result_dir):
r"""Initializes a logging.Logger in order to log the results after testing
a model
Args:
result_dir: path to the folder with the denoising results
"""
from os.path import join
logger = logging.getLogger('testlog')
logger.setLevel(level=logging.INFO)
fh = logging.FileHandler(join(result_dir, 'log.txt'), mode='w+')
formatter = logging.Formatter('%(asctime)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
def close_logger(logger):
'''Closes the logger instance
'''
x = list(logger.handlers)
for i in x:
logger.removeHandler(i)
i.flush()
i.close()
def normalize(data):
r"""Normalizes a unit8 image to a float32 image in the range [0, 1]
Args:
data: a unint8 numpy array to normalize from [0, 255] to [0, 1]
"""
return np.float32(data/255.)
def svd_orthogonalization(lyr):
r"""Applies regularization to the training by performing the
orthogonalization technique described in the paper "An Analysis and Implementation of
the FFDNet Image Denoising Method." Tassano et al. (2019).
For each Conv layer in the model, the method replaces the matrix whose columns
are the filters of the layer by new filters which are orthogonal to each other.
This is achieved by setting the singular values of a SVD decomposition to 1.
This function is to be called by the torch.nn.Module.apply() method,
which applies svd_orthogonalization() to every layer of the model.
"""
classname = lyr.__class__.__name__
if classname.find('Conv') != -1:
weights = lyr.weight.data.clone()
c_out, c_in, f1, f2 = weights.size()
dtype = lyr.weight.data.type()
# Reshape filters to columns
# From (c_out, c_in, f1, f2) to (f1*f2*c_in, c_out)
weights = weights.permute(2, 3, 1, 0).contiguous().view(f1*f2*c_in, c_out)
try:
# SVD decomposition and orthogonalization
mat_u, _, mat_v = torch.svd(weights)
weights = torch.mm(mat_u, mat_v.t())
lyr.weight.data = weights.view(f1, f2, c_in, c_out).permute(3, 2, 0, 1).type(dtype)
except:
pass
else:
pass
def remove_dataparallel_wrapper(state_dict):
r"""Converts a DataParallel model to a normal one by removing the "module."
wrapper in the module dictionary
Args:
state_dict: a torch.nn.DataParallel state dictionary
"""
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove 'module.' of DataParallel
new_state_dict[name] = v
return new_state_dict
def apply_jpeg_artifacts(im, q=50, format='jpeg'):
"""
Apply jpeg artifacts to a given image
:param im: tensor (3xhxw) representing the image
:param q: quantization factor. Default=50
:return: a tensor (3xhxw) representing the compressed image
"""
out = BytesIO()
im = torchvision.transforms.ToPILImage()(im.cpu())
im.save(out, format=format, quality=q)
out.seek(0)
im = torchvision.transforms.ToTensor()(Image.open(out))
out.close()
return im