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 的 tuple 或清單。若要計算特定類別的 IoU,應提供單一 ID 值的清單(或 tuple)。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,分割地圖中具有「無效」類別(通常為 -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 的 tuple 或清單。選項為 [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,
)

計算單熱編碼標籤的 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,其中標籤為單熱編碼(最後一個軸每個類別應有一個維度)。請注意,預測也應具有相同的形狀。為了計算 IoU,首先將標籤和預測透過在類別軸上取 argmax 轉換回整數格式。然後,會套用與基本 IoU 類別相同的計算步驟。

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

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

引數

  • num_classes:預測任務可能擁有的標籤數量。
  • target_class_ids:要傳回指標的目標類別 ID 的 tuple 或清單。若要計算特定類別的 IoU,應提供單一 ID 值的清單(或 tuple)。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,分割地圖中具有「無效」類別(通常為 -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
)

計算單熱編碼標籤的平均 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,其中標籤為單熱編碼(最後一個軸每個類別應有一個維度)。請注意,預測也應具有相同的形狀。為了計算平均 IoU,首先將標籤和預測透過在類別軸上取 argmax 轉換回整數格式。然後,會套用與基本 MeanIoU 類別相同的計算步驟。

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

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

引數

  • num_classes:預測任務可能擁有的標籤數量。
  • name:(選用)指標實例的字串名稱。
  • dtype:(選用)指標結果的資料類型。
  • ignore_class:選用整數。在指標計算期間要忽略的類別 ID。這在分割問題中很有用,例如,分割地圖中具有「無效」類別(通常為 -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。這在分割問題中很有用,例如,分割地圖中具有「無效」類別(通常為 -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)])