Keras中输入/输出通道尺寸不匹配

时间:2019-04-09 12:40:01

标签: python tensorflow keras image-segmentation

我目前正在与Keras一起进行多类图像分割。 我的输入图像为RGB格式。 我正在尝试对图像中的4种对象进行细分。

我的神经网络如下:

inputs = Input((patch_size, patch_size,nb_chan))

#
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D((2, 2))(conv1)

#
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D((2, 2))(conv2)

#
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D((2, 2))(conv3)

conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv4)

#
merge1 = UpSampling2D(size=(2, 2))(conv4)
merge1 = concatenate([conv3,merge1],axis=3)
conv5 = Conv2D(256, (3, 3), activation='relu', padding='same')(merge1)
conv5 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv5)

#
merge2 = UpSampling2D(size=(2, 2))(conv5)
merge2 = concatenate([conv2,merge2], axis=3)
conv6 = Conv2D(128, (3, 3), activation='relu', padding='same')(merge2)
conv6 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv6)

#
merge3 = UpSampling2D(size=(2, 2))(conv6)
merge3 = concatenate([conv1,merge3], axis=3)
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(merge3)
conv7 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv7)


conv8 = Conv2D(nbClass, (1, 1), activation='softmax',padding='same')(conv7)
model = Model(inputs=inputs, outputs=conv8)
model.summary()
model.compile(optimizer=Adam(lr=1e-5), loss='categorical_crossentropy', metrics=['categorical_accuracy'])

我输入的图像尺寸为256 * 256 nb_chan是输入通道大小,在我的情况下是3。 nbClass是要分割的对象数,在我的例子中是4。

但是当我要编译模型时,出现此错误:

ValueError: Error when checking target: expected conv2d_15 to have shape (256, 256, 4) but got array with shape (256, 256, 3)

我尝试使用重塑功能修改网络的最后一层:

conv8 = Conv2D(nbClass, (1, 1), activation='softmax',padding='same')(conv7)
conv8 = Reshape((patch_size*patch_size,nbClass))(conv8)
conv8 = core.Permute((2,1))(conv8)

但我收到此错误:

ValueError: Error when checking target: expected permute_1 to have 3 dimensions, but got array with shape (8, 256, 256, 3)

我有点迷茫,我忘了什么吗?

0 个答案:

没有答案