【发布时间】:2018-09-02 07:28:56
【问题描述】:
我目前正在开发一个用于免分割手写文本识别的应用程序。 因此,文本行是从输入文档中提取的,然后应该被识别。
出于开发目的,我使用IAM Handwriting Database。它提供文本行图像以及相应的 ASCII 文本。
为了获得认可,我采用了论文“An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition”和“Can We Build Language-independent OCR Using LSTM Networks?”中的方法。
基本上,我使用双向 GRU 架构和前向后向算法将转录本与神经网络的输出对齐。
图像显示为一维像素值序列,更准确地说,图像首先缩放到 32 像素的高度。
上图尺寸为 597 x 32 的 numpy 数组,其形状为:(597, 32)。
代表大小为 n 的整体训练图像的 numpy 数组具有以下形状: (n, w, 32) 其中 w 表示线条图像的可变宽度(例如 597)。
以下代码显示了如何表示训练图像和转录:
x_train = []
y_train = []
line_height_normalized = 32
for i in range(sample_size):
transcription_train, image_train = self._get_next_sample()
image_train = convert_to_grayscale(image_train)
image_train = scale_y(image_train, line_height_normalized)
image_train_patches = sklearn_image.extract_patches_2d(image_train, (line_height_normalized, 1))
image_train_patches = numpy.reshape(image_train_patches, (image_train_patches.shape[0], -1))
x_train.append(image_train_patches)
y_train.append(transcription_train)
我使用Keras,循环神经网络和CTC函数的创建都是基于this example。
charset = 68
number_of_memory_units = 512
time_steps = None
input_dimension = 32 # the height of a text line in pixel
# input shape see https://github.com/keras-team/keras/issues/3683
network_input = Input(name="input", shape=(time_steps, input_dimension))
gru_layer_1 = GRU(number_of_memory_units, return_sequences=True, kernel_initializer='he_normal',
name='gru_layer_1')(network_input)
gru_layer_1_backwards = GRU(number_of_memory_units, return_sequences=True, go_backwards=True,
kernel_initializer='he_normal',name='gru_layer_1_backwards')(network_input)
gru_layer_1_merged = add([gru_layer_1, gru_layer_1_backwards])
gru_layer_2 = GRU(number_of_memory_units, return_sequences=True, kernel_initializer='he_normal',
name='gru_layer_2')(gru_layer_1_merged)
gru_layer_2_backwards = GRU(number_of_memory_units, return_sequences=True, go_backwards=True, kernel_initializer='he_normal',
name='gru_layer_2_backwards')(gru_layer_1_merged)
output_layer = Dense(charset, kernel_initializer='he_normal',
name='dense_layer')(concatenate([gru_layer_2, gru_layer_2_backwards]))
prediction = Activation('softmax', name='output_to_ctc')(output_layer)
# create the ctc layer
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
max_line_length = 200 # see QUESTION 1
labels = Input(name='labels', shape=[max_line_length], dtype='float32')
loss_out = Lambda(RecurrentNeuralNetwork._ctc_function, name='ctc')(
[prediction, labels, input_length, label_length])
model = Model(inputs=[network_input, labels, input_length, label_length], outputs=loss_out)
sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)
model.compile(loss={'ctc': lambda l_truth, l_prediction: prediction}, optimizer=sgd)
问题 1
在示例中,使用了 max_line_length;正如我在互联网上阅读的那样(但我认为我不太理解它退出了),因为底层 CTC 函数需要知道应该创建多少张量,所以需要最大行长度。
什么长度适合可变行长?这对看不见的文本行的识别有何影响?
此外,input_length 变量和 label_length 变量究竟代表什么?
下一步训练网络:
batch_size = 1
number_of_epochs = 4
size = 32 # line height? see QUESTION 2
input_length = numpy.zeros([size, 1])
label_length = numpy.zeros([size, 1])
for epoch in range(number_of_epochs):
for x_train_batch, y_train_batch in zip(x_train, y_train_labels):
x_train_batch = numpy.reshape(x_train_batch, (1, len(x_train_batch), 32))
inputs = {'input': x_train_batch, 'labels': numpy.array(y_train_batch),
'input_length': input_length, 'label_length': label_length}
outputs = {'ctc': numpy.zeros([size])} # dummy data for dummy loss function
self.model.fit(x=inputs, y=outputs, batch_size=batch_size, epochs=1, shuffle=False)
self.model.reset_states()
由于时间步长(文本行的宽度)可变,因此以 1 大小的批次进行训练。
文本行的转录由一个numpy数组y_train_batch表示;每个字符都是数字编码的。
上面图像示例的转录如下所示:
[26 62 38 40 47 30 62 19 14 62 18 19 14 15 62 38 17 64 62 32 0 8 19 18 10 4 11 11 62 5 17 14 12]
问题 2
size 变量代表什么?它是单个图像块的尺寸,因此是每个时间步的特征吗?
错误
发生的错误如下:
-
预期标签的形状为 (200,),但数组的形状为 (1,)
是否有必要填充标签数组以包含 200 个元素?
当我将 max_line_length 的值替换为 1 时,会发生下一个错误:
-
所有输入数组 (x) 应具有相同数量的样本。得到数组形状:[(1, 597, 32), (33, 1), (32, 1), (32, 1)]
其他三个数组是否需要reshape?
我不确定解决此问题的“正确”方法是什么以及接下来可能发生的错误?
也许有人可以为我指明正确的方向。
非常感谢!
【问题讨论】:
-
最大行长度指定可以(1)在解码时识别或(2)用作每行损失计算的基本事实的最长文本。对于您正在使用的 IAM 数据集,200 的长度就足够了。通常 CNN 需要固定大小的输入,因此将图像缩小到固定高度但任意宽度可能会导致问题。尝试将图像拉伸到所需的大小(我认为在原始 CRNN 实现中它是 100x32)。高级概述 CTC:stats.stackexchange.com/questions/320868/…
-
@Harry 非常感谢您的评论!只是为了澄清:最大行长度(在官方 Keras 示例中'absolute_max_string_length',参见github.com/keras-team/keras/blob/master/examples/…)是否表示标签的最大字符数或行输入图像的最大宽度(以像素为单位)?尽管如此,您将图像拉伸到特定大小是正确的,也许填充它们会更好,这样图像就不会失真。