vall-e/scripts/process_dataset.py

226 lines
6.6 KiB
Python
Raw Normal View History

import os
import json
import torch
import torchaudio
2024-05-18 15:13:58 +00:00
import numpy as np
from tqdm.auto import tqdm
from pathlib import Path
from vall_e.config import cfg
# things that could be args
cfg.sample_rate = 24_000
cfg.audio_backend = "encodec"
2024-05-16 04:04:19 +00:00
"""
cfg.inference.weight_dtype = "bfloat16"
cfg.inference.dtype = torch.bfloat16
cfg.inference.amp = True
"""
from vall_e.emb.g2p import encode as valle_phonemize
from vall_e.emb.qnt import encode as valle_quantize, _replace_file_extension
2024-04-29 03:28:29 +00:00
input_audio = "voices"
2024-05-16 04:04:19 +00:00
input_metadata = "metadata"
2024-07-04 20:58:08 +00:00
output_dataset = f"training-{'2' if cfg.sample_rate == 24_000 else '4'}{'8' if cfg.sample_rate == 48_000 else '4'}KHz-{cfg.audio_backend}"
2024-04-29 03:28:29 +00:00
device = "cuda"
2024-07-04 20:58:08 +00:00
audio_extension = ".enc"
if cfg.audio_backend == "dac":
audio_extension = ".dac"
elif cfg.audio_backend == "audiodec":
audio_extension = ".dec"
2024-04-29 03:28:29 +00:00
slice = "auto"
missing = {
"transcription": [],
"audio": []
}
2024-05-05 02:03:46 +00:00
dataset = []
def pad(num, zeroes):
return str(num).zfill(zeroes+1)
2024-04-29 03:28:29 +00:00
for dataset_name in sorted(os.listdir(f'./{input_audio}/')):
if not os.path.isdir(f'./{input_audio}/{dataset_name}/'):
print("Is not dir:", f'./{input_audio}/{dataset_name}/')
continue
2024-04-29 03:28:29 +00:00
for speaker_id in tqdm(sorted(os.listdir(f'./{input_audio}/{dataset_name}/')), desc=f"Processing speaker in {dataset_name}"):
if not os.path.isdir(f'./{input_audio}/{dataset_name}/{speaker_id}'):
print("Is not dir:", f'./{input_audio}/{dataset_name}/{speaker_id}')
continue
os.makedirs(f'./{output_dataset}/{dataset_name}/{speaker_id}/', exist_ok=True)
if speaker_id == "Noise":
for filename in sorted(os.listdir(f'./{input_audio}/{dataset_name}/{speaker_id}/')):
inpath = Path(f'./{input_audio}/{dataset_name}/{speaker_id}/{filename}')
outpath = Path(f'./{output_dataset}/{dataset_name}/{speaker_id}/{filename}')
if _replace_file_extension(outpath, audio_extension).exists():
continue
waveform, sample_rate = torchaudio.load(inpath)
qnt = valle_quantize(waveform, sr=sample_rate, device=device)
2024-05-16 04:04:19 +00:00
if cfg.audio_backend == "dac":
2024-05-16 04:04:19 +00:00
np.save(open(_replace_file_extension(outpath, audio_extension), "wb"), {
2024-05-18 15:13:58 +00:00
"codes": qnt.codes.cpu().numpy().astype(np.uint16),
2024-05-16 04:04:19 +00:00
"metadata": {
"original_length": qnt.original_length,
"sample_rate": qnt.sample_rate,
2024-05-18 15:13:58 +00:00
"input_db": qnt.input_db.cpu().numpy().astype(np.float32),
2024-05-16 04:04:19 +00:00
"chunk_length": qnt.chunk_length,
"channels": qnt.channels,
"padding": qnt.padding,
"dac_version": "1.0.0",
},
})
else:
2024-05-16 04:04:19 +00:00
np.save(open(_replace_file_extension(outpath, audio_extension), "wb"), {
2024-05-18 15:13:58 +00:00
"codes": qnt.cpu().numpy().astype(np.uint16),
2024-05-16 04:04:19 +00:00
"metadata": {
"original_length": waveform.shape[-1],
"sample_rate": sample_rate,
},
})
continue
metadata_path = Path(f'./{input_metadata}/{dataset_name}/{speaker_id}/whisper.json')
if not metadata_path.exists():
missing["transcription"].append(str(metadata_path))
continue
try:
metadata = json.loads(open(metadata_path, "r", encoding="utf-8").read())
except Exception as e:
missing["transcription"].append(str(metadata_path))
continue
if f'{dataset_name}/{speaker_id}' not in dataset:
dataset.append(f'{dataset_name}/{speaker_id}')
txts = []
wavs = []
2024-04-29 03:28:29 +00:00
use_slices = slice == True or (slice == "auto" and len(metadata.keys()) == 1) or dataset_name in ["LibriVox", "Audiobooks"]
for filename in sorted(metadata.keys()):
inpath = Path(f'./{input_audio}/{dataset_name}/{speaker_id}/{filename}')
if not inpath.exists():
missing["audio"].append(str(inpath))
continue
extension = os.path.splitext(filename)[-1][1:]
fname = filename.replace(f'.{extension}', "")
waveform, sample_rate = None, None
2024-05-16 04:04:19 +00:00
language = metadata[filename]["language"] if "language" in metadata[filename] else "en"
2024-04-29 03:28:29 +00:00
if len(metadata[filename]["segments"]) == 0 or not use_slices:
outpath = Path(f'./{output_dataset}/{dataset_name}/{speaker_id}/{fname}.{extension}')
text = metadata[filename]["text"]
if len(text) == 0:
continue
2024-05-16 04:04:19 +00:00
if _replace_file_extension(outpath, audio_extension).exists():
continue
2024-05-16 04:04:19 +00:00
if waveform is None:
waveform, sample_rate = torchaudio.load(inpath)
if waveform.shape[0] > 1:
waveform = torch.mean(waveform, dim=0, keepdim=True)
wavs.append((
outpath,
text,
language,
waveform,
sample_rate
))
else:
2024-04-29 03:28:29 +00:00
i = 0
for segment in metadata[filename]["segments"]:
2024-04-29 03:28:29 +00:00
id = pad(i, 4)
i = i + 1
2024-05-16 04:04:19 +00:00
outpath = Path(f'./{output_dataset}/{dataset_name}/{speaker_id}/{fname}_{id}.{extension}')
2024-05-18 17:02:56 +00:00
text = segment["text"]
2024-05-16 04:04:19 +00:00
if len(text) == 0:
continue
if _replace_file_extension(outpath, audio_extension).exists():
continue
if waveform is None:
waveform, sample_rate = torchaudio.load(inpath)
if waveform.shape[0] > 1:
waveform = torch.mean(waveform, dim=0, keepdim=True)
start = int(segment['start'] * sample_rate)
end = int(segment['end'] * sample_rate)
2024-05-16 04:04:19 +00:00
if start < 0:
start = 0
if end >= waveform.shape[-1]:
end = waveform.shape[-1] - 1
if end - start < 0:
continue
2024-05-16 04:04:19 +00:00
wavs.append((
outpath,
text,
language,
waveform[:, start:end],
sample_rate
))
if len(wavs) > 0:
for job in tqdm(wavs, desc=f"Quantizing: {speaker_id}"):
try:
2024-05-16 04:04:19 +00:00
outpath, text, language, waveform, sample_rate = job
phones = valle_phonemize( text, language=language )
qnt = valle_quantize(waveform, sr=sample_rate, device=device)
2024-05-16 04:04:19 +00:00
if cfg.audio_backend == "dac":
2024-05-16 04:04:19 +00:00
np.save(open(_replace_file_extension(outpath, audio_extension), "wb"), {
2024-05-18 15:13:58 +00:00
"codes": qnt.codes.cpu().numpy().astype(np.uint16),
2024-05-16 04:04:19 +00:00
"metadata": {
"original_length": qnt.original_length,
"sample_rate": qnt.sample_rate,
2024-05-18 15:13:58 +00:00
"input_db": qnt.input_db.cpu().numpy().astype(np.float32),
2024-05-16 04:04:19 +00:00
"chunk_length": qnt.chunk_length,
"channels": qnt.channels,
"padding": qnt.padding,
"dac_version": "1.0.0",
"text": text.strip(),
"phonemes": "".join(phones),
"language": language,
},
})
else:
2024-05-16 04:04:19 +00:00
np.save(open(_replace_file_extension(outpath, audio_extension), "wb"), {
2024-05-18 15:13:58 +00:00
"codes": qnt.cpu().numpy().astype(np.uint16),
2024-05-16 04:04:19 +00:00
"metadata": {
"original_length": waveform.shape[-1],
"sample_rate": sample_rate,
"text": text.strip(),
"phonemes": "".join(phones),
"language": language,
},
})
except Exception as e:
print(f"Failed to quantize: {outpath}:", e)
continue
open("./missing.json", 'w', encoding='utf-8').write(json.dumps(missing))
2024-05-16 04:04:19 +00:00
open("./dataset_list.json", 'w', encoding='utf-8').write(json.dumps(dataset))