-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprepare_data.py
84 lines (68 loc) · 2.12 KB
/
prepare_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Prepare audio data for compressing speech SSL."""
from argparse import ArgumentParser
from pathlib import Path
from typing import Union
from tqdm import tqdm
import torchaudio
def create_tsv(
root_dir: Union[str, Path],
out_dir: Union[str, Path],
extension: str = "flac",
) -> None:
"""Create file lists for training and validation.
Args:
root_dir (str or Path): The directory of the dataset.
out_dir (str or Path): The directory to store the file lists.
extension (str, optional): The extension of audio files. (Default: ``flac``)
Returns:
None
"""
root_dir = Path(root_dir)
out_dir = Path(out_dir)
if not out_dir.exists():
out_dir.mkdir()
with open(
out_dir / "train100.tsv", "w"
) as train100_f, open(
out_dir / "train960.tsv", "w"
) as train960_f, open(
out_dir / "valid.tsv", "w"
) as valid_f:
print(root_dir, file=train100_f)
print(root_dir, file=train960_f)
print(root_dir, file=valid_f)
for fname in tqdm(root_dir.glob(f"**/*.{extension}")):
line = f"{fname.relative_to(root_dir)}\t{torchaudio.info(fname).num_frames}"
if "train-clean-100" in str(fname):
print(line, file=train100_f)
if "train" in str(fname):
print(line, file=train960_f)
if "dev" in str(fname):
print(line, file=valid_f)
print("Finished creating the file lists successfully")
def parse_args():
parser = ArgumentParser(
description="Prepare audio data."
)
parser.add_argument(
"--data",
type=Path,
required=True,
help="Path to the original dataset."
)
parser.add_argument(
"--out",
type=Path,
default=Path("data/librispeech"),
help="Path to save the output."
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
assert args.data.is_dir(), args.data
args.out.mkdir(parents=True, exist_ok=True)
create_tsv(
root_dir=args.data,
out_dir=args.out,
)