Keras 3 API 文件 / 指標 / 圖像分割指標

圖像分割指標

[來源]

IoU 類別

keras.metrics.IoU(
    num_classes,
    target_class_ids,
    name=None,
    dtype=None,
    ignore_class=None,
    sparse_y_true=True,
    sparse_y_pred=True,
    axis=-1,
)

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

公式

iou = true_positives / (true_positives + false_positives + false_negatives)

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

為了計算 IoU,預測會累積在混淆矩陣中,並依 sample_weight 加權,然後從中計算指標。

如果 sample_weightNone,權重預設為 1。使用 sample_weight 為 0 來遮罩值。

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

引數

  • num_classes:預測任務可能擁有的標籤數量。
  • target_class_ids:要傳回指標的目標類別 ID 的元組或列表。若要計算特定類別的 IoU,應提供包含單個 ID 值的列表(或元組)。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,在分割圖中具有「void」類別(通常為 -1 或 255)。預設情況下(ignore_class=None),所有類別都會被考慮。
  • sparse_y_true:標籤是否使用整數或密集浮點向量編碼。如果為 False,則使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • sparse_y_pred:預測是否使用整數或密集浮點向量編碼。如果為 False,則使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • axis:(選用)-1 是包含 logits 的維度。預設為 -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))
>>> # iou = [0.33, 0.33]
>>> m = keras.metrics.IoU(num_classes=2, target_class_ids=[0])
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
>>> m.result()
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()
0.33333334

compile() API 一起使用

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

[來源]

BinaryIoU 類別

keras.metrics.BinaryIoU(
    target_class_ids=(0, 1), threshold=0.5, name=None, dtype=None
)

計算類別 0 和/或 1 的 Intersection-Over-Union 指標。

公式

iou = true_positives / (true_positives + false_positives + false_negatives)

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

為了計算 IoU,預測會累積在混淆矩陣中,並依 sample_weight 加權,然後從中計算指標。

如果 sample_weightNone,權重預設為 1。使用 sample_weight 為 0 來遮罩值。

此類別可用於計算二元分類任務的 IoU,其中預測以 logits 形式提供。首先,將 threshold 應用於預測值,使得低於 threshold 的值轉換為類別 0,而高於 threshold 的值轉換為類別 1。

然後計算類別 0 和 1 的 IoU,並傳回由 target_class_ids 指定的類別的 IoU 平均值。

注意:當 threshold=0 時,此指標的行為與 IoU 相同。

引數

  • target_class_ids:要傳回指標的目標類別 ID 的元組或列表。選項為 [0][1][0, 1]。使用 [0](或 [1]),傳回類別 0(或分別為類別 1)的 IoU 指標。使用 [0, 1],傳回兩個類別的 IoU 平均值。
  • threshold:應用於預測 logits 的閾值,以將其轉換為預測類別 0(如果 logit 低於 threshold)或預測類別 1(如果 logit 高於 threshold)。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。

範例

>>> m = keras.metrics.BinaryIoU(target_class_ids=[0, 1], threshold=0.3)
>>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7])
>>> m.result()
0.33333334
>>> m.reset_state()
>>> m.update_state([0, 1, 0, 1], [0.1, 0.2, 0.4, 0.7],
...                sample_weight=[0.2, 0.3, 0.4, 0.1])
>>> # cm = [[0.2, 0.4],
>>> #        [0.3, 0.1]]
>>> # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5],
>>> # true_positives = [0.2, 0.1]
>>> # iou = [0.222, 0.125]
>>> m.result()
0.17361112

compile() API 一起使用

model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[keras.metrics.BinaryIoU(
        target_class_ids=[0],
        threshold=0.5
    )]
)

[來源]

OneHotIoU 類別

keras.metrics.OneHotIoU(
    num_classes,
    target_class_ids,
    name=None,
    dtype=None,
    ignore_class=None,
    sparse_y_pred=False,
    axis=-1,
)

計算 one-hot 編碼標籤的 Intersection-Over-Union 指標。

公式

iou = true_positives / (true_positives + false_positives + false_negatives)

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

為了計算 IoU,預測會累積在混淆矩陣中,並依 sample_weight 加權,然後從中計算指標。

如果 sample_weightNone,權重預設為 1。使用 sample_weight 為 0 來遮罩值。

此類別可用於計算多類別分類任務的 IoU,其中標籤是 one-hot 編碼的(最後一個軸應具有每個類別一個維度)。請注意,預測也應具有相同的形狀。為了計算 IoU,首先通過在類別軸上取 argmax 將標籤和預測轉換回整數格式。然後,應用與基礎 IoU 類別相同的計算步驟。

請注意,如果標籤和預測中只有一個通道,則此類別與類別 IoU 相同。在這種情況下,請改用 IoU

此外,請確保 num_classes 等於資料中的類別數量,以避免在計算混淆矩陣時出現「標籤超出範圍」錯誤。

引數

  • num_classes:預測任務可能擁有的標籤數量。
  • target_class_ids:要傳回指標的目標類別 ID 的元組或列表。若要計算特定類別的 IoU,應提供包含單個 ID 值的列表(或元組)。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,在分割圖中具有「void」類別(通常為 -1 或 255)。預設情況下(ignore_class=None),所有類別都會被考慮。
  • sparse_y_pred:預測是否使用整數或密集浮點向量編碼。如果為 False,則使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • axis:(選用)包含 logits 的維度。預設為 -1

