【问题标题】:How to create a Tensorflow dataset for image sequence prediction or next frame prediction from PNGs?如何为 PNG 的图像序列预测或下一帧预测创建 TensorFlow 数据集?
【发布时间】:2021-09-22 19:15:07
【问题描述】:

我正在尝试在 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")

我怀疑我需要以某种方式将时间戳添加到数据集,但我找不到任何方法。

【问题讨论】:

    标签: python tensorflow machine-learning keras


    【解决方案1】:

    这很容易。 LSTM 需要多一维。所以你只需要添加一个tf.keras.layers.Reshape(target_shape) 来将输入到该 ConvLSTM2D 层扩展一维。这应该可以解决问题。

    【讨论】:

    • 感谢您的回答。我添加了tf.keras.layers.Reshape((1, 900, 900, 3)),现在得到一个不同的错误:ValueError: No gradients provided for any variable: ['conv_lst_m2d_21/kernel:0', 'conv_lst_m2d_21/recurrent_kernel:0', 'conv_lst_m2d_21/bias:0', 'batch_normalization_14/gamma:0', 'batch_normalization_14/beta:0', 'conv_lst_m2d_22/kernel:0', 'conv_lst_m2d_22/recurrent_kernel:0', 'conv_lst_m2d_22/bias:0', ...] 你知道问题出在哪里吗? :-)
    • @magpielark 这通常发生在您的模型没有从数据集中获取 y_ture 以计算损失和梯度时。尝试使用 for i in dataset: print(i) break 查看您的数据集
    • 输出tf.Tensor( [[[[255. 0. 0.] [255. 0. 0.] [255. 0. 0.] ... [255. 0. 0.] [255. 0. 0.] [255. 0. 0.]] [[255. 0. 0.] [255. 0. 0.] [255. 0. 0.] ... [255. 0. 0.] [255. 0. 0.] [255. 0. 0.]]]], shape=(32, 900, 900, 3), dtype=float32)(缩短,完整输出here)因为这些是RGB值并且有很多红色像素对我来说很有意义。
    • 正如您在输出中看到的那样,您确实将图像提供给模型,但没有标签/Y 对应于模型将计算损失的那些图像。对于所有 32 张图像,应该有 32 个 Y 值。
    • 好的,但是如何添加 Y 的这些值,它们是什么?由于我想预测下一帧,下一帧不应该是预测的比较对象吗?
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2020-09-07
    • 2021-02-16
    • 2018-09-17
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多