-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking.py
166 lines (146 loc) · 7.79 KB
/
tracking.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
165
166
#!/usr/bin/python
# copyright (C) 2010 Jean-Louis Durrieu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from numpy import arange, zeros, array, argmax, vstack, amax, ones, outer
def viterbiTracking(logDensity, logPriorDensities, logTransitionMatrix,
verbose=False):
"""
Naive implementation of the Viterbi algorithm:
this is a bit slow, consider using viterbiTrackingArray instead.
bestStatePath = viterbiTracking(logDensity, logPriorDensities,
logTransitionMatrix, verbose=False)
viterbiTracking returns the best path through matrix logDensity,
assuming that logDensity contains the likelihood of the observation
sequence, conditionally upon the hidden states. A hidden Markov
model (HMM) is assumed, with prior probabilities for the states
given by logPriorDensities, and transition probabilities given
by the matrix logTransitionMatrix. More precisely:
Inputs:
logDensity is a S x N ndarray, where S is the number of hidden
states and N is the number of frames of the
observed signal. The element at row s and
column n contains the conditional likelihood
of the signal at frame n, conditionally upon
state s.
logPrioroDensities is a ndarray of size S, containing the prior
probabilities of the hidden states of the HMM.
logTransitionMatrix is a S x S ndarray containing the transition
probabilities: at row s and column t, it
contains the probability of having state t
after state s.
verbose defines whether to display evolution information or not.
Default is False.
Outputs:
bestStatePath is the sequence of best states, assuming the HMM
with the given parameters.
"""
numberOfStates, numberOfFrames = logDensity.shape
cumulativeProbability = zeros([numberOfStates, numberOfFrames])
antecedents = zeros([numberOfStates, numberOfFrames])
for state in arange(numberOfStates):
antecedents[state, 0] = -1
cumulativeProbability[state, 0] = logPriorDensities[state] \
+ logDensity[state, 0]
for n in arange(1, numberOfFrames):
if verbose:
print "frame number ", n, "over ", numberOfFrames
for state in arange(numberOfStates):
if verbose:
print " state number ",state, " over ", numberOfStates
cumulativeProbability[state, n] \
= cumulativeProbability[0, n - 1] \
+ logTransitionMatrix[0, state]
antecedents[state, n] = 0
for state_ in arange(1, numberOfStates):
if verbose:
print " state number ",
print state_, " over ", numberOfStates
tempCumProba = cumulativeProbability[state_, n - 1] \
+ logTransitionMatrix[state_, state]
if (tempCumProba > cumulativeProbability[state, n]):
cumulativeProbability[state, n] = tempCumProba
antecedents[state, n] = state_
cumulativeProbability[state, n] \
= cumulativeProbability[state, n] \
+ logDensity[state, n]
# backtracking:
bestStatePath = zeros(numberOfFrames)
bestStatePath[-1] = argmax(cumulativeProbability[:, numberOfFrames - 1])
for n in arange(numberOfFrames - 2, 0, -1):
bestStatePath[n] = antecedents[bestStatePath[n + 1], n + 1]
return bestStatePath
def viterbiTrackingArray(logDensity, logPriorDensities, logTransitionMatrix,
verbose=False):
"""
bestStatePath = viterbiTrackingArray(logDensity, logPriorDensities,
logTransitionMatrix, verbose=False)
viterbiTrackingArray returns the best path through matrix logDensity,
assuming that logDensity contains the likelihood of the observation
sequence, conditionally upon the hidden states. A hidden Markov
model (HMM) is assumed, with prior probabilities for the states
given by logPriorDensities, and transition probabilities given
by the matrix logTransitionMatrix. More precisely:
Inputs:
logDensity is a S x N ndarray, where S is the number of hidden
states and N is the number of frames of the
observed signal. The element at row s and
column n contains the conditional likelihood
of the signal at frame n, conditionally upon
state s. The given values should be given as the
logarithm of the probabilities.
logPrioroDensities is a ndarray of size S, containing the prior
probabilities of the hidden states of the HMM,
logarithm of these values are expected.
logTransitionMatrix is a S x S ndarray containing the transition
probabilities: at row s and column t, it
contains the probability of having state t
after state s, logarithm expected.
verbose defines whether to display evolution information or not.
Default is False.
Outputs:
bestStatePath is the sequence of best states, assuming the HMM
with the given parameters.
"""
numberOfStates, numberOfFrames = logDensity.shape
# logPriorDensities = vstack(logPriorDensities)
onesStates = ones(numberOfStates)
cumulativeProbability = zeros([numberOfStates, numberOfFrames])
antecedents = zeros([numberOfStates, numberOfFrames], dtype=int)
antecedents[:, 0] = -1
cumulativeProbability[:, 0] = logPriorDensities[:] \
+ logDensity[:, 0]
for n in arange(1, numberOfFrames):
if verbose:
print "frame number ", n, "over ", numberOfFrames
# Find the state that minimizes the transition and the cumulative
# probability. This operation can be done for all the target
# states using numpy operations on ndarrays:
# TODO: check that the transition
antecedents[:, n] \
= argmax(outer(onesStates,
cumulativeProbability[:, n - 1]) \
+ logTransitionMatrix.T, axis=1)
cumulativeProbability[:, n] \
= cumulativeProbability[antecedents[:, n], n - 1] \
+ logTransitionMatrix[antecedents[:, n],
arange(numberOfStates)] \
+ logDensity[:, n]
# backtracking:
bestStatePath = zeros(numberOfFrames)
bestStatePath[-1]= argmax(cumulativeProbability[:, numberOfFrames \
- 1])
for n in arange(numberOfFrames - 2, 0, -1):
bestStatePath[n] = antecedents[bestStatePath[n + 1], n + 1]
return bestStatePath