将图像分割模型输出从 2D(类,像素数)重塑为 3D 通道最后图像的正确方法是什么

what is the correct way to reshape image segmentation model output from 2D (num of classes, num of pixels) to 3D channel last images

我正在使用 keras 和 python 进行卫星图像分割。据我了解,为了获得图像分割的(像素级)预测,模型将维度层(-1,num_classes,高度,宽度)重塑为形状(-1,num_classes,高度*宽度). 然后应用像 softmax 或 sigmoid 这样的激活函数。我的问题是如何在这一步后以先通道还是后通道的格式恢复图像? 示例代码

o = (Reshape((  num_classes , outputHeight*outputWidth)))(o)
o = (Permute((2, 1)))(o)
o = (Activation('softmax'))(o)

我试过在模型的最后添加以下层

o = (Reshape((outputHeight, outputWidth, num_classes)))(o)

这是正确的吗?这是否会按照与原始顺序相同的顺序重新定向图像像素? 另一种选择可能是在单个图像上使用以下代码。

array.reshape(height, width, num_classes)

我应该使用哪种方法来获得像素级分割结果?

不,如果您对图像分割感兴趣,您应该而不是展平然后重塑您的张量。相反,使用 全卷积 模型,例如 U-Net. You find a lot of example implementations of it on github, e.g. here