forked from rakeshvar/rnn_ctc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_utils.py
46 lines (39 loc) · 1.41 KB
/
print_utils.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
# -*- coding: utf-8 -*-
def slab_print(slab):
"""
Prints a 'slab' of printed 'text' using ascii.
:param slab: A matrix of floats from [0, 1]
"""
for ir, r in enumerate(slab):
print('{:2d}¦'.format(ir), end='')
for val in r:
if val < 0.0: print('-', end='')
elif val < .15: print(' ', end=''),
elif val < .35: print('░', end=''),
elif val < .65: print('▒', end=''),
elif val < .85: print('▓', end=''),
elif val <= 1.: print('█', end=''),
else: print('+', end='')
print('¦')
def prediction_printer(chars):
"""
Returns a function that can print a predicted output of the CTC RNN
It removes the blank characters (need to be set to n_classes),
It also removes duplicates
:param list chars: list of characters
:return: the printing functions
"""
n_classes = len(chars)
def yprint(labels):
labels_out = []
for il, l in enumerate(labels):
if (l != n_classes) and (il == 0 or l != labels[il-1]):
labels_out.append(l)
print(labels_out, " ".join(chars[l] for l in labels_out))
def ylen(labels):
length = 0
for il, l in enumerate(labels):
if (l != n_classes) and (il == 0 or l != labels[il-1]):
length += 1
return length
return yprint, ylen