我一直在研究分段问题很多天,在最终找到如何正确读取数据集之后,我遇到了这个问题: “ ValueError:检查目标时出错:预期activation_1(Softmax)有3个维度,但得到的数组有形状(32,416,608,3)” 我使用了功能API,因为我采用了以下的FCNN架构: https://github.com/divamgupta/image-segmentation-keras/blob/master/Models/FCN32.py
稍微修改并根据我的任务进行调整(IMAGE_ORDERING =“channels_last”(TensorFlow后端))。
谁能帮帮我吗?
提前大力谢谢。
下面的体系结构适用于FCNN,我尝试将其用于分段。
这是架构(在调用model.summary()之后)
[1]:https://i.stack.imgur.com/2Ou5z.png
[2]:https://i.stack.imgur.com/zOFAz.png
[3]:具体错误是:https://i.stack.imgur.com/DVo2k.png
[4]:“导入数据集”功能:https://i.stack.imgur.com/UY2FE.png
[5]:“Fit_Generator方法调用”:https://i.stack.imgur.com/VskLj.png
IMAGE_ORDERING = 'channels_last'
def getFCN32(nb_classes,input_height=416, input_width=608):
img_input = Input(shape=(input_height,input_width,3))
#Block 1
x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = BatchNormalization()(x)
x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING )(x)
f2 = x
# Block 3
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING )(x)
f3 = x
# Block 4
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x)
f4 = x
# Block 5
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x)
f5 = x
x = (Convolution2D(4096,(7,7) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(x)
x = Dropout(0.5)(x)
x = (Convolution2D(4096,(1,1) , activation='relu' , padding='same',data_format=IMAGE_ORDERING))(x)
x = Dropout(0.5)(x)
#First parameter = number of classes+1 (de la background)
x = (Convolution2D(20,(1,1) ,kernel_initializer='he_normal' ,data_format=IMAGE_ORDERING))(x)
x = Convolution2DTranspose(20,kernel_size=(64,64), strides=(32,32),use_bias=False,data_format=IMAGE_ORDERING)(x)
o_shape = Model(img_input,x).output_shape
outputHeight = o_shape[1]
print('Output Height is:', outputHeight)
outputWidth = o_shape[2]
print('Output Width is:', outputWidth)
#https://keras.io/layers/core/#reshape
x = (Reshape((20,outputHeight*outputWidth)))(x)
#https://keras.io/layers/core/#permute
x = (Permute((2, 1)))(x)
print("Output shape before softmax is", o_shape)
x = (Activation('softmax'))(x)
print("Output shape after softmax is", o_shape)
model = Model(inputs = img_input,outputs = x)
model.outputWidth = outputWidth
model.outputHeight = outputHeight
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics =['accuracy'])
return model
答案 0 :(得分:1)
FCNN架构示例中的原始代码使用输入维度(416, 608)
。而在您的代码中,输入维度为(192, 192)
(忽略通道维度)。现在,如果你仔细注意,这个特定的层
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x)
生成维度(6, 6)
的输出(您可以在model.summary()
中进行验证。)
下一个 convoltuion图层
o = (Convolution2D(4096,(7,7) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(o)
使用大小为(7, 7)
的卷积滤镜,但您的输入已缩小到小于该值的卷积(即(6, 6)
)。首先尝试修复它。
另外,如果您查看model.summary()
输出,您会注意到它不包含 block5_pool 图层后定义的图层。其中有一个transposed convolution
图层(基本上会对您的输入进行上采样)。您可能需要查看并尝试解决此问题。
注意:在我的所有维度中,我忽略了频道维度。
编辑以下详细解答
首先,这是我的keras.json
文件。它使用 Tensorflow 后端,image_ordering
设置为 channel_last 。
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}
接下来,我复制粘贴我的确切模型代码。请特别注意以下代码中的内联注释。
from keras.models import *
from keras.layers import *
IMAGE_ORDERING = 'channels_last' # In consistency with the json file
def getFCN32(nb_classes = 20, input_height = 416, input_width = 608):
img_input = Input(shape=(input_height,input_width, 3)) # Expected input will have channel in the last dimension
#Block 1
x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = BatchNormalization()(x)
x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING )(x)
f2 = x
# Block 3
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING )(x)
f3 = x
# Block 4
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x)
f4 = x
# Block 5
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2',data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format=IMAGE_ORDERING)(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x)
f5 = x
x = (Convolution2D(4096,(7,7) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(x)
x = Dropout(0.5)(x)
x = (Convolution2D(4096,(1,1) , activation='relu' , padding='same',data_format=IMAGE_ORDERING))(x)
x = Dropout(0.5)(x)
x = (Convolution2D(20,(1,1) ,kernel_initializer='he_normal' ,data_format=IMAGE_ORDERING))(x)
x = Convolution2DTranspose(20,kernel_size=(64,64), strides=(32,32),use_bias=False,data_format=IMAGE_ORDERING)(x)
o_shape = Model(img_input, x).output_shape
# NOTE: Since this is channel last dimension ordering, the height and width dimensions are along [1] and [2], not [2] and [3]
outputHeight = o_shape[1]
outputWidth = o_shape[2]
x = (Reshape((outputHeight*outputWidth, 20)))(x) # Channel should be along the last dimenion of reshape
# No need of permute layer anymore
print("Output shape before softmax is", o_shape)
x = (Activation('softmax'))(x)
print("Output shape after softmax is", o_shape)
model = Model(inputs = img_input,outputs = x)
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics =['accuracy'])
return model
model = getFCN32(20)
print(model.summary())
接下来,我将提供我的model.summary()
外观的摘要。如果你看看最后几层,它是这样的:
这意味着,conv2d_transpose
图层在应用 softmax 之前会生成尺寸为(448, 640, 20)
的输出,并且将其展平。所以输出的维度是(286720, 20)
。同样,您的target_generator
(在您的情况下为mask_generator
)也应生成类似维度的目标。同样,您的input_generator
也应该生成大小为[batch size, input_height,input_width, 3]
的输入批次,如您函数的img_input
中所述。
希望这可以帮助您找到问题的根源并找出合适的解决方案。请查看代码中的细微变化(以及内嵌评论)以及如何创建输入和目标批次。
答案 1 :(得分:0)
您可能会在color_mode='grayscale'
面罩电话中丢失flow_from_directory()
。 RGB是color_mode
的默认值。
flow_args = dict(
batch_size=batch_size,
target_size=target_size,
class_mode=None,
seed=seed)
image_generator = image_datagen.flow_from_directory(
image_dir, subset='training', **flow_args)
mask_generator = mask_datagen.flow_from_directory(
mask_dir, subset='training', color_mode='grayscale', **flow_args)