输入矩阵需已做 softmax,形状为 TxC 且 CTC-blank 必须是最后一维,PyTorch 默认 blank 在首位需手动调整;不提供 TensorFlow/PyTorch 适配器,需自行转 numpy 并逐 batch 元素解码;lexicon search 需自行提供词典构建 BK-tree,语言模型需自行提供文本
CTCDecoder
githubharaldMIT更新放缓用 Python 实现的 CTC 解码算法集合,包含最优路径、束搜索、词典搜索、前缀搜索和 token passing,用于语音识别和手写识别等序列模型的推理解码。
Connectionist Temporal Classification (CTC) decoding algorithms: best path, beam search, lexicon search, prefix search, and token passing. Implemented in Python.
pip install .README 未说明,仅要求输入为 numpy 数组,未提及 GPU 或显存要求
纯 Python 实现的 CTC 解码算法库,提供 best path、beam search、词典搜索等常用解码器
用 Python 实现的 CTC 解码算法集合,包含最优路径、束搜索、词典搜索、前缀搜索和 token passing,用于语音识别和手写识别等序列模型的推理解码。
核心能力
- 实现 best path、beam search、lexicon search 等解码器
- beam search 可选集成字符级语言模型提升准确率
- lexicon search 用 BK-tree 检索相近词典词并打分
- 提供可安装的 Python 包并附带 pytest 测试
- 额外提供 OpenCL 版 best path 实现
容易踩的坑
暂无 release 数据(可能没有发版,或尚未同步)。
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
CTC Decoding Algorithms
Update 2021: installable Python package
Python implementation of some common Connectionist Temporal Classification (CTC) decoding algorithms. A minimalistic language model is provided.
Installation
- Go to the root level of the repository
- Execute
pip install . - Go to
tests/and executepytestto check if installation worked
Usage
Basic usage
Here is a minimalistic executable example:
import numpy as np
from ctc_decoder import best_path, beam_search
mat = np.array([[0.4, 0, 0.6], [0.4, 0, 0.6]])
chars = 'ab'
print(f'Best path: "{best_path(mat, chars)}"')
print(f'Beam search: "{beam_search(mat, chars)}"')
The output mat (numpy array, softmax already applied) of the CTC-trained neural network is expected to have shape TxC
and is passed as the first argument to the decoders.
T is the number of time-steps, and C the number of characters (the CTC-blank is the last element).
The characters that can be predicted by the neural network are passed as the chars string to the decoder.
Decoders return the decoded string.
Running the code outputs:
Best path: ""
Beam search: "a"
To see more examples on how to use the decoders,
please have a look at the scripts in the tests/ folder.
Language model and BK-tree
Beam search can optionally integrate a character-level language model. Text statistics (bigrams) are used by beam search to improve reading accuracy.
from ctc_decoder import beam_search, LanguageModel
# create language model instance from a (large) text
lm = LanguageModel('this is some text', chars)
# and use it in the beam search decoder
res = beam_search(mat, chars, lm=lm)
The lexicon search decoder computes a first approximation with best path decoding. Then, it uses a BK-tree to retrieve similar words, scores them and finally returns the best scoring word. The BK-tree is created by providing a list of dictionary words. A tolerance parameter defines the maximum edit distance from the query word to the returned dictionary words.
from ctc_decoder import lexicon_search, BKTree
# create BK-tree from a list of words
bk_tree = BKTree(['words', 'from', 'a', 'dictionary'])
# and use the tree in the lexicon search
res = lexicon_search(mat, chars, bk_tree, tolerance=2)
Usage with deep learning frameworks
Some notes:
- No adapter for TensorFlow or PyTorch is provided
- Apply softmax already in the model
- Convert to numpy array
- Usually, the output of an RNN layer
rnn_outputhas shape TxBxC, with B the batch dimension- Decoders work on single batch elements of shape TxC
- Therefore, iterate over all batch elements and apply the decoder to each of them separately
- Example: extract matrix of batch element 0
mat = rnn_output[:, 0, :]
- The CTC-blank is expected to be the last element along the character dimension
- TensorFlow has the CTC-blank as last element, so nothing to do here
- PyTorch, however, has the CTC-blank as first element by default, so you have to move it to the end, or change the default setting
List of provided decoders
Recommended decoders:
best_path: best path (or greedy) decoder, the fastest of all algorithms, however, other decoders often perform betterbeam_search: beam search decoder, optionally integrates a character-level language model, can be tuned via the beam width parameterlexicon_search: lexicon search decoder, returns the best scoring word from a dictionary
Other decoders, from my experience not really suited for practical purposes, but might be used for experiments or research:
prefix_search: prefix search decodertoken_passing: token passing algorithm- Best path decoder implementation in OpenCL (see
extras/folder)
This paper gives suggestions when to use best path decoding, beam search decoding and token passing.
Documentation of test cases and data
- Documentation of test cases
- Documentation of the data
References
- Graves - Supervised sequence labelling with recurrent neural networks
- Hwang - Character-level incremental speech recognition with recurrent neural networks
- Shi - An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition
- Marti - The IAM-database: an English sentence database for offline handwriting recognition
- Beam Search Decoding in CTC-trained Neural Networks
- An Intuitive Explanation of Connectionist Temporal Classification
- Scheidl - Comparison of Connectionist Temporal Classification Decoding Algorithms
- Scheidl - Word Beam Search: A Connectionist Temporal Classification Decoding Algorithm