微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 decode_batch_predictions() 方法添加到 Keras Captcha OCR 模型中?

如何解决如何将 decode_batch_predictions() 方法添加到 Keras Captcha OCR 模型中?

当前的Keras Captcha OCR model返回一个CTC编码的输出,需要推理后解码。

要对此进行解码,需要在推理后作为单独的步骤运行解码实用程序。

preds = prediction_model.predict(batch_images)
pred_texts = decode_batch_predictions(preds)

解码的效用函数使用 keras.backend.ctc_decode,后者又使用贪婪或波束搜索解码器。

# A utility function to decode the output of the network
def decode_batch_predictions(pred):
    input_len = np.ones(pred.shape[0]) * pred.shape[1]
    # Use greedy search. For complex tasks,you can use beam search
    results = keras.backend.ctc_decode(pred,input_length=input_len,greedy=True)[0][0][
        :,:max_length
    ]
    # Iterate over the results and get back the text
    output_text = []
    for res in results:
        res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
        output_text.append(res)
    return output_text

我想使用 Keras 训练 Captcha OCR 模型,该模型将解码后的 CTC 返回为输出,而无需在推理后进行额外的解码步骤。

我将如何实现这一目标?

解决方法

您的问题可以有两种解释。一个是:我想要一个神经网络来解决一个问题,其中 CTC 解码步骤已经在网络所学的内部。另一个是您希望有一个 Model 类在其中执行此 CTC 解码,而不使用外部函数。

我不知道第一个问题的答案。我什至无法判断它是否可行。在任何情况下,这听起来都是一个困难的理论问题,如果您在这里不走运,您可能想尝试将其发布到 datascience.stackexchange.com,这是一个更注重理论的社区。​​p>

现在,如果您要解决的是问题的第二个工程版本,那么我可以为您提供帮助。该问题的解决方案如下:

您需要将 keras.models.Model 子类化为具有您想要的方法的类。我浏览了您发布的链接中的教程,并提供了以下课程:

class ModifiedModel(keras.models.Model):
    
    # A utility function to decode the output of the network
    def decode_batch_predictions(self,pred):
        input_len = np.ones(pred.shape[0]) * pred.shape[1]
        # Use greedy search. For complex tasks,you can use beam search
        results = keras.backend.ctc_decode(pred,input_length=input_len,greedy=True)[0][0][
            :,:max_length
        ]
        # Iterate over the results and get back the text
        output_text = []
        for res in results:
            res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
            output_text.append(res)
        return output_text

    
    def predict_texts(self,batch_images):
        preds = self.predict(batch_images)
        return self.decode_batch_predictions(preds)

你可以给它取你想要的名字,这只是为了说明目的。 定义此类后,您将替换该行

# Get the prediction model by extracting layers till the output layer
prediction_model = keras.models.Model(
    model.get_layer(name="image").input,model.get_layer(name="dense2").output
)

prediction_model = ModifiedModel(
    model.get_layer(name="image").input,model.get_layer(name="dense2").output
)

然后你可以替换线条

preds = prediction_model.predict(batch_images)
pred_texts = decode_batch_predictions(preds)

pred_texts = prediction_model.predict_texts(batch_images)
,

最可靠的方法是添加一个作为模型定义一部分调用的方法:

def CTCDecoder():
  def decoder(y_pred):
    input_shape = tf.keras.backend.shape(y_pred)
    input_length = tf.ones(shape=input_shape[0]) * tf.keras.backend.cast(
        input_shape[1],'float32')
    unpadded = tf.keras.backend.ctc_decode(y_pred,input_length)[0][0]
    unpadded_shape = tf.keras.backend.shape(unpadded)
    padded = tf.pad(unpadded,paddings=[[0,0],[0,input_shape[1] - unpadded_shape[1]]],constant_values=-1)
    return padded

return tf.keras.layers.Lambda(decoder,name='decode')

然后定义模型如下:

prediction_model = keras.models.Model(inputs=inputs,outputs=CTCDecoder()(model.output))

信用转到tulasiram58827

此实现支持导出到 TFLite,但仅支持 float32。 Quantized (int8) TFLite 导出仍然抛出错误,并且是 TF 团队的公开票。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。