Tensorflow 2.0 Conv3D input_shape问题

时间:2020-04-21 11:02:41

标签: python tensorflow machine-learning keras conv-neural-network

我正在尝试在视频序列上训练CNN。我的input_data的形状为(5874、1、10、128、128),它们表示(n_samples,通道,帧,高度,宽度)。错误是给出了4个维度,但给出了5个预期值或给出了6个维度。管理Conv3D的正确方法是什么?

Input((1,10,128,128))设置为:ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1, 128, 128, 10)。但安装后会产生错误。

在执行模型后(拟合之前)将Input((1,1,10,128,128))设置为ValueError: Input 0 of layer conv3d_6 is incompatible with the layer: expected ndim=5, found ndim=6. Full shape received: [None, 1, 1, 128, 128, 10]

我已经浏览了所有可能的文档和论坛,但一无所获。任何提示都会有所帮助。

dataset = tf.data.Dataset.from_tensor_slices((data, labels)))
dataset = dataset.shuffle(10000)
train_dataset, valid_dataset = split_dataset(dataset, 0.02)


model = tf.keras.Sequential()
model.add(Input((1,10,128,128)))
model.add(Conv3D(filters = 8, kernel_size=(10,5,5), padding="same", activation="relu", data_format="channels_first"))
model.add(BatchNormalization())
model.add(Conv3D(filters = 8, kernel_size=(10,3,3), padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPool3D(pool_size=(1,2,2), strides=(1,1,1)))

model.add(Conv3D(filters = 16, kernel_size=(5,5,5), padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(Conv3D(filters = 16, kernel_size=(5,3,3), padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPool3D(pool_size=(2,2,2), strides=(1,1,1)))

model.add(Conv3D(filters = 32, kernel_size=(5,5,5), padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(Conv3D(filters = 32, kernel_size=(3,3,3), padding="same", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPool3D(pool_size=(2,2,2), strides=(1,1,1)))

model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(5, activation="softmax"))



model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.01)  , loss="sparse_categorical_crossentropy", metrics=["accuracy"])
r = model.fit(train_dataset, verbose=1, validation_data=valid_dataset, epochs=50)

1 个答案:

答案 0 :(得分:0)

Tensorflow在模型中的

会在数据的开头添加一个维度以进行迭代。因此,输入应仅获得最后四个维度。但是fit需要5。使用Dataset.from_tensor_slices后,必须使用dataset.batch,否则拟合时会出错。