我正在使用我自己的数据集进行图像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])
答案 0 :(得分:2)
目前在Keras中建模CTC的方式是你需要将损失函数作为一个层来实现,你已经这样做了(loss_out
)。您的问题是,您为该图层提供的输入不是Theano / TensorFlow的张量,而是numpy数组。
更改一个选项是将这些值建模为模型的输入。这正是您复制代码的implementation所做的:
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
模型并使用功能模型API,与上面链接的代码完全相同。