Keras Conv3d输入通道冲突



嗨,我正在TF2中创建一个包含conv3D层的网络。以下是我的网络定义

class Model(tf.keras.Model):
def __init__(
self,
numOutputClasses=None,
filters1=128,
filters2=256,
filters3=512,
):
super(Model, self).__init__()
self.regularizer = tf.keras.regularizers.l2(l=REGULARISATIONSCALE)
self.convblock1 = convBlock(filters1, self.regularizer)
self.convblock2 = convBlock(filters2, self.regularizer)
self.convblock3 = convBlock(filters3, self.regularizer)

def call(
self,
x
):
x = tf.reshape(x, [batchsize, vsize, vsize, vsize, VOXELFEATUREDIM])
print("input to convbloc1 has shape ", x.shape)
x = self.convblock1(x)
print("input to convbloc2 has shape ", x.shape)
x = self.convblock2(x)
print(" input to cb3 has shape ", x.shape)
x = self.convblock3(x)
return x

其中convBlock定义为

class convBlock(layers.Layer):
def __init__(self, filters, regularizer):
super(convBlock, self).__init__()
self.regularizer = regularizer
self.conv3d = tf.keras.Sequential(
[
layers.Conv3D(
filters=filters,
kernel_size=[3, 3, 3],
padding="same",
strides=[1, 1, 1],
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=self.regularizer,
bias_regularizer=self.regularizer,
),
layers.BatchNormalization(),
layers.LeakyReLU(),
]
)
self.maxpool = layers.MaxPooling3D(pool_size=[2, 2, 2], strides=[2, 2, 2])
def call(self, x):
print("in cb")
print("x shape ", x.shape)
x = self.conv3d(x)
print("x shape ", x.shape)
x = self.conv3d(x)
print("x shape ", x.shape)
x = self.maxpool(x)
print("op shape ", x.shape)
return x

我对Model的输入具有形状(经过第一次重塑)[32, 16, 16, 16, 128],它被馈送到第一个ConvBlock层。正如预期的那样,输出具有[32, 8, 8, 8, 128]的形状(具有128个输出通道+MaxPool用于将尺寸减半)。当这个输出被馈送到convblock2(Model中的第二个convblock)时,第一个conv3d层后的输出是[32, 8, 8, 8, 256](预期-保维+256输出通道)。但是当这个输出传递到convblock2的第二层conv3d层时,它返回一个值error

ValueError:expected axis -1 of input shape to have value 128 but received input with shape [32, 8, 8, 8, 256]

现在我的疑问是为什么conv3D期望输入通道的数量?我的代码中遗漏了什么吗?

任何帮助将不胜感激!谢谢你!

UPDATE

我意识到self.conv3d是一个单一的对象,被两个不同的3D卷积调用,具有不同的输入通道要求。因此,我初始化了同一层的两个不同副本,即self.conv3d1self.conv3d2,它们具有完全相同的架构。现在我的convBlock看起来像-

class convBlock(layers.Layer):
def __init__(self, filters, regularizer):
super(convBlock, self).__init__()
self.regularizer = regularizer
self.conv3d1 = tf.keras.Sequential(
[
layers.Conv3D(
filters=filters,
kernel_size=[3, 3, 3],
padding="same",
strides=[1, 1, 1],
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=self.regularizer,
bias_regularizer=self.regularizer,
),
layers.BatchNormalization(),
layers.LeakyReLU(),
]
)
self.conv3d2 = tf.keras.Sequential(
[
layers.Conv3D(
filters=filters,
kernel_size=[3, 3, 3],
padding="same",
strides=[1, 1, 1],
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=self.regularizer,
bias_regularizer=self.regularizer,
),
layers.BatchNormalization(),
layers.LeakyReLU(),
]
)
self.maxpool = layers.MaxPooling3D(pool_size=[2, 2, 2], strides=[2, 2, 2])
def call(self, x):
print("in cb")
print("x shape ", x.shape)
x = self.conv3d1(x)
print("x shape ", x.shape)
x = self.conv3d2(x)
print("x shape ", x.shape)
x = self.maxpool(x)
print("op shape ", x.shape)
return x

这对我有用!希望这能帮助到一些人!谢谢你!

相关内容

  • 没有找到相关文章

最新更新