當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.keras.metrics.IoU用法及代碼示例

計算特定目標類的 Intersection-Over-Union 指標。

繼承自:MetricLayerModule

用法

tf.keras.metrics.IoU(
    num_classes:int,
    target_class_ids:Union[List[int], Tuple[int, ...]],
    name=None,
    dtype=None
)

參數

  • num_classes 預測任務可能具有的標簽數量。將分配一個維度 = [num_classes, num_classes] 的混淆矩陣來累積計算指標的預測。
  • target_class_ids 返回指標的目標類 ID 的元組或列表。要計算特定類的 IoU,應提供單個 id 值的列表(或元組)。
  • name (可選)指標實例的字符串名稱。
  • dtype (可選)度量結果的數據類型。

一般定義和計算:

Intersection-Over-Union 是語義圖像分割的常用評估指標。

對於單個類,IoU 指標定義如下:

iou = true_positives / (true_positives + false_positives + false_negatives)

為了計算 IoU,預測被累積在一個混淆矩陣中,由 sample_weight 加權,然後從中計算度量。

如果 sample_weightNone ,則權重默認為 1。使用 0 的 sample_weight 來屏蔽值。

請注意,此類首先計算所有單個類的 IoU,然後返回由 target_class_ids 指定的類的 IoU 平均值。如果 target_class_ids 隻有一個 id 值,則返回該特定類的 IoU。

單機使用:

# 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))
# iou = [0.33, 0.33]
m = tf.keras.metrics.IoU(num_classes=2, target_class_id=[0])
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])
# cm = [[0.3, 0.3],
#        [0.3, 0.1]]
# sum_row = [0.6, 0.4], sum_col = [0.6, 0.4], true_positives = [0.3, 0.1]
# iou = [0.33, 0.14]
m.result().numpy()
0.33

compile() API 的用法:

model.compile(
  optimizer='sgd',
  loss='mse',
  metrics=[tf.keras.metrics.IoU(num_classes=2, target_class_id=[0])])

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.keras.metrics.IoU。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。