结合CNN和双向LSTM

时间:2020-10-01 07:08:23

标签: python tensorflow keras lstm cnn

我正在尝试将CNN和LSTM结合起来进行图像分类。

我尝试了以下代码,但出现错误。我有4个课程要训练和测试。

以下是代码:

from keras.models import Sequential
from keras.layers import LSTM,Conv2D,MaxPooling2D,Dense,Dropout,Input,Bidirectional,Softmax,TimeDistributed


input_shape = (200,300,3)
Model = Sequential()
Model.add(TimeDistributed(Conv2D(
            filters=16, kernel_size=(12, 16), activation='relu', input_shape=input_shape)))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(TimeDistributed(Conv2D(
            filters=24, kernel_size=(8, 12), activation='relu')))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(TimeDistributed(Conv2D(
            filters=32, kernel_size=(5, 7), activation='relu')))
Model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),strides=2)))
Model.add(Bidirectional(LSTM((10),return_sequences=True)))
Model.add(Dense(64,activation='relu'))
Model.add(Dropout(0.5))
Model.add(Softmax(4))
Model.compile(loss='sparse_categorical_crossentropy',optimizer='adam')
Model.build(input_shape)

我遇到以下错误:

“输入张量必须为3、4或5,但为{}。”。format(n + 2)) ValueError:输入张量必须为3、4或5,但必须为2。

1 个答案:

答案 0 :(得分:2)

我在代码中发现了很多问题:

  1. 您的数据采用4D格式,因此简单<script> var content = `<p>bla bla</p><script src="https://example"></script>`; //Uncaught SyntaxError: Unexpected end of input -> for: </script> <script> 就可以了,不需要Conv2D
  2. 您的输出是2D,因此在最后一个LSTM单元格中设置TimeDistributed
  3. 您的最后一层非常混乱:无需在层输出和激活之间放置辍学
  4. 您需要return_sequences=False而不是categorical_crossentropy,因为您的目标是单次编码的
  5. LSTM需要3D数据。因此,您需要从4D(卷积的输出)传递到3D。您可以采用两种可能性:1)进行重塑(batch_size,H,W *通道); 2)(batch_size,W,H * channel)。这样,您就可以在LSTM内部使用3D数据

此处是完整的示例示例:

sparse_categorical_crossentropy

here the running notebook