2017-05-02 10 views
0

自分のデータセットで画像OCRを作成していますが、可変長の画像が1000個あり、46X1のパッチ形式で画像を送りたいと思います。私は自分の画像のパッチを生成し、ラベルの値はウルドゥー語のテキストになっているので、私はutf-8としてそれらをエンコードしています。出力レイヤにCTCを実装したい。私はgithubでimage_ocrの例に続いてCTCを実装しようとしました。しかし、CTCの実装では次のエラーが発生します。KerasのエラーでのCTCの実装

'numpy.ndarray' object has no attribute '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]) 

答えて

1

CTCがKerasに現在モデル化された方法は、あなたがすでに(loss_out)ということでした、層として損失関数を実装する必要があるということです。あなたの問題は、あなたがその層に与える入力が、Theano/TensorFlowのテンソルではなくnumpyの配列であるということです。

これを変更するには、これらの値をモデルの入力としてモデル化します。あなたがSequentialモデルを捨て、上にリンクされたコードで行わまったく同じように、機能モデルのAPIを使用する必要があり、この作業を行うには

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]) 

:これはimplementationはあなたからのコードをコピーしたことをまったく同じものです。

+0

これに応じてモデルを変更しましたが、今度はこのエラーが発生します TypeError:7次元の値を持つ1次元のサブテンソルを増分しようとしています。 提案がありますか? –

関連する問題