Skip to content

Commit d6a88ea

Browse files
committed
best path dec. performance improvement: using groupby to replace for loop
1 parent 4af0222 commit d6a88ea

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/BestPath.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
from __future__ import division
22
from __future__ import print_function
3+
from itertools import groupby
34
import numpy as np
45

56

67
def ctcBestPath(mat, classes):
78
"implements best path decoding as shown by Graves (Dissertation, p63)"
89

9-
# get list of char indices along best path
10+
# get char indices along best path
1011
best_path = np.argmax(mat, axis=1)
1112

12-
# collapse best path and map char indices to string
13+
# collapse best path (using itertools.groupby), map to chars, join char list to string
1314
blank_idx = len(classes)
14-
last_char_idx = blank_idx
15-
res = ''
16-
for char_idx in best_path:
17-
if char_idx != last_char_idx and char_idx != blank_idx:
18-
res += classes[char_idx]
19-
last_char_idx = char_idx
20-
15+
best_chars_collapsed = [classes[k] for k, _ in groupby(best_path) if k != blank_idx]
16+
res = ''.join(best_chars_collapsed)
2117
return res
2218

2319

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy