3D CNN model throwing a Negative dimension error--维度问题
3D CNN model throwing a Negative dimension error-- dimension issue
我正在创建一个 3D CNN 模型,高度 = 128,宽度 = 128,通道 = 3.The 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)
并且有 1 个通道。
由于卷积运算应用于 (128,128,3)
的前 3 个维度,在第一个卷积 kernel_size=3
之后,第 3 个维度(您指定为 depth=3 的维度)缩小为 1。然后在下一层(MaxPooling3D
)它无法通过2进行池化,因为形状不适合。因此,考虑将深度维度更改为更大的数字或更改 kernel_size
参数。例如,输入形状可以是 (128,128,128,1)
或者 kernel_size
应该更改为其他形状,例如 (3,3,1)
.
P.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)
我正在创建一个 3D CNN 模型,高度 = 128,宽度 = 128,通道 = 3.The 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)
并且有 1 个通道。
由于卷积运算应用于 (128,128,3)
的前 3 个维度,在第一个卷积 kernel_size=3
之后,第 3 个维度(您指定为 depth=3 的维度)缩小为 1。然后在下一层(MaxPooling3D
)它无法通过2进行池化,因为形状不适合。因此,考虑将深度维度更改为更大的数字或更改 kernel_size
参数。例如,输入形状可以是 (128,128,128,1)
或者 kernel_size
应该更改为其他形状,例如 (3,3,1)
.
P.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)