Create examples/confusion_plot.py
Browse files- examples/confusion_plot.py +84 -0
examples/confusion_plot.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Plot the confusion matrix for whispers, voiced, and reverberant
|
| 4 |
+
whispers side by side. Writes to whisper_figures/confusion_3way.png.
|
| 5 |
+
|
| 6 |
+
Run: python examples/confusion_plot.py
|
| 7 |
+
"""
|
| 8 |
+
import os
|
| 9 |
+
import numpy as np
|
| 10 |
+
import matplotlib
|
| 11 |
+
matplotlib.use("Agg")
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
from whisper_decoder import (
|
| 14 |
+
MLP, train_mlp, generate_corpus, FEATURE_DIM, WORD_ORDER,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
OUT = "whisper_figures"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def confusion(model, corpus, mu, sigma):
|
| 21 |
+
X = (corpus.X_test - mu) / sigma
|
| 22 |
+
pred = model.predict(X)
|
| 23 |
+
cm = np.zeros((10, 10), dtype=int)
|
| 24 |
+
for t, p in zip(corpus.y_test, pred):
|
| 25 |
+
cm[t, p] += 1
|
| 26 |
+
return cm, float((pred == corpus.y_test).mean())
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
os.makedirs(OUT, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
print("training...")
|
| 33 |
+
train = generate_corpus('whisper', 150, 30, seed=0,
|
| 34 |
+
reverb_train_frac=0.25)
|
| 35 |
+
mu = train.X_train.mean(axis=0)
|
| 36 |
+
sigma = train.X_train.std(axis=0) + 1e-9
|
| 37 |
+
X_tr = (train.X_train - mu) / sigma
|
| 38 |
+
|
| 39 |
+
model = MLP(FEATURE_DIM, 64, 32, 10, seed=0)
|
| 40 |
+
train_mlp(model, X_tr, train.y_train, epochs=200,
|
| 41 |
+
batch=64, lr=3e-3, seed=0)
|
| 42 |
+
|
| 43 |
+
print("evaluating on three conditions...")
|
| 44 |
+
whisper = generate_corpus('whisper', 10, 30, seed=10)
|
| 45 |
+
voiced = generate_corpus('voiced', 10, 30, seed=11)
|
| 46 |
+
reverb = generate_corpus('whisper', 10, 30, seed=12, reverb=True)
|
| 47 |
+
|
| 48 |
+
fig, axes = plt.subplots(1, 3, figsize=(18, 5.5))
|
| 49 |
+
conditions = [
|
| 50 |
+
("whisper (in-distribution)", whisper),
|
| 51 |
+
("voiced (domain shift)", voiced),
|
| 52 |
+
("reverberant whisper", reverb),
|
| 53 |
+
]
|
| 54 |
+
for ax, (title, corpus) in zip(axes, conditions):
|
| 55 |
+
cm, acc = confusion(model, corpus, mu, sigma)
|
| 56 |
+
im = ax.imshow(cm, cmap="Blues", aspect="auto",
|
| 57 |
+
vmin=0, vmax=cm.max())
|
| 58 |
+
ax.set_xticks(np.arange(10))
|
| 59 |
+
ax.set_yticks(np.arange(10))
|
| 60 |
+
ax.set_xticklabels(WORD_ORDER, rotation=45, fontsize=8)
|
| 61 |
+
ax.set_yticklabels(WORD_ORDER, fontsize=8)
|
| 62 |
+
for i in range(10):
|
| 63 |
+
for j in range(10):
|
| 64 |
+
if cm[i, j] > 0:
|
| 65 |
+
ax.text(j, i, str(cm[i, j]),
|
| 66 |
+
ha="center", va="center",
|
| 67 |
+
fontsize=7,
|
| 68 |
+
color="white" if cm[i, j] > cm.max() / 2
|
| 69 |
+
else "black")
|
| 70 |
+
ax.set_xlabel("predicted")
|
| 71 |
+
ax.set_ylabel("true")
|
| 72 |
+
ax.set_title(f"{title}\naccuracy {acc*100:.1f}%",
|
| 73 |
+
fontsize=11)
|
| 74 |
+
plt.colorbar(im, ax=ax, shrink=0.7)
|
| 75 |
+
|
| 76 |
+
plt.tight_layout()
|
| 77 |
+
path = os.path.join(OUT, "confusion_3way.png")
|
| 78 |
+
plt.savefig(path, dpi=130, bbox_inches="tight")
|
| 79 |
+
plt.close()
|
| 80 |
+
print(f"saved: {path}")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
main()
|