forked from githubharald/CTCDecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestPath.py
More file actions
39 lines (28 loc) · 846 Bytes
/
BestPath.py
File metadata and controls
39 lines (28 loc) · 846 Bytes
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
from __future__ import division
from __future__ import print_function
import numpy as np
def ctcBestPath(mat, classes):
"implements best path decoding as shown by Graves (Dissertation, p63)"
# dim0=t, dim1=c
maxT, maxC = mat.shape
label = ''
blankIdx = len(classes)
lastMaxIdx = maxC # init with invalid label
for t in range(maxT):
maxIdx = np.argmax(mat[t, :])
if maxIdx != lastMaxIdx and maxIdx != blankIdx:
label += classes[maxIdx]
lastMaxIdx = maxIdx
return label
def testBestPath():
"test decoder"
classes = 'ab'
mat = np.array([[0.4, 0, 0.6], [0.4, 0, 0.6]])
print('Test best path decoding')
expected = ''
actual = ctcBestPath(mat, classes)
print('Expected: "' + expected + '"')
print('Actual: "' + actual + '"')
print('OK' if expected == actual else 'ERROR')
if __name__ == '__main__':
testBestPath()