2017-02-10 78 views
3

我在Keras是新,並試圖執行此網絡 enter image description here在Keras中使用有狀態的LSTM與微型配料和可變時間步長的輸入?

該網絡需要一個視頻幀爲x = {X1,........,XT}其中T是數量在視頻和x中的幀是幀的視覺特徵尺寸2048

我試圖使用有狀態LSTM作爲每個樣品具有如審閱here

幀數的 ,這是我的模型

x = Input(batch_shape=(1, None, 2048), name='x') 
lstmR = LSTM(256, return_sequences=True, name='lstmR', stateful=True)(x) 
lstmL = LSTM(256, return_sequences=True, go_backwards=True,name='lstmL', stateful=True)(x) 
merge = merge([x, lstmR, lstmL], mode='concat', name='merge') 
dense = Dense(256, activation='sigmoid', name='dense')(merge) 
y = Dense(1, activation='sigmoid', name='y')(dense) 
model = Model(input=x, output=y) 
model.compile(loss='mean_squared_error', 
      optimizer=SGD(lr=0.01), 
      metrics=['accuracy']) 

,並試圖使用mini-配料

for epoch in range(15): 
    mean_tr_acc = [] 
    mean_tr_loss = [] 
    for i in range(nb_samples): 
     x, y = get_train_sample(i) 
     for j in range(len(x)): 
      sample_x = x[j] 
      tr_loss, tr_acc = model.train_on_batch(np.expand_dims(np.expand_dims(sample_x, axis=0), axis=0),np.expand_dims(y, axis=0)) 
      mean_tr_acc.append(tr_acc) 
      mean_tr_loss.append(tr_loss) 
     model.reset_states() 

訓練模型,但它似乎是一個模型不能收斂,因爲它給了0.3精度

我也試圖與輸入形狀無國籍LSTM做到這一點(沒有,1024),但它並沒有收斂太

回答

0

我認爲你的LSTM不能從視頻幀中提取相關的功能,以實現一個很好的準確性。

處理圖像(或視頻幀)時通常會給出最佳結果的方法是使用一疊卷積+卷積+最大池化層來提取特徵(請參閱https://arxiv.org/abs/1612.02903這是對面部表情識別的調查,它們都使用卷積從圖像中提取有用的特徵)。

這些工作與二維輸入最好,但我看到你代表一個視頻幀的大小爲2048而不是矩陣的數組。通常圖像用類似於(rows, cols, color_channels)的形狀表示。

在你的情況下,輸入將有形狀(1, None, rows, cols, color_channels),然後盤旋會是這個樣子:

from keras.layers import Input, LSTM, Conv2D, MaxPool2D, TimeDistributed, Flatten 

x = Input(batch_shape=(1, None, rows, cols, color_channels), name='x') 
convs = TimeDistributed(Conv2D(16, kernel_size=(3,3), activation='relu', padding='same'))(x) 
convs = TimeDistributed(MaxPool2D(pool_size=(2,2)))(convs) 
convs = TimeDistributed(Conv2D(32, kernel_size=(3,3), activation='relu', padding='same'))(convs) 
convs = TimeDistributed(MaxPool2D(pool_size=(2,2)))(convs) 
lstm_input = TimeDistributed(Flatten())(convs) 
lstmR = LSTM(256, return_sequences=True, name='lstmR', stateful=True)(lstm_input) 
lstmL = LSTM(256, return_sequences=True, go_backwards=True, name='lstmL', stateful=True)(lstm_input) 
... 

TimeDistrubuted應用層給出每次一步。