-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_phoneme_to_principal_components.py
164 lines (145 loc) · 4.86 KB
/
test_phoneme_to_principal_components.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
####################################################################################################
#
# Test the autoencoder-based phoneme-to-articulation
#
####################################################################################################
import argparse
import os
import torch
import ujson
import yaml
from functools import reduce
from torch.utils.data import DataLoader
from helpers import sequences_from_dict, set_seeds, make_indices_dict
from phoneme_recognition.deepspeech2 import DeepSpeech2
from phoneme_to_articulation import RNNType
from phoneme_to_articulation.principal_components.dataset import (
PrincipalComponentsPhonemeToArticulationDataset2,
pad_sequence_collate_fn
)
from phoneme_to_articulation.principal_components.evaluation import run_phoneme_to_principal_components_test
from phoneme_to_articulation.principal_components.losses import AutoencoderLoss2
from phoneme_to_articulation.principal_components.models import PrincipalComponentsArtSpeech
from settings import BLANK, UNKNOWN
def main(
database_name,
datadir,
batch_size,
seq_dict,
indices_dict,
vocab_filepath,
state_dict_filepath,
modelkwargs,
autoencoder_kwargs,
save_to,
encoder_state_dict_filepath,
decoder_state_dict_filepath,
rnn_type="GRU",
beta1=1.0,
beta2=1.0,
beta3=1.0,
beta4=0.0,
recognizer_filepath=None,
recognizer_params=None,
voicing_filepath=None,
num_workers=0,
TV_to_phoneme_map=None,
clip_tails=True
):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
default_tokens = [BLANK, UNKNOWN]
vocabulary = {token: i for i, token in enumerate(default_tokens)}
with open(vocab_filepath) as f:
tokens = ujson.load(f)
for i, token in enumerate(tokens, start=len(vocabulary)):
vocabulary[token] = i
if voicing_filepath is not None:
with open(voicing_filepath) as f:
voiced_tokens = ujson.load(f)
else:
voiced_tokens = None
if isinstance(list(indices_dict.values())[0], int):
indices_dict = make_indices_dict(indices_dict)
articulators = sorted(indices_dict.keys())
sequences = sequences_from_dict(datadir, seq_dict)
test_dataset = PrincipalComponentsPhonemeToArticulationDataset2(
database_name,
datadir,
sequences,
vocabulary,
articulators,
TV_to_phoneme_map,
clip_tails=clip_tails,
voiced_tokens=voiced_tokens,
)
test_dataloader = DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
worker_init_fn=set_seeds,
collate_fn=pad_sequence_collate_fn,
)
if TV_to_phoneme_map is None:
TV_to_phoneme_map = {}
TVs = sorted(TV_to_phoneme_map.keys())
if recognizer_filepath:
recognizer = DeepSpeech2(num_classes=len(vocabulary), **recognizer_params)
recog_state_dict = torch.load(recognizer_filepath, map_location=device)
recognizer.load_state_dict(recog_state_dict)
recognizer.to(device)
for p in recognizer.parameters():
p.requires_grad = False
else:
recognizer = None
denorm_fn = {
articulator: normalize.inverse
for articulator, normalize in test_dataset.normalize.items()
}
loss_fn = AutoencoderLoss2(
indices_dict=indices_dict,
TVs=TVs,
device=device,
encoder_state_dict_filepath=encoder_state_dict_filepath,
decoder_state_dict_filepath=decoder_state_dict_filepath,
denormalize_fn=denorm_fn,
beta1=beta1,
beta2=beta2,
beta3=beta3,
beta4=beta4,
recognizer=recognizer,
**autoencoder_kwargs,
)
model = PrincipalComponentsArtSpeech(
vocab_size=len(vocabulary),
indices_dict=indices_dict,
rnn=RNNType[rnn_type.upper()],
**modelkwargs,
)
model_state_dict = torch.load(state_dict_filepath, map_location=device)
model.load_state_dict(model_state_dict)
model.to(device)
print(f"""
PrincipalComponentsArtSpeech -- {model.total_parameters} parameters
""")
test_outputs_dir = os.path.join(save_to, "test_outputs")
if not os.path.exists(test_outputs_dir):
os.makedirs(test_outputs_dir)
info_test = run_phoneme_to_principal_components_test(
epoch=0,
model=model,
dataloader=test_dataloader,
criterion=loss_fn,
outputs_dir=test_outputs_dir,
decode_transform=loss_fn.decode,
device=device
)
with open(os.path.join(save_to, "test_results.json"), "w") as f:
ujson.dump(info_test, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", dest="cfg_filepath")
args = parser.parse_args()
with open(args.cfg_filepath) as f:
cfg = yaml.safe_load(f.read())
main(**cfg)