mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-08-14 12:32:20 +00:00
added VITS recipe
This commit is contained in:
parent
e0136d9263
commit
2a5aa7c13a
@ -53,6 +53,9 @@ if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
|
|||||||
log "Downloading x-vector"
|
log "Downloading x-vector"
|
||||||
|
|
||||||
git clone https://huggingface.co/datasets/zrjin/xvector_nnet_1a_libritts_clean_460 $dl_dir/xvector_nnet_1a_libritts_clean_460
|
git clone https://huggingface.co/datasets/zrjin/xvector_nnet_1a_libritts_clean_460 $dl_dir/xvector_nnet_1a_libritts_clean_460
|
||||||
|
|
||||||
|
mkdir -p exp/xvector_nnet_1a/
|
||||||
|
cp -r $dl_dir/xvector_nnet_1a_libritts_clean_460/* exp/xvector_nnet_1a/
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
1
egs/libritts/TTS/vits/duration_predictor.py
Symbolic link
1
egs/libritts/TTS/vits/duration_predictor.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/duration_predictor.py
|
1
egs/libritts/TTS/vits/flow.py
Symbolic link
1
egs/libritts/TTS/vits/flow.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/flow.py
|
1
egs/libritts/TTS/vits/generator.py
Symbolic link
1
egs/libritts/TTS/vits/generator.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/generator.py
|
1
egs/libritts/TTS/vits/hifigan.py
Symbolic link
1
egs/libritts/TTS/vits/hifigan.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/hifigan.py
|
273
egs/libritts/TTS/vits/infer.py
Executable file
273
egs/libritts/TTS/vits/infer.py
Executable file
@ -0,0 +1,273 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# Copyright 2023 Xiaomi Corporation (Author: Zengwei Yao,
|
||||||
|
# Zengrui Jin,)
|
||||||
|
#
|
||||||
|
# See ../../../../LICENSE for clarification regarding multiple authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
"""
|
||||||
|
This script performs model inference on test set.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
./vits/infer.py \
|
||||||
|
--epoch 1000 \
|
||||||
|
--exp-dir ./vits/exp \
|
||||||
|
--max-duration 500
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
import k2
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchaudio
|
||||||
|
from tokenizer import Tokenizer
|
||||||
|
from train import get_model, get_params
|
||||||
|
from tts_datamodule import LibrittsTtsDataModule
|
||||||
|
|
||||||
|
from icefall.checkpoint import load_checkpoint
|
||||||
|
from icefall.utils import AttributeDict, setup_logger
|
||||||
|
|
||||||
|
|
||||||
|
def get_parser():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--epoch",
|
||||||
|
type=int,
|
||||||
|
default=1000,
|
||||||
|
help="""It specifies the checkpoint to use for decoding.
|
||||||
|
Note: Epoch counts from 1.
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--exp-dir",
|
||||||
|
type=str,
|
||||||
|
default="vits/exp",
|
||||||
|
help="The experiment dir",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--tokens",
|
||||||
|
type=str,
|
||||||
|
default="data/tokens.txt",
|
||||||
|
help="""Path to vocabulary.""",
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def infer_dataset(
|
||||||
|
dl: torch.utils.data.DataLoader,
|
||||||
|
subset: str,
|
||||||
|
params: AttributeDict,
|
||||||
|
model: nn.Module,
|
||||||
|
tokenizer: Tokenizer,
|
||||||
|
speaker_map: Dict[str, int],
|
||||||
|
) -> None:
|
||||||
|
"""Decode dataset.
|
||||||
|
The ground-truth and generated audio pairs will be saved to `params.save_wav_dir`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dl:
|
||||||
|
PyTorch's dataloader containing the dataset to decode.
|
||||||
|
params:
|
||||||
|
It is returned by :func:`get_params`.
|
||||||
|
model:
|
||||||
|
The neural model.
|
||||||
|
tokenizer:
|
||||||
|
Used to convert text to phonemes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Background worker save audios to disk.
|
||||||
|
def _save_worker(
|
||||||
|
subset: str,
|
||||||
|
batch_size: int,
|
||||||
|
cut_ids: List[str],
|
||||||
|
audio: torch.Tensor,
|
||||||
|
audio_pred: torch.Tensor,
|
||||||
|
audio_lens: List[int],
|
||||||
|
audio_lens_pred: List[int],
|
||||||
|
):
|
||||||
|
for i in range(batch_size):
|
||||||
|
torchaudio.save(
|
||||||
|
str(params.save_wav_dir / subset / f"{cut_ids[i]}_gt.wav"),
|
||||||
|
audio[i : i + 1, : audio_lens[i]],
|
||||||
|
sample_rate=params.sampling_rate,
|
||||||
|
)
|
||||||
|
torchaudio.save(
|
||||||
|
str(params.save_wav_dir / subset / f"{cut_ids[i]}_pred.wav"),
|
||||||
|
audio_pred[i : i + 1, : audio_lens_pred[i]],
|
||||||
|
sample_rate=params.sampling_rate,
|
||||||
|
)
|
||||||
|
|
||||||
|
device = next(model.parameters()).device
|
||||||
|
num_cuts = 0
|
||||||
|
log_interval = 5
|
||||||
|
|
||||||
|
try:
|
||||||
|
num_batches = len(dl)
|
||||||
|
except TypeError:
|
||||||
|
num_batches = "?"
|
||||||
|
|
||||||
|
futures = []
|
||||||
|
with ThreadPoolExecutor(max_workers=1) as executor:
|
||||||
|
for batch_idx, batch in enumerate(dl):
|
||||||
|
batch_size = len(batch["tokens"])
|
||||||
|
|
||||||
|
tokens = batch["tokens"]
|
||||||
|
tokens = tokenizer.tokens_to_token_ids(
|
||||||
|
tokens, intersperse_blank=True, add_sos=True, add_eos=True
|
||||||
|
)
|
||||||
|
tokens = k2.RaggedTensor(tokens)
|
||||||
|
row_splits = tokens.shape.row_splits(1)
|
||||||
|
tokens_lens = row_splits[1:] - row_splits[:-1]
|
||||||
|
tokens = tokens.to(device)
|
||||||
|
tokens_lens = tokens_lens.to(device)
|
||||||
|
# tensor of shape (B, T)
|
||||||
|
tokens = tokens.pad(mode="constant", padding_value=tokenizer.pad_id)
|
||||||
|
speakers = (
|
||||||
|
torch.Tensor([speaker_map[sid] for sid in batch["speakers"]])
|
||||||
|
.int()
|
||||||
|
.to(device)
|
||||||
|
)
|
||||||
|
|
||||||
|
audio = batch["audio"]
|
||||||
|
audio_lens = batch["audio_lens"].tolist()
|
||||||
|
cut_ids = [cut.id for cut in batch["cut"]]
|
||||||
|
|
||||||
|
audio_pred, _, durations = model.inference_batch(
|
||||||
|
text=tokens,
|
||||||
|
text_lengths=tokens_lens,
|
||||||
|
sids=speakers,
|
||||||
|
)
|
||||||
|
audio_pred = audio_pred.detach().cpu()
|
||||||
|
# convert to samples
|
||||||
|
audio_lens_pred = (
|
||||||
|
(durations.sum(1) * params.frame_shift).to(dtype=torch.int64).tolist()
|
||||||
|
)
|
||||||
|
|
||||||
|
futures.append(
|
||||||
|
executor.submit(
|
||||||
|
_save_worker,
|
||||||
|
subset,
|
||||||
|
batch_size,
|
||||||
|
cut_ids,
|
||||||
|
audio,
|
||||||
|
audio_pred,
|
||||||
|
audio_lens,
|
||||||
|
audio_lens_pred,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
num_cuts += batch_size
|
||||||
|
|
||||||
|
if batch_idx % log_interval == 0:
|
||||||
|
batch_str = f"{batch_idx}/{num_batches}"
|
||||||
|
|
||||||
|
logging.info(
|
||||||
|
f"batch {batch_str}, cuts processed until now is {num_cuts}"
|
||||||
|
)
|
||||||
|
# return results
|
||||||
|
for f in futures:
|
||||||
|
f.result()
|
||||||
|
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def main():
|
||||||
|
parser = get_parser()
|
||||||
|
LibrittsTtsDataModule.add_arguments(parser)
|
||||||
|
args = parser.parse_args()
|
||||||
|
args.exp_dir = Path(args.exp_dir)
|
||||||
|
|
||||||
|
params = get_params()
|
||||||
|
params.update(vars(args))
|
||||||
|
|
||||||
|
params.suffix = f"epoch-{params.epoch}"
|
||||||
|
|
||||||
|
params.res_dir = params.exp_dir / "infer" / params.suffix
|
||||||
|
params.save_wav_dir = params.res_dir / "wav"
|
||||||
|
params.save_wav_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
setup_logger(f"{params.res_dir}/log-infer-{params.suffix}")
|
||||||
|
logging.info("Infer started")
|
||||||
|
|
||||||
|
device = torch.device("cpu")
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
device = torch.device("cuda", 0)
|
||||||
|
|
||||||
|
tokenizer = Tokenizer(params.tokens)
|
||||||
|
params.blank_id = tokenizer.pad_id
|
||||||
|
params.vocab_size = tokenizer.vocab_size
|
||||||
|
|
||||||
|
# we need cut ids to display recognition results.
|
||||||
|
args.return_cuts = True
|
||||||
|
libritts = LibrittsTtsDataModule(args)
|
||||||
|
speaker_map = libritts.speakers()
|
||||||
|
params.num_spks = len(speaker_map)
|
||||||
|
|
||||||
|
logging.info(f"Device: {device}")
|
||||||
|
logging.info(params)
|
||||||
|
|
||||||
|
logging.info("About to create model")
|
||||||
|
model = get_model(params)
|
||||||
|
|
||||||
|
load_checkpoint(f"{params.exp_dir}/epoch-{params.epoch}.pt", model)
|
||||||
|
|
||||||
|
model.to(device)
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
num_param_g = sum([p.numel() for p in model.generator.parameters()])
|
||||||
|
logging.info(f"Number of parameters in generator: {num_param_g}")
|
||||||
|
num_param_d = sum([p.numel() for p in model.discriminator.parameters()])
|
||||||
|
logging.info(f"Number of parameters in discriminator: {num_param_d}")
|
||||||
|
logging.info(f"Total number of parameters: {num_param_g + num_param_d}")
|
||||||
|
|
||||||
|
test_cuts = libritts.test_cuts()
|
||||||
|
test_dl = libritts.test_dataloaders(test_cuts)
|
||||||
|
|
||||||
|
valid_cuts = libritts.valid_cuts()
|
||||||
|
valid_dl = libritts.valid_dataloaders(valid_cuts)
|
||||||
|
|
||||||
|
infer_sets = {"test": test_dl, "valid": valid_dl}
|
||||||
|
|
||||||
|
for subset, dl in infer_sets.items():
|
||||||
|
save_wav_dir = params.res_dir / "wav" / subset
|
||||||
|
save_wav_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
logging.info(f"Processing {subset} set, saving to {save_wav_dir}")
|
||||||
|
|
||||||
|
infer_dataset(
|
||||||
|
dl=dl,
|
||||||
|
subset=subset,
|
||||||
|
params=params,
|
||||||
|
model=model,
|
||||||
|
tokenizer=tokenizer,
|
||||||
|
speaker_map=speaker_map,
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.info(f"Wav files are saved to {params.save_wav_dir}")
|
||||||
|
logging.info("Done!")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
egs/libritts/TTS/vits/loss.py
Symbolic link
1
egs/libritts/TTS/vits/loss.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/loss.py
|
1
egs/libritts/TTS/vits/monotonic_align
Symbolic link
1
egs/libritts/TTS/vits/monotonic_align
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/monotonic_align
|
1
egs/libritts/TTS/vits/posterior_encoder.py
Symbolic link
1
egs/libritts/TTS/vits/posterior_encoder.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/posterior_encoder.py
|
1
egs/libritts/TTS/vits/residual_coupling.py
Symbolic link
1
egs/libritts/TTS/vits/residual_coupling.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/residual_coupling.py
|
141
egs/libritts/TTS/vits/test_onnx.py
Executable file
141
egs/libritts/TTS/vits/test_onnx.py
Executable file
@ -0,0 +1,141 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# Copyright 2023-2024 Xiaomi Corporation (Author: Zengwei Yao,
|
||||||
|
# Zengrui Jin,)
|
||||||
|
#
|
||||||
|
# See ../../../../LICENSE for clarification regarding multiple authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
"""
|
||||||
|
This script is used to test the exported onnx model by vits/export-onnx.py
|
||||||
|
|
||||||
|
Use the onnx model to generate a wav:
|
||||||
|
./vits/test_onnx.py \
|
||||||
|
--model-filename vits/exp/vits-epoch-1000.onnx \
|
||||||
|
--tokens data/tokens.txt
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import onnxruntime as ort
|
||||||
|
import torch
|
||||||
|
import torchaudio
|
||||||
|
from tokenizer import Tokenizer
|
||||||
|
|
||||||
|
|
||||||
|
def get_parser():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--model-filename",
|
||||||
|
type=str,
|
||||||
|
required=True,
|
||||||
|
help="Path to the onnx model.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--speakers",
|
||||||
|
type=Path,
|
||||||
|
default=Path("data/speakers.txt"),
|
||||||
|
help="Path to speakers.txt file.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--tokens",
|
||||||
|
type=str,
|
||||||
|
default="data/tokens.txt",
|
||||||
|
help="""Path to vocabulary.""",
|
||||||
|
)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
class OnnxModel:
|
||||||
|
def __init__(self, model_filename: str):
|
||||||
|
session_opts = ort.SessionOptions()
|
||||||
|
session_opts.inter_op_num_threads = 1
|
||||||
|
session_opts.intra_op_num_threads = 4
|
||||||
|
|
||||||
|
self.session_opts = session_opts
|
||||||
|
|
||||||
|
self.model = ort.InferenceSession(
|
||||||
|
model_filename,
|
||||||
|
sess_options=self.session_opts,
|
||||||
|
providers=["CPUExecutionProvider"],
|
||||||
|
)
|
||||||
|
logging.info(f"{self.model.get_modelmeta().custom_metadata_map}")
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self, tokens: torch.Tensor, tokens_lens: torch.Tensor, speaker: torch.Tensor
|
||||||
|
) -> torch.Tensor:
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
tokens:
|
||||||
|
A 1-D tensor of shape (1, T)
|
||||||
|
Returns:
|
||||||
|
A tensor of shape (1, T')
|
||||||
|
"""
|
||||||
|
noise_scale = torch.tensor([0.667], dtype=torch.float32)
|
||||||
|
noise_scale_dur = torch.tensor([0.8], dtype=torch.float32)
|
||||||
|
alpha = torch.tensor([1.0], dtype=torch.float32)
|
||||||
|
|
||||||
|
out = self.model.run(
|
||||||
|
[
|
||||||
|
self.model.get_outputs()[0].name,
|
||||||
|
],
|
||||||
|
{
|
||||||
|
self.model.get_inputs()[0].name: tokens.numpy(),
|
||||||
|
self.model.get_inputs()[1].name: tokens_lens.numpy(),
|
||||||
|
self.model.get_inputs()[2].name: noise_scale.numpy(),
|
||||||
|
self.model.get_inputs()[3].name: alpha.numpy(),
|
||||||
|
self.model.get_inputs()[4].name: noise_scale_dur.numpy(),
|
||||||
|
self.model.get_inputs()[5].name: speaker.numpy(),
|
||||||
|
},
|
||||||
|
)[0]
|
||||||
|
return torch.from_numpy(out)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = get_parser().parse_args()
|
||||||
|
|
||||||
|
tokenizer = Tokenizer(args.tokens)
|
||||||
|
|
||||||
|
with open(args.speakers) as f:
|
||||||
|
speaker_map = {line.strip(): i for i, line in enumerate(f)}
|
||||||
|
args.num_spks = len(speaker_map)
|
||||||
|
|
||||||
|
logging.info("About to create onnx model")
|
||||||
|
model = OnnxModel(args.model_filename)
|
||||||
|
|
||||||
|
text = "I went there to see the land, the people and how their system works, end quote."
|
||||||
|
tokens = tokenizer.texts_to_token_ids(
|
||||||
|
[text], intersperse_blank=True, add_sos=True, add_eos=True
|
||||||
|
)
|
||||||
|
tokens = torch.tensor(tokens) # (1, T)
|
||||||
|
tokens_lens = torch.tensor([tokens.shape[1]], dtype=torch.int64) # (1, T)
|
||||||
|
speaker = torch.tensor([1], dtype=torch.int64) # (1, )
|
||||||
|
audio = model(tokens, tokens_lens, speaker) # (1, T')
|
||||||
|
|
||||||
|
torchaudio.save(str("test_onnx.wav"), audio, sample_rate=22050)
|
||||||
|
logging.info("Saved to test_onnx.wav")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
||||||
|
logging.basicConfig(format=formatter, level=logging.INFO)
|
||||||
|
main()
|
1
egs/libritts/TTS/vits/text_encoder.py
Symbolic link
1
egs/libritts/TTS/vits/text_encoder.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/text_encoder.py
|
1
egs/libritts/TTS/vits/tokenizer.py
Symbolic link
1
egs/libritts/TTS/vits/tokenizer.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/tokenizer.py
|
1002
egs/libritts/TTS/vits/train.py
Executable file
1002
egs/libritts/TTS/vits/train.py
Executable file
File diff suppressed because it is too large
Load Diff
1
egs/libritts/TTS/vits/transform.py
Symbolic link
1
egs/libritts/TTS/vits/transform.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/transform.py
|
341
egs/libritts/TTS/vits/tts_datamodule.py
Normal file
341
egs/libritts/TTS/vits/tts_datamodule.py
Normal file
@ -0,0 +1,341 @@
|
|||||||
|
# Copyright 2021 Piotr Żelasko
|
||||||
|
# Copyright 2022-2024 Xiaomi Corporation (Authors: Mingshuang Luo,
|
||||||
|
# Zengwei Yao,
|
||||||
|
# Zengrui Jin,)
|
||||||
|
#
|
||||||
|
# See ../../../../LICENSE for clarification regarding multiple authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import logging
|
||||||
|
from functools import lru_cache
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from lhotse import CutSet, Spectrogram, SpectrogramConfig, load_manifest_lazy
|
||||||
|
from lhotse.dataset import ( # noqa F401 for PrecomputedFeatures
|
||||||
|
CutConcatenate,
|
||||||
|
CutMix,
|
||||||
|
DynamicBucketingSampler,
|
||||||
|
PrecomputedFeatures,
|
||||||
|
SimpleCutSampler,
|
||||||
|
SpeechSynthesisDataset,
|
||||||
|
)
|
||||||
|
from lhotse.dataset.input_strategies import ( # noqa F401 For AudioSamples
|
||||||
|
AudioSamples,
|
||||||
|
OnTheFlyFeatures,
|
||||||
|
)
|
||||||
|
from lhotse.utils import fix_random_seed
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
|
||||||
|
from icefall.utils import str2bool
|
||||||
|
|
||||||
|
|
||||||
|
class _SeedWorkers:
|
||||||
|
def __init__(self, seed: int):
|
||||||
|
self.seed = seed
|
||||||
|
|
||||||
|
def __call__(self, worker_id: int):
|
||||||
|
fix_random_seed(self.seed + worker_id)
|
||||||
|
|
||||||
|
LIBRITTS_SAMPLING_RATE = 24000
|
||||||
|
|
||||||
|
class LibrittsTtsDataModule:
|
||||||
|
"""
|
||||||
|
DataModule for tts experiments.
|
||||||
|
It assumes there is always one train and valid dataloader,
|
||||||
|
but there can be multiple test dataloaders (e.g. LibriSpeech test-clean
|
||||||
|
and test-other).
|
||||||
|
|
||||||
|
It contains all the common data pipeline modules used in ASR
|
||||||
|
experiments, e.g.:
|
||||||
|
- dynamic batch size,
|
||||||
|
- bucketing samplers,
|
||||||
|
- cut concatenation,
|
||||||
|
- on-the-fly feature extraction
|
||||||
|
|
||||||
|
This class should be derived for specific corpora used in ASR tasks.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, args: argparse.Namespace):
|
||||||
|
self.args = args
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def add_arguments(cls, parser: argparse.ArgumentParser):
|
||||||
|
group = parser.add_argument_group(
|
||||||
|
title="TTS data related options",
|
||||||
|
description="These options are used for the preparation of "
|
||||||
|
"PyTorch DataLoaders from Lhotse CutSet's -- they control the "
|
||||||
|
"effective batch sizes, sampling strategies, applied data "
|
||||||
|
"augmentations, etc.",
|
||||||
|
)
|
||||||
|
|
||||||
|
group.add_argument(
|
||||||
|
"--manifest-dir",
|
||||||
|
type=Path,
|
||||||
|
default=Path("data/spectrogram"),
|
||||||
|
help="Path to directory with train/valid/test cuts.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--speakers",
|
||||||
|
type=Path,
|
||||||
|
default=Path("data/speakers.txt"),
|
||||||
|
help="Path to speakers.txt file.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--max-duration",
|
||||||
|
type=int,
|
||||||
|
default=200.0,
|
||||||
|
help="Maximum pooled recordings duration (seconds) in a "
|
||||||
|
"single batch. You can reduce it if it causes CUDA OOM.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--bucketing-sampler",
|
||||||
|
type=str2bool,
|
||||||
|
default=True,
|
||||||
|
help="When enabled, the batches will come from buckets of "
|
||||||
|
"similar duration (saves padding frames).",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--num-buckets",
|
||||||
|
type=int,
|
||||||
|
default=30,
|
||||||
|
help="The number of buckets for the DynamicBucketingSampler"
|
||||||
|
"(you might want to increase it for larger datasets).",
|
||||||
|
)
|
||||||
|
|
||||||
|
group.add_argument(
|
||||||
|
"--on-the-fly-feats",
|
||||||
|
type=str2bool,
|
||||||
|
default=False,
|
||||||
|
help="When enabled, use on-the-fly cut mixing and feature "
|
||||||
|
"extraction. Will drop existing precomputed feature manifests "
|
||||||
|
"if available.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--shuffle",
|
||||||
|
type=str2bool,
|
||||||
|
default=True,
|
||||||
|
help="When enabled (=default), the examples will be "
|
||||||
|
"shuffled for each epoch.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--drop-last",
|
||||||
|
type=str2bool,
|
||||||
|
default=True,
|
||||||
|
help="Whether to drop last batch. Used by sampler.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--return-cuts",
|
||||||
|
type=str2bool,
|
||||||
|
default=False,
|
||||||
|
help="When enabled, each batch will have the "
|
||||||
|
"field: batch['cut'] with the cuts that "
|
||||||
|
"were used to construct it.",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--num-workers",
|
||||||
|
type=int,
|
||||||
|
default=8,
|
||||||
|
help="The number of training dataloader workers that "
|
||||||
|
"collect the batches.",
|
||||||
|
)
|
||||||
|
|
||||||
|
group.add_argument(
|
||||||
|
"--input-strategy",
|
||||||
|
type=str,
|
||||||
|
default="PrecomputedFeatures",
|
||||||
|
help="AudioSamples or PrecomputedFeatures",
|
||||||
|
)
|
||||||
|
|
||||||
|
def train_dataloaders(
|
||||||
|
self,
|
||||||
|
cuts_train: CutSet,
|
||||||
|
sampler_state_dict: Optional[Dict[str, Any]] = None,
|
||||||
|
) -> DataLoader:
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
cuts_train:
|
||||||
|
CutSet for training.
|
||||||
|
sampler_state_dict:
|
||||||
|
The state dict for the training sampler.
|
||||||
|
"""
|
||||||
|
logging.info("About to create train dataset")
|
||||||
|
train = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=eval(self.args.input_strategy)(),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.args.on_the_fly_feats:
|
||||||
|
sampling_rate = LIBRITTS_SAMPLING_RATE
|
||||||
|
config = SpectrogramConfig(
|
||||||
|
sampling_rate=sampling_rate,
|
||||||
|
frame_length=1024 / sampling_rate, # (in second),
|
||||||
|
frame_shift=256 / sampling_rate, # (in second)
|
||||||
|
use_fft_mag=True,
|
||||||
|
)
|
||||||
|
train = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=OnTheFlyFeatures(Spectrogram(config)),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.args.bucketing_sampler:
|
||||||
|
logging.info("Using DynamicBucketingSampler.")
|
||||||
|
train_sampler = DynamicBucketingSampler(
|
||||||
|
cuts_train,
|
||||||
|
max_duration=self.args.max_duration,
|
||||||
|
shuffle=self.args.shuffle,
|
||||||
|
num_buckets=self.args.num_buckets,
|
||||||
|
buffer_size=self.args.num_buckets * 2000,
|
||||||
|
shuffle_buffer_size=self.args.num_buckets * 5000,
|
||||||
|
drop_last=self.args.drop_last,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logging.info("Using SimpleCutSampler.")
|
||||||
|
train_sampler = SimpleCutSampler(
|
||||||
|
cuts_train,
|
||||||
|
max_duration=self.args.max_duration,
|
||||||
|
shuffle=self.args.shuffle,
|
||||||
|
)
|
||||||
|
logging.info("About to create train dataloader")
|
||||||
|
|
||||||
|
if sampler_state_dict is not None:
|
||||||
|
logging.info("Loading sampler state dict")
|
||||||
|
train_sampler.load_state_dict(sampler_state_dict)
|
||||||
|
|
||||||
|
# 'seed' is derived from the current random state, which will have
|
||||||
|
# previously been set in the main process.
|
||||||
|
seed = torch.randint(0, 100000, ()).item()
|
||||||
|
worker_init_fn = _SeedWorkers(seed)
|
||||||
|
|
||||||
|
train_dl = DataLoader(
|
||||||
|
train,
|
||||||
|
sampler=train_sampler,
|
||||||
|
batch_size=None,
|
||||||
|
num_workers=self.args.num_workers,
|
||||||
|
persistent_workers=False,
|
||||||
|
worker_init_fn=worker_init_fn,
|
||||||
|
)
|
||||||
|
|
||||||
|
return train_dl
|
||||||
|
|
||||||
|
def valid_dataloaders(self, cuts_valid: CutSet) -> DataLoader:
|
||||||
|
logging.info("About to create dev dataset")
|
||||||
|
if self.args.on_the_fly_feats:
|
||||||
|
sampling_rate = LIBRITTS_SAMPLING_RATE
|
||||||
|
config = SpectrogramConfig(
|
||||||
|
sampling_rate=sampling_rate,
|
||||||
|
frame_length=1024 / sampling_rate, # (in second),
|
||||||
|
frame_shift=256 / sampling_rate, # (in second)
|
||||||
|
use_fft_mag=True,
|
||||||
|
)
|
||||||
|
validate = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=OnTheFlyFeatures(Spectrogram(config)),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
validate = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=eval(self.args.input_strategy)(),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
valid_sampler = DynamicBucketingSampler(
|
||||||
|
cuts_valid,
|
||||||
|
max_duration=self.args.max_duration,
|
||||||
|
shuffle=False,
|
||||||
|
)
|
||||||
|
logging.info("About to create valid dataloader")
|
||||||
|
valid_dl = DataLoader(
|
||||||
|
validate,
|
||||||
|
sampler=valid_sampler,
|
||||||
|
batch_size=None,
|
||||||
|
num_workers=2,
|
||||||
|
persistent_workers=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
return valid_dl
|
||||||
|
|
||||||
|
def test_dataloaders(self, cuts: CutSet) -> DataLoader:
|
||||||
|
logging.info("About to create test dataset")
|
||||||
|
if self.args.on_the_fly_feats:
|
||||||
|
sampling_rate = LIBRITTS_SAMPLING_RATE
|
||||||
|
config = SpectrogramConfig(
|
||||||
|
sampling_rate=sampling_rate,
|
||||||
|
frame_length=1024 / sampling_rate, # (in second),
|
||||||
|
frame_shift=256 / sampling_rate, # (in second)
|
||||||
|
use_fft_mag=True,
|
||||||
|
)
|
||||||
|
test = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=OnTheFlyFeatures(Spectrogram(config)),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
test = SpeechSynthesisDataset(
|
||||||
|
return_text=False,
|
||||||
|
return_tokens=True,
|
||||||
|
return_spk_ids=True,
|
||||||
|
feature_input_strategy=eval(self.args.input_strategy)(),
|
||||||
|
return_cuts=self.args.return_cuts,
|
||||||
|
)
|
||||||
|
test_sampler = DynamicBucketingSampler(
|
||||||
|
cuts,
|
||||||
|
max_duration=self.args.max_duration,
|
||||||
|
shuffle=False,
|
||||||
|
)
|
||||||
|
logging.info("About to create test dataloader")
|
||||||
|
test_dl = DataLoader(
|
||||||
|
test,
|
||||||
|
batch_size=None,
|
||||||
|
sampler=test_sampler,
|
||||||
|
num_workers=self.args.num_workers,
|
||||||
|
)
|
||||||
|
return test_dl
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def train_cuts(self) -> CutSet:
|
||||||
|
logging.info("About to get train cuts")
|
||||||
|
return load_manifest_lazy(self.args.manifest_dir / "vctk_cuts_train.jsonl.gz")
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def valid_cuts(self) -> CutSet:
|
||||||
|
logging.info("About to get validation cuts")
|
||||||
|
return load_manifest_lazy(self.args.manifest_dir / "vctk_cuts_valid.jsonl.gz")
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def test_cuts(self) -> CutSet:
|
||||||
|
logging.info("About to get test cuts")
|
||||||
|
return load_manifest_lazy(self.args.manifest_dir / "vctk_cuts_test.jsonl.gz")
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
|
def speakers(self) -> Dict[str, int]:
|
||||||
|
logging.info("About to get speakers")
|
||||||
|
with open(self.args.speakers) as f:
|
||||||
|
speakers = {line.strip(): i for i, line in enumerate(f)}
|
||||||
|
return speakers
|
1
egs/libritts/TTS/vits/utils.py
Symbolic link
1
egs/libritts/TTS/vits/utils.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/utils.py
|
1
egs/libritts/TTS/vits/vits.py
Symbolic link
1
egs/libritts/TTS/vits/vits.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/vits.py
|
1
egs/libritts/TTS/vits/wavenet.py
Symbolic link
1
egs/libritts/TTS/vits/wavenet.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../ljspeech/TTS/vits/wavenet.py
|
Loading…
x
Reference in New Issue
Block a user