範例

>>> y_true = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
>>> y_pred = np.array([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
...                       [0.1, 0.4, 0.5]])
>>> sample_weight = [0.1, 0.2, 0.3, 0.4]
>>> m = keras.metrics.OneHotIoU(num_classes=3, target_class_ids=[0, 2])
>>> m.update_state(
...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
>>> # cm = [[0, 0, 0.2+0.4],
>>> #       [0.3, 0, 0],
>>> #       [0, 0, 0.1]]
>>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
>>> # true_positives = [0, 0, 0.1]
>>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
>>> # mean_iou = (0 / (0.3 + 0.6 - 0) + 0.1 / (0.7 + 0.1 - 0.1)) / 2
>>> m.result()
0.071

compile() API 一起使用

model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[keras.metrics.OneHotIoU(
        num_classes=3,
        target_class_id=[1]
    )]
)

[來源]

OneHotMeanIoU 類別

keras.metrics.OneHotMeanIoU(
    num_classes, name=None, dtype=None, ignore_class=None, sparse_y_pred=False, axis=-1
)

計算 one-hot 編碼標籤的平均 Intersection-Over-Union 指標。

公式

iou = true_positives / (true_positives + false_positives + false_negatives)

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

為了計算 IoU,預測會累積在混淆矩陣中,並依 sample_weight 加權,然後從中計算指標。

如果 sample_weightNone,權重預設為 1。使用 sample_weight 為 0 來遮罩值。

此類別可用於計算多類別分類任務的平均 IoU,其中標籤是 one-hot 編碼的(最後一個軸應具有每個類別一個維度)。請注意,預測也應具有相同的形狀。為了計算平均 IoU,首先通過在類別軸上取 argmax 將標籤和預測轉換回整數格式。然後,應用與基礎 MeanIoU 類別相同的計算步驟。

請注意,如果標籤和預測中只有一個通道,則此類別與類別 MeanIoU 相同。在這種情況下,請改用 MeanIoU

此外,請確保 num_classes 等於資料中的類別數量,以避免在計算混淆矩陣時出現「標籤超出範圍」錯誤。

引數

  • num_classes:預測任務可能擁有的標籤數量。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,在分割圖中具有「void」類別(通常為 -1 或 255)。預設情況下(ignore_class=None),所有類別都會被考慮。
  • sparse_y_pred:預測是否使用自然數或機率分佈向量編碼。如果為 False,則將使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • axis:(選用)包含 logits 的維度。預設為 -1

範例

>>> y_true = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 0, 0]])
>>> y_pred = np.array([[0.2, 0.3, 0.5], [0.1, 0.2, 0.7], [0.5, 0.3, 0.1],
...                       [0.1, 0.4, 0.5]])
>>> sample_weight = [0.1, 0.2, 0.3, 0.4]
>>> m = keras.metrics.OneHotMeanIoU(num_classes=3)
>>> m.update_state(
...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight)
>>> # cm = [[0, 0, 0.2+0.4],
>>> #       [0.3, 0, 0],
>>> #       [0, 0, 0.1]]
>>> # sum_row = [0.3, 0, 0.7], sum_col = [0.6, 0.3, 0.1]
>>> # true_positives = [0, 0, 0.1]
>>> # single_iou = true_positives / (sum_row + sum_col - true_positives))
>>> # mean_iou = (0 + 0 + 0.1 / (0.7 + 0.1 - 0.1)) / 3
>>> m.result()
0.048

compile() API 一起使用

model.compile(
    optimizer='sgd',
    loss='mse',
    metrics=[keras.metrics.OneHotMeanIoU(num_classes=3)])

[來源]

MeanIoU 類別

keras.metrics.MeanIoU(
    num_classes,
    name=None,
    dtype=None,
    ignore_class=None,
    sparse_y_true=True,
    sparse_y_pred=True,
    axis=-1,
)

計算平均 Intersection-Over-Union 指標。

公式

iou = true_positives / (true_positives + false_positives + false_negatives)

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

為了計算 IoU,預測會累積在混淆矩陣中,並依 sample_weight 加權,然後從中計算指標。

如果 sample_weightNone,權重預設為 1。使用 sample_weight 為 0 來遮罩值。

請注意,此類別首先計算所有個別類別的 IoU,然後傳回這些值的平均值。

引數

  • num_classes:預測任務可能擁有的標籤數量。必須提供此值,因為將分配維度 = [num_classes, num_classes] 的混淆矩陣。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,在分割圖中具有「void」類別(通常為 -1 或 255)。預設情況下(ignore_class=None),所有類別都會被考慮。
  • sparse_y_true:標籤是否使用整數或密集浮點向量編碼。如果為 False,則使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • sparse_y_pred:預測是否使用整數或密集浮點向量編碼。如果為 False,則使用 argmax 函數來確定每個樣本最有可能相關聯的標籤。
  • axis:(選用)包含 logits 的維度。預設為 -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 = keras.metrics.MeanIoU(num_classes=2)
>>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1])
>>> m.result()
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=[keras.metrics.MeanIoU(num_classes=2)])