Shortcuts

IoU#

ignite.metrics.IoU(cm, ignore_index=None)[source]#

Calculates Intersection over Union using ConfusionMatrix metric.

J(A,B)=ABAB\text{J}(A, B) = \frac{ \lvert A \cap B \rvert }{ \lvert A \cup B \rvert }
Parameters
  • cm (ConfusionMatrix) – instance of confusion matrix metric

  • ignore_index (Optional[int]) – index to ignore, e.g. background index

Returns

MetricsLambda

Return type

MetricsLambda

Examples

cm = ConfusionMatrix(num_classes=3)
metric = IoU(cm)
metric.attach(default_evaluator, 'iou')
y_true = torch.Tensor([0, 1, 0, 1, 2]).long()
y_pred = torch.Tensor([
    [0.0, 1.0, 0.0],
    [0.0, 1.0, 0.0],
    [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.0, 1.0, 0.0],
])
state = default_evaluator.run([[y_pred, y_true]])
print(state.metrics['iou'])
tensor([0.5000, 0.5000, 0.0000], dtype=torch.float64)