MCPcopy Create free account
hub / github.com/TeleHuman/TextOp / Patcher1D

Class Patcher1D

TextOpRobotMDAR/robotmdar/model/modules.py:14–73  ·  view source on GitHub ↗

A module to convert 1D signal tensors into patches using torch operations.

Source from the content-addressed store, hash-verified

12_PERSISTENT = True
13
14class Patcher1D(torch.nn.Module):
15 """A module to convert 1D signal tensors into patches using torch operations."""
16
17 def __init__(self, patch_size=1, patch_method="haar"):
18 super().__init__()
19 self.patch_size = patch_size
20 self.patch_method = patch_method
21 self.register_buffer("wavelets", _WAVELETS[patch_method], persistent=_PERSISTENT)
22 self.range = range(int(torch.log2(torch.tensor(self.patch_size)).item()))
23 self.register_buffer(
24 "_arange",
25 torch.arange(_WAVELETS[patch_method].shape[0]),
26 persistent=_PERSISTENT,
27 )
28 for param in self.parameters():
29 param.requires_grad = False
30
31 def forward(self, x):
32 if self.patch_method == "haar":
33 return self._haar(x)
34 elif self.patch_method == "rearrange":
35 return self._arrange(x)
36 else:
37 raise ValueError("Unknown patch method: " + self.patch_method)
38
39 def _dwt(self, x, mode="reflect", rescale=False):
40 dtype = x.dtype
41 h = self.wavelets
42
43 n = h.shape[0]
44 g = x.shape[1]
45 hl = h.flip(0).reshape(1, 1, -1).repeat(g, 1, 1)
46 hh = (h * ((-1) ** self._arange)).reshape(1, 1, -1).repeat(g, 1, 1)
47 hh = hh.to(dtype=dtype)
48 hl = hl.to(dtype=dtype)
49
50 # 1D padding
51 x = F.pad(x, pad=(n - 2, n - 1), mode=mode).to(dtype)
52
53 # 1D小波变换
54 xl = F.conv1d(x, hl, groups=g, stride=2) # 低通滤波
55 xh = F.conv1d(x, hh, groups=g, stride=2) # 高通滤波
56
57 out = torch.cat([xl, xh], dim=1)
58 if rescale:
59 out = out / 2
60 return out
61
62 def _haar(self, x):
63 for _ in self.range:
64 x = self._dwt(x, rescale=True)
65 return x
66
67 def _arrange(self, x):
68 x = rearrange(
69 x,
70 "b c (l p) -> b (c p) l",
71 p=self.patch_size,

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected