2017-03-04 75 views
4

我试图用unet设计做多类语义分割。与unet纸相似,我想做一个丢失功能来超重边框(第5页)。Keras语义分割加权损失像素图

因此,我想为每个图像制作一个自定义损失地图,其中对象之间的边界超过了限制。我正在使用分类交叉熵,在损失函数之前将图像展平为here。我会很好地制作像素丢失蒙版,但我想知道如果可能的话,如何通过像素蒙版多次丢失。

+0

你能否提供更多关于如何存储这些额外的权重和设置你的问题的细节? –

+0

我可以以任何必要的方式存储权重。但我正在考虑将它们存储在与输出图像大小相同的数组中,其中阵列中的每个像素都是该地面实际像素的损耗因子。这样,我可以将类别不平衡和边界问题作为每个像素的一个权重来处理。设置为标准的语义分割问题,除了有多个类以外,与unet类似。 – TSW

+0

我得到了同样的问题,解决了在模型中添加一个图层的损失:https://stackoverflow.com/questions/48555820/keras-binary-segmentation-add-weight-to-loss-function/48577360# 48577360 –

回答

0

如果你知道如何在2D地图上做到这一点,你总是可以使用多个输出,并使用自定义像素掩模额外的交叉熵损失。一个U形网络的多重损失的示例实现可以在这里找到:https://github.com/EdwardTyantov/ultrasound-nerve-segmentation

+0

它只是使用2个损失,没有损失像素权重。 – mrgloom

0

这里是张量流http://tf-unet.readthedocs.io/en/latest/_modules/tf_unet/unet.html中的权重映射的实现,你应该能够适应它在自定义丢失函数中的keras。我报告相关的代码:

def _get_cost(self, logits, cost_name, cost_kwargs): 

    Optional arguments are: 
    class_weights: weights for the different classes in case of multi-class imbalance 
    regularizer: power of the L2 regularizers added to the loss function 

    flat_logits = tf.reshape(logits, [-1, self.n_class]) 
    flat_labels = tf.reshape(self.y, [-1, self.n_class]) 
    if cost_name == "cross_entropy": 
     class_weights = cost_kwargs.pop("class_weights", None) 

     if class_weights is not None: 
      class_weights = tf.constant(np.array(class_weights, dtype=np.float32)) 

      weight_map = tf.multiply(flat_labels, class_weights) 
      weight_map = tf.reduce_sum(weight_map, axis=1) 

      loss_map = tf.nn.softmax_cross_entropy_with_logits(flat_logits, flat_labels) 
      weighted_loss = tf.multiply(loss_map, weight_map) 

      loss = tf.reduce_mean(weighted_loss) 

     else: 
      loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=flat_logits, 
                      labels=flat_labels))} 
+0

看起来像是平衡类,而不是特定的像素。 – mrgloom