【问题标题】:Intersection over union (IOU) metric for multi-class semantic segmentation task多类语义分割任务的交并联合(IOU)度量
【发布时间】:2021-05-04 12:54:29
【问题描述】:

我有一个语义分割任务,使用 UNET 预测 5 通道掩码,例如掩码形状为 (224,244,5)。

我在借条上使用这个功能:

    def mean_iou(y_true, y_pred):
        y_pred = tf.round(tf.cast(y_pred, tf.int32))
        intersect = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32), axis=[1])
        union = tf.reduce_sum(tf.cast(y_true, tf.float32),axis=[1]) + tf.reduce_sum(tf.cast(y_pred, tf.float32),axis=[1])
        smooth = tf.ones(tf.shape(intersect))
        return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))

 def iou_loss(y_true, y_pred):
      y_true = tf.reshape(y_true, [-1])
      y_pred = tf.reshape(y_pred, [-1])
      intersection = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32))
      score = (intersection + 1.) / (tf.reduce_sum(tf.cast(y_true, tf.float32)) + 
      tf.reduce_sum(tf.cast(y_pred, tf.float32)) - intersection + 1.)
      return 1 - score`

以及UNET模型的输出层:

  outputs = tf.keras.layers.Conv2D(5, (1, 1), activation='softmax')(c9)
   model = tf.keras.Model(inputs=[input_img], outputs=[outputs])
   opti =  tf.keras.optimizers.Adam(lr=0.003, clipvalue=0.7)
   model.compile(optimizer=opti, loss=iou_loss, metrics=['accuracy',mean_iou])

但我不确定 IOU 函数是否正确实现,

你能澄清一下吗?

【问题讨论】:

    标签: python tensorflow keras semantic-segmentation


    【解决方案1】:

    让我们把它分解成更小的部分来了解发生了什么:

    1. tf.reshape(y_true, [-1]),y_pred = tf.reshape(y_pred, [-1]); 预测和基本事实被转换为一维数组。之所以会发生这种情况,是因为从本质上讲,尽管您有多个基本真值掩码,但它们都仅由 1s0s 组成。

    2. intersection = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32))。我们在这里相乘,因为只有当预测和基本事实在某个位置都为 1 时,相乘才会产生 1...0x11x00x0 当然不属于交集。

    3. tf.reduce_sum()。我们只是将交叉点的 1 相加。

    4. score = (intersection + 1.) / (tf.reduce_sum(tf.cast(y_true, tf.float32)) + tf.reduce_sum(tf.cast(y_pred, tf.float32)) - intersection + 1.)。这就是 IoU 的定义。请注意,分子和分母都加了 1,以避免除以 0。在分母级别,由于 Union 操作本身已经包含交集,为了正确计算 IoU,我们需要记住减去交集,从而产生正确的 IoU 值。

    5. 1 - score。我们返回1-score,因为如果IoU是0.75,例如,损失是0.25(完美IoU == 1

    【讨论】:

    • 非常感谢@timbus Calin 的澄清,如果我想跟踪 IOU 和准确性的损失,我应该监控什么损失,CategoricalCrossentropy() 或 IOU 损失如果我选择其中之一会发生什么其中?
    • 您应该同时跟踪两者,但如果您的数据集不平衡,请更加注意 IoU 损失;如果您的数据集非常不平衡,准确度指标可能会产生误导。\
    猜你喜欢
    • 2019-10-31
    • 2021-06-22
    • 2017-10-17
    • 2018-06-23
    • 2018-12-23
    • 2020-06-01
    • 2021-09-28
    • 2019-01-06
    • 2021-05-12
    相关资源
    最近更新 更多