-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
60 lines (43 loc) · 1.73 KB
/
main.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
import multiprocessing as mp
from functools import partial
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
from preprocess import *
from inference import viterbi
from hmm import HMM, StateGraph
def label_to_word(label):
word_dict = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven',
'8': 'eight', '9': 'nine', 'o': 'oh', 'z': 'zero'}
return [word_dict[label] for label in label]
def main():
test_data = get_test_data("./data/tst")
unigram_dict = get_unigram_dict("./data/unigram.txt")
bigram_dict = get_bigram_dict("./data/bigram.txt")
phoneme_dict = get_phoneme_dict("./data/dictionary.txt")
hmm_dict = get_hmm_dict("./data/hmm.txt")
hmm = HMM(hmm_dict)
state_graph = StateGraph(hmm, phoneme_dict, bigram_dict)
y_preds = []
y_trues = list(test_data.keys())
#Loop
#for label in y_trues:
# words_pred = continuous_recognition(unigram_dict, bigram_dict, phoneme_dict, hmm, test_data[label]['mfcc'])
# y_preds.append([word for word in words_pred if word != "<s>"])
mfccs = []
for label in y_trues:
mfccs.append(test_data[label]['mfcc'])
y_preds = y_preds[:10]
y_trues = y_trues[:10]
mfccs = mfccs[:10]
# Multiprocess
agents = mp.cpu_count() - 1
with mp.Pool(processes=agents) as pool:
recognition = partial(viterbi, unigram_dict, hmm, state_graph)
y_preds = pool.map(recognition, mfccs)
result_dict = {"y_trues": y_trues, "y_preds": y_preds}
np.save("results.npy", result_dict)
result_dict = np.load("results.npy").item()
print(len(result_dict["y_trues"]), len(result_dict["y_preds"]))
if __name__ == "__main__":
main()