Skip to content

Commit

Permalink
Merge pull request #64 from sp-nitech/use_pad
Browse files Browse the repository at this point in the history
Use pad instead of cat
  • Loading branch information
takenori-y committed Jan 31, 2024
2 parents 9e0aadf + f870707 commit 09d866e
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 29 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
torch: 1.11.0
torchaudio: 0.11.0
- python: 3.11
torch: 2.1.2
torchaudio: 2.1.2
torch: 2.2.0
torchaudio: 2.2.0

steps:
- name: Clone
Expand All @@ -37,10 +37,9 @@ jobs:
run: |
sudo apt-get update -qq
sudo apt-get install -qq -y libsndfile1
pip3 install torch==${{ matrix.torch }}+cpu torchaudio==${{ matrix.torchaudio }}+cpu -f https://download.pytorch.org/whl/torch_stable.html
- name: Install
run: make PYTHON_VERSION=${{ matrix.python }} dev tool
run: make PYTHON_VERSION=${{ matrix.python }} TORCH_VERSION=${{ matrix.torch }} TORCHAUDIO_VERSION=${{ matrix.torchaudio }} PLATFORM=cpu dev tool

- name: Check
run: make check
Expand Down
20 changes: 13 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
# limitations under the License. #
# ------------------------------------------------------------------------ #

PROJECT := diffsptk
PYTHON_VERSION := 3.8
MODULE :=
PROJECT := diffsptk
MODULE :=

PYTHON_VERSION := 3.8
TORCH_VERSION := 1.11.0
TORCHAUDIO_VERSION := 0.11.0
PLATFORM := cu113

init:
pip install -e .

dev:
test -d venv || python$(PYTHON_VERSION) -m venv venv; \
. ./venv/bin/activate; python -m pip install pip --upgrade; \
python -m pip install torch==1.11.0+cu113 torchaudio==0.11.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html; \
python -m pip install torch==$(TORCH_VERSION)+$(PLATFORM) torchaudio==$(TORCHAUDIO_VERSION)+$(PLATFORM) \
-f https://download.pytorch.org/whl/$(PLATFORM)/torch_stable.html; \
python -m pip install -e .[dev]

