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
|
#!/usr/bin/env python
from cmusphinx import hmm, s3model, s2mfc
import unittest
from cmusphinx.feat import _1s_c_d_dd
import os
from math import log
class TestHMM(unittest.TestCase):
def setUp(self):
thisdir = os.path.dirname(__file__)
self.testdir = os.path.join(thisdir, 'data', 'tidigits')
self.acmod = s3model.S3Model(self.testdir)
def test_create(self):
h1 = hmm.HMM(self.acmod.mdef.pid2sseq(352),
self.acmod.tmat[self.acmod.mdef.pid2tmat(352)])
def test_forward_backward(self):
pid = self.acmod.mdef.phone_id('SIL')
h1 = hmm.HMM(self.acmod.mdef.pid2sseq(pid),
self.acmod.tmat[self.acmod.mdef.pid2tmat(pid)])
mfcc = s2mfc.open(os.path.join(self.testdir, 'man.ah.111a.mfc')).getall()
mfcc -= mfcc.mean(0)
feat = _1s_c_d_dd.compute(mfcc)
alpha = None
self.alpha = []
for f in feat[0:50]:
senscr = self.acmod.senone_compute(h1.iter_senones(), f)
alpha = hmm.forward_evaluate(h1, senscr, alpha)
self.alpha.append(alpha)
beta = None
self.beta = []
for f in feat[50:0:-1]: # Note that this is time-shifted by
# one from the forward pass above
senscr = self.acmod.senone_compute(h1.iter_senones(), f)
beta = hmm.backward_evaluate(h1, senscr, beta)
self.beta.append(beta)
self.beta.reverse()
ll = 0
for a, b in zip(self.alpha, self.beta):
newll = sum(a*b)
if ll != 0:
self.assertTrue(abs(log(ll) - log(newll)) < 0.1)
ll = newll
class TestPhoneHMM(unittest.TestCase):
def setUp(self):
thisdir = os.path.dirname(__file__)
self.testdir = os.path.join(thisdir, 'data', 'tidigits')
self.acmod = s3model.S3Model(self.testdir)
self.factory = hmm.PhoneHMMFactory(self.acmod)
def test_create(self):
pid = self.acmod.mdef.phone_id('OW_four', 'F_four', 'R_four')
h1 = hmm.HMM(self.acmod.mdef.pid2sseq(pid),
self.acmod.tmat[self.acmod.mdef.pid2tmat(pid)])
h2 = self.factory.create('OW_four', 'F_four', 'R_four')
self.assertEqual(h1[0], h2[0])
class TestHMMGraph(unittest.TestCase):
def setUp(self):
thisdir = os.path.dirname(__file__)
self.testdir = os.path.join(thisdir, 'data', 'tidigits')
self.acmod = s3model.S3Model(self.testdir)
self.factory = hmm.PhoneHMMFactory(self.acmod)
def test_create(self):
h1 = self.factory.create('F_four', 'SIL', 'OW_four')
h2 = self.factory.create('OW_four', 'F_four', 'R_four')
h3 = self.factory.create('R_four', 'OW_four', 'SIL')
hg = hmm.HMMGraph(h1, h2, h3)
self.assertEqual(hg[0], h1[0])
self.assertEqual(hg[4], h2[0])
self.assertEqual(hg[8], h3[0])
def test_forward_backward(self):
mfcc = s2mfc.open(os.path.join(self.testdir, 'man.ah.111a.mfc')).getall()
mfcc -= mfcc.mean(0)
feat = _1s_c_d_dd.compute(mfcc)
hg = hmm.HMMGraph(self.factory.create('SIL'),
self.factory.create('SIL'))
alpha = None
self.alpha = []
for f in feat[0:15]:
senscr = self.acmod.senone_compute(hg.iter_senones(), f)
alpha = hmm.forward_evaluate(hg, senscr, alpha)
self.alpha.append(alpha)
beta = None
self.beta = []
for f in feat[15:0:-1]: # Note that this is time-shifted by
# one from the forward pass above
senscr = self.acmod.senone_compute(hg.iter_senones(), f)
beta = hmm.backward_evaluate(hg, senscr, beta)
self.beta.append(beta)
self.beta.reverse()
ll = 0
for a,b in zip(self.alpha, self.beta):
newll = sum(a*b)
if ll != 0:
self.assertTrue(abs(log(ll) - log(newll)) < 0.1)
ll = newll
def read_dict(dictfile):
fh = open(dictfile)
out = {}
for spam in fh:
eggs = spam.rstrip().split()
word = eggs[0]
phones = eggs[1:]
out[word] = phones
return out
class TestSentenceHMM(unittest.TestCase):
def setUp(self):
thisdir = os.path.dirname(__file__)
self.testdir = os.path.join(thisdir, 'data', 'tidigits')
acmod = s3model.new(self.testdir)
dictionary = read_dict(os.path.join(self.testdir, 'dictionary'))
self.factory = hmm.SentenceHMMFactory(acmod, dictionary)
if __name__ == '__main__':
unittest.main()
|