簡體   English   中英

將 CNN 連接到 RNN

[英]Connecting CNN to RNN

我想訓練一個神經網絡來分類簡單的視頻。 我的方法是使用 CNN,其 output 連接到 RNN (LSTM)。 我在嘗試將兩者連接在一起時遇到了一些麻煩。

X_train.shape
(2400, 256, 256, 3)

Y_train.shape
(2400, 6)

這是我定義的網絡

model = Sequential()
model.add(Conv2D(32 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu' , input_shape = (256,256,3)))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(128 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(256 , (3,3) , strides = 1 , padding = 'same' , activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Flatten())

model.add(layers.LSTM(64, return_sequences=True, input_shape=(1,256)))

model.add(layers.LSTM(32, return_sequences=True))

model.add(layers.LSTM(32))

model.add(layers.Dense(6, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

我收到以下錯誤

ValueError: Input 0 of layer lstm_7 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 65536]

我感覺它與 RNN 的輸入形狀有關。 目的是讓 CNN 獲取幀的特征,然后 RNN 獲取幀之間的高級差異。 用兩個完全不同的網絡來做這件事會更好嗎? 如果是這樣,我該如何實現? 還有一種方法可以用批量數據訓練這兩個網絡,因為它非常大。

你說的很對。 在 tensorflow LSTM 中需要一個形狀為(batch_size, time_steps, embedding_size)的輸入,有關更多詳細信息,請參見示例 在您的情況下,請嘗試使用model.add(Reshape((16, 16*256)))而不是model.add(Flatten()) 不是最漂亮的解決方案,但它可以讓您測試事物。

問題是傳遞給 LSTM 的數據,它可以在您的網絡內部解決。 它期望 3D 並且使用 Flatten 您正在摧毀它。 您可以采用兩種可能性:1)進行重塑(batch_size, H, W*channel) 2) (batch_size, W, H*channel) 通過這種方式,您可以在 LSTM 中使用 3D 數據。 下面是一個例子

model = Sequential()
model.add(Conv2D(32 , (3,3) , strides = 1 , padding = 'same' , 
                 activation = 'relu' , input_shape = (256,256,3)))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(64 , (3,3) , strides = 1 , padding = 'same' , 
                 activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(128 , (3,3) , strides = 1 , padding = 'same' , 
                 activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

model.add(Conv2D(256 , (3,3) , strides = 1 , padding = 'same' , 
                 activation = 'relu'))
model.add(MaxPool2D((2,2) , strides = 2 , padding = 'same'))

def ReshapeLayer(x):
    
    shape = x.shape
    
    # 1 possibility: H,W*channel
    reshape = Reshape((shape[1],shape[2]*shape[3]))(x)
    
    # 2 possibility: W,H*channel
    # transpose = Permute((2,1,3))(x)
    # reshape = Reshape((shape[1],shape[2]*shape[3]))(transpose)
    
    return reshape

model.add(Lambda(ReshapeLayer))
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))

model.add(Dense(6, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', 
              metrics=['accuracy'])
model.summary()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM