2017-06-22 48 views
1

我試圖做類似的架構的一個在這個例子:
https://github.com/fchollet/keras/blob/master/examples/image_ocr.py#L480
但是我的數據我一直有尺寸的問題,我還沒有找到一個很好的網站這解釋了用您自己的數據而不是MNIST或默認數據來控制尺寸。Keras非順序的,尺寸麻煩,重塑

上下文:即時嘗試前文提到的圖像與文字讓我說我第一次嘗試與2000.對於標籤,我決定做one_hot編碼,這是數據特徵:
圖像固定形狀: (2000,208,352,1)#B & W
one_hot標籤大小:(2000,346,1)#2000個樣本和346個類,最後一個值是具有三維數組,因爲它對於softmax顯然是需要的

現在的代碼:

nb_classes = 346 
max_lin, max_col = (208, 352) 
input_shape = (max_lin, max_col, 1) 
conv_filters = 16 
kernel_size = (3, 3) 
pool_size = 2 
time_dense_size = 32 
rnn_size = 512 
act = 'relu' 

input_data = Input(name='the_input', shape=input_shape) 
inner = Conv2D(conv_filters, kernel_size, padding='same', 
      activation=act, name='CONV2D_1')(input_data) 
inner = MaxPooling2D(pool_size=(pool_size, pool_size), 
      name='MXPOOL2D_1')(inner) 
inner = Conv2D(conv_filters, kernel_size, padding='same', 
      activation=act, name='CONV2D_1')(input_data) 
inner = MaxPooling2D(pool_size=(pool_size, pool_size), 
      name='MXPOOL2D_1')(inner) 

#This is my problem, I dont really know how to reshape it with my data, 
#I chose (104,2816) because other stuff didnt worked and I found it was 
#the Layer Before (104,176,16) = (104, 176*16) = (104,2816); others values 
#gives me ValueError: total size of new array must be unchanged 

conv_to_rnn_dims = (104,2816) 
inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner) 

inner = Dense(time_dense_size, activation=act, name='dense1')(inner) 
gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner) 
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner) 
gru1_merged = add([gru_1, gru_1b]) 
gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged) 
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged) 
gru_conc = concatenate([gru_2, gru_2b]) 
print("GruCOnc: ",gru_conc.shape) 
inner = Dense(nb_classes, kernel_initializer='he_normal', 
      name='DENSE_2')(gru_conc) 
print("2ndDense: ",inner.shape) 
y_pred = Activation('softmax',name='softmax')(inner) 
print(y_pred.shape) 
model = Model(inputs=input_data, outputs=y_pred) 
print(model.summary()) 

sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5) 
model.compile(loss='categorical_crossentropy',optimizer=sgd) 
model.fit(train_data, train_label, batch_size=10, epochs=2, verbose=1) 
score = model.evaluate(x_test, y_test, verbose=1) 

print(score) 

和運行代碼後,我得到:

ValueError: Error when checking target: expected softmax to have shape (None, 104, 346) but got array with shape (2000, 346, 1) 

所以這裏的大問題是,什麼是104?因爲346顯然是班級的數量,但另一個值讓我完全失去了。

感謝大家閱讀我的問題。

回答

0
  1. conv_to_rnn_dims = (104,2816)這是虛構的。據我所知,你正嘗試將你的CNN輸出提供給Dense圖層。但CNN的最後一層是MaxPooling,它產生2D輸出。您應該使用Flatten來執行此連接。讓我們檢查一下這個例子。

    model=Sequential()    
    model.add(Conv2D(16,3,3,border_mode="same",input_shape=(208,352,1)) 
    #Produces 2000 x 208 x 352 x 16 
    model.add(Conv2D(32,3,3,activation="tanh",border_mode="valid")) 
    #Produces 2000 x 208 x 352 x 32 
    model.add(Flatten()) 
    #Produces 2000 x 2342912 
    model.add(Dense(100,activation="sigmoid")) 
    #Produces 2000 x 100 
    

這意味着你不需要重塑層這裏。

  • Dense後,你應該使用Reshape,使輸出準備GRU。現在你有100 時間步數閱讀。所以你應該重塑爲model.add(Reshape((100,1))所以網絡的結果現在是2000 x 100 x 1。你可以用一個熱載體安全適合給你的GRU
  • 最後,對於分類問題和Dense層在輸出你的目標形狀應該是2000×346因此,最終Dense層應該有346個節點。
  • +0

    好的我明白了,我需要在我的Dense之前展開展平,然後我可以在GRU圖層中進行重塑,感謝您的幫助! – alohapinilla