我正在创建一个高度=128,宽度=128,通道=3的3D CNN模型。3D CNN的代码-
def get_model(width=128, height=128, depth=3):
"""
Build a 3D convolutional neural network
"""
inputs = tf.keras.Input((width, height, depth, 1))
x = layers.Conv3D(filters=64, kernel_size=3, activation="relu")(inputs)
x = layers.MaxPool3D(pool_size=2)(x)
x = layers.BatchNormalization()(x)
x = layers.Conv3D(filters=128, kernel_size=3, activation="relu")(x)
x = layers.MaxPool3D(pool_size=2)(x)
x = layers.BatchNormalization()(x)
x = layers.Conv3D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.MaxPool3D(pool_size=2)(x)
x = layers.BatchNormalization()(x)
x = layers.GlobalAveragePooling3D()(x)
x = layers.Dense(units=512, activation="relu")(x)
x = layers.Dropout(0.3)(x)
outputs = layers.Dense(units=4, activation='softmax')(x)
model= keras.Model(inputs, outputs, name="3DCNN")
return model
因此,在创建模型函数后,当我试图构建模型时,它会抛出一个值错误
-ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node max_pooling3d_5/MaxPool3D}} = MaxPool3D[T=DT_FLOAT, data_format="NDHWC", ksize=[1, 2, 2, 2, 1], padding="VALID", strides=[1, 2, 2, 2, 1]](Placeholder)' with input shapes: [?,126,126,1,64].
构建模型的代码:-#建立模型
model = get_model(width=128, height=128, depth=3)
model.summary()
完全错误-
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1879 try:
-> 1880 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1881 except errors.InvalidArgumentError as e:
InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for '{{node max_pooling3d_5/MaxPool3D}} = MaxPool3D[T=DT_FLOAT, data_format="NDHWC", ksize=[1, 2, 2, 2, 1], padding="VALID", strides=[1, 2, 2, 2, 1]](Placeholder)' with input shapes: [?,126,126,1,64].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
14 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1881 except errors.InvalidArgumentError as e:
1882 # Convert to ValueError for backwards compatibility.
-> 1883 raise ValueError(str(e))
1884
1885 return c_op
ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node max_pooling3d_5/MaxPool3D}} = MaxPool3D[T=DT_FLOAT, data_format="NDHWC", ksize=[1, 2, 2, 2, 1], padding="VALID", strides=[1, 2, 2, 2, 1]](Placeholder)' with input shapes:
[?,126,126,1,64].
这个错误是什么意思??我的尺寸有问题吗??
提前感谢!!!!!
在不指定data_format
参数的情况下,Conv3D
层将输入形状视为:
batch_shape + (conv_dim1, conv_dim2, conv_dim3, channels)
您已指定为:
batch_shape + (width=128, height=128, depth=3, channels=1)
因此,您有一个数据,它的形状是(128,128,3)
,并且有一个通道。
由于卷积运算适用于(128,128,3)
的前3个维度,因此在kernel_size=3
进行第一次卷积后,第3个维度(指定为深度=3的维度(将缩小为1。然后在下一层(MaxPooling3D
(中,由于形状不合适,它无法通过2获得池化。因此,可以考虑将深度维度更改较大的数字或更改kernel_size
参数。例如,输入形状可以是(128,128,128,1)
,或者kernel_size
应该改变为类似(3,3,1)
的其他形状。
第页。S: 如果有RGB图像,则通道数为3,最后一个维度应设置为3。在3D图像中,有另一个不同于通道的概念,称为深度(另一个维度(。因此:
- 3D图像RGB:
(width, height, depth, 3)
- 3D图像灰度:
(width, height, depth, 1)
- 2D图像RGB:
(width, height, 3)
- 2D图像灰度:
(width, height, 1)