File size: 8,890 Bytes
7f2690b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import numpy as np

TO_WARN_USER_ONCE = True

class AbstractPermuter(nn.Module):
    def __init__(self, *args, **kwargs):
        super().__init__()
    def forward(self, x, reverse=False):
        raise NotImplementedError


class Identity(AbstractPermuter):
    def __init__(self):
        super().__init__()

    def forward(self, x, reverse=False):
        return x

class ColumnMajor(AbstractPermuter):
    '''Useful for spectrograms which are from left to right (features, time)'''
    def __init__(self, H, W):
        super().__init__()
        self.H = H
        self.W = W
        idx = self.make_idx(H, W)
        self.register_buffer('forward_shuffle_idx', idx)
        self.register_buffer('backward_shuffle_idx', torch.argsort(idx))

    def forward(self, x, reverse=False):
        B, L = x.shape
        L_idx = len(self.forward_shuffle_idx)
        if L > L_idx:
            # an ugly patch for "infinite" sampling because self.*_shuffle_idx are shorter
            # otherwise even uglier patch in other places. 'if' is triggered only on sampling.
            assert L % L_idx == 0 and L / L_idx == int(L / L_idx), f'L: {L}, L_idx: {L_idx}'
            W_scale = L // L_idx
            # print(f'Permuter is making a guess on the temp scale: {W_scale}. Ignore on "infinite" sampling')
            idx = self.make_idx(self.H, self.W * W_scale)
            if not reverse:
                return x[:, idx]
            else:
                return x[:, torch.argsort(idx)]
        else:
            if not reverse:
                return x[:, self.forward_shuffle_idx]
            else:
                return x[:, self.backward_shuffle_idx]

    def make_idx(self, H, W):
        idx = np.arange(H * W).reshape(H, W)
        idx = idx.T
        idx = torch.tensor(idx.ravel())
        return idx

