-
Notifications
You must be signed in to change notification settings - Fork 8
/
resample.py
97 lines (81 loc) · 3.61 KB
/
resample.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from .filter import LowPassFilter1d, LowPassFilter2d
from .filter import kaiser_sinc_filter1d, kaiser_sinc_filter2d
class UpSample1d(nn.Module):
def __init__(self, ratio=2, kernel_size=None):
super().__init__()
self.ratio = ratio
self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None \
else kernel_size
self.stride = ratio
self.pad = self.kernel_size // ratio - 1
self.pad_left = self.pad * self.stride + (self.kernel_size -
self.stride) // 2
self.pad_right = self.pad * self.stride + (self.kernel_size - self.stride +
1) // 2
filter = kaiser_sinc_filter1d(cutoff=0.5 / ratio,
half_width=0.6 / ratio,
kernel_size=self.kernel_size)
self.register_buffer("filter", filter)
# x: [B,C,T]
def forward(self, x):
_, C, _ = x.shape
x = F.pad(x, (self.pad, self.pad), mode='replicate')
x = self.ratio * F.conv_transpose1d(
x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
x = x[..., self.pad_left:-self.pad_right]
return x
class DownSample1d(nn.Module):
def __init__(self, ratio=2, kernel_size=None):
super().__init__()
self.ratio = ratio
self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None \
else kernel_size
self.lowpass = LowPassFilter1d(cutoff=0.5 / ratio,
half_width=0.6 / ratio,
stride=ratio,
kernel_size=self.kernel_size)
def forward(self, x):
xx = self.lowpass(x)
return xx
class UpSample2d(nn.Module):
def __init__(self, ratio=2, kernel_size=None):
super().__init__()
self.ratio = ratio
self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None \
else kernel_size
self.stride = ratio
self.pad = self.kernel_size // 2 - ratio // 2
self.pad_left = self.pad * self.stride + (self.kernel_size -
self.stride) // 2
self.pad_right = self.pad * self.stride + (self.kernel_size - self.stride +
1) // 2
filter = kaiser_sinc_filter2d(cutoff=0.5 / ratio,
half_width=0.6 / ratio,
kernel_size=self.kernel_size)
self.register_buffer("filter", filter)
# x: [B,C,W,H]
def forward(self, x):
_, C, _, _ = x.shape
x = F.pad(x, (self.pad, self.pad, self.pad, self.pad), mode='replicate')
x = self.ratio**2 * F.conv_transpose2d(
x, self.filter.expand(C, -1, -1, -1), stride=self.stride, groups=C)
x = x[..., self.pad_left:-self.pad_right,
self.pad_left:-self.pad_right]
return x
class DownSample2d(nn.Module):
def __init__(self, ratio=2, kernel_size=None):
super().__init__()
self.ratio = ratio
self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None \
else kernel_size
self.lowpass = LowPassFilter2d(cutoff=0.5 / ratio,
half_width=0.6 / ratio,
stride=ratio,
kernel_size=self.kernel_size)
# x: [B,C,W,H]
def forward(self, x):
xx = self.lowpass(x)
return xx