MeanIoU
类tf_keras.metrics.MeanIoU(
num_classes: int,
name: Optional[str] = None,
dtype: Union[str, tensorflow.python.framework.dtypes.DType, NoneType] = None,
ignore_class: Optional[int] = None,
sparse_y_true: bool = True,
sparse_y_pred: bool = True,
axis: int = -1,
)
计算平均交并比指标。
一般定义和计算
交并比是语义图像分割中常用的评估指标。
对于单个类别,IoU 指标定义如下:
iou = true_positives / (true_positives + false_positives + false_negatives)
为了计算 IoU,预测结果会累积到混淆矩阵中,并根据 sample_weight
加权,然后根据混淆矩阵计算指标。
如果 sample_weight
为 None
,则权重默认为 1。使用 sample_weight
为 0 来掩盖值。
请注意,此类首先计算所有单个类别的 IoU,然后返回这些值的平均值。
参数
ignore_class=None
),会考虑所有类别。False
,则将使用 tf.argmax
函数来确定每个样本最可能关联的标签。False
,则将使用 tf.argmax
函数来确定每个样本最可能关联的标签。-1
。独立使用
>>> # cm = [[1, 1],
>>> # [1, 1]]
>>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
>>> # iou = true_positives / (sum_row + sum_col - true_positives))
>>> # result = (1 / (2 + 2 - 1) + 1 / (2 + 2 - 1)) / 2 = 0.33
>>> m = tf.keras.metrics.MeanIoU(num_classes=2)
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
>>> m.result().numpy()
0.33333334
>>> m.reset_state()
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1],
... sample_weight=[0.3, 0.3, 0.3, 0.1])
>>> m.result().numpy()
0.23809525
与 compile()
API 一起使用
model.compile(
optimizer='sgd',
loss='mse',
metrics=[tf.keras.metrics.MeanIoU(num_classes=2)])