因此,我对如何在Keras中将CNN与RNN结合存在一个疑问。在发布问题时,有人指出我这是解决问题的正确方法。显然我只是忽略了原始代码中的某些内容,这使我回答了自己的问题。

原始问题如下:

您如何在Keras中创建一个模型,该模型以图像序列作为输入,而CNN会“看”每个单独的图像,并将CNN输出的序列输入到RNN中?

为了更清楚一点:

模型一:观看单个图像的CNN。
模型二:RNN,位于模型一CNN输出的序列上。

因此,例如CNN应该看到5幅图像,并且CNN的5个输出的序列应该传递给RNN。

输入数据的格式如下:
(图像数,宽度,高度, channel )=(4000,120,60,1)

最佳答案

这个问题的答案如下。

采用这个过于简化的CNN模型:

cnn = Sequential()
cnn.add(Conv2D(16, (50, 50), input_shape=(120, 60, 1)))

cnn.add(Conv2D(16, (40, 40)))

cnn.add(Flatten()) # Not sure if this if the proper way to do this.

然后是一个简单的RNN模型:
rnn = Sequential()

rnn = GRU(64, return_sequences=False, input_shape=(120, 60))

哪个应连接到密集网络:
dense = Sequential()
dense.add(Dense(128))
dense.add(Dense(64))

dense.add(Dense(1)) # Model output

请注意,为了便于阅读,省略了激活功能等。

现在剩下的就是结合这三个主要模型。
main_input = Input(shape=(5, 120, 60, 1)) # Data has been reshaped to (800, 5, 120, 60, 1)

model = TimeDistributed(cnn)(main_input) # this should make the cnn 'run' 5 times?
model = rnn(model) # combine timedistributed cnn with rnn
model = dense(model) # add dense

然后最后
final_model = Model(inputs=main_input, outputs=model)

final_model.compile...
final_model.fit...

关于Keras功能API : Combine CNN model with a RNN to to look at sequences of images,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53488768/

10-13 00:09