Refactor: Format Code with Ruff and Update Deprecated G2PW Link (#2255)
* ruff check --fix * ruff format --line-length 120 --target-version py39 * Change the link for G2PW Model * update pytorch version and colab
This commit is contained in:
@@ -5,24 +5,31 @@ import torchaudio
|
||||
import torch.utils.data
|
||||
import torchaudio.functional as aF
|
||||
|
||||
def amp_pha_stft(audio, n_fft, hop_size, win_size, center=True):
|
||||
|
||||
def amp_pha_stft(audio, n_fft, hop_size, win_size, center=True):
|
||||
hann_window = torch.hann_window(win_size).to(audio.device)
|
||||
stft_spec = torch.stft(audio, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window,
|
||||
center=center, pad_mode='reflect', normalized=False, return_complex=True)
|
||||
log_amp = torch.log(torch.abs(stft_spec)+1e-4)
|
||||
stft_spec = torch.stft(
|
||||
audio,
|
||||
n_fft,
|
||||
hop_length=hop_size,
|
||||
win_length=win_size,
|
||||
window=hann_window,
|
||||
center=center,
|
||||
pad_mode="reflect",
|
||||
normalized=False,
|
||||
return_complex=True,
|
||||
)
|
||||
log_amp = torch.log(torch.abs(stft_spec) + 1e-4)
|
||||
pha = torch.angle(stft_spec)
|
||||
|
||||
com = torch.stack((torch.exp(log_amp)*torch.cos(pha),
|
||||
torch.exp(log_amp)*torch.sin(pha)), dim=-1)
|
||||
com = torch.stack((torch.exp(log_amp) * torch.cos(pha), torch.exp(log_amp) * torch.sin(pha)), dim=-1)
|
||||
|
||||
return log_amp, pha, com
|
||||
|
||||
|
||||
def amp_pha_istft(log_amp, pha, n_fft, hop_size, win_size, center=True):
|
||||
|
||||
amp = torch.exp(log_amp)
|
||||
com = torch.complex(amp*torch.cos(pha), amp*torch.sin(pha))
|
||||
com = torch.complex(amp * torch.cos(pha), amp * torch.sin(pha))
|
||||
hann_window = torch.hann_window(win_size).to(com.device)
|
||||
audio = torch.istft(com, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window, center=center)
|
||||
|
||||
@@ -30,18 +37,28 @@ def amp_pha_istft(log_amp, pha, n_fft, hop_size, win_size, center=True):
|
||||
|
||||
|
||||
def get_dataset_filelist(a):
|
||||
with open(a.input_training_file, 'r', encoding='utf-8') as fi:
|
||||
training_indexes = [x.split('|')[0] for x in fi.read().split('\n') if len(x) > 0]
|
||||
with open(a.input_training_file, "r", encoding="utf-8") as fi:
|
||||
training_indexes = [x.split("|")[0] for x in fi.read().split("\n") if len(x) > 0]
|
||||
|
||||
with open(a.input_validation_file, 'r', encoding='utf-8') as fi:
|
||||
validation_indexes = [x.split('|')[0] for x in fi.read().split('\n') if len(x) > 0]
|
||||
with open(a.input_validation_file, "r", encoding="utf-8") as fi:
|
||||
validation_indexes = [x.split("|")[0] for x in fi.read().split("\n") if len(x) > 0]
|
||||
|
||||
return training_indexes, validation_indexes
|
||||
|
||||
|
||||
class Dataset(torch.utils.data.Dataset):
|
||||
def __init__(self, training_indexes, wavs_dir, segment_size, hr_sampling_rate, lr_sampling_rate,
|
||||
split=True, shuffle=True, n_cache_reuse=1, device=None):
|
||||
def __init__(
|
||||
self,
|
||||
training_indexes,
|
||||
wavs_dir,
|
||||
segment_size,
|
||||
hr_sampling_rate,
|
||||
lr_sampling_rate,
|
||||
split=True,
|
||||
shuffle=True,
|
||||
n_cache_reuse=1,
|
||||
device=None,
|
||||
):
|
||||
self.audio_indexes = training_indexes
|
||||
random.seed(1234)
|
||||
if shuffle:
|
||||
@@ -59,7 +76,7 @@ class Dataset(torch.utils.data.Dataset):
|
||||
def __getitem__(self, index):
|
||||
filename = self.audio_indexes[index]
|
||||
if self._cache_ref_count == 0:
|
||||
audio, orig_sampling_rate = torchaudio.load(os.path.join(self.wavs_dir, filename + '.wav'))
|
||||
audio, orig_sampling_rate = torchaudio.load(os.path.join(self.wavs_dir, filename + ".wav"))
|
||||
self.cached_wav = audio
|
||||
self._cache_ref_count = self.n_cache_reuse
|
||||
else:
|
||||
@@ -79,14 +96,13 @@ class Dataset(torch.utils.data.Dataset):
|
||||
if audio_hr.size(1) >= self.segment_size:
|
||||
max_audio_start = audio_hr.size(1) - self.segment_size
|
||||
audio_start = random.randint(0, max_audio_start)
|
||||
audio_hr = audio_hr[:, audio_start: audio_start+self.segment_size]
|
||||
audio_lr = audio_lr[:, audio_start: audio_start+self.segment_size]
|
||||
audio_hr = audio_hr[:, audio_start : audio_start + self.segment_size]
|
||||
audio_lr = audio_lr[:, audio_start : audio_start + self.segment_size]
|
||||
else:
|
||||
audio_hr = torch.nn.functional.pad(audio_hr, (0, self.segment_size - audio_hr.size(1)), 'constant')
|
||||
audio_lr = torch.nn.functional.pad(audio_lr, (0, self.segment_size - audio_lr.size(1)), 'constant')
|
||||
audio_hr = torch.nn.functional.pad(audio_hr, (0, self.segment_size - audio_hr.size(1)), "constant")
|
||||
audio_lr = torch.nn.functional.pad(audio_lr, (0, self.segment_size - audio_lr.size(1)), "constant")
|
||||
|
||||
return (audio_hr.squeeze(), audio_lr.squeeze())
|
||||
|
||||
def __len__(self):
|
||||
|
||||
return len(self.audio_indexes)
|
||||
|
||||
Reference in New Issue
Block a user