Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learnable PQMF #41

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
support learnable pqmf
  • Loading branch information
takenori-y committed Jun 19, 2023
commit b46fa82970a1ddb87e753aaec71cf89fb662261c
11 changes: 9 additions & 2 deletions diffsptk/core/ipqmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ class InversePseudoQuadratureMirrorFilterBanks(nn.Module):
alpha : float > 0 [scalar]
Stopband attenuation in dB.

learnable : bool [scalar]
Whether to make filter-bank coefficients learnable.

**kwargs : additional keyword arguments
Parameters to find optimal filter-bank coefficients.

"""

def __init__(self, n_band, filter_order, alpha=100, **kwargs):
def __init__(self, n_band, filter_order, alpha=100, learnable=False, **kwargs):
super(InversePseudoQuadratureMirrorFilterBanks, self).__init__()

assert 1 <= n_band
Expand All @@ -59,7 +62,11 @@ def __init__(self, n_band, filter_order, alpha=100, **kwargs):
warnings.warn("Failed to find PQMF coefficients")
filters = np.expand_dims(filters, 0)
filters = np.flip(filters, 2).copy()
self.register_buffer("filters", numpy_to_torch(filters))
filters = numpy_to_torch(filters)
if learnable:
self.filters = nn.Parameter(filters)
else:
self.register_buffer("filters", filters)

# Make padding module.
if filter_order % 2 == 0:
Expand Down
11 changes: 9 additions & 2 deletions diffsptk/core/pqmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,15 @@ class PseudoQuadratureMirrorFilterBanks(nn.Module):
alpha : float > 0 [scalar]
Stopband attenuation in dB.

learnable : bool [scalar]
Whether to make filter-bank coefficients learnable.

**kwargs : additional keyword arguments
Parameters to find optimal filter-bank coefficients.

"""

def __init__(self, n_band, filter_order, alpha=100, **kwargs):
def __init__(self, n_band, filter_order, alpha=100, learnable=False, **kwargs):
super(PseudoQuadratureMirrorFilterBanks, self).__init__()

assert 1 <= n_band
Expand All @@ -174,7 +177,11 @@ def __init__(self, n_band, filter_order, alpha=100, **kwargs):
warnings.warn("Failed to find PQMF coefficients")
filters = np.expand_dims(filters, 1)
filters = np.flip(filters, 2).copy()
self.register_buffer("filters", numpy_to_torch(filters))
filters = numpy_to_torch(filters)
if learnable:
self.filters = nn.Parameter(filters)
else:
self.register_buffer("filters", filters)

# Make padding module.
if filter_order % 2 == 0:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_ipqmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ def test_compatibility(device, a, M, tau=0.01, eps=0.01, K=4, T=20):
)

U.check_differentiable(device, ipqmf, [K, T], opt={"keepdim": False})


def test_learnable(K=4, M=10, T=20):
ipqmf = diffsptk.IPQMF(K, M, learnable=True)
U.check_learnable(ipqmf, (K, T))
5 changes: 5 additions & 0 deletions tests/test_pqmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ def test_compatibility(device, a, M, tau=0.01, eps=0.01, K=4, T=20):
def test_various_shape(K=4, M=10, T=20):
pqmf = diffsptk.PQMF(K, M)
U.check_various_shape(pqmf, [(T,), (1, T), (1, 1, T)])


def test_learnable(K=4, M=10, T=20):
pqmf = diffsptk.PQMF(K, M, learnable=True)
U.check_learnable(pqmf, (T,))
21 changes: 21 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,24 @@ def check_various_shape(module, shapes):
target = y
else:
assert torch.allclose(y, target)


def check_learnable(module, shape):
params_before = []
for p in module.parameters():
params_before.append(p.clone())

optimizer = torch.optim.SGD(module.parameters(), lr=0.01)
x = torch.randn(*shape)
y = module(x)
optimizer.zero_grad()
loss = y.mean()
loss.backward()
optimizer.step()

params_after = []
for p in module.parameters():
params_after.append(p.clone())

for pb, pa in zip(params_before, params_after):
assert not torch.allclose(pb, pa)