forked from ariesiitr/Lip-Reading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
45 lines (36 loc) · 1.78 KB
/
Copy pathdecode.py
File metadata and controls
45 lines (36 loc) · 1.78 KB
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
import tensorflow.keras.backend as K
def _decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1):
decoded = K.ctc_decode(y_pred=y_pred, input_length=input_length,
greedy=greedy, beam_width=beam_width, top_paths=top_paths)
paths = [path.numpy() for path in decoded[0]]
logprobs = decoded[1].numpy()
return (paths, logprobs)
def decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1, **kwargs):
language_model = kwargs.get('language_model', None)
paths, logprobs = _decode(y_pred=y_pred, input_length=input_length,
greedy=greedy, beam_width=beam_width, top_paths=top_paths)
if language_model is not None:
# TODO: compute using language model
raise NotImplementedError("Language model search is not implemented yet")
else:
# simply output highest probability sequence
# paths has been sorted from the start
result = paths[0]
return result
class Decoder(object):
def __init__(self, greedy=True, beam_width=100, top_paths=1, **kwargs):
self.greedy = greedy
self.beam_width = beam_width
self.top_paths = top_paths
self.language_model = kwargs.get('language_model', None)
self.postprocessors = kwargs.get('postprocessors', [])
def decode(self, y_pred, input_length):
decoded = decode(y_pred, input_length, greedy=self.greedy, beam_width=self.beam_width,
top_paths=self.top_paths, language_model=self.language_model)
preprocessed = []
for output in decoded:
out = output
for postprocessor in self.postprocessors:
out = postprocessor(out)
preprocessed.append(out)
return preprocessed