class Subsample(AbstractPermuter):
    def __init__(self, H, W):
        super().__init__()
        C = 1
        indices = np.arange(H*W).reshape(C,H,W)
        while min(H, W) > 1:
            indices = indices.reshape(C,H//2,2,W//2,2)
            indices = indices.transpose(0,2,4,1,3)
            indices = indices.reshape(C*4,H//2, W//2)
            H = H//2
            W = W//2
            C = C*4
        assert H == W == 1
        idx = torch.tensor(indices.ravel())
        self.register_buffer('forward_shuffle_idx',
                             nn.Parameter(idx, requires_grad=False))
        self.register_buffer('backward_shuffle_idx',
                             nn.Parameter(torch.argsort(idx), requires_grad=False))

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


def mortonify(i, j):
    """(i,j) index to linear morton code"""
    i = np.uint64(i)
    j = np.uint64(j)

    z = np.uint(0)

    for pos in range(32):
        z = (z |
             ((j & (np.uint64(1) << np.uint64(pos))) << np.uint64(pos)) |
             ((i & (np.uint64(1) << np.uint64(pos))) << np.uint64(pos+1))
             )
    return z


class ZCurve(AbstractPermuter):
    def __init__(self, H, W):
        super().__init__()
        reverseidx = [np.int64(mortonify(i,j)) for i in range(H) for j in range(W)]
        idx = np.argsort(reverseidx)
        idx = torch.tensor(idx)
        reverseidx = torch.tensor(reverseidx)
        self.register_buffer('forward_shuffle_idx',
                             idx)
        self.register_buffer('backward_shuffle_idx',
                             reverseidx)

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


class SpiralOut(AbstractPermuter):
    def __init__(self, H, W):
        super().__init__()
        assert H == W
        size = W
        indices = np.arange(size*size).reshape(size,size)

        i0 = size//2
        j0 = size//2-1

        i = i0
        j = j0

        idx = [indices[i0, j0]]
        step_mult = 0
        for c in range(1, size//2+1):
            step_mult += 1
            # steps left
            for k in range(step_mult):
                i = i - 1
                j = j
                idx.append(indices[i, j])

            # step down
            for k in range(step_mult):
                i = i
                j = j + 1
                idx.append(indices[i, j])

            step_mult += 1
            if c < size//2:
                # step right
                for k in range(step_mult):
                    i = i + 1
                    j = j
                    idx.append(indices[i, j])

                # step up
                for k in range(step_mult):
                    i = i
                    j = j - 1
                    idx.append(indices[i, j])
            else:
                # end reached
                for k in range(step_mult-1):
                    i = i + 1
                    idx.append(indices[i, j])

        assert len(idx) == size*size
        idx = torch.tensor(idx)
        self.register_buffer('forward_shuffle_idx', idx)
        self.register_buffer('backward_shuffle_idx', torch.argsort(idx))

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


class SpiralIn(AbstractPermuter):
    def __init__(self, H, W):
        super().__init__()
        assert H == W
        size = W
        indices = np.arange(size*size).reshape(size,size)

        i0 = size//2
        j0 = size//2-1

        i = i0
        j = j0

        idx = [indices[i0, j0]]
        step_mult = 0
        for c in range(1, size//2+1):
            step_mult += 1
            # steps left
            for k in range(step_mult):
                i = i - 1
                j = j
                idx.append(indices[i, j])

            # step down
            for k in range(step_mult):
                i = i
                j = j + 1
                idx.append(indices[i, j])

            step_mult += 1
            if c < size//2:
                # step right
                for k in range(step_mult):
                    i = i + 1
                    j = j
                    idx.append(indices[i, j])

                # step up
                for k in range(step_mult):
                    i = i
                    j = j - 1
                    idx.append(indices[i, j])
            else:
                # end reached
                for k in range(step_mult-1):
                    i = i + 1
                    idx.append(indices[i, j])

        assert len(idx) == size*size
        idx = idx[::-1]
        idx = torch.tensor(idx)
        self.register_buffer('forward_shuffle_idx', idx)
        self.register_buffer('backward_shuffle_idx', torch.argsort(idx))

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


class Random(nn.Module):
    def __init__(self, H, W):
        super().__init__()
        indices = np.random.RandomState(1).permutation(H*W)
        idx = torch.tensor(indices.ravel())
        self.register_buffer('forward_shuffle_idx', idx)
        self.register_buffer('backward_shuffle_idx', torch.argsort(idx))

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


class AlternateParsing(AbstractPermuter):
    def __init__(self, H, W):
        super().__init__()
        indices = np.arange(W*H).reshape(H,W)
        for i in range(1, H, 2):
            indices[i, :] = indices[i, ::-1]
        idx = indices.flatten()
        assert len(idx) == H*W
        idx = torch.tensor(idx)
        self.register_buffer('forward_shuffle_idx', idx)
        self.register_buffer('backward_shuffle_idx', torch.argsort(idx))

    def forward(self, x, reverse=False):
        if not reverse:
            return x[:, self.forward_shuffle_idx]
        else:
            return x[:, self.backward_shuffle_idx]


if __name__ == "__main__":
    p0 = AlternateParsing(16, 16)
    print(p0.forward_shuffle_idx)
    print(p0.backward_shuffle_idx)

    x = torch.randint(0, 768, size=(11, 256))
    y = p0(x)
    xre = p0(y, reverse=True)
    assert torch.equal(x, xre)

    p1 = SpiralOut(2, 2)
    print(p1.forward_shuffle_idx)
    print(p1.backward_shuffle_idx)
    x = torch.randint(0, 768, size=(11, 2*2))
    y = p1(x)
    xre = p1(y, reverse=True)
    assert torch.equal(x, xre)

    p2 = ColumnMajor(5, 53)
    print(p2.forward_shuffle_idx)
    print(p2.backward_shuffle_idx)
    x = torch.randint(0, 768, size=(11, 5*53))
    xre = p2(p2(x), reverse=True)
    assert torch.equal(x, xre)