dist:
. ./venv/bin/activate; python -m build --wheel; \
twine check dist/*
. ./venv/bin/activate; \
python -m build --wheel; \
python -m twine check dist/*.whl

dist-clean:
rm -rf dist
Expand Down Expand Up @@ -65,7 +71,7 @@ test:
fi
[ -n "$(MODULE)" ] && module=tests/test_$(MODULE).py || module=; \
. ./venv/bin/activate; export PATH=tools/SPTK/bin:$$PATH; \
python -m pytest -s --cov=./ --cov-report=xml $$module
python -m pytest -s --cov=./ --cov-report=xml $$module

test-clean:
rm -rf tests/__pycache__
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ diffsptk
*diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.

[![Latest Manual](https://img.shields.io/badge/docs-latest-blue.svg)](https://sp-nitech.github.io/diffsptk/latest/)
[![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.1.0/)
[![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.2.0/)
[![Downloads](https://static.pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk)
[![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.1.2-orange.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.2.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
[![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
[![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
Expand Down
8 changes: 4 additions & 4 deletions diffsptk/core/linear_intpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import replicate1


class LinearInterpolation(nn.Module):
"""Perform linear interpolation.
Expand All @@ -38,8 +40,6 @@ def __init__(self, upsampling_factor):

assert 1 <= self.upsampling_factor

self.pad = nn.ReplicationPad1d((0, 1))

def forward(self, x):
"""Interpolate filter coefficients.
Expand Down Expand Up @@ -76,8 +76,8 @@ def forward(self, x):
assert x.dim() == 3, "Input must be 3D tensor"
B, T, D = x.shape

x = x.mT
x = self.pad(x)
x = x.mT # (B, D, T)
x = replicate1(x, left=False)
x = F.interpolate(
x,
size=T * self.upsampling_factor + 1,
Expand Down
3 changes: 3 additions & 0 deletions diffsptk/core/mgc2mgc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import torch.nn.functional as F

from ..misc.utils import cexp
from ..misc.utils import check_size
from ..misc.utils import clog
from ..misc.utils import numpy_to_torch
from .freqt import FrequencyTransform
Expand Down Expand Up @@ -80,6 +81,8 @@ def forward(self, c1):
Output cepstrum.
"""
check_size(c1.size(-1), self.in_order + 1, "dimension of cepstrum")

c01 = F.pad(c1[..., 1:], (1, 0))
C1 = torch.fft.fft(c01, n=self.n_fft)

Expand Down
1 change: 1 addition & 0 deletions diffsptk/core/par2lpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, lpc_order):
super(ParcorCoefficientsToLinearPredictiveCoefficients, self).__init__()

self.lpc_order = lpc_order

assert 0 <= self.lpc_order

def forward(self, k):
Expand Down
3 changes: 2 additions & 1 deletion diffsptk/core/plp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import torch.nn as nn

from ..misc.utils import numpy_to_torch
from ..misc.utils import replicate1
from .fbank import MelFilterBankAnalysis
from .levdur import LevinsonDurbin
from .mgc2mgc import MelGeneralizedCepstrumToMelGeneralizedCepstrum
Expand Down Expand Up @@ -156,7 +157,7 @@ def forward(self, x):
"""
y, E = self.fbank(x)
y = (torch.exp(y) * self.equal_loudness_curve) ** self.compression_factor
y = torch.cat((y[..., :1], y, y[..., -1:]), dim=-1)
y = replicate1(y)
y = torch.fft.hfft(y, norm="forward")[..., : self.plp_order + 1].real
y = self.levdur(y)
y = self.lpc2c(y)
Expand Down
4 changes: 3 additions & 1 deletion diffsptk/core/zcross.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import torch
import torch.nn as nn

from ..misc.utils import replicate1


class ZeroCrossingAnalysis(nn.Module):
"""See `this page <https://sp-nitech.github.io/sptk/latest/main/zcross.html>`_
Expand Down Expand Up @@ -65,7 +67,7 @@ def forward(self, x):
"""
x = torch.sign(x)
x = torch.cat((x[..., :1], x), dim=-1)
x = replicate1(x, right=False)
x = x.unfold(-1, self.frame_length + 1, self.frame_length)
z = 0.5 * (x[..., 1:] - x[..., :-1]).abs().sum(-1)
if self.norm:
Expand Down
26 changes: 18 additions & 8 deletions diffsptk/misc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,23 @@ def numpy_to_torch(x):


def to_3d(x):
d = x.dim()
if d == 1:
y = x.view(1, 1, -1)
elif d == 2:
y = x.unsqueeze(1)
else:
y = x.view(-1, 1, x.size(-1))
y = x.view(-1, 1, x.size(-1))
return y


def reflect(x):
d = x.size(-1)
y = x.view(-1, d)
y = F.pad(y, (d - 1, 0), mode="reflect")
y = y.view(*x.size()[:-1], -1)
return y


def replicate1(x, left=True, right=True):
d = x.size(-1)
y = x.view(-1, d)
y = F.pad(y, (1 if left else 0, 1 if right else 0), mode="replicate")
y = y.view(*x.size()[:-1], -1)
return y


Expand Down Expand Up @@ -177,7 +187,7 @@ def get_gamma(gamma, c):

def symmetric_toeplitz(x):
d = x.size(-1)
xx = torch.cat((x[..., 1:].flip(-1), x), dim=-1)
xx = reflect(x)
X = xx.unfold(-1, d, 1).flip(-2)
return X

Expand Down
2 changes: 1 addition & 1 deletion diffsptk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.0"
__version__ = "1.2.0"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"torchaudio >= 0.11.0",
"torchcrepe >= 0.0.21",
"torchlpc >= 0.2.0",
"vector-quantize-pytorch >= 0.8.0",
"vector-quantize-pytorch >= 0.8.0, <= 1.12.12",
"numpy",
]
dynamic = ["version"]
Expand Down

0 comments on commit 09d866e

Please sign in to comment.