我正在开发一个输入形状为(128,128,3). 我想在 CNN 层之后添加 LSTM 层Conv2D。但是,既然 LSTM 接受维度为 3 的输入,我如何重塑来自 Conv2D 的 4-dim 输出以馈入 LSTM?
我尝试以这种方式建模,但它给出了尺寸误差。
model = Sequential()
model.add(Conv2D(16, kernel_size=(3,3), activation='relu',input_shape=(128,128,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(16, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(LSTM(16, return_sequences=False,input_shape=(30,30,16)))
model.add(Flatten())
model.add(LSTM(16, return_sequences=True))
model.add(Dense(units=2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), metrics=['accuracy'])
#model.build(input_shape=(128,128,3))
model.summary()
这是错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-190-aea1537745bd> in <module>()
23 model.add(MaxPooling2D(pool_size=(2,2)))
24 #model.add(GlobalAveragePooling2D())
---> 25 model.add(LSTM(16, return_sequences=False,input_shape=(30,30,16)))
26 model.add(Flatten())
27 #model.add(LSTM(16, return_sequences=True))
8 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
221 'expected ndim=' + str(spec.ndim) + ', found ndim=' +
222 str(ndim) + '. Full shape received: ' +
--> 223 str(tuple(shape)))
224 if spec.max_ndim is not None:
225 ndim = x.shape.rank
ValueError: Input 0 of layer lstm_37 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 30, 30, 16)