-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgenerate.py
51 lines (42 loc) · 1.48 KB
/
generate.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
import argparse
from pathlib import Path
import numpy as np
from tqdm import tqdm
import torch
def generate(args):
print("Loading acoustic model checkpoint")
acoustic = torch.hub.load("bshall/acoustic-model:main", f"hubert_{args.model}").cuda()
print(f"Generating from {args.in_dir} -> {args.out_dir}")
for path in tqdm(list(args.in_dir.rglob("*.npy"))):
units = np.load(path)
units_dtype = torch.long if args.model == "discrete" else torch.float
units = torch.tensor(units, dtype=units_dtype).cuda()
with torch.inference_mode():
mel_ = acoustic.generate(units)
mel_ = mel_.transpose(1, 2)
out_path = args.out_dir / path.relative_to(args.in_dir)
out_path.parent.mkdir(exist_ok=True, parents=True)
np.save(out_path.with_suffix(".npy"), mel_.squeeze().cpu().numpy())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate spectrograms from input speech units (discrete or soft)."
)
parser.add_argument(
"model",
help="available models (HuBERT-Soft or HuBERT-Discrete)",
choices=["soft", "discrete"],
)
parser.add_argument(
"in_dir",
metavar="in-dir",
help="path to the dataset directory.",
type=Path,
)
parser.add_argument(
"out_dir",
metavar="out-dir",
help="path to the output directory.",
type=Path,
)
args = parser.parse_args()
generate(args)