我正在尝试使用Google Colab上的Tensorflow建立一个图像序列预测模型。基本上,该模型应该预测给定图像序列的下一帧。
但是,在将模型拟合到训练数据时,我得到了以下错误:
ValueError: Input 0 of layer conv_lst_m2d_1 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: (None, 900, 900, 3)为了下一帧预测,我遵循了这个教程。
https://keras.io/examples/vision/conv_lstm/
并尝试使用此代码来加载我的图像
https://www.tensorflow.org/tutorials/load_data/images
我的图像是PNG (900x900px,rgb),并像这样存储,文件名是以毫秒为单位的unix时间戳:
raw_images/
1626008400000.png
1626008700000.png
1626009000000.png
1626009300000.png
1626009600000.png
1626009900000.png
...我的代码(最小工作版本):
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
IMAGE_FOLDER = "raw_images/"
# Load the dataset
dataset = keras.preprocessing.image_dataset_from_directory(
IMAGE_FOLDER,
None,
None,
None,
"rgb",
32,
(900, 900),
False,
None,
None,
None,
"bilinear",
False,
True
)
dataset = dataset.cache().prefetch(buffer_size=tf.data.AUTOTUNE)
model = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
tf.keras.layers.ConvLSTM2D(
filters=64,
kernel_size=(5, 5),
padding="same",
return_sequences=True,
activation="relu",
),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ConvLSTM2D(
filters=64,
kernel_size=(3, 3),
padding="same",
return_sequences=True,
activation="relu",
),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ConvLSTM2D(
filters=64,
kernel_size=(1, 1),
padding="same",
return_sequences=True,
activation="relu",
),
tf.keras.layers.Conv3D(
filters=1, kernel_size=(3, 3, 3), activation="sigmoid", padding="same"
)
])
model.compile(
loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adam(),
)
# Define some callbacks to improve training.
early_stopping = keras.callbacks.EarlyStopping(monitor="val_loss", patience=10)
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor="val_loss", patience=5)
# Define modifiable training hyperparameters.
epochs = 20
batch_size = 32
# Fit the model to the training data.
model.fit(
dataset,
None,
batch_size=batch_size,
epochs=epochs
)
model.save("model")我怀疑我需要以某种方式将时间戳添加到数据集中,但我找不到任何方法来做到这一点。
发布于 2021-07-14 07:00:17
这很简单。LSTM还需要一个维度。因此,您只需添加一个tf.keras.layers.Reshape(target_shape),将该ConvLSTM2D层的输入扩展1维。这应该能起到作用。
https://stackoverflow.com/questions/68367392
复制相似问题