【问题标题】:How do i add ctc beam search decoder in crnn model (pytorch)如何在 crnn 模型(pytorch)中添加 ctc 波束搜索解码器
【发布时间】:2018-07-19 12:31:09
【问题描述】:

我正在关注https://github.com/meijieru/crnn.pytorch 的 CRNN 实现,但似乎它没有使用波束搜索来解码单词。有人可以告诉我如何在同一模型中添加波束搜索解码吗?同时在Tensorflow中,有一个内置的tf.nn.ctc_beam_search_decoder

【问题讨论】:

标签: tensorflow deep-learning ocr speech-recognition pytorch


【解决方案1】:

为什么不简单地将您自己的波束搜索解码器添加到模型中? 应该不会太难。

通过 CRNN 代码搜索找到line where decoding happens at the moment

sim_preds = converter.decode(preds.data, preds_size.data, raw=False)

好的,看起来 preds.data 保存了神经网络的输出张量。 不要调用converter.decode(...),而是将此张量传递给波束搜索解码器。 你可以拿我的CTC beam search implementation

调用 BeamSearch.ctcBeamSearch(...),传递一个已经应用了 softmax 的批处理元素(mat),传递一个包含所有字符的字符串(按照神经网络输出它们的顺序),并传递 None 用于语言模型(如果您愿意,您可以稍后添加它)。 矩阵 mat 的形状必须为 Tx(C+1),其中 T 是时间步数,C+1 是字符数加上空白。 空白被假定为最后一个条目,因此请注意它。

这是一个简约的例子:

mat = np.array([[0.4, 0, 0.6], [0.4, 0, 0.6]]) # TxC with T=2, C=3
classes = 'ab' # all chars in the order they appear in mat (without blank)
res = BeamSearch.ctcBeamSearch(mat, classes, None) # decode it

Here is another example 获取更真实的用例来解码真实文本识别系统的输出。

【讨论】:

    【解决方案2】:

    我知道这不是一个好主意,但我在 pytorch 中使用了 tensorflow。

    if(beam):
            decodes, _ = tf.nn.ctc_beam_search_decoder(inputs=preds_.cpu().detach().numpy(), 
                         sequence_length=25*np.ones(1), merge_repeated=False)
            with tf.Session(config = tf.ConfigProto(device_count = {'GPU': 0})) as sess:
                t_ = sess.run(decodes)[0].values
                char_list = []
                for i in range(len(sess.run(decodes)[0].values)):
                        if t_[i] != 0 and (not (i > 0 and t_[i - 1] == t_[i])):
                            char_list.append(alphabet[t_[i] - 1])
                sim_pred = ''.join(char_list)
    else:        
            raw_pred = converter.decode(preds.data, preds_size.data, raw=True)
            sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多