-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
197 lines (179 loc) · 7.75 KB
/
layers.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
# from doconv_pytorch import *
import math
import torch
import numpy as np
from torch.nn import functional as F
from torch._jit_internal import Optional
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
from torch import nn
from torch.nn import init
def window_partitions(x, window_size):
"""
Args:
x: (B, C, H, W)
window_size (int): window size
Returns:
windows: (num_windows*B, C, window_size, window_size)
"""
if isinstance(window_size, int):
window_size = [window_size, window_size]
B, C, H, W = x.shape
x = x.view(B, C, H // window_size[0], window_size[0], W // window_size[1], window_size[1])
windows = x.permute(0, 2, 4, 1, 3, 5).contiguous().view(-1, C, window_size[0], window_size[1])
return windows
def window_reverses(windows, window_size, H, W):
"""
Args:
windows: (num_windows*B, C, window_size, window_size)
window_size (int): Window size
H (int): Height of image
W (int): Width of image
Returns:
x: (B, C, H, W)
"""
# B = int(windows.shape[0] / (H * W / window_size / window_size))
# print('B: ', B)
# print(H // window_size)
# print(W // window_size)
if isinstance(window_size, int):
window_size = [window_size, window_size]
C = windows.shape[1]
# print('C: ', C)
x = windows.view(-1, H // window_size[0], W // window_size[1], C, window_size[0], window_size[1])
x = x.permute(0, 3, 1, 4, 2, 5).contiguous().view(-1, C, H, W)
return x
def window_partitionx(x, window_size):
_, _, H, W = x.shape
h, w = window_size * (H // window_size), window_size * (W // window_size)
x_main = window_partitions(x[:, :, :h, :w], window_size)
b_main = x_main.shape[0]
if h == H and w == W:
return x_main, [b_main]
if h != H and w != W:
x_r = window_partitions(x[:, :, :h, -window_size:], window_size)
b_r = x_r.shape[0] + b_main
x_d = window_partitions(x[:, :, -window_size:, :w], window_size)
b_d = x_d.shape[0] + b_r
x_dd = x[:, :, -window_size:, -window_size:]
b_dd = x_dd.shape[0] + b_d
# batch_list = [b_main, b_r, b_d, b_dd]
return torch.cat([x_main, x_r, x_d, x_dd], dim=0), [b_main, b_r, b_d, b_dd]
if h == H and w != W:
x_r = window_partitions(x[:, :, :h, -window_size:], window_size)
b_r = x_r.shape[0] + b_main
return torch.cat([x_main, x_r], dim=0), [b_main, b_r]
if h != H and w == W:
x_d = window_partitions(x[:, :, -window_size:, :w], window_size)
b_d = x_d.shape[0] + b_main
return torch.cat([x_main, x_d], dim=0), [b_main, b_d]
def window_reversex(windows, window_size, H, W, batch_list):
h, w = window_size * (H // window_size), window_size * (W // window_size)
# print(windows[:batch_list[0], ...].shape)
x_main = window_reverses(windows[:batch_list[0], ...], window_size, h, w)
B, C, _, _ = x_main.shape
# print('windows: ', windows.shape)
# print('batch_list: ', batch_list)
if torch.is_complex(windows):
res = torch.complex(torch.zeros([B, C, H, W]), torch.zeros([B, C, H, W]))
res = res.to(windows.device)
else:
res = torch.zeros([B, C, H, W], device=windows.device)
res[:, :, :h, :w] = x_main
if h == H and w == W:
return res
if h != H and w != W and len(batch_list) == 4:
x_dd = window_reverses(windows[batch_list[2]:, ...], window_size, window_size, window_size)
res[:, :, h:, w:] = x_dd[:, :, h - H:, w - W:]
x_r = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, h, window_size)
res[:, :, :h, w:] = x_r[:, :, :, w - W:]
x_d = window_reverses(windows[batch_list[1]:batch_list[2], ...], window_size, window_size, w)
res[:, :, h:, :w] = x_d[:, :, h - H:, :]
return res
if w != W and len(batch_list) == 2:
x_r = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, h, window_size)
res[:, :, :h, w:] = x_r[:, :, :, w - W:]
if h != H and len(batch_list) == 2:
x_d = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, window_size, w)
res[:, :, h:, :w] = x_d[:, :, h - H:, :]
return res
def window_partitions_old(x, window_size):
"""
Args:
x: (B, C, H, W)
window_size (int): window size
Returns:
windows: (num_windows*B, C, window_size, window_size)
"""
B, C, H, W = x.shape
x = x.view(B, C, H // window_size, window_size, W // window_size, window_size)
windows = x.permute(0, 2, 4, 1, 3, 5).contiguous().view(-1, C, window_size, window_size)
return windows
def window_reverses_old(windows, window_size, H, W):
"""
Args:
windows: (num_windows*B, C, window_size, window_size)
window_size (int): Window size
H (int): Height of image
W (int): Width of image
Returns:
x: (B, C, H, W)
"""
# B = int(windows.shape[0] / (H * W / window_size / window_size))
# print('B: ', B)
# print(H // window_size)
# print(W // window_size)
C = windows.shape[1]
# print('C: ', C)
x = windows.view(-1, H // window_size, W // window_size, C, window_size, window_size)
x = x.permute(0, 3, 1, 4, 2, 5).contiguous().view(-1, C, H, W)
return x
def window_partitionx_old(x, window_size):
_, _, H, W = x.shape
h, w = window_size * (H // window_size), window_size * (W // window_size)
x_main = window_partitions(x[:, :, :h, :w], window_size)
b_main = x_main.shape[0]
if h == H and w == W:
return x_main, [b_main]
if h != H and w != W:
x_r = window_partitions(x[:, :, :h, -window_size:], window_size)
b_r = x_r.shape[0] + b_main
x_d = window_partitions(x[:, :, -window_size:, :w], window_size)
b_d = x_d.shape[0] + b_r
x_dd = x[:, :, -window_size:, -window_size:]
b_dd = x_dd.shape[0] + b_d
# batch_list = [b_main, b_r, b_d, b_dd]
return torch.cat([x_main, x_r, x_d, x_dd], dim=0), [b_main, b_r, b_d, b_dd]
if h == H and w != W:
x_r = window_partitions(x[:, :, :h, -window_size:], window_size)
b_r = x_r.shape[0] + b_main
return torch.cat([x_main, x_r], dim=0), [b_main, b_r]
if h != H and w == W:
x_d = window_partitions(x[:, :, -window_size:, :w], window_size)
b_d = x_d.shape[0] + b_main
return torch.cat([x_main, x_d], dim=0), [b_main, b_d]
def window_reversex_old(windows, window_size, H, W, batch_list):
h, w = window_size * (H // window_size), window_size * (W // window_size)
x_main = window_reverses(windows[:batch_list[0], ...], window_size, h, w)
B, C, _, _ = x_main.shape
# print('windows: ', windows.shape)
# print('batch_list: ', batch_list)
res = torch.zeros([B, C, H, W], device=windows.device)
res[:, :, :h, :w] = x_main
if h == H and w == W:
return res
if h != H and w != W and len(batch_list) == 4:
x_dd = window_reverses(windows[batch_list[2]:, ...], window_size, window_size, window_size)
res[:, :, h:, w:] = x_dd[:, :, h - H:, w - W:]
x_r = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, h, window_size)
res[:, :, :h, w:] = x_r[:, :, :, w - W:]
x_d = window_reverses(windows[batch_list[1]:batch_list[2], ...], window_size, window_size, w)
res[:, :, h:, :w] = x_d[:, :, h - H:, :]
return res
if w != W and len(batch_list) == 2:
x_r = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, h, window_size)
res[:, :, :h, w:] = x_r[:, :, :, w - W:]
if h != H and len(batch_list) == 2:
x_d = window_reverses(windows[batch_list[0]:batch_list[1], ...], window_size, window_size, w)
res[:, :, h:, :w] = x_d[:, :, h - H:, :]
return res