-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.py
369 lines (314 loc) · 11.5 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
"""
Different utilities such as orthogonalization of weights, initialization of
loggers, etc
Copyright (C) 2018, 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 skimage.measure.simple_metrics import compare_psnr
IMAGETYPES = ('*.bmp', '*.png', '*.jpg', '*.jpeg', '*.tif') # Supported image types
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 = []
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)
print("\tLoaded sequence ", seq_dir)
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 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().numpy().astype(np.float32)
imgclean = imclean.data.cpu().numpy().astype(np.float32)
psnr = 0
for i in range(img_cpu.shape[0]):
psnr += compare_psnr(imgclean[i, :, :, :], img_cpu[i, :, :, :], \
data_range=data_range)
return psnr/img_cpu.shape[0]
def augment_train_pair(inarr):
r"""Performs data augmentation to every image in the input array
Args:
inarr: float32 [0., 1.] normalized (temp_psz+1)xCxHxW array
"""
assert len(inarr.shape) == 4
# define transformations
# def scale_img(img):
# from random import choice, choices
# assert img.max() <= 1.0
#
# scales = [0.9, 0.8, 0.7, 0.6]
# sca = choice(scales)
# # CxHxW to HxWxC, uint8
# img = (np.moveaxis(img, 0, 2)*255.).clip(0, 255).astype(np.uint8)
# img = cv2.resize(img, (0, 0), fx=sca, fy=sca, interpolation=cv2.INTER_CUBIC)
# img = normalize(np.moveaxis(img, 2, 0))
# return img
do_nothing = lambda x: x
flipud = lambda x: x[:, ::-1, :]
rot90 = lambda x: np.rot90(x, axes=(1, 2))
rot90_flipud = lambda x: (np.rot90(x, axes=(1, 2)))[:, ::-1, :]
rot180 = lambda x: np.rot90(x, k=2, axes=(1, 2))
rot180_flipud = lambda x: (np.rot90(x, k=2, axes=(1, 2)))[:, ::-1, :]
rot270 = lambda x: np.rot90(x, k=3, axes=(1, 2))
rot270_flipud = lambda x: (np.rot90(x, k=3, axes=(1, 2)))[:, ::-1, :]
N, _, _, _ = inarr.shape
# define transformations and their frequency, then pick one.
# aug_list = [do_nothing, scale_img, flipud, rot90, rot90_flipud, \
# rot180, rot180_flipud, rot270, rot270_flipud]
# w_aug = [7, 21, 2, 2, 2, 2, 2, 2, 2]
aug_list = [do_nothing, flipud, rot90, rot90_flipud, \
rot180, rot180_flipud, rot270, rot270_flipud]
w_aug = [7, 4, 4, 4, 4, 4, 4, 4]
transf = choices(aug_list, w_aug)
# transform all images in array
tr_list = list()
for j in range(N):
tr_list.append(transf[0](inarr[j, ...]))
return np.array(tr_list)
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):
r"""Closes a logger instance
Args:
logger: logging.Logger
"""
xh = list(logger.handlers)
for i in xh:
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)
# Convert filter matrix to numpy array
weights = weights.cpu().numpy()
# SVD decomposition and orthogonalization
mat_u, _, mat_vh = np.linalg.svd(weights, full_matrices=False)
weights = np.dot(mat_u, mat_vh)
# As full_matrices=False we don't need to set s[:] = 1 and do mat_u*s
lyr.weight.data = torch.Tensor(weights).view(f1, f2, c_in, c_out).\
permute(3, 2, 0, 1).type(dtype)
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, vl in state_dict.items():
name = k[7:] # remove 'module.' of DataParallel
new_state_dict[name] = vl
return new_state_dict
def sequence_psnr(seq, seqclean, data_range=1.0):
r"""
Computes the mean PSNR of a sequence (not pixel-wise)
Args:
seq: array of dims [num_frames, C, H, W], C=1 grayscale or C=3 RGB, H and W are even.
seqclean: reference array of dims [num_frames, C, H, W],
C=1 grayscale or C=3 RGB, H and W are even.
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.
"""
assert len(seq.shape) == 4
psnr = 0
for i in range(seq.shape[0]):
psnr += compare_psnr(seq[i, :, :, :], seqclean[i, :, :, :], data_range=data_range)
return psnr/seq.shape[0]