-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1024 lines (891 loc) · 41.9 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
import os
import random
import shutil
import sys
from datetime import datetime
import numpy as np
import torch
from torch.utils.tensorboard import SummaryWriter
import gdown
class Logger(object):
"""Reference: https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514"""
def __init__(self, fn, ask=True):
if not os.path.exists("results/"):
os.mkdir("results/")
logdir = self._make_dir(fn)
if not os.path.exists(logdir):
os.mkdir(logdir)
if len(os.listdir(logdir)) != 0 and ask:
exit(1)
self.set_dir(logdir)
def _make_dir(self, fn):
# today = datetime.today().strftime("%y%m%d")
logdir = f'./results/{fn}/'
return logdir
def set_dir(self, logdir, log_fn='log.txt'):
self.logdir = logdir
if not os.path.exists(logdir):
os.mkdir(logdir)
self.writer = SummaryWriter(logdir)
self.log_file = open(os.path.join(logdir, log_fn), 'a')
def log(self, string):
self.log_file.write('[%s] %s' % (datetime.now(), string) + '\n')
self.log_file.flush()
print('[%s] %s' % (datetime.now(), string))
sys.stdout.flush()
def log_dirname(self, string):
self.log_file.write('%s (%s)' % (string, self.logdir) + '\n')
self.log_file.flush()
print('%s (%s)' % (string, self.logdir))
sys.stdout.flush()
def scalar_summary(self, tag, value, step):
"""Log a scalar variable."""
self.writer.add_scalar(tag, value, step)
def image_summary(self, tag, images, step):
"""Log a list of images."""
self.writer.add_image(tag, images, step)
def video_summary(self, tag, videos, step):
self.writer.add_video(tag, videos, step, fps=16)
def histo_summary(self, tag, values, step):
"""Log a histogram of the tensor of values."""
self.writer.add_histogram(tag, values, step, bins='auto')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.value = 0
self.average = 0
self.sum = 0
self.count = 0
def reset(self):
self.value = 0
self.average = 0
self.sum = 0
self.count = 0
def update(self, value, n=1):
self.value = value
self.sum += value * n
self.count += n
self.average = self.sum / self.count
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def file_name(args):
fn = f'{args.exp}_{args.id}_{args.data}'
fn += f'_{args.seed}'
return fn
def psnr(mse):
"""
Computes PSNR from MSE.
"""
return -10.0 * mse.log10()
def download(id, fname, root=os.path.expanduser('~/.cache/video-diffusion')):
os.makedirs(root, exist_ok=True)
destination = os.path.join(root, fname)
if os.path.exists(destination):
return destination
gdown.download(id=id, output=destination, quiet=False)
return destination
def make_pairs(l, t1, t2, num_pairs, given_vid):
B, T, C, H, W = given_vid.size()
idx1 = t1.view(B, num_pairs, 1, 1, 1, 1).expand(B, num_pairs, 1, C, H, W).type(torch.int64)
frame1 = torch.gather(given_vid.unsqueeze(1).repeat(1,num_pairs, 1,1,1,1), 2, idx1).squeeze()
t1 = t1.float() / (l - 1)
idx2 = t2.view(B, num_pairs, 1, 1, 1, 1).expand(B, num_pairs, 1, C, H, W).type(torch.int64)
frame2 = torch.gather(given_vid.unsqueeze(1).repeat(1,num_pairs,1,1,1,1), 2, idx2).squeeze()
t2 = t2.float() / (l - 1)
frame1 = frame1.view(-1, C, H, W)
frame2 = frame2.view(-1, C, H, W)
# sort by t
t1 = t1.view(-1, 1, 1, 1).repeat(1, C, H, W)
t2 = t2.view(-1, 1, 1, 1).repeat(1, C, H, W)
ret_frame1 = torch.where(t1 < t2, frame1, frame2)
ret_frame2 = torch.where(t1 < t2, frame2 ,frame1)
t1 = t1[:, 0:1]
t2 = t2[:, 0:1]
ret_t1 = torch.where(t1 < t2, t1, t2)
ret_t2 = torch.where(t1 < t2, t2, t1)
dt = ret_t2 - ret_t1
return torch.cat([ret_frame1, ret_frame2, dt], dim=1)
def make_mixed_pairs(l, t1, t2, given_vid_real, given_vid_fake):
B, T, C, H, W = given_vid_real.size()
idx1 = t1.view(-1, 1, 1, 1, 1).expand(B, 1, C, H, W).type(torch.int64)
frame1 = torch.gather(given_vid_real, 1, idx1).squeeze()
t1 = t1.float() / (l - 1)
idx2 = t2.view(-1, 1, 1, 1, 1).expand(B, 1, C, H, W).type(torch.int64)
frame2 = torch.gather(given_vid_fake, 1, idx2).squeeze()
t2 = t2.float() / (l - 1)
# sort by t
t1 = t1.view(-1, 1, 1, 1).repeat(1, C, H, W)
t2 = t2.view(-1, 1, 1, 1).repeat(1, C, H, W)
ret_frame1 = torch.where(t1 < t2, frame1, frame2)
ret_frame2 = torch.where(t1 < t2, frame2 ,frame1)
t1 = t1[:, 0:1]
t2 = t2[:, 0:1]
ret_t1 = torch.where(t1 < t2, t1, t2)
ret_t2 = torch.where(t1 < t2, t2, t1)
dt = ret_t2 - ret_t1
return torch.cat([ret_frame1, ret_frame2, dt], dim=1)
# Wavelet Diffusion Models are fast and scalable Image Generators
# Hao Phung, Quan Dao, Anh Tran
# https://arxiv.org/abs/2211.16152
import torch
from torch.autograd import Function
class DWTFunction_1D(Function):
@staticmethod
def forward(ctx, input, matrix_Low, matrix_High):
ctx.save_for_backward(matrix_Low, matrix_High)
L = torch.matmul(input, matrix_Low.t())
H = torch.matmul(input, matrix_High.t())
return L, H
@staticmethod
def backward(ctx, grad_L, grad_H):
matrix_L, matrix_H = ctx.saved_variables
grad_input = torch.add(torch.matmul(
grad_L, matrix_L), torch.matmul(grad_H, matrix_H))
return grad_input, None, None
class IDWTFunction_1D(Function):
@staticmethod
def forward(ctx, input_L, input_H, matrix_L, matrix_H):
ctx.save_for_backward(matrix_L, matrix_H)
output = torch.add(torch.matmul(input_L, matrix_L),
torch.matmul(input_H, matrix_H))
return output
@staticmethod
def backward(ctx, grad_output):
matrix_L, matrix_H = ctx.saved_variables
grad_L = torch.matmul(grad_output, matrix_L.t())
grad_H = torch.matmul(grad_output, matrix_H.t())
return grad_L, grad_H, None, None
class DWTFunction_2D(Function):
@staticmethod
def forward(ctx, input, matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1):
ctx.save_for_backward(matrix_Low_0, matrix_Low_1,
matrix_High_0, matrix_High_1)
L = torch.matmul(matrix_Low_0, input)
H = torch.matmul(matrix_High_0, input)
LL = torch.matmul(L, matrix_Low_1)
LH = torch.matmul(L, matrix_High_1)
HL = torch.matmul(H, matrix_Low_1)
HH = torch.matmul(H, matrix_High_1)
return LL, LH, HL, HH
@staticmethod
def backward(ctx, grad_LL, grad_LH, grad_HL, grad_HH):
matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1 = ctx.saved_variables
grad_L = torch.add(torch.matmul(grad_LL, matrix_Low_1.t()),
torch.matmul(grad_LH, matrix_High_1.t()))
grad_H = torch.add(torch.matmul(grad_HL, matrix_Low_1.t()),
torch.matmul(grad_HH, matrix_High_1.t()))
grad_input = torch.add(torch.matmul(
matrix_Low_0.t(), grad_L), torch.matmul(matrix_High_0.t(), grad_H))
return grad_input, None, None, None, None
class DWTFunction_2D_tiny(Function):
@staticmethod
def forward(ctx, input, matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1):
ctx.save_for_backward(matrix_Low_0, matrix_Low_1,
matrix_High_0, matrix_High_1)
L = torch.matmul(matrix_Low_0, input)
LL = torch.matmul(L, matrix_Low_1)
return LL
@staticmethod
def backward(ctx, grad_LL):
matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1 = ctx.saved_variables
grad_L = torch.matmul(grad_LL, matrix_Low_1.t())
grad_input = torch.matmul(matrix_Low_0.t(), grad_L)
return grad_input, None, None, None, None
class IDWTFunction_2D(Function):
@staticmethod
def forward(ctx, input_LL, input_LH, input_HL, input_HH,
matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1):
ctx.save_for_backward(matrix_Low_0, matrix_Low_1,
matrix_High_0, matrix_High_1)
L = torch.add(torch.matmul(input_LL, matrix_Low_1.t()),
torch.matmul(input_LH, matrix_High_1.t()))
H = torch.add(torch.matmul(input_HL, matrix_Low_1.t()),
torch.matmul(input_HH, matrix_High_1.t()))
output = torch.add(torch.matmul(matrix_Low_0.t(), L),
torch.matmul(matrix_High_0.t(), H))
return output
@staticmethod
def backward(ctx, grad_output):
matrix_Low_0, matrix_Low_1, matrix_High_0, matrix_High_1 = ctx.saved_variables
grad_L = torch.matmul(matrix_Low_0, grad_output)
grad_H = torch.matmul(matrix_High_0, grad_output)
grad_LL = torch.matmul(grad_L, matrix_Low_1)
grad_LH = torch.matmul(grad_L, matrix_High_1)
grad_HL = torch.matmul(grad_H, matrix_Low_1)
grad_HH = torch.matmul(grad_H, matrix_High_1)
return grad_LL, grad_LH, grad_HL, grad_HH, None, None, None, None
class DWTFunction_3D(Function):
@staticmethod
def forward(ctx, input,
matrix_Low_0, matrix_Low_1, matrix_Low_2,
matrix_High_0, matrix_High_1, matrix_High_2):
ctx.save_for_backward(matrix_Low_0, matrix_Low_1, matrix_Low_2,
matrix_High_0, matrix_High_1, matrix_High_2)
L = torch.matmul(matrix_Low_0, input)
H = torch.matmul(matrix_High_0, input)
LL = torch.matmul(L, matrix_Low_1).transpose(dim0=2, dim1=3)
LH = torch.matmul(L, matrix_High_1).transpose(dim0=2, dim1=3)
HL = torch.matmul(H, matrix_Low_1).transpose(dim0=2, dim1=3)
HH = torch.matmul(H, matrix_High_1).transpose(dim0=2, dim1=3)
LLL = torch.matmul(matrix_Low_2, LL).transpose(dim0=2, dim1=3)
LLH = torch.matmul(matrix_Low_2, LH).transpose(dim0=2, dim1=3)
LHL = torch.matmul(matrix_Low_2, HL).transpose(dim0=2, dim1=3)
LHH = torch.matmul(matrix_Low_2, HH).transpose(dim0=2, dim1=3)
HLL = torch.matmul(matrix_High_2, LL).transpose(dim0=2, dim1=3)
HLH = torch.matmul(matrix_High_2, LH).transpose(dim0=2, dim1=3)
HHL = torch.matmul(matrix_High_2, HL).transpose(dim0=2, dim1=3)
HHH = torch.matmul(matrix_High_2, HH).transpose(dim0=2, dim1=3)
return LLL, LLH, LHL, LHH, HLL, HLH, HHL, HHH
@staticmethod
def backward(ctx, grad_LLL, grad_LLH, grad_LHL, grad_LHH,
grad_HLL, grad_HLH, grad_HHL, grad_HHH):
matrix_Low_0, matrix_Low_1, matrix_Low_2, matrix_High_0, matrix_High_1, matrix_High_2 = ctx.saved_variables
grad_LL = torch.add(torch.matmul(matrix_Low_2.t(), grad_LLL.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), grad_HLL.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
grad_LH = torch.add(torch.matmul(matrix_Low_2.t(), grad_LLH.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), grad_HLH.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
grad_HL = torch.add(torch.matmul(matrix_Low_2.t(), grad_LHL.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), grad_HHL.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
grad_HH = torch.add(torch.matmul(matrix_Low_2.t(), grad_LHH.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), grad_HHH.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
grad_L = torch.add(torch.matmul(grad_LL, matrix_Low_1.t()),
torch.matmul(grad_LH, matrix_High_1.t()))
grad_H = torch.add(torch.matmul(grad_HL, matrix_Low_1.t()),
torch.matmul(grad_HH, matrix_High_1.t()))
grad_input = torch.add(torch.matmul(
matrix_Low_0.t(), grad_L), torch.matmul(matrix_High_0.t(), grad_H))
return grad_input, None, None, None, None, None, None, None, None
class IDWTFunction_3D(Function):
@staticmethod
def forward(ctx, input_LLL, input_LLH, input_LHL, input_LHH,
input_HLL, input_HLH, input_HHL, input_HHH,
matrix_Low_0, matrix_Low_1, matrix_Low_2,
matrix_High_0, matrix_High_1, matrix_High_2):
ctx.save_for_backward(matrix_Low_0, matrix_Low_1, matrix_Low_2,
matrix_High_0, matrix_High_1, matrix_High_2)
input_LL = torch.add(torch.matmul(matrix_Low_2.t(), input_LLL.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), input_HLL.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
input_LH = torch.add(torch.matmul(matrix_Low_2.t(), input_LLH.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), input_HLH.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
input_HL = torch.add(torch.matmul(matrix_Low_2.t(), input_LHL.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), input_HHL.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
input_HH = torch.add(torch.matmul(matrix_Low_2.t(), input_LHH.transpose(dim0=2, dim1=3)), torch.matmul(
matrix_High_2.t(), input_HHH.transpose(dim0=2, dim1=3))).transpose(dim0=2, dim1=3)
input_L = torch.add(torch.matmul(input_LL, matrix_Low_1.t()),
torch.matmul(input_LH, matrix_High_1.t()))
input_H = torch.add(torch.matmul(input_HL, matrix_Low_1.t()),
torch.matmul(input_HH, matrix_High_1.t()))
output = torch.add(torch.matmul(matrix_Low_0.t(), input_L),
torch.matmul(matrix_High_0.t(), input_H))
return output
@staticmethod
def backward(ctx, grad_output):
matrix_Low_0, matrix_Low_1, matrix_Low_2, matrix_High_0, matrix_High_1, matrix_High_2 = ctx.saved_variables
grad_L = torch.matmul(matrix_Low_0, grad_output)
grad_H = torch.matmul(matrix_High_0, grad_output)
grad_LL = torch.matmul(grad_L, matrix_Low_1).transpose(dim0=2, dim1=3)
grad_LH = torch.matmul(grad_L, matrix_High_1).transpose(dim0=2, dim1=3)
grad_HL = torch.matmul(grad_H, matrix_Low_1).transpose(dim0=2, dim1=3)
grad_HH = torch.matmul(grad_H, matrix_High_1).transpose(dim0=2, dim1=3)
grad_LLL = torch.matmul(
matrix_Low_2, grad_LL).transpose(dim0=2, dim1=3)
grad_LLH = torch.matmul(
matrix_Low_2, grad_LH).transpose(dim0=2, dim1=3)
grad_LHL = torch.matmul(
matrix_Low_2, grad_HL).transpose(dim0=2, dim1=3)
grad_LHH = torch.matmul(
matrix_Low_2, grad_HH).transpose(dim0=2, dim1=3)
grad_HLL = torch.matmul(
matrix_High_2, grad_LL).transpose(dim0=2, dim1=3)
grad_HLH = torch.matmul(
matrix_High_2, grad_LH).transpose(dim0=2, dim1=3)
grad_HHL = torch.matmul(
matrix_High_2, grad_HL).transpose(dim0=2, dim1=3)
grad_HHH = torch.matmul(
matrix_High_2, grad_HH).transpose(dim0=2, dim1=3)
return grad_LLL, grad_LLH, grad_LHL, grad_LHH, grad_HLL, grad_HLH, grad_HHL, grad_HHH, None, None, None, None, None, None
import math
import numpy as np
import pywt
import torch
from torch.nn import Module
__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)