-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathutil.py
595 lines (447 loc) · 19.9 KB
/
util.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# -*- coding: utf-8 -*-
'''
This is a PyTorch implementation of CURL: Neural Curve Layers for Global Image Enhancement
https://arxiv.org/pdf/1911.13175.pdf
Please cite paper if you use this code.
Tested with Pytorch 1.7.1, Python 3.7.9
Authors: Sean Moran (sean.j.moran@gmail.com),
'''
from skimage.metrics import structural_similarity as ssim
from PIL import Image
import math
import numpy as np
from torch.autograd import Variable
import torch
import matplotlib
import sys
matplotlib.use('agg')
np.set_printoptions(threshold=sys.maxsize)
class ImageProcessing(object):
@staticmethod
def rgb_to_lab(img, is_training=True):
""" PyTorch implementation of RGB to LAB conversion: https://docs.opencv.org/3.3.0/de/d25/imgproc_color_conversions.html
Based roughly on a similar implementation here: https://github.com/affinelayer/pix2pix-tensorflow/blob/master/pix2pix.py
:param img: image to be adjusted
:returns: adjusted image
:rtype: Tensor
"""
img = img.permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
img = img.view(-1, 3)
img = (img / 12.92) * img.le(0.04045).float() + (((torch.clamp(img,
min=0.0001) + 0.055) / 1.055) ** 2.4) * img.gt(0.04045).float()
rgb_to_xyz = Variable(torch.FloatTensor([ # X Y Z
[0.412453, 0.212671, 0.019334], # R
[0.357580, 0.715160, 0.119193], # G
[0.180423, 0.072169,
0.950227], # B
]), requires_grad=False).cuda()
img = torch.matmul(img, rgb_to_xyz)
img = torch.mul(img, Variable(torch.FloatTensor(
[1/0.950456, 1.0, 1/1.088754]), requires_grad=False).cuda())
epsilon = 6/29
img = ((img / (3.0 * epsilon**2) + 4.0/29.0) * img.le(epsilon**3).float()) + \
(torch.clamp(img, min=0.0001) **
(1.0/3.0) * img.gt(epsilon**3).float())
fxfyfz_to_lab = Variable(torch.FloatTensor([[0.0, 500.0, 0.0], # fx
# fy
[116.0, -500.0, 200.0],
# fz
[0.0, 0.0, -200.0],
]), requires_grad=False).cuda()
img = torch.matmul(img, fxfyfz_to_lab) + Variable(
torch.FloatTensor([-16.0, 0.0, 0.0]), requires_grad=False).cuda()
img = img.view(shape)
img = img.permute(2, 1, 0)
'''
L_chan: black and white with input range [0, 100]
a_chan/b_chan: color channels with input range ~[-110, 110], not exact
[0, 100] => [0, 1], ~[-110, 110] => [0, 1]
'''
img[0, :, :] = img[0, :, :]/100
img[1, :, :] = (img[1, :, :]/110 + 1)/2
img[2, :, :] = (img[2, :, :]/110 + 1)/2
img[(img != img).detach()] = 0
img = img.contiguous()
return img.cuda()
@staticmethod
def lab_to_rgb(img, is_training=True):
""" PyTorch implementation of LAB to RGB conversion: https://docs.opencv.org/3.3.0/de/d25/imgproc_color_conversions.html
Based roughly on a similar implementation here: https://github.com/affinelayer/pix2pix-tensorflow/blob/master/pix2pix.py
:param img: image to be adjusted
:returns: adjusted image
:rtype: Tensor
"""
img = img.permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
img = img.view(-1, 3)
img_copy = img.clone()
img_copy[:, 0] = img[:, 0] * 100
img_copy[:, 1] = ((img[:, 1] * 2)-1)*110
img_copy[:, 2] = ((img[:, 2] * 2)-1)*110
img = img_copy.clone().cuda()
del img_copy
lab_to_fxfyfz = Variable(torch.FloatTensor([ # X Y Z
[1/116.0, 1/116.0, 1/116.0], # R
[1/500.0, 0, 0], # G
[0, 0, -1/200.0], # B
]), requires_grad=False).cuda()
img = torch.matmul(
img + Variable(torch.cuda.FloatTensor([16.0, 0.0, 0.0])), lab_to_fxfyfz)
epsilon = 6.0/29.0
img = (((3.0 * epsilon**2 * (img-4.0/29.0)) * img.le(epsilon).float()) +
((torch.clamp(img, min=0.0001)**3.0) * img.gt(epsilon).float()))
# denormalize for D65 white point
img = torch.mul(img, Variable(
torch.cuda.FloatTensor([0.950456, 1.0, 1.088754])))
xyz_to_rgb = Variable(torch.FloatTensor([ # X Y Z
[3.2404542, -0.9692660, 0.0556434], # R
[-1.5371385, 1.8760108, -0.2040259], # G
[-0.4985314, 0.0415560, 1.0572252], # B
]), requires_grad=False).cuda()
img = torch.matmul(img, xyz_to_rgb)
img = (img * 12.92 * img.le(0.0031308).float()) + ((torch.clamp(img,
min=0.0001) ** (1/2.4) * 1.055) - 0.055) * img.gt(0.0031308).float()
img = img.view(shape)
img = img.permute(2, 1, 0)
img = img.contiguous()
img[(img != img).detach()] = 0
return img
@staticmethod
def swapimdims_3HW_HW3(img):
"""Move the image channels to the first dimension of the numpy
multi-dimensional array
:param img: numpy nd array representing the image
:returns: numpy nd array with permuted axes
:rtype: numpy nd array
"""
if img.ndim == 3:
return np.swapaxes(np.swapaxes(img, 1, 2), 0, 2)
elif img.ndim == 4:
return np.swapaxes(np.swapaxes(img, 2, 3), 1, 3)
@staticmethod
def swapimdims_HW3_3HW(img):
"""Move the image channels to the last dimensiion of the numpy
multi-dimensional array
:param img: numpy nd array representing the image
:returns: numpy nd array with permuted axes
:rtype: numpy nd array
"""
if img.ndim == 3:
return np.swapaxes(np.swapaxes(img, 0, 2), 1, 2)
elif img.ndim == 4:
return np.swapaxes(np.swapaxes(img, 1, 3), 2, 3)
@staticmethod
def load_image(img_filepath, normaliser):
"""Loads an image from file as a numpy multi-dimensional array
:param img_filepath: filepath to the image
:returns: image as a multi-dimensional numpy array
:rtype: multi-dimensional numpy array
"""
img = ImageProcessing.normalise_image(np.array(Image.open(img_filepath)), normaliser) # NB: imread normalises to 0-1
return img
@staticmethod
def normalise_image(img, normaliser):
"""Normalises image data to be a float between 0 and 1
:param img: Image as a numpy multi-dimensional image array
:returns: Normalised image as a numpy multi-dimensional image array
:rtype: Numpy array
"""
img = img.astype('float32') / normaliser
return img
@staticmethod
def compute_mse(original, result):
"""Computes the mean squared error between to RGB images represented as multi-dimensional numpy arrays.
:param original: input RGB image as a numpy array
:param result: target RGB image as a numpy array
:returns: the mean squared error between the input and target images
:rtype: float
"""
return ((original - result) ** 2).mean()
@staticmethod
def compute_psnr(image_batchA, image_batchB, max_intensity):
"""Computes the PSNR for a batch of input and output images
:param image_batchA: numpy nd-array representing the image batch A of shape Bx3xWxH
:param image_batchB: numpy nd-array representing the image batch A of shape Bx3xWxH
:param max_intensity: maximum intensity possible in the image (e.g. 255)
:returns: average PSNR for the batch of images
:rtype: float
"""
num_images = image_batchA.shape[0]
psnr_val = 0.0
for i in range(0, num_images):
imageA = image_batchA[i, 0:3, :, :]
imageB = image_batchB[i, 0:3, :, :]
imageB = np.maximum(0, np.minimum(imageB, max_intensity))
psnr_val += 10 * \
np.log10(max_intensity ** 2 /
ImageProcessing.compute_mse(imageA, imageB))
return psnr_val / num_images
@staticmethod
def compute_ssim(image_batchA, image_batchB):
"""Computes the SSIM for a batch of input and output images
:param image_batchA: numpy nd-array representing the image batch A of shape Bx3xWxH
:param image_batchB: numpy nd-array representing the image batch A of shape Bx3xWxH
:param max_intensity: maximum intensity possible in the image (e.g. 255)
:returns: average PSNR for the batch of images
:rtype: float
"""
num_images = image_batchA.shape[0]
ssim_val = 0.0
for i in range(0, num_images):
imageA = ImageProcessing.swapimdims_3HW_HW3(
image_batchA[i, 0:3, :, :])
imageB = ImageProcessing.swapimdims_3HW_HW3(
image_batchB[i, 0:3, :, :])
ssim_val += ssim(imageA, imageB, data_range=imageA.max() - imageA.min(), multichannel=True,
gaussian_weights=True, win_size=11)
return ssim_val / num_images
@staticmethod
def hsv_to_rgb(img):
"""Converts a HSV image to RGB
PyTorch implementation of RGB to HSV conversion: https://docs.opencv.org/3.3.0/de/d25/imgproc_color_conversions.html
Based roughly on a similar implementation here: http://code.activestate.com/recipes/576919-python-rgb-and-hsv-conversion/
:param img: HSV image
:returns: RGB image
:rtype: Tensor
"""
img=torch.clamp(img,0,1)
img = img.permute(2, 1, 0)
m1 = 0
m2 = (img[:, :, 2]*(1-img[:, :, 1])-img[:, :, 2])/60
m3 = 0
m4 = -1*m2
m5 = 0
r = img[:, :, 2]+torch.clamp(img[:, :, 0]*360-0, 0, 60)*m1+torch.clamp(img[:, :, 0]*360-60, 0, 60)*m2+torch.clamp(
img[:, :, 0]*360-120, 0, 120)*m3+torch.clamp(img[:, :, 0]*360-240, 0, 60)*m4+torch.clamp(img[:, :, 0]*360-300, 0, 60)*m5
m1 = (img[:, :, 2]-img[:, :, 2]*(1-img[:, :, 1]))/60
m2 = 0
m3 = -1*m1
m4 = 0
g = img[:, :, 2]*(1-img[:, :, 1])+torch.clamp(img[:, :, 0]*360-0, 0, 60)*m1+torch.clamp(img[:, :, 0]*360-60,
0, 120)*m2+torch.clamp(img[:, :, 0]*360-180, 0, 60)*m3+torch.clamp(img[:, :, 0]*360-240, 0, 120)*m4
m1 = 0
m2 = (img[:, :, 2]-img[:, :, 2]*(1-img[:, :, 1]))/60
m3 = 0
m4 = -1*m2
b = img[:, :, 2]*(1-img[:, :, 1])+torch.clamp(img[:, :, 0]*360-0, 0, 120)*m1+torch.clamp(img[:, :, 0]*360 -
120, 0, 60)*m2+torch.clamp(img[:, :, 0]*360-180, 0, 120)*m3+torch.clamp(img[:, :, 0]*360-300, 0, 60)*m4
img = torch.stack((r, g, b), 2)
img[(img != img).detach()] = 0
img = img.permute(2, 1, 0)
img = img.contiguous()
img = torch.clamp(img, 0, 1)
return img
@staticmethod
def rgb_to_hsv(img):
"""Converts an RGB image to HSV
PyTorch implementation of RGB to HSV conversion: https://docs.opencv.org/3.3.0/de/d25/imgproc_color_conversions.html
Based roughly on a similar implementation here: http://code.activestate.com/recipes/576919-python-rgb-and-hsv-conversion/
:param img: RGB image
:returns: HSV image
:rtype: Tensor
"""
img=torch.clamp(img,1e-9,1)
img = img.permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
img = img.view(-1, 3)
mx = torch.max(img, 1)[0]
mn = torch.min(img, 1)[0]
ones = Variable(torch.FloatTensor(
torch.ones((img.shape[0])))).cuda()
zero = Variable(torch.FloatTensor(torch.zeros(shape[0:2]))).cuda()
img = img.view(shape)
ones1 = ones[0:math.floor((ones.shape[0]/2))]
ones2 = ones[math.floor(ones.shape[0]/2):(ones.shape[0])]
mx1 = mx[0:math.floor((ones.shape[0]/2))]
mx2 = mx[math.floor(ones.shape[0]/2):(ones.shape[0])]
mn1 = mn[0:math.floor((ones.shape[0]/2))]
mn2 = mn[math.floor(ones.shape[0]/2):(ones.shape[0])]
df1 = torch.add(mx1, torch.mul(ones1*-1, mn1))
df2 = torch.add(mx2, torch.mul(ones2*-1, mn2))
df = torch.cat((df1, df2), 0)
del df1, df2
df = df.view(shape[0:2])+1e-10
mx = mx.view(shape[0:2])
img = img.cuda()
df = df.cuda()
mx = mx.cuda()
g = img[:, :, 1].clone().cuda()
b = img[:, :, 2].clone().cuda()
r = img[:, :, 0].clone().cuda()
img_copy = img.clone()
img_copy[:, :, 0] = (((g-b)/df)*r.eq(mx).float() + (2.0+(b-r)/df)
* g.eq(mx).float() + (4.0+(r-g)/df)*b.eq(mx).float())
img_copy[:, :, 0] = img_copy[:, :, 0]*60.0
zero = zero.cuda()
img_copy2 = img_copy.clone()
img_copy2[:, :, 0] = img_copy[:, :, 0].lt(zero).float(
)*(img_copy[:, :, 0]+360) + img_copy[:, :, 0].ge(zero).float()*(img_copy[:, :, 0])
img_copy2[:, :, 0] = img_copy2[:, :, 0]/360
del img, r, g, b
img_copy2[:, :, 1] = mx.ne(zero).float()*(df/mx) + \
mx.eq(zero).float()*(zero)
img_copy2[:, :, 2] = mx
img_copy2[(img_copy2 != img_copy2).detach()] = 0
img = img_copy2.clone()
img = img.permute(2, 1, 0)
img = torch.clamp(img, 1e-9, 1)
return img
@staticmethod
def apply_curve(img, C, slope_sqr_diff, channel_in, channel_out,
clamp=False):
"""Applies a peicewise linear curve defined by a set of knot points to
an image channel
:param img: image to be adjusted
:param C: predicted knot points of curve
:returns: adjusted image
:rtype: Tensor
"""
slope = Variable(torch.zeros((C.shape[0]-1))).cuda()
curve_steps = C.shape[0]-1
'''
Compute the slope of the line segments
'''
for i in range(0, C.shape[0]-1):
slope[i] = C[i+1]-C[i]
'''
Compute the squared difference between slopes
'''
for i in range(0, slope.shape[0]-1):
slope_sqr_diff += (slope[i+1]-slope[i])*(slope[i+1]-slope[i])
'''
Use predicted line segments to compute scaling factors for the channel
'''
scale = float(C[0])
for i in range(0, slope.shape[0]-1):
if clamp:
scale += float(slope[i])*(torch.clamp(img[:, :,channel_in]*curve_steps-i,0,1))
else:
scale += float(slope[i])*(img[:, :,channel_in]*curve_steps-i)
img_copy = img.clone()
img_copy[:, :, channel_out] = img[:, :, channel_out]*scale
img_copy = torch.clamp(img_copy,0,1)
return img_copy, slope_sqr_diff
@staticmethod
def adjust_hsv(img, S):
"""Adjust the HSV channels of a HSV image using learnt curves
:param img: image to be adjusted
:param S: predicted parameters of piecewise linear curves
:returns: adjust image, regularisation term
:rtype: Tensor, float
"""
img = img.squeeze(0).permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
S1 = torch.exp(S[0:int(S.shape[0]/4)])
S2 = torch.exp(S[(int(S.shape[0]/4)):(int(S.shape[0]/4)*2)])
S3 = torch.exp(S[(int(S.shape[0]/4)*2):(int(S.shape[0]/4)*3)])
S4 = torch.exp(S[(int(S.shape[0]/4)*3):(int(S.shape[0]/4)*4)])
slope_sqr_diff = Variable(torch.zeros(1)*0.0).cuda()
'''
Adjust Hue channel based on Hue using the predicted curve
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img, S1, slope_sqr_diff, channel_in=0, channel_out=0)
'''
Adjust Saturation channel based on Hue using the predicted curve
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, S2, slope_sqr_diff, channel_in=0, channel_out=1)
'''
Adjust Saturation channel based on Saturation using the predicted curve
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, S3, slope_sqr_diff, channel_in=1, channel_out=1)
'''
Adjust Value channel based on Value using the predicted curve
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, S4, slope_sqr_diff, channel_in=2, channel_out=2)
img = img_copy.clone()
del img_copy
img[(img != img).detach()] = 0
img = img.permute(2, 1, 0)
img = img.contiguous()
return img, slope_sqr_diff
@staticmethod
def adjust_rgb(img, R):
"""Adjust the RGB channels of a RGB image using learnt curves
:param img: image to be adjusted
:param S: predicted parameters of piecewise linear curves
:returns: adjust image, regularisation term
:rtype: Tensor, float
"""
img = img.squeeze(0).permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
'''
Extract the parameters of the three curves
'''
R1 = torch.exp(R[0:int(R.shape[0]/3)])
R2 = torch.exp(R[(int(R.shape[0]/3)):(int(R.shape[0]/3)*2)])
R3 = torch.exp(R[(int(R.shape[0]/3)*2):(int(R.shape[0]/3)*3)])
'''
Apply the curve to the R channel
'''
slope_sqr_diff = Variable(torch.zeros(1)*0.0).cuda()
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img, R1, slope_sqr_diff, channel_in=0, channel_out=0)
'''
Apply the curve to the G channel
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, R2, slope_sqr_diff, channel_in=1, channel_out=1)
'''
Apply the curve to the B channel
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, R3, slope_sqr_diff, channel_in=2, channel_out=2)
img = img_copy.clone()
del img_copy
img[(img != img).detach()] = 0
img = img.permute(2, 1, 0)
img = img.contiguous()
return img, slope_sqr_diff
@staticmethod
def adjust_lab(img, L):
"""Adjusts the image in LAB space using the predicted curves
:param img: Image tensor
:param L: Predicited curve parameters for LAB channels
:returns: adjust image, and regularisation parameter
:rtype: Tensor, float
"""
img = img.permute(2, 1, 0)
shape = img.shape
img = img.contiguous()
'''
Extract predicted parameters for each L,a,b curve
'''
L1 = torch.exp(L[0:int(L.shape[0]/3)])
L2 = torch.exp(L[(int(L.shape[0]/3)):(int(L.shape[0]/3)*2)])
L3 = torch.exp(L[(int(L.shape[0]/3)*2):(int(L.shape[0]/3)*3)])
slope_sqr_diff = Variable(torch.zeros(1)*0.0).cuda()
'''
Apply the curve to the L channel
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img, L1, slope_sqr_diff, channel_in=0, channel_out=0)
'''
Now do the same for the a channel
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, L2, slope_sqr_diff, channel_in=1, channel_out=1)
'''
Now do the same for the b channel
'''
img_copy, slope_sqr_diff = ImageProcessing.apply_curve(
img_copy, L3, slope_sqr_diff, channel_in=2, channel_out=2)
img = img_copy.clone()
del img_copy
img[(img != img).detach()] = 0
img = img.permute(2, 1, 0)
img = img.contiguous()
return img, slope_sqr_diff