我正在用我自己的数据集处理图像OCR,我有1000个可变长度的图像,我想以46X1补丁的形式提供图像。我已经生成了图像的补丁,我的标签值是以乌尔都语文本表示的,所以我将它们编码为utf-8。我想在输出层实现CTC。我试图按照github的image_ocr示例来实现CTC。但是,在我的CTC实现中,我得到了以下错误。
'numpy.ndarray‘对象没有属性'get_shape’
有人能指点我的错误吗?请提出解决办法。
我的代码是:
X_train, X_test, Y_train, Y_test =train_test_split(imageList, labelList, test_size=0.3)
X_train_patches = np.array([image.extract_patches_2d(X_train[i], (46, 1))for i in range (700)]).reshape(700,1,1) #(Samples, timesteps,dimensions)
X_test_patches = np.array([image.extract_patches_2d(X_test[i], (46, 1))for i in range (300)]).reshape(300,1,1)
Y_train=np.array([i.encode("utf-8") for i in str(Y_train)])
Label_length=1
input_length=1
####################Loss Function########
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
# the 2 is critical here since the first couple outputs of the RNN
# tend to be garbage:
y_pred = y_pred[:, 2:, :]
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
#Building Model
model =Sequential()
model.add(LSTM(20, input_shape=(None, X_train_patches.shape[2]), return_sequences=True))
model.add(Activation('relu'))
model.add(TimeDistributed(Dense(12)))
model.add(Activation('tanh'))
model.add(LSTM(60, return_sequences=True))
model.add(Activation('relu'))
model.add(TimeDistributed(Dense(40)))
model.add(Activation('tanh'))
model.add(LSTM(100, return_sequences=True))
model.add(Activation('relu'))
loss_out = Lambda(ctc_lambda_func, name='ctc')([X_train_patches, Y_train, input_length, Label_length])发布于 2017-05-02 10:16:46
目前在Keras中对CTC建模的方式是,您需要将丢失函数作为一个层来实现,您已经这样做了(loss_out)。您的问题是,您给该层的输入不是来自Theano/TensorFlow的张量,而是numpy数组。
要改变这一点,一个选项是将这些值建模为模型的输入。这正是您复制代码的实现所做的:
labels = Input(name='the_labels', shape=[img_gen.absolute_max_string_len], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
# Keras doesn't currently support loss funcs with extra parameters
# so CTC loss is implemented in a lambda layer
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])要完成这项工作,您需要抛弃Sequential模型并使用functional,就像在上面链接的代码中所做的那样。
https://stackoverflow.com/questions/43731400
复制相似问题