zeechimp commited on
Commit
fba07ba
·
verified ·
1 Parent(s): 664e67e

Create examples/single_word.py

Browse files
Files changed (1) hide show
  1. examples/single_word.py +47 -0
examples/single_word.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Train on whispers, classify a single voiced word.
4
+
5
+ Run: python examples/single_word.py
6
+ """
7
+ import numpy as np
8
+ from whisper_decoder import (
9
+ MLP, train_mlp, extract_features, synth_word,
10
+ generate_corpus, FEATURE_DIM, WORD_ORDER,
11
+ )
12
+
13
+
14
+ def main():
15
+ print("generating whisper training set...")
16
+ train = generate_corpus('whisper', 150, 20, seed=0,
17
+ reverb_train_frac=0.25)
18
+
19
+ mu = train.X_train.mean(axis=0)
20
+ sigma = train.X_train.std(axis=0) + 1e-9
21
+ X_tr = (train.X_train - mu) / sigma
22
+
23
+ model = MLP(FEATURE_DIM, 64, 32, 10, seed=0)
24
+ print("training...")
25
+ train_mlp(model, X_tr, train.y_train, epochs=200,
26
+ batch=64, lr=3e-3, seed=0)
27
+
28
+ word_index_inv = {i: w for w, i in train.word_index.items()}
29
+
30
+ print()
31
+ print("classify three voiced words with the whisper-trained model:")
32
+ for word in ['zero', 'three', 'seven']:
33
+ sig = synth_word(word, 'voiced', seed=hash(word) & 0xFFFF)
34
+ feats = extract_features(sig)
35
+ probs = model.predict_proba(((feats - mu) / sigma)[None, :])[0]
36
+ top3 = np.argsort(-probs)[:3]
37
+ pred = word_index_inv[int(top3[0])]
38
+ print(f" true: {word:<6s} "
39
+ f"predicted: {pred:<6s} "
40
+ f"p = {probs[top3[0]]:.3f}")
41
+ print(f" top-3: "
42
+ + ", ".join(f"{word_index_inv[int(i)]}({probs[i]:.2f})"
43
+ for i in top3))
44
+
45
+
46
+ if __name__ == "__main__":
47
+ main()