我可以使用带有二分类标签的带标注图像来训练 U-net。但是,我在配置Keras/Theano的最后一层进行 多类别 分类(4个类别)方面遇到了困难。
我有634张图像和相应的634个掩模,它们是unit8
和64 x 64像素。
我的掩模不是黑色(0)和白色(1),而是用颜色标记为3个类别的对象及背景,如下所示:
- 黑色(0),背景
- 红色(1),对象类别1
- 绿色(2),对象类别2
- 黄色(3),对象类别3
在训练运行之前,包含掩模的数组被独热编码如下:
mask_train = to_categorical(mask_train, 4)
这将使mask_train.shape
从(634, 1, 64, 64)
变成(2596864, 4)
。
我的模型紧密遵循Unet结构,但最后几层似乎存在问题,因为我无法对其进行扁平化处理,以匹配one-hot编码的数组。
[...]
up3 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=1)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up3)
conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8)
up4 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=1)
conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up4)
conv10 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9)
# here I used number classes = number of filters and softmax although
# not sure if a dense layer should be here instead
conv11 = Conv2D(4, (1, 1), activation='softmax')(conv10)
model = Model(inputs=[inputs], outputs=[conv11])
# here categorical cross entropy is being used but may not be correct
model.compile(optimizer='sgd', loss='categorical_crossentropy',
metrics=['accuracy'])
return model
你对如何修改模型的最后部分以便成功训练有何建议?我遇到了各种形状不匹配的错误,而且少数几次成功运行模型时,损失在整个时期内都没有变化。
softmax
和categorical_crossentropy
是否是合适的选择? - pepeSoftmax(axis=1)
。这是因为您期望有多个“1”结果。但不幸的是,我不知道哪个轴是适当的,我认为它是轴1,即通道轴。(您希望在同一像素的四个通道中只有一个1) - Daniel Möller