Keras Unet: ' Concatenate '层需要具有匹配形状的输入,除了Concatenate轴Error



我已经浏览了许多具有相同类型错误的线程,但似乎没有一个与我所面临的问题相似。下面是一个简单的Unet架构:

tdata = np.zeros([100,500,500,5])
def unet2(input_data):
inputs = layers.Input((input_data.shape[1],input_data.shape[2],input_data.shape[3]))
conv1 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
conv1 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = layers.Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
conv2 = layers.Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
pool2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = layers.Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
conv3 = layers.Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
pool3 = layers.MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = layers.Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
conv4 = layers.Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)
drop4 = layers.Dropout(0.5)(conv4)
pool4 = layers.MaxPooling2D(pool_size=(2, 2))(drop4)
conv5 = layers.Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
conv5 = layers.Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
drop5 = layers.Dropout(0.5)(conv5)
up6 = layers.Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(layers.UpSampling2D(size = (2,2))(drop5))
merge6 = layers.concatenate([drop4,up6], axis = 3)
conv6 = layers.Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)
conv6 = layers.Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)
up7 = layers.Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(layers.UpSampling2D(size = (2,2))(conv6))
merge7 = layers.concatenate([conv3,up7], axis = 3)
conv7 = layers.Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)
conv7 = layers.Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)
up8 = layers.Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(layers.UpSampling2D(size = (2,2))(conv7))
merge8 = layers.concatenate([conv2,up8], axis = 3)
conv8 = layers.Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)
conv8 = layers.Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)
up9 = layers.Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(layers.UpSampling2D(size = (2,2))(conv8))
merge9 = layers.concatenate([conv1,up9], axis = 3)
conv9 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)
conv9 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
conv9 = layers.Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
conv10 = layers.Conv2D(1, 1, activation = 'sigmoid')(conv9)
model = Model(input = input_data, output = conv10)
model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy'])

然后我简单地运行unet:

unet2(tdata)

,其中tdata表示我可能已经为Unet准备好的样本数据集。在这里,维度为[N,x,y,v],其中N =样本数,x = x维,y = y维,v =变量数。换句话说,我有100个由5个变量组成的500x500数据样本。当我运行这段代码时,我得到了错误:

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 125, 125, 256), (None, 124, 124, 256)]

在'merge7'部分。因此,有一个抵消1个问题的地方,我似乎无法查明-为什么这个问题发生,如何最好地解决这个问题?

我的Keras版本是2.4.0和tf = 2.3.1

考虑特征映射的大小。500x500向下采样到25x250,好的。将样本降至125x125。但是当你按2倍采样125x125(就像最大池化层那样)时,你会得到什么?你是否能够拥有带有不同尺寸的特征地图(不能)?因此,这将导致62x62大小的特征图(基本上在输入中切断一个像素),稍后将上采样回124x124并创建不匹配。

你可以通过小心地控制隐藏层中的填充来解决这个问题,但到目前为止,最简单的选择是填充或调整输入的大小到2的幂,或者至少是一个可以被网络的整体降尺度因子整除的数字(如果我没有弄错的话,是16)。在您的示例中,最接近2的幂是512,这与输入大小非常接近,因此对于这个用例应该是可以的。

具体地说,在这个玩具示例中,您需要更改的所有内容都是tdata = np.zeros([100,512,512,5])

相关内容

  • 没有找到相关文章

最新更新