| 304 | |
| 305 | # ## |
| 306 | class MWT_CZ1d(nn.Module): |
| 307 | def __init__(self, |
| 308 | k=3, alpha=64, |
| 309 | L=0, c=1, |
| 310 | base='legendre', |
| 311 | initializer=None, |
| 312 | **kwargs): |
| 313 | super(MWT_CZ1d, self).__init__() |
| 314 | |
| 315 | self.k = k |
| 316 | self.L = L |
| 317 | H0, H1, G0, G1, PHI0, PHI1 = get_filter(base, k) |
| 318 | H0r = H0 @ PHI0 |
| 319 | G0r = G0 @ PHI0 |
| 320 | H1r = H1 @ PHI1 |
| 321 | G1r = G1 @ PHI1 |
| 322 | |
| 323 | H0r[np.abs(H0r) < 1e-8] = 0 |
| 324 | H1r[np.abs(H1r) < 1e-8] = 0 |
| 325 | G0r[np.abs(G0r) < 1e-8] = 0 |
| 326 | G1r[np.abs(G1r) < 1e-8] = 0 |
| 327 | self.max_item = 3 |
| 328 | |
| 329 | self.A = sparseKernelFT1d(k, alpha, c) |
| 330 | self.B = sparseKernelFT1d(k, alpha, c) |
| 331 | self.C = sparseKernelFT1d(k, alpha, c) |
| 332 | |
| 333 | self.T0 = nn.Linear(k, k) |
| 334 | |
| 335 | self.register_buffer('ec_s', torch.Tensor( |
| 336 | np.concatenate((H0.T, H1.T), axis=0))) |
| 337 | self.register_buffer('ec_d', torch.Tensor( |
| 338 | np.concatenate((G0.T, G1.T), axis=0))) |
| 339 | |
| 340 | self.register_buffer('rc_e', torch.Tensor( |
| 341 | np.concatenate((H0r, G0r), axis=0))) |
| 342 | self.register_buffer('rc_o', torch.Tensor( |
| 343 | np.concatenate((H1r, G1r), axis=0))) |
| 344 | |
| 345 | def forward(self, x): |
| 346 | B, N, c, k = x.shape # (B, N, k) |
| 347 | ns = math.floor(np.log2(N)) |
| 348 | nl = pow(2, math.ceil(np.log2(N))) |
| 349 | extra_x = x[:, 0:nl - N, :, :] |
| 350 | x = torch.cat([x, extra_x], 1) |
| 351 | Ud = torch.jit.annotate(List[Tensor], []) |
| 352 | Us = torch.jit.annotate(List[Tensor], []) |
| 353 | # decompose |
| 354 | for i in range(ns - self.L): |
| 355 | # print('x shape',x.shape) |
| 356 | d, x = self.wavelet_transform(x) |
| 357 | Ud += [self.A(d) + self.B(x)] |
| 358 | Us += [self.C(d)] |
| 359 | x = self.T0(x) # coarsest scale transform |
| 360 | |
| 361 | # reconstruct |
| 362 | for i in range(ns - 1 - self.L, -1, -1): |
| 363 | x = x + Us[i] |