-
Notifications
You must be signed in to change notification settings - Fork 28
/
model.py
1003 lines (766 loc) · 37.7 KB
/
model.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
#This program is free software; you can redistribute it and/or modify it under the terms of the BSD 0-Clause License.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD 0-Clause License for more details.
'''
This is a PyTorch implementation of the CVPR 2020 paper:
"Deep Local Parametric Filters for Image Enhancement": https://arxiv.org/abs/2003.13985
Please cite the paper if you use this code
Tested with Pytorch 1.7.1, Python 3.7.9
Authors: Sean Moran (sean.j.moran@gmail.com),
Pierre Marza (pierre.marza@gmail.com)
'''
import matplotlib
import torch
import torch.nn as nn
import unet
from math import exp
import math
import torch.nn.functional as F
from util import ImageProcessing
from torch.autograd import Variable
matplotlib.use('agg')
class DeepLPFLoss(nn.Module):
def __init__(self, ssim_window_size=5, alpha=0.5):
"""Initialisation of the DeepLPF loss function
:param ssim_window_size: size of averaging window for SSIM
:param alpha: interpolation paramater for L1 and SSIM parts of the loss
:returns: N/A
:rtype: N/A
"""
super(DeepLPFLoss, self).__init__()
self.alpha = alpha
self.ssim_window_size = ssim_window_size
def create_window(self, window_size, num_channel):
"""Window creation function for SSIM metric. Gaussian weights are applied to the window.
Code adapted from: https://github.com/Po-Hsun-Su/pytorch-ssim/blob/master/pytorch_ssim/__init__.py
:param window_size: size of the window to compute statistics
:param num_channel: number of channels
:returns: Tensor of shape Cx1xWindow_sizexWindow_size
:rtype: Tensor
"""
_1D_window = self.gaussian(window_size, 1.5).unsqueeze(1)
_2D_window = _1D_window.mm(
_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
window = Variable(_2D_window.expand(
num_channel, 1, window_size, window_size).contiguous())
return window
def gaussian(self, window_size, sigma):
"""
Code adapted from: https://github.com/Po-Hsun-Su/pytorch-ssim/blob/master/pytorch_ssim/__init__.py
:param window_size: size of the SSIM sampling window e.g. 11
:param sigma: Gaussian variance
:returns: 1xWindow_size Tensor of Gaussian weights
:rtype: Tensor
"""
gauss = torch.Tensor(
[exp(-(x - window_size // 2) ** 2 / float(2 * sigma ** 2)) for x in range(window_size)])
return gauss / gauss.sum()
def compute_ssim(self, img1, img2):
"""Computes the structural similarity index between two images. This function is differentiable.
Code adapted from: https://github.com/Po-Hsun-Su/pytorch-ssim/blob/master/pytorch_ssim/__init__.py
:param img1: image Tensor BxCxHxW
:param img2: image Tensor BxCxHxW
:returns: mean SSIM
:rtype: float
"""
(_, num_channel, _, _) = img1.size()
window = self.create_window(self.ssim_window_size, num_channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
mu1 = F.conv2d(
img1, window, padding=self.ssim_window_size // 2, groups=num_channel)
mu2 = F.conv2d(
img2, window, padding=self.ssim_window_size // 2, groups=num_channel)
mu1_sq = mu1.pow(2)
mu2_sq = mu2.pow(2)
mu1_mu2 = mu1 * mu2
sigma1_sq = F.conv2d(
img1 * img1, window, padding=self.ssim_window_size // 2, groups=num_channel) - mu1_sq
sigma2_sq = F.conv2d(
img2 * img2, window, padding=self.ssim_window_size // 2, groups=num_channel) - mu2_sq
sigma12 = F.conv2d(
img1 * img2, window, padding=self.ssim_window_size // 2, groups=num_channel) - mu1_mu2
C1 = 0.01 ** 2
C2 = 0.03 ** 2
ssim_map1 = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2))
ssim_map2 = ((mu1_sq.cuda() + mu2_sq.cuda() + C1) *
(sigma1_sq.cuda() + sigma2_sq.cuda() + C2))
ssim_map = ssim_map1.cuda() / ssim_map2.cuda()
v1 = 2.0 * sigma12.cuda() + C2
v2 = sigma1_sq.cuda() + sigma2_sq.cuda() + C2
cs = torch.mean(v1 / v2)
return ssim_map.mean(), cs
def compute_msssim(self, img1, img2):
"""Computes the multi scale structural similarity index between two images. This function is differentiable.
Code adapted from: https://github.com/Po-Hsun-Su/pytorch-ssim/blob/master/pytorch_ssim/__init__.py
:param img1: image Tensor BxCxHxW
:param img2: image Tensor BxCxHxW
:returns: mean SSIM
:rtype: float
"""
if img1.shape[2]!=img2.shape[2]:
img1=img1.transpose(2,3)
if img1.shape != img2.shape:
raise RuntimeError('Input images must have the same shape (%s vs. %s).',
img1.shape, img2.shape)
if img1.ndim != 4:
raise RuntimeError('Input images must have four dimensions, not %d',
img1.ndim)
device = img1.device
weights = torch.FloatTensor([0.0448, 0.2856, 0.3001, 0.2363, 0.1333]).to(device)
levels = weights.size()[0]
ssims = []
mcs = []
for _ in range(levels):
ssim, cs = self.compute_ssim(img1, img2)
# Relu normalize (not compliant with original definition)
ssims.append(ssim)
mcs.append(cs)
img1 = F.avg_pool2d(img1, (2, 2))
img2 = F.avg_pool2d(img2, (2, 2))
ssims = torch.stack(ssims)
mcs = torch.stack(mcs)
# Simple normalize (not compliant with original definition)
# TODO: remove support for normalize == True (kept for backward support)
ssims = (ssims + 1) / 2
mcs = (mcs + 1) / 2
pow1 = mcs ** weights
pow2 = ssims ** weights
# From Matlab implementation https://ece.uwaterloo.ca/~z70wang/research/iwssim/
output = torch.prod(pow1[:-1] * pow2[-1])
return output
def forward(self, predicted_img_batch, target_img_batch):
"""Forward function for the DeepLPF loss
:param predicted_img_batch:
:param target_img_batch: Tensor of shape BxCxWxH
:returns: value of loss function
:rtype: float
"""
if predicted_img_batch.shape[2]!=target_img_batch.shape[2]:
target_img_batch=target_img_batch.transpose(2,3)
num_images = target_img_batch.shape[0]
target_img_batch = target_img_batch
ssim_loss_value = Variable(
torch.cuda.FloatTensor(torch.zeros(1, 1).cuda()))
l1_loss_value = Variable(
torch.cuda.FloatTensor(torch.zeros(1, 1).cuda()))
for i in range(0, num_images):
target_img = target_img_batch[i, :, :, :].cuda()
predicted_img = predicted_img_batch[i, :, :, :].cuda()
predicted_img_lab = ImageProcessing.rgb_to_lab(
predicted_img.squeeze(0))
target_img_lab = ImageProcessing.rgb_to_lab(target_img.squeeze(0))
target_img_L_ssim = target_img_lab[0, :, :].unsqueeze(0)
predicted_img_L_ssim = predicted_img_lab[0, :, :].unsqueeze(0)
target_img_L_ssim = target_img_L_ssim.unsqueeze(0)
predicted_img_L_ssim = predicted_img_L_ssim.unsqueeze(0)
ssim_value = self.compute_msssim(
predicted_img_L_ssim, target_img_L_ssim)
ssim_loss_value += (1.0 - ssim_value)
l1_loss_value += F.l1_loss(predicted_img_lab, target_img_lab)
l1_loss_value = l1_loss_value/num_images
ssim_loss_value = ssim_loss_value/num_images
deeplpf_loss = l1_loss_value + 1e-3*ssim_loss_value
return deeplpf_loss
class BinaryLayer(nn.Module):
def forward(self, input):
"""Forward function for binary layer
:param input: data
:returns: sign of data
:rtype: Tensor
"""
return torch.sign(input)
def backward(self, grad_output):
"""Straight through estimator
:param grad_output: gradient tensor
:returns: truncated gradient tensor
:rtype: Tensor
"""
input = self.saved_tensors
grad_output[input > 1] = 0
grad_output[input < -1] = 0
return grad_output
class CubicFilter(nn.Module):
def __init__(self, num_in_channels=64, num_out_channels=64, batch_size=1):
"""Initialisation function
:param block: a block (layer) of the neural network
:param num_layers: number of neural network layers
:returns: initialises parameters of the neural networ
:rtype: N/A
"""
super(CubicFilter, self).__init__()
self.cubic_layer1 = ConvBlock(num_in_channels, num_out_channels)
self.cubic_layer2 = MaxPoolBlock()
self.cubic_layer3 = ConvBlock(num_out_channels, num_out_channels)
self.cubic_layer4 = MaxPoolBlock()
self.cubic_layer5 = ConvBlock(num_out_channels, num_out_channels)
self.cubic_layer6 = MaxPoolBlock()
self.cubic_layer7 = ConvBlock(num_out_channels, num_out_channels)
self.cubic_layer8 = GlobalPoolingBlock(2)
self.fc_cubic = torch.nn.Linear(
num_out_channels, 60) # cubic
self.upsample = torch.nn.Upsample(size=(300, 300), mode='bilinear',align_corners=False)
self.dropout = nn.Dropout(0.5)
def get_cubic_mask(self, feat, img):
"""Cubic filter definition
:param feat: feature map
:param img: image
:returns: cubic scaling map
:rtype: Tensor
"""
#######################################################
####################### Cubic #########################
feat_cubic = torch.cat((feat, img), 1)
feat_cubic = self.upsample(feat_cubic)
x = self.cubic_layer1(feat_cubic)
x = self.cubic_layer2(x)
x = self.cubic_layer3(x)
x = self.cubic_layer4(x)
x = self.cubic_layer5(x)
x = self.cubic_layer6(x)
x = self.cubic_layer7(x)
x = self.cubic_layer8(x)
x = x.view(x.size()[0], -1)
x = self.dropout(x)
R = self.fc_cubic(x)
cubic_mask = torch.zeros_like(img)
x_axis = Variable(torch.arange(
img.shape[2]).view(-1, 1).repeat(1, img.shape[3]).cuda()) / img.shape[2]
y_axis = Variable(torch.arange(img.shape[3]).repeat(
img.shape[2], 1).cuda()) / img.shape[3]
'''
Cubic for R channel
'''
cubic_mask[0, 0, :, :] = R[0, 0] * (x_axis ** 3) + R[0, 1] * (x_axis ** 2) * y_axis + R[0, 2] * (
x_axis ** 2) * img[0, 0, :, :] + R[0, 3] * (x_axis ** 2) + R[0, 4] * x_axis * (y_axis ** 2) + R[
0, 5] * x_axis * y_axis * img[0, 0, :, :] \
+ R[0, 6] * x_axis * y_axis + R[0, 7] * x_axis * (img[0, 0, :, :] ** 2) + R[
0, 8] * x_axis * img[0, 0, :, :] + R[0, 9] * x_axis + R[0, 10] * (
y_axis ** 3) + R[0, 11] * (y_axis ** 2) * img[0, 0, :, :] \
+ R[0, 12] * (y_axis ** 2) + R[0, 13] * y_axis * (img[0, 0, :, :] ** 2) + R[
0, 14] * y_axis * img[0, 0, :, :] + R[0, 15] * y_axis + R[0, 16] * (
img[0, 0, :, :] ** 3) + R[0, 17] * (img[0, 0, :, :] ** 2) \
+ R[0, 18] * \
img[0, 0, :, :] + R[0, 19]
'''
Cubic for G channel
'''
cubic_mask[0, 1, :, :] = R[0, 20] * (x_axis ** 3) + R[0, 21] * (x_axis ** 2) * y_axis + R[0, 22] * (
x_axis ** 2) * img[0, 1, :, :] + R[0, 23] * (x_axis ** 2) + R[0, 24] * x_axis * (y_axis ** 2) + R[
0, 25] * x_axis * y_axis * img[0, 1, :, :] \
+ R[0, 26] * x_axis * y_axis + R[0, 27] * x_axis * (img[0, 1, :, :] ** 2) + R[
0, 28] * x_axis * img[0, 1, :, :] + R[0, 29] * x_axis + R[0, 30] * (
y_axis ** 3) + R[0, 31] * (y_axis ** 2) * img[0, 1, :, :] \
+ R[0, 32] * (y_axis ** 2) + R[0, 33] * y_axis * (img[0, 1, :, :] ** 2) + R[
0, 34] * y_axis * img[0, 1, :, :] + R[0, 35] * y_axis + R[0, 36] * (
img[0, 1, :, :] ** 3) + R[0, 37] * (img[0, 1, :, :] ** 2) \
+ R[0, 38] * \
img[0, 1, :, :] + R[0, 39]
'''
Cubic for B channel
'''
cubic_mask[0, 2, :, :] = R[0, 40] * (x_axis ** 3) + R[0, 41] * (x_axis ** 2) * y_axis + R[0, 42] * (
x_axis ** 2) * img[0, 2, :, :] + R[0, 43] * (x_axis ** 2) + R[0, 44] * x_axis * (y_axis ** 2) + R[
0, 45] * x_axis * y_axis * img[0, 2, :, :] \
+ R[0, 46] * x_axis * y_axis + R[0, 47] * x_axis * (img[0, 2, :, :] ** 2) + R[
0, 48] * x_axis * img[0, 2, :, :] + R[0, 49] * x_axis + R[0, 50] * (
y_axis ** 3) + R[0, 51] * (y_axis ** 2) * img[0, 2, :, :] \
+ R[0, 52] * (y_axis ** 2) + R[0, 53] * y_axis * (img[0, 2, :, :] ** 2) + R[
0, 54] * y_axis * img[0, 2, :, :] + R[0, 55] * y_axis + R[0, 56] * (
img[0, 2, :, :] ** 3) + R[0, 57] * (img[0, 2, :, :] ** 2) \
+ R[0, 58] * \
img[0, 2, :, :] + R[0, 59]
img_cubic = torch.clamp(img + cubic_mask, 0, 1)
return img_cubic
class GraduatedFilter(nn.Module):
def __init__(self, num_in_channels=64, num_out_channels=64):
"""Initialisation function for the graduated filter
:param num_in_channels: input channels
:param num_out_channels: output channels
:returns: N/A
:rtype: N/A
"""
super(GraduatedFilter, self).__init__()
self.graduated_layer1 = ConvBlock(num_in_channels, num_out_channels)
self.graduated_layer2 = MaxPoolBlock()
self.graduated_layer3 = ConvBlock(num_out_channels, num_out_channels)
self.graduated_layer4 = MaxPoolBlock()
self.graduated_layer5 = ConvBlock(num_out_channels, num_out_channels)
self.graduated_layer6 = MaxPoolBlock()
self.graduated_layer7 = ConvBlock(num_out_channels, num_out_channels)
self.graduated_layer8 = GlobalPoolingBlock(2)
self.fc_graduated = torch.nn.Linear(
num_out_channels, 24)
self.upsample = torch.nn.Upsample(size=(300, 300), mode='bilinear',align_corners=False)
self.dropout = nn.Dropout(0.5)
self.bin_layer = BinaryLayer()
def tanh01(self, x):
"""Adjust Tanh to return values between 0 and 1
:param x: Tensor arbitrary range
:returns: Tensor between 0 and 1
:rtype: tensor
"""
tanh = nn.Tanh()
return 0.5 * (tanh(x) + 1)
def where(self, cond, x_1, x_2):
"""Differentiable where function to compare two Tensors
:param cond: condition e.g. <
:param x_1: Tensor 1
:param x_2: Tensor 2
:returns: Boolean comparison result
:rtype: Tensor
"""
cond = cond.float()
return (cond * x_1) + ((1 - cond) * x_2)
def get_inverted_mask(self, factor, invert, d1, d2, max_scale, top_line):
""" Inverts the graduated filter based on a learnt binary variable
:param factor: scale factor
:param invert: binary indicator variable
:param d1: distance between top and mid line
:param d2: distannce between botto and mid line
:param max_scale: maximum scaling factor possible
:param top_line: representation of top line
:returns: inverted scaling mask
:rtype: Tensor
"""
if (invert == 1).all():
if (factor >= 1).all():
diff = ((factor-1))/2 + 1
grad1 = (diff-factor)/d1
grad2 = (1-diff)/d2
mask_scale = torch.clamp(
factor+grad1*top_line+grad2*top_line, min=1, max=max_scale)
else:
diff = ((1-factor))/2 + factor
grad1 = (diff-factor)/d1
grad2 = (1-diff)/d2
mask_scale = torch.clamp(
factor+grad1*top_line+grad2*top_line, min=0, max=1)
else:
if (factor >= 1).all():
diff = ((factor-1))/2 + 1
grad1 = (diff-factor)/d1
grad2 = (factor-diff)/d2
mask_scale = torch.clamp(
1+grad1*top_line+grad2*top_line, min=1, max=max_scale)
else:
diff = ((1-factor))/2 + factor
grad1 = (diff-1)/d1
grad2 = (factor-diff)/d2
mask_scale = torch.clamp(
1+grad1*top_line+grad2*top_line, min=0, max=1)
mask_scale = torch.clamp(mask_scale.unsqueeze(0), 0, max_scale)
return mask_scale
def get_graduated_mask(self, feat, img):
""" Graduated filter definition
:param feat: features
:param img: image
:returns: scaling map
:rtype: Tensor
"""
#######################################################
####################### Graduated #####################
eps = 1e-10
x_axis = Variable(torch.arange(
img.shape[2]).view(-1, 1).repeat(1, img.shape[3]).cuda()) / img.shape[2]
y_axis = Variable(torch.arange(img.shape[3]).repeat(
img.shape[2], 1).cuda()) / img.shape[3]
feat_graduated = torch.cat((feat, img), 1)
feat_graduated = self.upsample(feat_graduated)
# The following layers calculate the parameters of the graduated filters that we use for image enhancement
x = self.graduated_layer1(feat_graduated)
x = self.graduated_layer2(x)
x = self.graduated_layer3(x)
x = self.graduated_layer4(x)
x = self.graduated_layer5(x)
x = self.graduated_layer6(x)
x = self.graduated_layer7(x)
x = self.graduated_layer8(x)
x = x.view(x.size()[0], -1)
x = self.dropout(x)
G = self.fc_graduated(x)
# Classification values (above or below the line)
above_or_below_line1 = ((self.bin_layer(G[0, 0]))+1)/2
above_or_below_line2 = ((self.bin_layer(G[0, 1]))+1)/2
above_or_below_line3 = ((self.bin_layer(G[0, 2]))+1)/2
slope1 = G[0, 3].clone()
slope2 = G[0, 4].clone()
slope3 = G[0, 5].clone()
y_axis_dist1 = self.tanh01(G[0, 6]) + eps
y_axis_dist2 = self.tanh01(G[0, 7]) + eps
y_axis_dist3 = self.tanh01(G[0, 8]) + eps
y_axis_dist1 = torch.clamp(self.tanh01(G[0, 9]), y_axis_dist1.data, 1.0)
y_axis_dist2 = torch.clamp(self.tanh01(G[0, 10]), y_axis_dist2.data, 1.0)
y_axis_dist3 = torch.clamp(self.tanh01(G[0, 11]), y_axis_dist3.data, 1.0)
y_axis_dist4= torch.clamp(self.tanh01(G[0, 12]), 0, y_axis_dist1.data)
y_axis_dist5 = torch.clamp(self.tanh01(G[0, 13]), 0, y_axis_dist2.data)
y_axis_dist6 = torch.clamp(self.tanh01(G[0, 14]), 0, y_axis_dist3.data)
# Scales
max_scale = 2
min_scale = 0
scale_factor1 = self.tanh01(G[0, 15]) * max_scale
scale_factor2 = self.tanh01(G[0, 16]) * max_scale
scale_factor3 = self.tanh01(G[0, 17]) * max_scale
scale_factor4 = self.tanh01(G[0, 18]) * max_scale
scale_factor5 = self.tanh01(G[0, 19]) * max_scale
scale_factor6 = self.tanh01(G[0, 20]) * max_scale
scale_factor7 = self.tanh01(G[0, 21]) * max_scale
scale_factor8 = self.tanh01(G[0, 22]) * max_scale
scale_factor9= self.tanh01(G[0, 23]) * max_scale
slope1_angle = torch.atan(slope1)
slope2_angle = torch.atan(slope2)
slope3_angle = torch.atan(slope3)
# Distances between central line and two outer lines
d1 = self.tanh01(y_axis_dist1*torch.cos(slope1_angle))
d2 = self.tanh01(y_axis_dist4*torch.cos(slope1_angle))
d3 = self.tanh01(y_axis_dist2*torch.cos(slope2_angle))
d4 = self.tanh01(y_axis_dist5*torch.cos(slope2_angle))
d5 = self.tanh01(y_axis_dist3*torch.cos(slope3_angle))
d6 = self.tanh01(y_axis_dist6*torch.cos(slope3_angle))
top_line1 = self.tanh01(y_axis - (slope1 * x_axis + y_axis_dist1 + d1))
top_line2 = self.tanh01(y_axis - (slope2 * x_axis + y_axis_dist2 + d3))
top_line3 = self.tanh01(y_axis - (slope3 * x_axis + y_axis_dist3 + d5))
'''
The following are the scale factors for each of the 9 graduated filters
'''
mask_scale1 = self.get_inverted_mask(
scale_factor1, above_or_below_line1, d1, d2, max_scale, top_line1)
mask_scale2 = self.get_inverted_mask(
scale_factor2, above_or_below_line1, d1, d2, max_scale, top_line1)
mask_scale3 = self.get_inverted_mask(
scale_factor3, above_or_below_line1, d1, d2, max_scale, top_line1)
mask_scale_1 = torch.cat(
(mask_scale1, mask_scale2, mask_scale3), dim=0)
mask_scale_1 = torch.clamp(mask_scale_1.unsqueeze(0), 0, max_scale)
mask_scale4 = self.get_inverted_mask(
scale_factor4, above_or_below_line2, d3, d4, max_scale, top_line2)
mask_scale5 = self.get_inverted_mask(
scale_factor5, above_or_below_line2, d3, d4, max_scale, top_line2)
mask_scale6 = self.get_inverted_mask(
scale_factor6, above_or_below_line2, d3, d4, max_scale, top_line2)
mask_scale_4 = torch.cat(
(mask_scale4, mask_scale5, mask_scale6), dim=0)
mask_scale_4 = torch.clamp(mask_scale_4.unsqueeze(0), 0, max_scale)
mask_scale7 = self.get_inverted_mask(
scale_factor7, above_or_below_line3, d5, d6, max_scale, top_line3)
mask_scale8 = self.get_inverted_mask(
scale_factor8, above_or_below_line3, d5, d6, max_scale, top_line3)
mask_scale9 = self.get_inverted_mask(
scale_factor9, above_or_below_line3, d5, d6, max_scale, top_line3)
mask_scale_7 = torch.cat(
(mask_scale7, mask_scale8, mask_scale9), dim=0)
mask_scale_7 = torch.clamp(mask_scale_7.unsqueeze(0), 0, max_scale)
mask_scale = torch.clamp(
mask_scale_1*mask_scale_4*mask_scale_7, 0, max_scale)
return mask_scale
class EllipticalFilter(nn.Module):
def __init__(self, num_in_channels=64, num_out_channels=64):
"""Initialisation function
:param block: a block (layer) of the neural network
:param num_layers: number of neural network layers
:returns: initialises parameters of the neural networ
:rtype: N/A
"""
super(EllipticalFilter, self).__init__()
self.elliptical_layer1 = ConvBlock(num_in_channels, num_out_channels)
self.elliptical_layer2 = MaxPoolBlock()
self.elliptical_layer3 = ConvBlock(num_out_channels, num_out_channels)
self.elliptical_layer4 = MaxPoolBlock()
self.elliptical_layer5 = ConvBlock(num_out_channels, num_out_channels)
self.elliptical_layer6 = MaxPoolBlock()
self.elliptical_layer7 = ConvBlock(num_out_channels, num_out_channels)
self.elliptical_layer8 = GlobalPoolingBlock(2)
self.fc_elliptical = torch.nn.Linear(
num_out_channels, 24) # elliptical
self.upsample = torch.nn.Upsample(size=(300, 300), mode='bilinear',align_corners=False)
self.dropout = nn.Dropout(0.5)
def tanh01(self, x):
"""Adjust Tanh to return values between 0 and 1
:param x: Tensor arbitrary range
:returns: Tensor between 0 and 1
:rtype: tensor
"""
tanh = nn.Tanh()
return 0.5 * (tanh(x) + 1)
def where(self, cond, x_1, x_2):
"""Differentiable where function to compare two Tensors
:param cond: condition e.g. <
:param x_1: Tensor 1
:param x_2: Tensor 2
:returns: Boolean comparison result
:rtype: Tensor
"""
cond = cond.float()
return (cond * x_1) + ((1 - cond) * x_2)
def get_mask(self, x_axis, y_axis, shift_x=0, shift_y=0, semi_axis_x=1, semi_axis_y=1, alpha=0,
scale_factor=2, max_scale=2, eps=1e-7, radius=1):
"""Gets the elliptical scaling mask according to the equation of a
rotated ellipse
:returns: scaling mask
:rtype: Tensor
"""
# Check whether a point is inside our outside of the ellipse and set the scaling factor accordingly
ellipse_equation_part1 = (((x_axis - shift_x)*torch.cos(alpha) + (y_axis - shift_y)*torch.sin(alpha)) ** 2) / ((semi_axis_x)**2)
ellipse_equation_part2 = (((x_axis - shift_x)*torch.sin(alpha) - (y_axis - shift_y)*torch.cos(alpha)) ** 2) / ((semi_axis_y)**2)
# Set the scaling factors to decay with radius inside the ellipse
mask_scale = self.where(ellipse_equation_part1+ellipse_equation_part2 < 1,
(torch.sqrt((x_axis - shift_x) ** 2 + (y_axis - shift_y) ** 2 + eps) * (1 - scale_factor)) / radius + scale_factor, 1)
mask_scale = torch.clamp(mask_scale.unsqueeze(0), 0, max_scale)
return mask_scale
def get_elliptical_mask(self, feat, img):
"""Gets the elliptical scaling mask according to the equation of a
rotated ellipse
:param feat: features from the backbone
:param img: image
:returns: elliptical adjustment maps for each channel
:rtype: Tensor
"""
# The two eps parameters are used to avoid numerical issues in the learning
eps2 = 1e-7
eps1 = 1e-10
# max_scale is the maximum an ellipse can scale the image R,G,B values by
max_scale = 2
min_scale = 0
feat_elliptical = torch.cat((feat, img), 1)
feat_elliptical = self.upsample(feat_elliptical)
# The following layers calculate the parameters of the ellipses that we use for image enhancement
x = self.elliptical_layer1(feat_elliptical)
x = self.elliptical_layer2(x)
x = self.elliptical_layer3(x)
x = self.elliptical_layer4(x)
x = self.elliptical_layer5(x)
x = self.elliptical_layer6(x)
x = self.elliptical_layer7(x)
x = self.elliptical_layer8(x)
x = x.view(x.size()[0], -1)
x = self.dropout(x)
G = self.fc_elliptical(x)
# The next code implements a rotated ellipse according to:
# https://math.stackexchange.com/questions/426150/what-is-the-general-equation-of-the-ellipse-that-is-not-in-the-origin-and-rotate
# Normalised coordinates for x and y-axes, we instantiate the ellipses in these coordinates
x_axis = Variable(torch.arange(
img.shape[2]).view(-1, 1).repeat(1, img.shape[3]).cuda()) / img.shape[2]
y_axis = Variable(torch.arange(img.shape[3]).repeat(
img.shape[2], 1).cuda()) / img.shape[3]
# x coordinate - h position
right_x = (img.shape[2] - 1) / img.shape[2]
left_x = 0
# Centre of ellipse, x-coordinate
x_coord1 = self.tanh01(G[0, 0]) + eps1
x_coord2 = self.tanh01(G[0, 1]) + eps1
x_coord3 = self.tanh01(G[0, 2]) + eps1
# y coordinate - k coordinate
right_y = (img.shape[3] - 1) // img.shape[3]
left_y = 0
# Centre of ellipse, y-coordinate
y_coord1 = self.tanh01(G[0, 3]) + eps1
y_coord2 = self.tanh01(G[0, 4]) + eps1
y_coord3 = self.tanh01(G[0, 5]) + eps1
# a value of ellipse
a1 = self.tanh01(G[0, 6]) + eps1
a2 = self.tanh01(G[0, 7]) + eps1
a3 = self.tanh01(G[0, 8]) + eps1
# b value
b1 = self.tanh01(G[0, 9]) + eps1
b2 = self.tanh01(G[0, 10]) + eps1
b3 = self.tanh01(G[0, 11]) + eps1
# A value is angle to the x-axis
A1 = self.tanh01(G[0, 12]) * math.pi + eps1
A2 = self.tanh01(G[0, 13]) * math.pi + eps1
A3 = self.tanh01(G[0, 14]) * math.pi + eps1
'''
The following are the scale factors for each of the 9 ellipses
'''
scale1 = self.tanh01(G[0, 15]) * max_scale + eps1
scale2 = self.tanh01(G[0, 16]) * max_scale + eps1
scale3 = self.tanh01(G[0, 17]) * max_scale + eps1
scale4 = self.tanh01(G[0, 18]) * max_scale + eps1
scale5 = self.tanh01(G[0, 19]) * max_scale + eps1
scale6 = self.tanh01(G[0, 20]) * max_scale + eps1
scale7 = self.tanh01(G[0, 21]) * max_scale + eps1
scale8 = self.tanh01(G[0, 22]) * max_scale + eps1
scale9 = self.tanh01(G[0, 23]) * max_scale + eps1
############ Angle of orientation of the ellipses with respect to the y semi-axis
angle_1 = torch.acos(torch.clamp((y_axis-y_coord1) /
(torch.sqrt((x_axis-x_coord1)**2 + (y_axis-y_coord1)**2 + eps1)), -1+eps2, 1-eps2))-A1
angle_2 = torch.acos(torch.clamp((y_axis-y_coord2) /
(torch.sqrt((x_axis-x_coord2) ** 2 + (y_axis-y_coord2)**2 + eps1)), -1+eps2, 1-eps2))-A2
angle_3 = torch.acos(torch.clamp((y_axis-y_coord3) /
(torch.sqrt((x_axis-x_coord3) ** 2 + (y_axis-y_coord3)**2 + eps1)), -1+eps2, 1-eps2))-A3
############ Radius of the ellipses
# https://math.stackexchange.com/questions/432902/how-to-get-the-radius-of-an-ellipse-at-a-specific-angle-by-knowing-its-semi-majo
radius_1 = (a1*b1)/torch.sqrt((a1**2)*(torch.sin(angle_1)**2)+(b1**2)*(torch.cos(angle_1)**2) + eps1)
radius_2 = (a2*b2)/torch.sqrt((a2**2)*(torch.sin(angle_2)**2)+(b2**2)*(torch.cos(angle_2)**2) + eps1)
radius_3 = (a3*b3)/torch.sqrt((a3**2)*(torch.sin(angle_3)**2)+(b3**2)*(torch.cos(angle_3)**2) + eps1)
############ Scaling factors for the R,G,B channels, here we learn three ellipses
mask_scale1 = self.get_mask(x_axis, y_axis,
shift_x=x_coord1, shift_y=y_coord1, semi_axis_x=a1, semi_axis_y=b1, alpha=angle_1, scale_factor=scale1,
radius=radius_1)
mask_scale2 = self.get_mask(x_axis, y_axis,
shift_x=x_coord1, shift_y=y_coord1, semi_axis_x=a1, semi_axis_y=b1, alpha=angle_1, scale_factor=scale2,
radius=radius_1)
mask_scale3 = self.get_mask(x_axis, y_axis,
shift_x=x_coord1, shift_y=y_coord1, semi_axis_x=a1, semi_axis_y=b1, alpha=angle_1, scale_factor=scale3,
radius=radius_1)
mask_scale_1 = torch.cat(
(mask_scale1, mask_scale2, mask_scale3), dim=0)
mask_scale_1_rad = torch.clamp(mask_scale_1.unsqueeze(0), 0, max_scale)
############ Scaling factors for the R,G,B channels, here we learn three ellipses
mask_scale4 = self.get_mask(x_axis, y_axis,
shift_x=x_coord2, shift_y=y_coord2, semi_axis_x=a2, semi_axis_y=b2, alpha=angle_2, scale_factor=scale4,
radius=radius_2)
mask_scale5 = self.get_mask(x_axis, y_axis,
shift_x=x_coord2, shift_y=y_coord2, semi_axis_x=a2, semi_axis_y=b2, alpha=angle_2, scale_factor=scale5,
radius=radius_2)
mask_scale6 = self.get_mask(x_axis, y_axis,
shift_x=x_coord2, shift_y=y_coord2, semi_axis_x=a2, semi_axis_y=b3, alpha=angle_2, scale_factor=scale6,
radius=radius_2)
mask_scale_4 = torch.cat(
(mask_scale4, mask_scale5, mask_scale6), dim=0)
mask_scale_4_rad = torch.clamp(mask_scale_4.unsqueeze(0), 0, max_scale)
############ Scaling factors for the R,G,B channels, here we learn three ellipses
mask_scale7 = self.get_mask(x_axis, y_axis,
shift_x=x_coord3, shift_y=y_coord3, semi_axis_x=a3, semi_axis_y=b3, alpha=angle_3, scale_factor=scale7,
radius=radius_3)
mask_scale8 = self.get_mask(x_axis, y_axis,
shift_x=x_coord3, shift_y=y_coord3, semi_axis_x=a3, semi_axis_y=b3, alpha=angle_3, scale_factor=scale8,
radius=radius_3)
mask_scale9 = self.get_mask(x_axis, y_axis,
shift_x=x_coord3, shift_y=y_coord3, semi_axis_x=a3, semi_axis_y=b3, alpha=angle_3, scale_factor=scale9,
radius=radius_3)
mask_scale_7 = torch.cat(
(mask_scale7, mask_scale8, mask_scale9), dim=0)
mask_scale_7_rad = torch.clamp(mask_scale_7.unsqueeze(0), 0, max_scale)
############ Mix the ellipses together by multiplication
mask_scale_elliptical = torch.clamp(
mask_scale_1_rad * mask_scale_4_rad * mask_scale_7_rad, 0, max_scale)
return mask_scale_elliptical
class Block(nn.Module):
def __init__(self):
"""Initialisation for a lower-level DeepLPF conv block
:returns: N/A
:rtype: N/A
"""
super(Block, self).__init__()
def conv3x3(self, in_channels, out_channels, stride=1):
"""Represents a convolution of shape 3x3
:param in_channels: number of input channels
:param out_channels: number of output channels
:param stride: the convolution stride
:returns: convolution function with the specified parameterisation
:rtype: function
"""
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=True)
class ConvBlock(Block, nn.Module):
def __init__(self, num_in_channels, num_out_channels, stride=1):
"""Initialise function for the higher level convolution block
:param in_channels:
:param out_channels:
:param stride:
:param padding:
:returns:
:rtype:
"""
super(Block, self).__init__()
self.conv = self.conv3x3(num_in_channels, num_out_channels, stride=2)
self.lrelu = nn.LeakyReLU()
def forward(self, x):
""" Forward function for the higher level convolution block
:param x: Tensor representing the input BxCxWxH, where B is the batch size, C is the number of channels, W and H are the width and image height
:returns: Tensor representing the output of the block
:rtype: Tensor
"""
img_out = self.lrelu(self.conv(x))
return img_out
class MaxPoolBlock(Block, nn.Module):
def __init__(self):
"""Initialise function for the max pooling block
:returns: N/A
:rtype: N/A
"""
super(Block, self).__init__()
self.max_pool = nn.MaxPool2d(kernel_size=2, stride=2)
def forward(self, x):
""" Forward function for the max pooling block
:param x: Tensor representing the input BxCxWxH, where B is the batch size, C is the number of channels, W and H are the width and image height
:returns: Tensor representing the output of the block
:rtype: Tensor
"""
img_out = self.max_pool(x)
return img_out
class GlobalPoolingBlock(Block, nn.Module):
def __init__(self, receptive_field):
"""Implementation of the global pooling block. Takes the average over a 2D receptive field.
:param receptive_field:
:returns: N/A
:rtype: N/A
"""
super(Block, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
def forward(self, x):
"""Forward function for the high-level global pooling block
:param x: Tensor of shape BxCxAxA
:returns: Tensor of shape BxCx1x1, where B is the batch size
:rtype: Tensor
"""
out = self.avg_pool(x)
return out
class DeepLPFParameterPrediction(nn.Module):
import torch.nn.functional as F
def __init__(self, num_in_channels=64, num_out_channels=64, batch_size=1):
"""Initialisation function
:param num_in_channels: Number of input feature maps
:param num_out_channels: Number of output feature maps
:param batch_size: Size of image batch
:returns: N/A
:rtype: N/A
"""
super(DeepLPFParameterPrediction, self).__init__()
self.num_in_channels = num_in_channels
self.num_out_channels = num_out_channels
self.cubic_filter = CubicFilter()
self.graduated_filter = GraduatedFilter()
self.elliptical_filter = EllipticalFilter()
def forward(self, x):
"""DeepLPF combined architecture fusing cubic, graduated and elliptical filters
:param x: forward the data Tensor x through the network
:returns: Tensor representing the predicted image batch of shape BxCxWxH
:rtype: Tensor
"""
x.contiguous() # remove memory holes
x.cuda()
feat = x[:, 3:64, :, :]
img = x[:, 0:3, :, :]
torch.cuda.empty_cache()
shape = x.shape
img_cubic = self.cubic_filter.get_cubic_mask(feat, img)
mask_scale_graduated = self.graduated_filter.get_graduated_mask(
feat, img_cubic)
mask_scale_elliptical = self.elliptical_filter.get_elliptical_mask(
feat, img_cubic)
mask_scale_fuse = torch.clamp(
mask_scale_graduated+mask_scale_elliptical, 0, 2)
img_fuse = torch.clamp(img_cubic*mask_scale_fuse, 0, 1)
img = torch.clamp(img_fuse+img, 0, 1)
return img
class DeepLPFNet(nn.Module):
def __init__(self):
"""Initialisation function
:returns: initialises parameters of the neural networ
:rtype: N/A
"""
super(DeepLPFNet, self).__init__()
self.backbonenet = unet.UNetModel()
self.deeplpfnet = DeepLPFParameterPrediction()
def forward(self, img):
"""Neural network forward function
:param img: forward the data img through the network
:returns: residual image
:rtype: numpy ndarray
"""
feat = self.backbonenet(img)