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,
)
计算平均交并比(Intersection-Over-Union)指标。
通用定义与计算
交并比(Intersection-Over-Union)是用于语义图像分割的一种常用评估指标。
对于单个类别,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)])