-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_scripted_model.py
More file actions
98 lines (77 loc) · 2.86 KB
/
Copy pathcreate_scripted_model.py
File metadata and controls
98 lines (77 loc) · 2.86 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import argparse
import numpy as np
import torch
from huggingface_hub import HfApi, create_repo
from data import LibrispeechDataset
from src.scoreq_original import SCOREQScore
from src.scoreq_pytorch import SCOREQScoreTorch
from utils import SCRIPT_DATA_DIR, TARGET_SR
def create_scripted_model(args, data_domain, mode, dataset):
data = dataset[0]
if data_domain == "natural":
test_wav = data["codec_audio"]
else:
test_wav = data["tts_audio"]
ref_wav = data["audio"]
device = "cpu"
orig_scoreq = SCOREQScore(
data_domain=data_domain, mode=mode, device=device, use_onnx=False
)
torch_scoreq = SCOREQScoreTorch(
data_domain=data_domain, mode=mode, device=device, verbose=True
)
# # create TorchScript version
script_scoreq = torch.jit.script(torch_scoreq)
orig_score = np.array([orig_scoreq.predict(test_wav, ref_wav)])
print(f"Orig. SCOREQ {data_domain} {mode}: {orig_score}")
torch_score = torch_scoreq.score(test_wav, ref_wav)
torch_score = torch_score.detach().cpu().numpy()
print(f"Torch SCOREQ {data_domain} {mode}: {torch_score}")
# there is no .score for a scripted module
# use plain forward
script_score = script_scoreq(test_wav, ref_wav)
script_score = script_score.detach().cpu().numpy()
print(f"Script SCOREQ {data_domain} {mode}: {script_score}")
np.testing.assert_allclose(orig_score, torch_score, rtol=1e-6, atol=1e-6)
np.testing.assert_allclose(orig_score, script_score, rtol=1e-6, atol=1e-6)
scripted_checkpoint = f"scoreq_{data_domain}_{mode}_scripted.pt"
save_path = SCRIPT_DATA_DIR / scripted_checkpoint
script_scoreq.save(save_path)
print(f"Saved scripted SCOREQ {data_domain} {mode} to: {save_path}")
create_repo(
repo_id=args.repo_id,
repo_type="model",
private=args.private,
exist_ok=True,
)
api = HfApi()
api.upload_file(
path_or_fileobj=str(save_path),
path_in_repo=scripted_checkpoint,
repo_id=args.repo_id,
repo_type="model",
commit_message=f"Upload scripted SCOREQ {data_domain} {mode} checkpoint",
)
return save_path
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Create a scripted version of PyTorch SCOREQ"
)
parser.add_argument(
"--repo-id",
type=str,
default="Blinorot/SCOREQ-PyTorch",
help="Target Hugging Face Hub repo ID.",
)
parser.add_argument(
"--private",
action="store_true",
help="Create/push to a private Hub repository.",
)
args = parser.parse_args()
dataset = LibrispeechDataset(part="test-clean")
for data_domain in ["natural", "synthetic"]:
for mode in ["nr", "ref"]:
create_scripted_model(
args, data_domain=data_domain, mode=mode, dataset=dataset
)