【发布时间】:2021-07-25 14:13:19
【问题描述】:
我正在尝试在尺寸为 (19,163,279) 的图像(以 NumPy 数组的形式)上训练 3D CNN。我的 X_train 的形状为 (740,19,163,279),y_train 的形状为 (185,19,163,279)。
if K.image_data_format() == 'channels_first':
INPUT_SHAPE = (1, 19, 163, 279)
else:
INPUT_SHAPE = (19, 163, 279, 1)
这是我的模型
def get_model(width=163, height=279, depth=19):
"""Build a 3D convolutional neural network model."""
inputs = keras.Input(INPUT_SHAPE)
x = layers.Conv3D(filters=64, kernel_size=3, activation="relu",padding='same')(inputs)
x = layers.MaxPool3D(pool_size=2,padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Conv3D(filters=64, kernel_size=3, activation="relu",padding='same')(x)
x = layers.MaxPool3D(pool_size=2,padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Conv3D(filters=128, kernel_size=3, activation="relu", padding='same')(x)
x = layers.MaxPool3D(pool_size=2,padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Conv3D(filters=64, kernel_size=3, activation="relu", padding='same')(x)
x = layers.MaxPool3D(pool_size=2,padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.GlobalAveragePooling3D()(x)
x = layers.Dense(units=16, activation="relu")(x)
x = layers.Dropout(0.3)(x)
outputs = layers.Dense(units=1, activation="sigmoid")(x)
# Define the model.
model = keras.Model(inputs, outputs, name="3dcnn")
return model
# Build model.
model = get_model(width=163, height=279, depth=19)
model.summary()
但是,在使用以下代码进行训练时,
# Train the model, doing validation at the end of each epoch
epochs = 100
model.fit(
X_train,
validation_data=y_test,
epochs=epochs,
shuffle=True,
verbose=2,
callbacks=[checkpoint_cb, early_stopping_cb],
)
我收到以下错误
Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (740, 19, 163, 279)
我该如何解决这个问题?
【问题讨论】:
-
根据您的数据,您的 INPUT_SHAPE 必须为 (19, 163, 279)... 无需添加其他维度。另外,我不知道你为什么不带标签(y=None)而只使用 y_test 作为验证数据
-
标签在那里。我只是没有在问题中包含那部分代码。我将 INPUT_SHAPE 从 (1,19,163,279) 和 (19,163,279,1) 更改为 (19,163,279)。但现在我收到一个新错误,“conv3d 层的输入 0 与该层不兼容:预期 ndim=5,发现 ndim=4。收到完整形状:[None, 19, 163, 279]”
-
对于您的数据,您应该使用 Conv2d
-
但这 19 个通道之间存在某种关系,我希望我的模型也能够从中学习以进行分类。如何使用 Conva3D 解决?
-
如果你应该为你的输入数组添加维度... np.expand_dims(X_train, -1) (这是最后一个通道)
标签: python tensorflow keras deep-learning