π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
import torch
from torch._dynamo.testing import CompileCounter
import kornia
from kornia.core._compat import torch_version_lt
from kornia.core.exceptions import BaseError
from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available
class TestMeanIoU(BaseTester):
def test_transposed_label_maps(self, device):
pred = torch.tensor([[[0, 1, 2], [2, 1, 0]], [[2, 0, 1], [1, 2, 0]]], device=device).transpose(1, 2)
target = torch.tensor([[[0, 2, 2], [1, 1, 0]], [[2, 0, 0], [1, 2, 1]]], device=device).transpose(1, 2)
expected = torch.tensor([[1, 1 / 3, 1 / 3], [1 / 3, 1 / 3, 1]], device=device, dtype=torch.float32)
assert not pred.is_contiguous()
assert not target.is_contiguous()
self.assert_close(kornia.metrics.mean_iou(pred, target, 3), expected)
def test_two_classes_perfect(self, device, dtype):
batch_size = 1
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
self.assert_close(mean_iou, mean_iou_real)
def test_two_classes_perfect_batch2(self, device, dtype):
batch_size = 2
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0], [1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
self.assert_close(mean_iou, mean_iou_real)
def test_two_classes(self, device, dtype):
batch_size = 1
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[0.75, 0.80]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
self.assert_close(mean_iou, mean_iou_real)
def test_four_classes_2d_perfect(self, device, dtype):
batch_size = 1
num_classes = 4
actual = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0, 1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
self.assert_close(mean_iou, mean_iou_real)
def test_four_classes_one_missing(self, device, dtype):
batch_size = 1
num_classes = 4
actual = torch.tensor(
[[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[0.0, 1.0, 0.5, 0.5]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
self.assert_close(mean_iou, mean_iou_real)
def test_exception_shape_mismatch(self, device, dtype):
pred = torch.zeros(1, 4, dtype=torch.long, device=device)
target = torch.zeros(1, 5, dtype=torch.long, device=device)
with pytest.raises(ValueError, match="same shape"):
kornia.metrics.mean_iou(pred, target, num_classes=2)
def test_exception_num_classes_too_small(self, device, dtype):
pred = torch.zeros(1, 4, dtype=torch.long, device=device)
with pytest.raises(ValueError, match="at least two"):
kornia.metrics.mean_iou(pred, pred, num_classes=1)
def test_exception_out_of_range(self, device, dtype):
# A prediction of num_classes was counted in another cell and gave an IoU for it (#5549).
pred = torch.tensor([[0, 3]], device=device, dtype=torch.long)
target = torch.tensor([[0, 0]], device=device, dtype=torch.long)
with pytest.raises(BaseError, match=r"Input pred must contain values in \[0, 3\)"):
kornia.metrics.mean_iou(pred, target, num_classes=3)
def test_uint8_labels_match_int64(self, device, dtype):
# The confusion matrix index wrapped in uint8 from num_classes = 17, so a perfect uint8 prediction reported
# an IoU of about 5e-7 for two classes that do not occur (#5549).
labels = torch.tensor([[[20, 0, 5], [5, 20, 0]]], device=device, dtype=torch.uint8)
actual = kornia.metrics.mean_iou(labels, labels, num_classes=21)
self.assert_close(actual, kornia.metrics.mean_iou(labels.long(), labels.long(), num_classes=21), rtol=0, atol=0)
self.assert_close(actual, torch.ones(1, 21, device=device, dtype=torch.float32))
def test_empty_batch(self, device, dtype):
num_classes = 3
pred = torch.zeros(0, 4, 4, device=device, dtype=torch.long)
target = torch.zeros(0, 4, 4, device=device, dtype=torch.long)
mean_iou = kornia.metrics.mean_iou(pred, target, num_classes)
assert mean_iou.shape == (0, num_classes)
assert mean_iou.dtype == torch.float32
assert mean_iou.device == pred.device
@pytest.mark.skipif(
torch_version_lt(2, 14, 0), reason="fullgraph tracing cannot capture the bincount before torch 2.14"
)
def test_trace_counts_like_eager(self, device, dtype):
# mean_iou counts through confusion_matrix, whose range check reads the data (#5037).
pred = torch.tensor([[0, 1, 2, 2], [1, 1, 0, 2]], device=device)
target = torch.tensor([[0, 2, 2, 1], [1, 0, 0, 2]], device=device)
counter = CompileCounter()
torch._dynamo.reset()
traced = torch.compile(kornia.metrics.mean_iou, backend=counter, fullgraph=True)
actual = traced(pred, target, num_classes=3)
self.assert_close(actual, kornia.metrics.mean_iou(pred, target, num_classes=3), rtol=0, atol=0)
assert counter.frame_count == 1
class TestMeanIoUBBox(BaseTester):
"""Tests for mean_iou_bbox with different box formats."""
@pytest.mark.parametrize(
"box_format,coordinates",
[
("xyxy", [[0, 0, 512, 512], [256, 0, 768, 512]]),
("xywh", [[0, 0, 512, 512], [256, 0, 512, 512]]),
("cxcywh", [[256, 256, 512, 512], [512, 256, 512, 512]]),
],
)
def test_image_sized_boxes(self, box_format, coordinates, device, dtype):
boxes = torch.tensor(coordinates, device=device, dtype=dtype)
original = boxes.clone()
actual = kornia.metrics.mean_iou_bbox(boxes, boxes, box_format)
# Each box has area 512**2; their intersection is 256*512 and their union is 768*512.
expected = torch.tensor([[1.0, 1.0 / 3.0], [1.0 / 3.0, 1.0]], device=device, dtype=dtype)
self.assert_close(actual, expected, atol=0.0, rtol=0.0)
self.assert_close(boxes, original, atol=0.0, rtol=0.0)
assert actual.dtype == dtype
assert actual.device == device
@pytest.mark.parametrize("empty_shape", [(0, 4), (2, 4)])
def test_empty_box_set(self, empty_shape, device, dtype):
empty = torch.empty(0, 4, device=device, dtype=dtype)
boxes = torch.tensor([[0, 0, 512, 512], [256, 0, 768, 512]], device=device, dtype=dtype)
other = empty if empty_shape[0] == 0 else boxes
actual = kornia.metrics.mean_iou_bbox(empty, other)
assert actual.shape == (0, empty_shape[0])
assert actual.dtype == dtype
def test_image_sized_boxes_backward(self, device, dtype):
first_coordinates = [[0, 0, 512, 512]]
second_coordinates = [[256, 128, 768, 640]]
first = torch.tensor(first_coordinates, device=device, dtype=dtype, requires_grad=True)
second = torch.tensor(second_coordinates, device=device, dtype=dtype)
actual = kornia.metrics.mean_iou_bbox(first, second)
reference_input = torch.tensor(first_coordinates, dtype=torch.float64, requires_grad=True)
reference = kornia.metrics.mean_iou_bbox(reference_input, torch.tensor(second_coordinates, dtype=torch.float64))
actual.sum().backward()
reference.sum().backward()
assert first.grad is not None
assert reference_input.grad is not None
self.assert_close(first.grad, reference_input.grad.to(dtype=dtype).to(device=device), atol=1e-6, rtol=1e-3)
@pytest.mark.parametrize("swap", [False, True])
def test_areas_are_not_rounded_to_the_box_dtype(self, swap, device, dtype):
# In float16 the areas 40000 and 40800 sum past the maximum, which gave an IoU of 0. Computed in bfloat16 the
# IoU was 0.9765625, and rounding only the taller box's area gives 0.984375, instead of 0.98046875. Every
# coordinate is exact in both dtypes.
square = torch.tensor([[0, 0, 200, 200]], device=device, dtype=dtype)
taller = torch.tensor([[0, 0, 200, 204]], device=device, dtype=dtype)
boxes_1, boxes_2 = (taller, square) if swap else (square, taller)
actual = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2)
expected = torch.tensor([[40000 / 40800]], device=device, dtype=dtype)
self.assert_close(actual, expected, atol=0.0, rtol=0.0)
def test_integer_boxes_return_float32(self, device):
boxes = torch.tensor([[0, 0, 512, 512], [256, 0, 768, 512]], device=device)
actual = kornia.metrics.mean_iou_bbox(boxes, boxes)
assert actual.dtype == torch.float32
expected = torch.tensor([[1.0, 1.0 / 3.0], [1.0 / 3.0, 1.0]], device=device)
self.assert_close(actual, expected, atol=0.0, rtol=0.0)
def test_bbox_xyxy_format(self, device, dtype):
"""Test XYXY format (original behavior)."""
boxes_1 = torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]], device=device, dtype=dtype)
boxes_2 = torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]], device=device, dtype=dtype)
iou = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format="xyxy")
expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)
self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)
def test_bbox_xywh_format(self, device, dtype):
"""Test XYWH format."""
# Same boxes as xyxy test, but in xywh format
boxes_1_xywh = torch.tensor([[40, 40, 20, 20], [30, 40, 20, 20]], device=device, dtype=dtype)
boxes_2_xywh = torch.tensor([[40, 50, 20, 20], [30, 40, 10, 10]], device=device, dtype=dtype)
iou = kornia.metrics.mean_iou_bbox(boxes_1_xywh, boxes_2_xywh, box_format="xywh")
expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)
self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)
def test_bbox_cxcywh_format(self, device, dtype):
"""Test CXCYWH format."""
# Same boxes as xyxy test, but in cxcywh format
boxes_1_cxcywh = torch.tensor([[50, 50, 20, 20], [40, 50, 20, 20]], device=device, dtype=dtype)
boxes_2_cxcywh = torch.tensor([[50, 60, 20, 20], [35, 45, 10, 10]], device=device, dtype=dtype)
iou = kornia.metrics.mean_iou_bbox(boxes_1_cxcywh, boxes_2_cxcywh, box_format="cxcywh")
expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)
self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)
def test_bbox_format_consistency(self, device, dtype):
"""Test that all formats produce same results for equivalent boxes."""
# Define same boxes in three formats
boxes_xyxy = torch.tensor([[10, 10, 20, 20]], device=device, dtype=dtype)
boxes_xywh = torch.tensor([[10, 10, 10, 10]], device=device, dtype=dtype)
boxes_cxcywh = torch.tensor([[15, 15, 10, 10]], device=device, dtype=dtype)
iou_xyxy = kornia.metrics.mean_iou_bbox(boxes_xyxy, boxes_xyxy, box_format="xyxy")
iou_xywh = kornia.metrics.mean_iou_bbox(boxes_xywh, boxes_xywh, box_format="xywh")
iou_cxcywh = kornia.metrics.mean_iou_bbox(boxes_cxcywh, boxes_cxcywh, box_format="cxcywh")
# All should give perfect IoU (1.0)
expected = torch.tensor([[1.0]], device=device, dtype=dtype)
self.assert_close(iou_xyxy, expected)
self.assert_close(iou_xywh, expected)
self.assert_close(iou_cxcywh, expected)
def test_bbox_invalid_format(self, device, dtype):
"""Test that invalid format raises ValueError."""
boxes = torch.tensor([[10, 10, 20, 20]], device=device, dtype=dtype)
with pytest.raises(ValueError, match="Unsupported box format"):
kornia.metrics.mean_iou_bbox(boxes, boxes, box_format="invalid")
def test_bbox_default_format(self, device, dtype):
"""Test that default format is xyxy."""
boxes_1 = torch.tensor([[40, 40, 60, 60]], device=device, dtype=dtype)
boxes_2 = torch.tensor([[40, 50, 60, 70]], device=device, dtype=dtype)
iou_default = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2)
iou_explicit = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format="xyxy")
self.assert_close(iou_default, iou_explicit)
@pytest.mark.parametrize("box_format", ["xyxy", "xywh", "cxcywh"])
@pytest.mark.parametrize(
"int_dtype,side",
[
# The area side**2 is outside the dtype's range in every case: 400 > 255 and 127, 40000 > 32767, and
# 512**2 is a multiple of 2**16, which wrapped the area, the intersection and the union to 0 (NaN IoU).
(torch.uint8, 20),
(torch.int8, 20),
(torch.int16, 200),
(torch.int16, 512),
(torch.int32, 200),
(torch.int64, 200),
],
)
def test_integer_boxes_do_not_overflow(self, int_dtype, side, box_format, device):
half = side // 2
coordinates = {
"xyxy": [[0, 0, side, side], [half, 0, side + half, side]],
"xywh": [[0, 0, side, side], [half, 0, side, side]],
"cxcywh": [[half, half, side, side], [side, half, side, side]],
}[box_format]
boxes = torch.tensor(coordinates, device=device, dtype=int_dtype)
original = boxes.clone()
actual = kornia.metrics.mean_iou_bbox(boxes, boxes, box_format)
# The same boxes held as int64 never wrap, so they are the reference for every narrower integer dtype.
reference = kornia.metrics.mean_iou_bbox(boxes.to(torch.int64), boxes.to(torch.int64), box_format)
expected = torch.tensor([[1.0, 1.0 / 3.0], [1.0 / 3.0, 1.0]], device=device)
self.assert_close(actual, reference, atol=0.0, rtol=0.0)
self.assert_close(actual, expected, atol=0.0, rtol=0.0)
self.assert_close(boxes, original, atol=0.0, rtol=0.0)
assert actual.dtype == torch.float32
assert actual.device == device
def test_uint8_xywh_corner_does_not_wrap_during_conversion(self, device):
# x + w is 300 and 300, past the uint8 maximum. The overflow is in the xywh -> xyxy conversion itself, so the
# integer boxes have to be promoted before it. The true IoU is 500 / 1000.
boxes_1 = torch.tensor([[200, 0, 100, 10]], device=device, dtype=torch.uint8)
boxes_2 = torch.tensor([[250, 0, 50, 10]], device=device, dtype=torch.uint8)
actual = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, "xywh")
self.assert_close(actual, torch.tensor([[0.5]], device=device), atol=0.0, rtol=0.0)
def test_integer_boxes_are_validated_without_wraparound(self, device):
# 5 - 10 wraps to 251 in uint8, so an inverted box used to pass the validation and return a plausible IoU.
inverted = torch.tensor([[10, 10, 5, 5]], device=device, dtype=torch.uint8)
valid = torch.tensor([[0, 0, 4, 4]], device=device, dtype=torch.uint8)
with pytest.raises(AssertionError, match="Boxes_1 contains invalid boxes"):
kornia.metrics.mean_iou_bbox(inverted, valid)
with pytest.raises(AssertionError, match="Boxes_2 contains invalid boxes"):
kornia.metrics.mean_iou_bbox(valid, inverted)
# 100 - (-100) wraps to -56 in int8, so a valid wide box used to be rejected as invalid.
wide = torch.tensor([[-100, 0, 100, 10]], device=device, dtype=torch.int8)
self.assert_close(kornia.metrics.mean_iou_bbox(wide, wide), torch.tensor([[1.0]], device=device))
def test_bool_boxes(self, device):
# Subtracting bool tensors raises in torch, so bool boxes used to fail. They are 0/1 coordinates.
boxes_1 = torch.tensor([[False, False, True, True]], device=device)
boxes_2 = torch.tensor([[False, False, True, True]], device=device)
actual = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2)
assert actual.dtype == torch.float32
self.assert_close(actual, torch.tensor([[1.0]], device=device))
@pytest.mark.skipif(not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
def test_trace_boxes_like_eager(self, device, dtype):
# The box-validity check reads the data, so any capture has to skip it, not only export (#5037).
boxes_1 = torch.tensor([[0.0, 0.0, 4.0, 4.0], [1.0, 1.0, 3.0, 6.0]], device=device, dtype=dtype)
boxes_2 = torch.tensor([[2.0, 2.0, 6.0, 6.0]], device=device, dtype=dtype)
counter = CompileCounter()
torch._dynamo.reset()
traced = torch.compile(kornia.metrics.mean_iou_bbox, backend=counter, fullgraph=True)
actual = traced(boxes_1, boxes_2)
self.assert_close(actual, kornia.metrics.mean_iou_bbox(boxes_1, boxes_2))
assert counter.frame_count == 1
def test_jit_keeps_the_box_check(self, device, dtype):
# TorchScript cannot call is_compiling(); a scripted call keeps the box check, as an eager call does.
boxes_1 = torch.tensor([[0.0, 0.0, 4.0, 4.0], [1.0, 1.0, 3.0, 6.0]], device=device, dtype=dtype)
boxes_2 = torch.tensor([[2.0, 2.0, 6.0, 6.0]], device=device, dtype=dtype)
scripted = torch.jit.script(kornia.metrics.mean_iou_bbox)
self.assert_close(scripted(boxes_1, boxes_2), kornia.metrics.mean_iou_bbox(boxes_1, boxes_2))
with pytest.raises(torch.jit.Error, match="Boxes_1 contains invalid boxes"):
scripted(boxes_1.flip(-1), boxes_2)
class TestConventionsMeanIoU(BaseTester):
# The label maps of TestConventionsConfusionMatrix: confusion matrices [[2, 0, 1], [1, 3, 1], [0, 1, 3]] and
# [[3, 1, 0], [1, 2, 2], [0, 0, 3]] (rows = target).
PRED = [[[0, 0, 1, 2], [1, 1, 2, 2], [0, 2, 2, 1]], [[2, 2, 2, 2], [0, 1, 1, 0], [0, 0, 1, 2]]]
TARGET = [[[0, 1, 1, 2], [1, 1, 1, 2], [0, 0, 2, 2]], [[2, 2, 1, 1], [0, 0, 1, 0], [1, 0, 1, 2]]]
def test_convention_mean_iou_is_per_sample_per_class(self, device, dtype):
"""mean_iou returns the IoU of every class in every sample, (B, K) float32; it averages neither axis."""
pred = torch.tensor(self.PRED, device=device)
target = torch.tensor(self.TARGET, device=device)
# IoU_k = TP / (TP + FP + FN) per sample: sample 0 gives 2/4, 3/6, 3/6; sample 1 gives 3/5, 2/6, 3/5
expected = torch.tensor([[0.5, 0.5, 0.5], [0.6, 1.0 / 3.0, 0.6]], device=device)
iou = kornia.metrics.mean_iou(pred, target, 3)
assert iou.shape == (2, 3)
assert iou.dtype == torch.float32
self.assert_close(iou, expected)
# relabel: renaming class c to perm[c] in both maps permutes the columns
perm = torch.tensor([2, 0, 1], device=device)
self.assert_close(kornia.metrics.mean_iou(perm[pred], perm[target], 3), expected[:, torch.argsort(perm)])
def test_convention_mean_iou_absent_class_scores_one(self, device, dtype):
"""A class in neither map scores (0 + eps) / (0 + eps) = 1; a class only predicted scores about 0."""
pred = torch.tensor([[0, 0, 1, 1]], device=device)
target = torch.tensor([[0, 1, 1, 1]], device=device)
# classes 0 and 1: 1/2 and 2/3; class 2 is absent from both maps. scikit-learn 1.9.0 jaccard_score gives 0 there
# (with a warning) and torchmetrics 1.9.0 leaves it out of its macro average.
self.assert_close(
kornia.metrics.mean_iou(pred, target, 3), torch.tensor([[0.5, 2.0 / 3.0, 1.0]], device=device)
)
# a larger num_classes appends absent classes at 1, which raises the class average (0.7222 -> 0.7917)
self.assert_close(kornia.metrics.mean_iou(pred, target, 4)[0, 2:], torch.ones(2, device=device))
# the 1 comes from eps: eps=0 leaves 0 / 0
assert torch.isnan(kornia.metrics.mean_iou(pred, target, 3, eps=0.0)[0, 2])
# a class predicted once and never in the target scores eps / (1 + eps)
predicted_only = kornia.metrics.mean_iou(torch.tensor([[0, 2, 1, 1]], device=device), target, 3)
assert 0.0 < predicted_only[0, 2] < 1e-5
def test_convention_mean_iou_bbox_is_pairwise_and_exclusive(self, device, dtype):
"""mean_iou_bbox returns the (N1, N2) IoU of every pair; boxes are exclusive, area (x2 - x1) * (y2 - y1)."""
boxes_1 = torch.tensor([[0.0, 0.0, 2.0, 2.0], [10.0, 20.0, 40.0, 30.0]], device=device, dtype=dtype)
boxes_2 = torch.tensor(
[[1.0, 1.0, 3.0, 3.0], [20.0, 20.0, 40.0, 30.0], [10.0, 20.0, 40.0, 25.0]], device=device, dtype=dtype
)
# [0, 0, 2, 2] and [1, 1, 3, 3] overlap in 1 of 4 + 4 - 1 = 7 (the VOC devkit's inclusive +1 areas give 4 / 14);
# the 30 x 10 box covers the 20 x 10 one (2 / 3) and the 30 x 5 one (1 / 2). torchvision 0.29.0
# box_iou(boxes_1, boxes_2) returns the same matrix.
expected = torch.tensor([[1.0 / 7.0, 0.0, 0.0], [0.0, 2.0 / 3.0, 0.5]], device=device, dtype=dtype)
iou = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2)
assert iou.shape == (2, 3)
assert iou.dtype == dtype
self.assert_close(iou, expected)
# swapping the two sets transposes the matrix
self.assert_close(kornia.metrics.mean_iou_bbox(boxes_2, boxes_1), expected.T)
# the same boxes in the two other formats give the same matrix
def to_xywh(boxes):
return torch.cat([boxes[:, :2], boxes[:, 2:] - boxes[:, :2]], 1)
def to_cxcywh(boxes):
return torch.cat([(boxes[:, :2] + boxes[:, 2:]) / 2, boxes[:, 2:] - boxes[:, :2]], 1)
self.assert_close(kornia.metrics.mean_iou_bbox(to_xywh(boxes_1), to_xywh(boxes_2), "xywh"), expected)
self.assert_close(kornia.metrics.mean_iou_bbox(to_cxcywh(boxes_1), to_cxcywh(boxes_2), "cxcywh"), expected)
class TestHalfBoxConversion(BaseTester):
@pytest.mark.parametrize("box_format", ["xywh", "cxcywh"])
def test_realistic_box_sizes(self, device, dtype, box_format):
# A real-input pair from #6204: widths/heights are tens of pixels, not sub-spacing boxes.
boxes = torch.tensor(
[[70.375, 289.5, 39.0625, 22.375], [55.65625, 290.5, 54.375, 30.03125]],
dtype=torch.float64,
)
if box_format == "cxcywh":
boxes[:, :2] += boxes[:, 2:] / 2
boxes = boxes.to(device=device, dtype=dtype)
# Build corners independently in double precision from the SAME quantized input boxes.
reference = boxes.cpu().double()
origin = reference[:, :2]
if box_format == "cxcywh":
origin = origin - reference[:, 2:] / 2
end = origin + reference[:, 2:]
overlap = (torch.minimum(end[0], end[1]) - torch.maximum(origin[0], origin[1])).clamp_min(0).prod()
area = reference[:, 2:].prod(dim=-1)
expected = (overlap / (area.sum() - overlap)).to(device=device, dtype=dtype).reshape(1, 1)
actual = kornia.metrics.mean_iou_bbox(boxes[:1], boxes[1:], box_format)
assert actual.dtype == dtype
self.assert_close(actual, expected, rtol=0, atol=0)
@pytest.mark.parametrize("box_format", ["xywh", "cxcywh"])
def test_small_width_at_large_position(self, device, dtype, box_format):
boxes_1 = torch.tensor([[2048.0, 0.0, 1.0, 2.0]], device=device, dtype=dtype, requires_grad=True)
boxes_2 = torch.tensor([[2048.0, 1.0, 1.0, 2.0]], device=device, dtype=dtype, requires_grad=True)
# Both boxes have area 2, their overlap has area 1, and the union has area 3.
actual = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format)
assert actual.dtype == dtype
self.assert_close(actual, boxes_1.new_tensor([[1 / 3]]))
grad_1, grad_2 = torch.autograd.grad(actual.sum(), (boxes_1, boxes_2))
assert torch.isfinite(grad_1).all()
assert torch.isfinite(grad_2).all()
# Moving the first box vertically toward the second changes I / U by 4 / 9.
self.assert_close(grad_1[:, 1], boxes_1.new_tensor([4 / 9]))
self.assert_close(grad_2[:, 1], boxes_2.new_tensor([-4 / 9]))