首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从PNG创建用于图像序列预测或下一帧预测的Tensorflow数据集?

如何从PNG创建用于图像序列预测或下一帧预测的Tensorflow数据集?
EN

Stack Overflow用户
提问于 2021-07-14 02:04:27
回答 1查看 172关注 0票数 1

我正在尝试使用Google Colab上的Tensorflow建立一个图像序列预测模型。基本上,该模型应该预测给定图像序列的下一帧。

但是,在将模型拟合到训练数据时,我得到了以下错误:

代码语言:javascript
复制
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时间戳:

代码语言:javascript
复制
raw_images/
    1626008400000.png
    1626008700000.png
    1626009000000.png
    1626009300000.png
    1626009600000.png
    1626009900000.png
    ...

我的代码(最小工作版本):

代码语言:javascript
复制
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")

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

EN

回答 1

Stack Overflow用户

发布于 2021-07-14 07:00:17

这很简单。LSTM还需要一个维度。因此,您只需添加一个tf.keras.layers.Reshape(target_shape),将该ConvLSTM2D层的输入扩展1维。这应该能起到作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68367392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档