forked from parlance/ctcdecode
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add seq length and arbitrary blank index support
- Loading branch information
Showing
6 changed files
with
38 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import torch | ||
import pytorch_ctc as ctc | ||
from torch.utils.ffi import _wrap_function | ||
from ._ctc_decode import lib as _lib, ffi as _ffi | ||
|
||
from cffi import FFI | ||
ffi = FFI() | ||
from ._ext.ctc_decode import ctc_beam_decode | ||
__all__ = [] | ||
|
||
|
||
def beam_decode(probs, seq_len=None, top_paths=1, beam_width=10, merge_repeated=True): | ||
def _import_symbols(locals): | ||
for symbol in dir(_lib): | ||
fn = getattr(_lib, symbol) | ||
locals[symbol] = _wrap_function(fn, _ffi) | ||
__all__.append(symbol) | ||
|
||
|
||
_import_symbols(locals()) | ||
|
||
|
||
def beam_decode(probs, seq_len=None, top_paths=1, beam_width=10, blank_index=0, merge_repeated=True): | ||
prob_size = probs.size() | ||
max_seq_len = prob_size[0] | ||
batch_size = prob_size[1] | ||
num_classes = prob_size[2] | ||
|
||
if seq_len is not None and batch_size != seq_len.size(): | ||
raise ValueError("seq_len shape must be a 1xbatch_size tensor or None") | ||
if blank_index < 0 or blank_index >= num_classes: | ||
raise ValueError("blank_index must be within num_classes") | ||
if seq_len is not None and batch_size != seq_len.size(0): | ||
raise ValueError("seq_len shape must be a (batch_size) tensor or None") | ||
if top_paths < 1 or top_paths > beam_width: | ||
raise ValueError("top_paths must be greater than 1 and less than or equal to the beam_width") | ||
|
||
seq_len = torch.IntTensor(batch_size).zero_().add_(max_seq_len) if seq_len is None else seq_len | ||
output = torch.IntTensor(top_paths, batch_size, max_seq_len) | ||
scores = torch.FloatTensor(top_paths, batch_size) | ||
out_seq_len = torch.IntTensor(top_paths, batch_size) | ||
|
||
merge_int = 1 if merge_repeated else 0 | ||
result = ctc_beam_decode(probs, seq_len, output, scores, top_paths, beam_width, merge_int) | ||
result = ctc.ctc_beam_decode(probs, seq_len, output, scores, out_seq_len, top_paths, beam_width, blank_index, merge_int) | ||
|
||
return output, scores | ||
return output, scores, out_seq_len |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
int ctc_beam_decode(THFloatTensor *probs, THIntTensor *seq_len, THIntTensor *output, | ||
THFloatTensor *scores, int top_paths, int beam_width, int merge_repeated); | ||
THFloatTensor *scores, THIntTensor *th_out_len, int top_paths, | ||
int beam_width, int blank_index, int merge_repeated); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters