gpt4 book ai didi

python - Keras - 将 3 channel 图像输入 LSTM

转载 作者:太空狗 更新时间:2023-10-29 20:23:37 27 4
gpt4 key购买 nike

我已经将一系列图像读取到形状为 (7338, 225, 1024, 3) 的 numpy 数组中,其中 7338 是样本大小,225 是时间步长,1024 (32x32) 是扁平图像像素,在 3 channel (RGB) 中。

我有一个带有 LSTM 层的顺序模型:

model = Sequential()
model.add(LSTM(128, input_shape=(225, 1024, 3))

但这会导致错误:

Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

documentation提到 LSTM 层的输入张量应该是形状为 (batch_size, timesteps, input_dim) 的 3D 张量,但在我的例子中,我的 input_dim 是 2D。

在 Keras 中将 3 channel 图像输入到 LSTM 层的建议方法是什么?

最佳答案

如果你希望图像的数量是一个序列(比如有帧的电影),你需要把像素和 channel 作为特征:

input_shape = (225,3072)  #a 3D input where the batch size 7338 wasn't informed

如果您希望在将 3072 个特征放入 LSTM 之前进行更多处理,您可以组合或交错 2D 卷积和 LSTM 以获得更精细的模型(虽然不一定更好,但每个应用程序都有其特定的行为)。

你也可以尝试使用新的ConvLSTM2D ,这将采用五维输入:

input_shape=(225,32,32,3) #a 5D input where the batch size 7338 wasn't informed

我可能会用几个 TimeDistributed(Conv2D(...)) 创建一个卷积网络和 TimeDistributed(MaxPooling2D(...))在添加 TimeDistributed(Flatten()) 之前最后是 LSTM() .这很可能会提高您对图像的理解和 LSTM 的性能。

关于python - Keras - 将 3 channel 图像输入 LSTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47671732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com