从您提供的形状概述((size, sequence_length, height, width, channels))来看,您似乎有每个标签的图像序列。为此,我们通常使用Conv3D。我在下面附上了一个示例代码:
import tensorflow as tf
SIZE = 64
SEQUENCE_LENGTH = 50
HEIGHT = 128
WIDTH = 128
CHANNELS = 3
data = tf.random.normal((SIZE, SEQUENCE_LENGTH, HEIGHT, WIDTH, CHANNELS))
input = tf.keras.layers.Input((SEQUENCE_LENGTH, HEIGHT, WIDTH, CHANNELS))
hidden = tf.keras.layers.Conv3D(32, (3, 3, 3))(input)
hidden = tf.keras.layers.Reshape((-1, 32))(hidden)
hidden = tf.keras.layers.LSTM(200)(hidden)
model = tf.keras.models.Model(inputs=input, outputs=hidden)
model.summary()
输出:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 50, 128, 128, 3)] 0
_________________________________________________________________
conv3d (Conv3D) (None, 48, 126, 126, 32) 2624
_________________________________________________________________
reshape (Reshape) (None, None, 32) 0
_________________________________________________________________
lstm (LSTM) (None, 200) 186400
=================================================================
Total params: 189,024
Trainable params: 189,024
Non-trainable params: 0
如果您仍想使用在您的情况下不建议使用的Conv2D,则必须执行如下所示的操作。基本上,您是在高度维度上附加图像序列,这将使您失去时间维度。
import tensorflow as tf
SIZE = 64
SEQUENCE_LENGTH = 50
HEIGHT = 128
WIDTH = 128
CHANNELS = 3
data = tf.random.normal((SIZE, SEQUENCE_LENGTH, HEIGHT, WIDTH, CHANNELS))
input = tf.keras.layers.Input((SEQUENCE_LENGTH, HEIGHT, WIDTH, CHANNELS))
hidden = tf.keras.layers.Reshape((SEQUENCE_LENGTH * HEIGHT, WIDTH, CHANNELS))(input)
hidden = tf.keras.layers.Conv2D(32, (3, 3))(hidden)
hidden = tf.keras.layers.Reshape((-1, 32))(hidden)
hidden = tf.keras.layers.LSTM(200)(hidden)
model = tf.keras.models.Model(inputs=input, outputs=hidden)
model.summary()
输出:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 50, 128, 128, 3)] 0
_________________________________________________________________
reshape (Reshape) (None, 6400, 128, 3) 0
_________________________________________________________________
conv2d (Conv2D) (None, 6398, 126, 32) 896
_________________________________________________________________
reshape_1 (Reshape) (None, None, 32) 0
_________________________________________________________________
lstm (LSTM) (None, 200) 186400
=================================================================
Total params: 187,296
Trainable params: 187,296
Non-trainable params: 0
_________________________________________________________________