-
Notifications
You must be signed in to change notification settings - Fork 0
/
DWT_IDWT_layer.py
executable file
·758 lines (698 loc) · 33 KB
/
DWT_IDWT_layer.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
"""
自定义 pytorch 层,实现一维、二维、三维张量的 DWT 和 IDWT,未考虑边界延拓
只有当图像行列数都是偶数,且重构滤波器组低频分量长度为 2 时,才能精确重构,否则在边界处有误差。
"""
import numpy as np
import math
from torch.nn import Module
from DWT_IDWT_Functions import *
import pywt
__all__ = ['DWT_1D', 'IDWT_1D', 'DWT_2D', 'IDWT_2D', 'DWT_3D', 'IDWT_3D', 'DWT_2D_tiny']
class DWT_1D(Module):
"""
input: the 1D data to be decomposed -- (N, C, Length)
output: lfc -- (N, C, Length/2)
hfc -- (N, C, Length/2)
"""
def __init__(self, wavename):
"""
1D discrete wavelet transform (DWT) for sequence decomposition
用于序列分解的一维离散小波变换 DWT
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(DWT_1D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.rec_lo
self.band_high = wavelet.rec_hi
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = self.input_height
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_h = matrix_h[:,(self.band_length_half-1):end]
matrix_g = matrix_g[:,(self.band_length_half-1):end]
if torch.cuda.is_available():
self.matrix_low = torch.Tensor(matrix_h).cuda()
self.matrix_high = torch.Tensor(matrix_g).cuda()
else:
self.matrix_low = torch.Tensor(matrix_h)
self.matrix_high = torch.Tensor(matrix_g)
def forward(self, input):
"""
input_low_frequency_component = \mathcal{L} * input
input_high_frequency_component = \mathcal{H} * input
:param input: the data to be decomposed
:return: the low-frequency and high-frequency components of the input data
"""
assert len(input.size()) == 3
self.input_height = input.size()[-1]
self.get_matrix()
return DWTFunction_1D.apply(input, self.matrix_low, self.matrix_high)
class IDWT_1D(Module):
"""
input: lfc -- (N, C, Length/2)
hfc -- (N, C, Length/2)
output: the original data -- (N, C, Length)
"""
def __init__(self, wavename):
"""
1D inverse DWT (IDWT) for sequence reconstruction
用于序列重构的一维离散小波逆变换 IDWT
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(IDWT_1D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.dec_lo
self.band_high = wavelet.dec_hi
self.band_low.reverse()
self.band_high.reverse()
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
generating the matrices: \mathcal{L}, \mathcal{H}
生成变换矩阵
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = self.input_height
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_h = matrix_h[:,(self.band_length_half-1):end]
matrix_g = matrix_g[:,(self.band_length_half-1):end]
if torch.cuda.is_available():
self.matrix_low = torch.Tensor(matrix_h).cuda()
self.matrix_high = torch.Tensor(matrix_g).cuda()
else:
self.matrix_low = torch.Tensor(matrix_h)
self.matrix_high = torch.Tensor(matrix_g)
def forward(self, L, H):
"""
:param L: the low-frequency component of the original data
:param H: the high-frequency component of the original data
:return: the original data
"""
assert len(L.size()) == len(H.size()) == 3
self.input_height = L.size()[-1] + H.size()[-1]
self.get_matrix()
return IDWTFunction_1D.apply(L, H, self.matrix_low, self.matrix_high)
class DWT_2D_tiny(Module):
"""
input: the 2D data to be decomposed -- (N, C, H, W)
output -- lfc: (N, C, H/2, W/2)
#hfc_lh: (N, C, H/2, W/2)
#hfc_hl: (N, C, H/2, W/2)
#hfc_hh: (N, C, H/2, W/2)
DWT_2D_tiny only outputs the low-frequency component, which is used in WaveCNet;
the all four components could be get using DWT_2D, which is used in WaveUNet.
"""
def __init__(self, wavename):
"""
2D discrete wavelet transform (DWT) for 2D image decomposition
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(DWT_2D_tiny, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.rec_lo
self.band_high = wavelet.rec_hi
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = np.max((self.input_height, self.input_width))
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
matrix_h_0 = matrix_h[0:(math.floor(self.input_height / 2)), 0:(self.input_height + self.band_length - 2)]
matrix_h_1 = matrix_h[0:(math.floor(self.input_width / 2)), 0:(self.input_width + self.band_length - 2)]
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_g_0 = matrix_g[0:(self.input_height - math.floor(self.input_height / 2)),0:(self.input_height + self.band_length - 2)]
matrix_g_1 = matrix_g[0:(self.input_width - math.floor(self.input_width / 2)),0:(self.input_width + self.band_length - 2)]
matrix_h_0 = matrix_h_0[:,(self.band_length_half-1):end]
matrix_h_1 = matrix_h_1[:,(self.band_length_half-1):end]
matrix_h_1 = np.transpose(matrix_h_1)
matrix_g_0 = matrix_g_0[:,(self.band_length_half-1):end]
matrix_g_1 = matrix_g_1[:,(self.band_length_half-1):end]
matrix_g_1 = np.transpose(matrix_g_1)
if torch.cuda.is_available():
self.matrix_low_0 = torch.Tensor(matrix_h_0).cuda()
self.matrix_low_1 = torch.Tensor(matrix_h_1).cuda()
self.matrix_high_0 = torch.Tensor(matrix_g_0).cuda()
self.matrix_high_1 = torch.Tensor(matrix_g_1).cuda()
else:
self.matrix_low_0 = torch.Tensor(matrix_h_0)
self.matrix_low_1 = torch.Tensor(matrix_h_1)
self.matrix_high_0 = torch.Tensor(matrix_g_0)
self.matrix_high_1 = torch.Tensor(matrix_g_1)
def forward(self, input):
"""
input_lfc = \mathcal{L} * input * \mathcal{L}^T
#input_hfc_lh = \mathcal{H} * input * \mathcal{L}^T
#input_hfc_hl = \mathcal{L} * input * \mathcal{H}^T
#input_hfc_hh = \mathcal{H} * input * \mathcal{H}^T
:param input: the 2D data to be decomposed
:return: the low-frequency component of the input 2D data
"""
assert len(input.size()) == 4
self.input_height = input.size()[-2]
self.input_width = input.size()[-1]
self.get_matrix()
return DWTFunction_2D_tiny.apply(input, self.matrix_low_0, self.matrix_low_1, self.matrix_high_0, self.matrix_high_1)
class DWT_2D(Module):
"""
input: the 2D data to be decomposed -- (N, C, H, W)
output -- lfc: (N, C, H/2, W/2)
hfc_lh: (N, C, H/2, W/2)
hfc_hl: (N, C, H/2, W/2)
hfc_hh: (N, C, H/2, W/2)
"""
def __init__(self, wavename):
"""
2D discrete wavelet transform (DWT) for 2D image decomposition
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(DWT_2D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.rec_lo
self.band_high = wavelet.rec_hi
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = np.max((self.input_height, self.input_width))
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
matrix_h_0 = matrix_h[0:(math.floor(self.input_height / 2)), 0:(self.input_height + self.band_length - 2)]
matrix_h_1 = matrix_h[0:(math.floor(self.input_width / 2)), 0:(self.input_width + self.band_length - 2)]
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_g_0 = matrix_g[0:(self.input_height - math.floor(self.input_height / 2)),0:(self.input_height + self.band_length - 2)]
matrix_g_1 = matrix_g[0:(self.input_width - math.floor(self.input_width / 2)),0:(self.input_width + self.band_length - 2)]
matrix_h_0 = matrix_h_0[:,(self.band_length_half-1):end]
matrix_h_1 = matrix_h_1[:,(self.band_length_half-1):end]
matrix_h_1 = np.transpose(matrix_h_1)
matrix_g_0 = matrix_g_0[:,(self.band_length_half-1):end]
matrix_g_1 = matrix_g_1[:,(self.band_length_half-1):end]
matrix_g_1 = np.transpose(matrix_g_1)
if torch.cuda.is_available():
self.matrix_low_0 = torch.Tensor(matrix_h_0).cuda()
self.matrix_low_1 = torch.Tensor(matrix_h_1).cuda()
self.matrix_high_0 = torch.Tensor(matrix_g_0).cuda()
self.matrix_high_1 = torch.Tensor(matrix_g_1).cuda()
else:
self.matrix_low_0 = torch.Tensor(matrix_h_0)
self.matrix_low_1 = torch.Tensor(matrix_h_1)
self.matrix_high_0 = torch.Tensor(matrix_g_0)
self.matrix_high_1 = torch.Tensor(matrix_g_1)
def forward(self, input):
"""
input_lfc = \mathcal{L} * input * \mathcal{L}^T
input_hfc_lh = \mathcal{H} * input * \mathcal{L}^T
input_hfc_hl = \mathcal{L} * input * \mathcal{H}^T
input_hfc_hh = \mathcal{H} * input * \mathcal{H}^T
:param input: the 2D data to be decomposed
:return: the low-frequency and high-frequency components of the input 2D data
"""
assert len(input.size()) == 4
self.input_height = input.size()[-2]
self.input_width = input.size()[-1]
self.get_matrix()
return DWTFunction_2D.apply(input, self.matrix_low_0, self.matrix_low_1, self.matrix_high_0, self.matrix_high_1)
class IDWT_2D(Module):
"""
input: lfc -- (N, C, H/2, W/2)
hfc_lh -- (N, C, H/2, W/2)
hfc_hl -- (N, C, H/2, W/2)
hfc_hh -- (N, C, H/2, W/2)
output: the original 2D data -- (N, C, H, W)
"""
def __init__(self, wavename):
"""
2D inverse DWT (IDWT) for 2D image reconstruction
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(IDWT_2D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.dec_lo
self.band_low.reverse()
self.band_high = wavelet.dec_hi
self.band_high.reverse()
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = np.max((self.input_height, self.input_width))
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
matrix_h_0 = matrix_h[0:(math.floor(self.input_height / 2)), 0:(self.input_height + self.band_length - 2)]
matrix_h_1 = matrix_h[0:(math.floor(self.input_width / 2)), 0:(self.input_width + self.band_length - 2)]
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_g_0 = matrix_g[0:(self.input_height - math.floor(self.input_height / 2)),0:(self.input_height + self.band_length - 2)]
matrix_g_1 = matrix_g[0:(self.input_width - math.floor(self.input_width / 2)),0:(self.input_width + self.band_length - 2)]
matrix_h_0 = matrix_h_0[:,(self.band_length_half-1):end]
matrix_h_1 = matrix_h_1[:,(self.band_length_half-1):end]
matrix_h_1 = np.transpose(matrix_h_1)
matrix_g_0 = matrix_g_0[:,(self.band_length_half-1):end]
matrix_g_1 = matrix_g_1[:,(self.band_length_half-1):end]
matrix_g_1 = np.transpose(matrix_g_1)
if torch.cuda.is_available():
self.matrix_low_0 = torch.Tensor(matrix_h_0).cuda()
self.matrix_low_1 = torch.Tensor(matrix_h_1).cuda()
self.matrix_high_0 = torch.Tensor(matrix_g_0).cuda()
self.matrix_high_1 = torch.Tensor(matrix_g_1).cuda()
else:
self.matrix_low_0 = torch.Tensor(matrix_h_0)
self.matrix_low_1 = torch.Tensor(matrix_h_1)
self.matrix_high_0 = torch.Tensor(matrix_g_0)
self.matrix_high_1 = torch.Tensor(matrix_g_1)
def forward(self, LL, LH, HL, HH):
"""
recontructing the original 2D data
the original 2D data = \mathcal{L}^T * lfc * \mathcal{L}
+ \mathcal{H}^T * hfc_lh * \mathcal{L}
+ \mathcal{L}^T * hfc_hl * \mathcal{H}
+ \mathcal{H}^T * hfc_hh * \mathcal{H}
:param LL: the low-frequency component
:param LH: the high-frequency component, hfc_lh
:param HL: the high-frequency component, hfc_hl
:param HH: the high-frequency component, hfc_hh
:return: the original 2D data
"""
assert len(LL.size()) == len(LH.size()) == len(HL.size()) == len(HH.size()) == 4
self.input_height = LL.size()[-2] + HH.size()[-2]
self.input_width = LL.size()[-1] + HH.size()[-1]
self.get_matrix()
return IDWTFunction_2D.apply(LL, LH, HL, HH, self.matrix_low_0, self.matrix_low_1, self.matrix_high_0, self.matrix_high_1)
class DWT_3D(Module):
"""
input: the 3D data to be decomposed -- (N, C, D, H, W)
output: lfc -- (N, C, D/2, H/2, W/2)
hfc_llh -- (N, C, D/2, H/2, W/2)
hfc_lhl -- (N, C, D/2, H/2, W/2)
hfc_lhh -- (N, C, D/2, H/2, W/2)
hfc_hll -- (N, C, D/2, H/2, W/2)
hfc_hlh -- (N, C, D/2, H/2, W/2)
hfc_hhl -- (N, C, D/2, H/2, W/2)
hfc_hhh -- (N, C, D/2, H/2, W/2)
"""
def __init__(self, wavename):
"""
3D discrete wavelet transform (DWT) for 3D data decomposition
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(DWT_3D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.rec_lo
self.band_high = wavelet.rec_hi
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = np.max((self.input_height, self.input_width))
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
matrix_h_0 = matrix_h[0:(math.floor(self.input_height / 2)), 0:(self.input_height + self.band_length - 2)]
matrix_h_1 = matrix_h[0:(math.floor(self.input_width / 2)), 0:(self.input_width + self.band_length - 2)]
matrix_h_2 = matrix_h[0:(math.floor(self.input_depth / 2)), 0:(self.input_depth + self.band_length - 2)]
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_g_0 = matrix_g[0:(self.input_height - math.floor(self.input_height / 2)),0:(self.input_height + self.band_length - 2)]
matrix_g_1 = matrix_g[0:(self.input_width - math.floor(self.input_width / 2)),0:(self.input_width + self.band_length - 2)]
matrix_g_2 = matrix_g[0:(self.input_depth - math.floor(self.input_depth / 2)),0:(self.input_depth + self.band_length - 2)]
matrix_h_0 = matrix_h_0[:,(self.band_length_half-1):end]
matrix_h_1 = matrix_h_1[:,(self.band_length_half-1):end]
matrix_h_1 = np.transpose(matrix_h_1)
matrix_h_2 = matrix_h_2[:,(self.band_length_half-1):end]
matrix_g_0 = matrix_g_0[:,(self.band_length_half-1):end]
matrix_g_1 = matrix_g_1[:,(self.band_length_half-1):end]
matrix_g_1 = np.transpose(matrix_g_1)
matrix_g_2 = matrix_g_2[:,(self.band_length_half-1):end]
if torch.cuda.is_available():
self.matrix_low_0 = torch.Tensor(matrix_h_0).cuda()
self.matrix_low_1 = torch.Tensor(matrix_h_1).cuda()
self.matrix_low_2 = torch.Tensor(matrix_h_2).cuda()
self.matrix_high_0 = torch.Tensor(matrix_g_0).cuda()
self.matrix_high_1 = torch.Tensor(matrix_g_1).cuda()
self.matrix_high_2 = torch.Tensor(matrix_g_2).cuda()
else:
self.matrix_low_0 = torch.Tensor(matrix_h_0)
self.matrix_low_1 = torch.Tensor(matrix_h_1)
self.matrix_low_2 = torch.Tensor(matrix_h_2)
self.matrix_high_0 = torch.Tensor(matrix_g_0)
self.matrix_high_1 = torch.Tensor(matrix_g_1)
self.matrix_high_2 = torch.Tensor(matrix_g_2)
def forward(self, input):
"""
:param input: the 3D data to be decomposed
:return: the eight components of the input data, one low-frequency and seven high-frequency components
"""
assert len(input.size()) == 5
self.input_depth = input.size()[-3]
self.input_height = input.size()[-2]
self.input_width = input.size()[-1]
self.get_matrix()
return DWTFunction_3D.apply(input, self.matrix_low_0, self.matrix_low_1, self.matrix_low_2,
self.matrix_high_0, self.matrix_high_1, self.matrix_high_2)
class IDWT_3D(Module):
"""
input: lfc -- (N, C, D/2, H/2, W/2)
hfc_llh -- (N, C, D/2, H/2, W/2)
hfc_lhl -- (N, C, D/2, H/2, W/2)
hfc_lhh -- (N, C, D/2, H/2, W/2)
hfc_hll -- (N, C, D/2, H/2, W/2)
hfc_hlh -- (N, C, D/2, H/2, W/2)
hfc_hhl -- (N, C, D/2, H/2, W/2)
hfc_hhh -- (N, C, D/2, H/2, W/2)
output: the original 3D data -- (N, C, D, H, W)
"""
def __init__(self, wavename):
"""
3D inverse DWT (IDWT) for 3D data reconstruction
:param wavename: pywt.wavelist(); in the paper, 'chx.y' denotes 'biorx.y'.
"""
super(IDWT_3D, self).__init__()
wavelet = pywt.Wavelet(wavename)
self.band_low = wavelet.dec_lo
self.band_high = wavelet.dec_hi
self.band_low.reverse()
self.band_high.reverse()
assert len(self.band_low) == len(self.band_high)
self.band_length = len(self.band_low)
assert self.band_length % 2 == 0
self.band_length_half = math.floor(self.band_length / 2)
def get_matrix(self):
"""
生成变换矩阵
generating the matrices: \mathcal{L}, \mathcal{H}
:return: self.matrix_low = \mathcal{L}, self.matrix_high = \mathcal{H}
"""
L1 = np.max((self.input_height, self.input_width))
L = math.floor(L1 / 2)
matrix_h = np.zeros( ( L, L1 + self.band_length - 2 ) )
matrix_g = np.zeros( ( L1 - L, L1 + self.band_length - 2 ) )
end = None if self.band_length_half == 1 else (-self.band_length_half+1)
index = 0
for i in range(L):
for j in range(self.band_length):
matrix_h[i, index+j] = self.band_low[j]
index += 2
matrix_h_0 = matrix_h[0:(math.floor(self.input_height / 2)), 0:(self.input_height + self.band_length - 2)]
matrix_h_1 = matrix_h[0:(math.floor(self.input_width / 2)), 0:(self.input_width + self.band_length - 2)]
matrix_h_2 = matrix_h[0:(math.floor(self.input_depth / 2)), 0:(self.input_depth + self.band_length - 2)]
index = 0
for i in range(L1 - L):
for j in range(self.band_length):
matrix_g[i, index+j] = self.band_high[j]
index += 2
matrix_g_0 = matrix_g[0:(self.input_height - math.floor(self.input_height / 2)),0:(self.input_height + self.band_length - 2)]
matrix_g_1 = matrix_g[0:(self.input_width - math.floor(self.input_width / 2)),0:(self.input_width + self.band_length - 2)]
matrix_g_2 = matrix_g[0:(self.input_depth - math.floor(self.input_depth / 2)),0:(self.input_depth + self.band_length - 2)]
matrix_h_0 = matrix_h_0[:,(self.band_length_half-1):end]
matrix_h_1 = matrix_h_1[:,(self.band_length_half-1):end]
matrix_h_1 = np.transpose(matrix_h_1)
matrix_h_2 = matrix_h_2[:,(self.band_length_half-1):end]
matrix_g_0 = matrix_g_0[:,(self.band_length_half-1):end]
matrix_g_1 = matrix_g_1[:,(self.band_length_half-1):end]
matrix_g_1 = np.transpose(matrix_g_1)
matrix_g_2 = matrix_g_2[:,(self.band_length_half-1):end]
if torch.cuda.is_available():
self.matrix_low_0 = torch.Tensor(matrix_h_0).cuda()
self.matrix_low_1 = torch.Tensor(matrix_h_1).cuda()
self.matrix_low_2 = torch.Tensor(matrix_h_2).cuda()
self.matrix_high_0 = torch.Tensor(matrix_g_0).cuda()
self.matrix_high_1 = torch.Tensor(matrix_g_1).cuda()
self.matrix_high_2 = torch.Tensor(matrix_g_2).cuda()
else:
self.matrix_low_0 = torch.Tensor(matrix_h_0)
self.matrix_low_1 = torch.Tensor(matrix_h_1)
self.matrix_low_2 = torch.Tensor(matrix_h_2)
self.matrix_high_0 = torch.Tensor(matrix_g_0)
self.matrix_high_1 = torch.Tensor(matrix_g_1)
self.matrix_high_2 = torch.Tensor(matrix_g_2)
def forward(self, LLL, LLH, LHL, LHH, HLL, HLH, HHL, HHH):
"""
:param LLL: the low-frequency component, lfc
:param LLH: the high-frequency componetn, hfc_llh
:param LHL: the high-frequency componetn, hfc_lhl
:param LHH: the high-frequency componetn, hfc_lhh
:param HLL: the high-frequency componetn, hfc_hll
:param HLH: the high-frequency componetn, hfc_hlh
:param HHL: the high-frequency componetn, hfc_hhl
:param HHH: the high-frequency componetn, hfc_hhh
:return: the original 3D input data
"""
assert len(LLL.size()) == len(LLH.size()) == len(LHL.size()) == len(LHH.size()) == 5
assert len(HLL.size()) == len(HLH.size()) == len(HHL.size()) == len(HHH.size()) == 5
self.input_depth = LLL.size()[-3] + HHH.size()[-3]
self.input_height = LLL.size()[-2] + HHH.size()[-2]
self.input_width = LLL.size()[-1] + HHH.size()[-1]
self.get_matrix()
return IDWTFunction_3D.apply(LLL, LLH, LHL, LHH, HLL, HLH, HHL, HHH,
self.matrix_low_0, self.matrix_low_1, self.matrix_low_2,
self.matrix_high_0, self.matrix_high_1, self.matrix_high_2)
if __name__ == '__main__':
from datetime import datetime
from torch.autograd import gradcheck
wavelet = pywt.Wavelet('bior1.1')
h = wavelet.rec_lo
g = wavelet.rec_hi
h_ = wavelet.dec_lo
g_ = wavelet.dec_hi
h_.reverse()
g_.reverse()
"""
image_full_name = '/home/li-qiufu/Pictures/standard_test_images/lena_color_512.tif'
image = cv2.imread(image_full_name, flags = 1)
image = image[0:512,0:512,:]
print(image.shape)
height, width, channel = image.shape
#image = image.reshape((1,height,width))
t0 = datetime.now()
for index in range(100):
m0 = DWT_2D(band_low = h, band_high = g)
image_tensor = torch.Tensor(image)
image_tensor.unsqueeze_(dim = 0)
print('image_re shape: {}'.format(image_tensor.size()))
image_tensor.transpose_(1,3)
print('image_re shape: {}'.format(image_tensor.size()))
image_tensor.transpose_(2,3)
print('image_re shape: {}'.format(image_tensor.size()))
image_tensor.requires_grad = False
LL, LH, HL, HH = m0(image_tensor)
matrix_low_0 = torch.Tensor(m0.matrix_low_0)
matrix_low_1 = torch.Tensor(m0.matrix_low_1)
matrix_high_0 = torch.Tensor(m0.matrix_high_0)
matrix_high_1 = torch.Tensor(m0.matrix_high_1)
#image_tensor.requires_grad = True
#input = (image_tensor.double(), matrix_low_0.double(), matrix_low_1.double(), matrix_high_0.double(), matrix_high_1.double())
#test = gradcheck(DWTFunction_2D.apply, input)
#print(test)
#print(LL.requires_grad)
#print(LH.requires_grad)
#print(HL.requires_grad)
#print(HH.requires_grad)
#LL.requires_grad = True
#input = (LL.double(), LH.double(), HL.double(), HH.double(), matrix_low_0.double(), matrix_low_1.double(), matrix_high_0.double(), matrix_high_1.double())
#test = gradcheck(IDWTFunction_2D.apply, input)
#print(test)
m1 = IDWT_2D(band_low = h_, band_high = g_)
image_re = m1(LL,LH,HL,HH)
t1 = datetime.now()
image_re.transpose_(2,3)
image_re.transpose_(1,3)
image_re_np = image_re.detach().numpy()
print('image_re shape: {}'.format(image_re_np.shape))
image_zero = image - image_re_np[0]
print(np.max(image_zero), np.min(image_zero))
print(image_zero[:,8])
print('taking {} secondes'.format(t1 - t0))
cv2.imshow('reconstruction', image_re_np[0]/255)
cv2.imshow('image_zero', image_zero/255)
cv2.waitKey(0)
"""
"""
image_full_name = '/home/liqiufu/Pictures/standard_test_images/lena_color_512.tif'
image = cv2.imread(image_full_name, flags = 1)
image = image[0:512,0:512,:]
print(image.shape)
image_3d = np.concatenate((image, image, image, image, image, image), axis = 2)
print(image_3d.shape)
image_tensor = torch.Tensor(image_3d)
#image_tensor = image_tensor.transpose(dim0 = 2, dim1 = 1)
#image_tensor = image_tensor.transpose(dim0 = 1, dim1 = 0)
image_tensor.unsqueeze_(dim = 0)
image_tensor.unsqueeze_(dim = 0)
t0 = datetime.now()
for index in range(10):
m0 = DWT_3D(wavename = 'haar')
print('image_re shape: {}'.format(image_tensor.size()))
image_tensor.requires_grad = False
LLL, LLH, LHL, LHH, HLL, HLH, HHL, HHH = m0(image_tensor)
matrix_low_0 = torch.Tensor(m0.matrix_low_0)
matrix_low_1 = torch.Tensor(m0.matrix_low_1)
matrix_low_2 = torch.Tensor(m0.matrix_low_2)
matrix_high_0 = torch.Tensor(m0.matrix_high_0)
matrix_high_1 = torch.Tensor(m0.matrix_high_1)
matrix_high_2 = torch.Tensor(m0.matrix_high_2)
#image_tensor.requires_grad = True
#input = (image_tensor.double(), matrix_low_0.double(), matrix_low_1.double(), matrix_low_2.double(),
# matrix_high_0.double(), matrix_high_1.double(), matrix_high_2.double())
#test = gradcheck(DWTFunction_3D.apply, input)
#print('testing dwt3d -- {}'.format(test))
#LLL.requires_grad = True
#input = (LLL.double(), LLH.double(), LHL.double(), LHH.double(),
# HLL.double(), HLH.double(), HHL.double(), HHH.double(),
# matrix_low_0.double(), matrix_low_1.double(), matrix_low_2.double(),
# matrix_high_0.double(), matrix_high_1.double(), matrix_high_2.double())
#test = gradcheck(IDWTFunction_3D.apply, input)
#print('testing idwt3d -- {}'.format(test))
m1 = IDWT_3D(wavename = 'haar')
image_re = m1(LLL,LLH,LHL,LHH,HLL,HLH,HHL,HHH)
t1 = datetime.now()
image_re.squeeze_(dim = 0)
image_re.squeeze_(dim = 0)
#image_re.transpose_(0,1)
#image_re.transpose_(1,2)
image_re_np = image_re.detach().numpy()
print('image_re shape: {}'.format(image_re_np.shape))
image_zero = image - image_re_np[:,:,0:3]
print(np.max(image_zero), np.min(image_zero))
#print(image_zero[:,8,0])
print('taking {} secondes'.format(t1 - t0))
cv2.imshow('reconstruction', image_re_np[:,:,0:3]/255)
cv2.imshow('image_zero', image_zero/255)
cv2.waitKey(0)
"""
"""
import matplotlib.pyplot as plt
import numpy as np
vector_np = np.array(list(range(1280)))#.reshape((128,1))
print(vector_np.shape)
t0 = datetime.now()
for index in range(100):
vector = torch.Tensor(vector_np)
vector.unsqueeze_(dim = 0)
vector.unsqueeze_(dim = 0)
m0 = DWT_1D(band_low = h, band_high = g)
L, H = m0(vector)
#matrix_low = torch.Tensor(m0.matrix_low)
#matrix_high = torch.Tensor(m0.matrix_high)
#vector.requires_grad = True
#input = (vector.double(), matrix_low.double(), matrix_high.double())
#test = gradcheck(DWTFunction_1D.apply, input)
#print('testing 1D-DWT: {}'.format(test))
#print(L.requires_grad)
#print(H.requires_grad)
#L.requires_grad = True
#H.requires_grad = True
#input = (L.double(), H.double(), matrix_low.double(), matrix_high.double())
#test = gradcheck(IDWTFunction_1D.apply, input)
#print('testing 1D-IDWT: {}'.format(test))
m1 = IDWT_1D(band_low = h_, band_high = g_)
vector_re = m1(L, H)
t1 = datetime.now()
vector_re_np = vector_re.detach().numpy()
print('image_re shape: {}'.format(vector_re_np.shape))
vector_zero = vector_np - vector_re_np.reshape(vector_np.shape)
print(np.max(vector_zero), np.min(vector_zero))
print(vector_zero[:8])
print('taking {} secondes'.format(t1 - t0))
"""