Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Update tensor equality tests to use`assert_close()` (#2233)

Thien Tran2811fc304a13
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 5cbfc8236..e37261bec 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -48,7 +48,7 @@ from kornia.augmentation import (
from kornia.augmentation._2d.base import AugmentationBase2D
from kornia.constants import Resample, pi
from kornia.geometry import transform_points
-from kornia.testing import BaseTester, default_with_one_parameter_changed
+from kornia.testing import BaseTester, assert_close, default_with_one_parameter_changed
from kornia.utils import create_meshgrid
from kornia.utils.helpers import _torch_inverse_cast
@@ -195,7 +195,7 @@ class CommonTests(BaseTester):
augmentation = self._create_augmentation_from_params(**params, p=0.0)
test_input = torch.rand((2, 3, 4, 5), device=self.device, dtype=self.dtype)
output = augmentation(test_input)
- assert (output == test_input).all()
+ assert_close(output, test_input)
def _test_random_p_1_implementation(self, input_tensor, expected_output, params, expected_transformation=None):
augmentation = self._create_augmentation_from_params(**params, p=1.0)
@@ -684,33 +684,33 @@ class TestRandomHorizontalFlip:
assert str(f) == repr
def test_random_hflip(self, device, dtype):
- f = RandomHorizontalFlip(p=1.0)
- f1 = RandomHorizontalFlip(p=0.0)
+ f = RandomHorizontalFlip(p=1.0, keepdim=True)
+ f1 = RandomHorizontalFlip(p=0.0, keepdim=True)
input = torch.tensor(
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]], device=device, dtype=dtype
- ) # 3 x 4
+ [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]]], device=device, dtype=dtype
+ ) # 1 x 3 x 4
expected = torch.tensor(
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]], device=device, dtype=dtype
- ) # 3 x 4
+ [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]]], device=device, dtype=dtype
+ ) # 1 x 3 x 4
expected = expected.to(device)
expected_transform = torch.tensor(
- [[-1.0, 0.0, 3.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype
- ) # 3 x 3
+ [[[-1.0, 0.0, 3.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype
+ ) # 1 x 3 x 3
identity = torch.tensor(
- [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype
- ) # 3 x 3
+ [[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype
+ ) # 1 x 3 x 3
- assert (f(input) == expected).all()
- assert (f.transform_matrix == expected_transform).all()
- assert (f1(input) == input).all()
- assert (f1.transform_matrix == identity).all()
- assert (f.inverse(expected) == input).all()
- assert (f1.inverse(expected) == expected).all()
+ assert_close(f(input), expected)
+ assert_close(f.transform_matrix, expected_transform)
+ assert_close(f1(input), input)
+ assert_close(f1.transform_matrix, identity)
+ assert_close(f.inverse(expected), input)
+ assert_close(f1.inverse(expected), expected)
def test_batch_random_hflip(self, device, dtype):
f = RandomHorizontalFlip(p=1.0)
@@ -737,19 +737,19 @@ class TestRandomHorizontalFlip:
expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 3 x 3
identity = identity.repeat(5, 1, 1) # 5 x 3 x 3
- assert (f(input) == expected).all()
- assert (f.transform_matrix == expected_transform).all()
- assert (f1(input) == input).all()
- assert (f1.transform_matrix == identity).all()
- assert (f.inverse(expected) == input).all()
- assert (f1.inverse(expected) == expected).all()
+ assert_close(f(input), expected)
+ assert_close(f.transform_matrix, expected_transform)
+ assert_close(f1(input), input)
+ assert_close(f1.transform_matrix, identity)
+ assert_close(f.inverse(expected), input)
+ assert_close(f1.inverse(expected), expected)
def test_same_on_batch(self, device, dtype):
f = RandomHorizontalFlip(p=0.5, same_on_batch=True)
input = torch.eye(3, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
- assert (f.inverse(res) == input).all()
+ assert_close(res[0], res[1])
+ assert_close(f.inverse(res), input)
def test_sequential(self, device, dtype):
f = AugmentationSequential(RandomHorizontalFlip(p=1.0), RandomHorizontalFlip(p=1.0))
@@ -765,9 +765,9 @@ class TestRandomHorizontalFlip:
expected_transform_1 = expected_transform @ expected_transform
out = f(input)
- assert (out == input).all()
- assert (f.transform_matrix == expected_transform_1).all()
- assert (f.inverse(out) == input).all()
+ assert_close(out, input)
+ assert_close(f.transform_matrix, expected_transform_1)
+ assert_close(f.inverse(out), input)
def test_random_hflip_coord_check(self, device, dtype):
f = RandomHorizontalFlip(p=1.0)
@@ -801,7 +801,7 @@ class TestRandomHorizontalFlip:
# Tensors must have the same shapes and values
assert output.shape == expected_output.shape
- assert (output == expected_output).all()
+ assert_close(output, expected_output)
# Transformed indices must not be out of bound
assert (
torch.torch.logical_and(result_coordinates[0, 0, :] >= 0, result_coordinates[0, 0, :] < input.shape[-1])
@@ -812,10 +812,10 @@ class TestRandomHorizontalFlip:
# Values in the output tensor at the places of transformed indices must
# have the same value as the input tensor has at the corresponding
# positions
- assert (
- output[..., result_coordinates[0, 1, :], result_coordinates[0, 0, :]]
- == input[..., input_coordinates[0, 1, :], input_coordinates[0, 0, :]]
- ).all()
+ assert_close(
+ output[..., result_coordinates[0, 1, :], result_coordinates[0, 0, :]],
+ input[..., input_coordinates[0, 1, :], input_coordinates[0, 0, :]],
+ )
def test_gradcheck(self, device, dtype):
input = torch.rand((3, 3), device=device, dtype=dtype) # 3 x 3
@@ -892,8 +892,8 @@ class TestRandomVerticalFlip(BaseTester):
f = RandomVerticalFlip(p=0.5, same_on_batch=True)
input = torch.eye(3, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
- assert (f.inverse(res) == input).all()
+ assert_close(res[0], res[1])
+ assert_close(f.inverse(res), input)
def test_sequential(self, device, dtype):
f = AugmentationSequential(RandomVerticalFlip(p=1.0), RandomVerticalFlip(p=1.0))
@@ -943,7 +943,7 @@ class TestRandomVerticalFlip(BaseTester):
# Tensors must have the same shapes and values
assert output.shape == expected_output.shape
- assert (output == expected_output).all()
+ assert_close(output, expected_output)
# Transformed indices must not be out of bound
assert (
torch.torch.logical_and(result_coordinates[0, 0, :] >= 0, result_coordinates[0, 0, :] < input.shape[-1])
@@ -954,10 +954,10 @@ class TestRandomVerticalFlip(BaseTester):
# Values in the output tensor at the places of transformed indices must
# have the same value as the input tensor has at the corresponding
# positions
- assert (
- output[..., result_coordinates[0, 1, :], result_coordinates[0, 0, :]]
- == input[..., input_coordinates[0, 1, :], input_coordinates[0, 0, :]]
- ).all()
+ assert_close(
+ output[..., result_coordinates[0, 1, :], result_coordinates[0, 0, :]],
+ input[..., input_coordinates[0, 1, :], input_coordinates[0, 0, :]],
+ )
@pytest.mark.skip(reason="not implemented yet")
def test_cardinality(self, device, dtype):
@@ -1019,7 +1019,7 @@ class TestColorJiggle(BaseTester):
f = ColorJiggle(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1, same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_brightness(self, device, dtype):
return torch.tensor(
@@ -1346,7 +1346,7 @@ class TestColorJitter(BaseTester):
f = ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1, same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_brightness(self, device, dtype):
return torch.tensor(
@@ -1652,7 +1652,7 @@ class TestRandomBrightness(BaseTester):
f = RandomBrightness(brightness=(0.5, 1.5), same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_brightness(self, device, dtype):
return torch.tensor(
@@ -1753,7 +1753,7 @@ class TestRandomContrast(BaseTester):
f = RandomContrast(contrast=(0.5, 1.5), same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_contrast(self, device, dtype):
return torch.tensor(
@@ -1854,7 +1854,7 @@ class TestRandomHue(BaseTester):
f = RandomHue(hue=(-0.5, 0.5), same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_hue(self, device, dtype):
return torch.tensor(
@@ -1963,7 +1963,7 @@ class TestRandomSaturation(BaseTester):
f = RandomSaturation(saturation=(0.5, 1.5), same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_saturation(self, device, dtype):
return torch.tensor(
@@ -2074,7 +2074,7 @@ class TestRectangleRandomErasing(BaseTester):
f = RandomErasing(erase_scale_range, aspect_ratio_range, same_on_batch=True, p=0.5)
input = torch.rand(shape).unsqueeze(dim=0).repeat(2, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_gradcheck(self, device, dtype):
# test parameters
@@ -2126,7 +2126,7 @@ class TestRandomGamma(BaseTester):
f = RandomGamma(gamma=(0.5, 2.0), gain=(0.5, 0.5), same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def _get_expected_gamma(self, device, dtype):
return torch.tensor(
@@ -2258,7 +2258,7 @@ class TestRandomGrayscale(BaseTester):
f = RandomGrayscale(p=0.5, same_on_batch=True)
input = torch.eye(3, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_opencv_true(self, device, dtype):
data = torch.tensor(
@@ -2666,7 +2666,7 @@ class TestRandomRotation(BaseTester):
f = RandomRotation(degrees=40, same_on_batch=True)
input = torch.eye(6, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_sequential(self, device, dtype):
torch.manual_seed(0) # for random reproductibility
@@ -2790,7 +2790,7 @@ class TestRandomCrop(BaseTester):
f = RandomCrop(size=(2, 3), padding=1, same_on_batch=True, align_corners=True, p=1.0)
input = torch.eye(3, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_padding(self, device, dtype):
torch.manual_seed(42)
@@ -3294,7 +3294,7 @@ class TestRandomEqualize(BaseTester):
input = torch.eye(4, device=device, dtype=dtype)
input = input.unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_gradcheck(self, device, dtype):
torch.manual_seed(0) # for random reproductibility
@@ -3680,7 +3680,7 @@ class TestRandomElasticTransform:
f = RandomElasticTransform(p=1.0, same_on_batch=True)
input = torch.eye(3, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_mask_transform(self, device, dtype):
torch.manual_seed(0)
@@ -3698,7 +3698,7 @@ class TestRandomElasticTransform:
# The transformed values are fine if we use mask input type
labels_transformed = compose(features, labels, data_keys=["input", "mask"])[1]
- assert torch.all(labels_transformed.unique() == torch.tensor([0, 10], dtype=dtype, device=device))
+ assert_close(labels_transformed.unique(), torch.tensor([0, 10], dtype=dtype, device=device))
class TestRandomThinPlateSpline:
@@ -3721,7 +3721,7 @@ class TestPadTo:
aug = PadTo(size=(4, 5))
out = aug(img)
assert out.shape == (1, 1, 4, 5)
- assert (aug.inverse(out) == img).all()
+ assert_close(aug.inverse(out), img)
class TestResize:
diff --git a/test/augmentation/test_augmentation_3d.py b/test/augmentation/test_augmentation_3d.py
index 59656801c..c221c8a8d 100644
--- a/test/augmentation/test_augmentation_3d.py
+++ b/test/augmentation/test_augmentation_3d.py
@@ -29,42 +29,41 @@ class TestRandomHorizontalFlip3D:
assert str(f) == repr
def test_random_hflip(self, device):
- f = RandomHorizontalFlip3D(p=1.0)
- f1 = RandomHorizontalFlip3D(p=0.0)
+ f = RandomHorizontalFlip3D(p=1.0, keepdim=True)
+ f1 = RandomHorizontalFlip3D(p=0.0, keepdim=True)
input = torch.tensor(
[
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- ]
- ) # 2 x 3 x 4
-
- input = input.to(device)
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ ]
+ ],
+ device=device,
+ ) # 1 x 2 x 3 x 4
expected = torch.tensor(
[
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
- ]
- ) # 2 x 3 x 4
-
- expected = expected.to(device)
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
+ ]
+ ],
+ device=device,
+ ) # 1 x 2 x 3 x 4
expected_transform = torch.tensor(
- [[-1.0, 0.0, 0.0, 3.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
- ) # 4 x 4
-
- expected_transform = expected_transform.to(device)
+ [[[-1.0, 0.0, 0.0, 3.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
+ ) # 1 x 4 x 4
identity = torch.tensor(
- [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
- ) # 4 x 4
- identity = identity.to(device)
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
+ ) # 1 x 4 x 4
- assert (f(input) == expected).all()
- assert (f.transform_matrix == expected_transform).all()
- assert (f1(input) == input).all()
- assert (f1.transform_matrix == identity).all()
+ assert_close(f(input), expected)
+ assert_close(f.transform_matrix, expected_transform)
+ assert_close(f1(input), input)
+ assert_close(f1.transform_matrix, identity)
def test_batch_random_hflip(self, device):
f = RandomHorizontalFlip3D(p=1.0)
@@ -90,19 +89,19 @@ class TestRandomHorizontalFlip3D:
expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
- assert (f(input) == expected).all()
- assert (f.transform_matrix == expected_transform).all()
+ assert_close(f(input), expected)
+ assert_close(f.transform_matrix, expected_transform)
def test_same_on_batch(self, device):
f = RandomHorizontalFlip3D(p=0.5, same_on_batch=True)
input = torch.eye(3, device=device).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_sequential(self, device):
f = AugmentationSequential(RandomHorizontalFlip3D(p=1.0), RandomHorizontalFlip3D(p=1.0))
- input = torch.tensor([[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]) # 1 x 1 x 3 x 3
+ input = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
input = input.to(device)
expected_transform = torch.tensor(
@@ -113,8 +112,8 @@ class TestRandomHorizontalFlip3D:
expected_transform_1 = expected_transform @ expected_transform
expected_transform_1 = expected_transform_1.to(device)
- assert (f(input) == input).all()
- assert (f.transform_matrix == expected_transform_1).all()
+ assert_close(f(input), input)
+ assert_close(f.transform_matrix, expected_transform_1)
def test_gradcheck(self, device):
input = torch.rand((1, 3, 3)).to(device) # 3 x 3
@@ -212,7 +211,7 @@ class TestRandomVerticalFlip3D:
f = RandomVerticalFlip3D(p=0.5, same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_sequential(self, device):
f = AugmentationSequential(RandomVerticalFlip3D(p=1.0), RandomVerticalFlip3D(p=1.0))
@@ -337,7 +336,7 @@ class TestRandomDepthicalFlip3D:
f = RandomDepthicalFlip3D(p=0.5, same_on_batch=True)
input = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_sequential(self, device):
f = AugmentationSequential(RandomDepthicalFlip3D(p=1.0), RandomDepthicalFlip3D(p=1.0))
@@ -545,7 +544,7 @@ class TestRandomRotation3D:
f = RandomRotation3D(degrees=40, same_on_batch=True)
input = torch.eye(6, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 6, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_sequential(self, device, dtype):
torch.manual_seed(24) # for random reproductibility
@@ -702,7 +701,7 @@ class TestRandomCrop3D:
.repeat(2, 3, 5, 1, 1)
)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
@pytest.mark.parametrize("padding", [1, (1, 1, 1), (1, 1, 1, 1, 1, 1)])
def test_padding_batch(self, padding, device, dtype):
@@ -868,7 +867,7 @@ class TestRandomEqualize3D:
input = torch.eye(4, device=device, dtype=dtype)
input = input.unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
res = f(input)
- assert (res[0] == res[1]).all()
+ assert_close(res[0], res[1])
def test_gradcheck(self, device, dtype):
torch.manual_seed(0) # for random reproductibility
@@ -915,5 +914,5 @@ class TestRandomAffine3D:
expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
- assert (f(tensor) == expected).all()
- assert (f.transform_matrix == expected_transform).all()
+ assert_close(f(tensor), expected)
+ assert_close(f.transform_matrix, expected_transform)
diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.py
index d1243acbb..29afd7f2b 100644
--- a/test/augmentation/test_augmentation_mix.py
+++ b/test/augmentation/test_augmentation_mix.py
@@ -18,7 +18,7 @@ class TestRandomMixUpV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
lam = torch.tensor([0.1320, 0.3074], device=device, dtype=dtype)
expected = torch.stack(
@@ -31,8 +31,8 @@ class TestRandomMixUpV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[:, 0] == label).all()
- assert (out_label[:, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()
+ assert_close(out_label[:, 0], label)
+ assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))
assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)
def test_random_mixup_p0(self, device, dtype):
@@ -59,7 +59,7 @@ class TestRandomMixUpV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
lam = torch.tensor([0.0, 0.0], device=device, dtype=dtype)
expected = input.clone()
@@ -67,8 +67,8 @@ class TestRandomMixUpV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[:, 0] == label).all()
- assert (out_label[:, 1] == torch.tensor([0, 1], device=device)).all()
+ assert_close(out_label[:, 0], label)
+ assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))
assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)
def test_random_mixup_same_on_batch(self, device, dtype):
@@ -78,7 +78,7 @@ class TestRandomMixUpV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
lam = torch.tensor([0.0885, 0.0885], device=device, dtype=dtype)
expected = torch.stack(
@@ -90,8 +90,8 @@ class TestRandomMixUpV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[:, 0] == label).all()
- assert (out_label[:, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()
+ assert_close(out_label[:, 0], label)
+ assert_close(out_label[:, 1], torch.tensor([0, 1], device=device, dtype=dtype))
assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)
@@ -108,7 +108,7 @@ class TestRandomCutMixV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
expected = torch.tensor(
[
@@ -122,9 +122,9 @@ class TestRandomCutMixV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[0, :, 0] == label).all()
- assert (out_label[0, :, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()
- assert (out_label[0, :, 2] == torch.tensor([0.5, 0.5], device=device, dtype=dtype)).all()
+ assert_close(out_label[0, :, 0], label)
+ assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))
+ assert_close(out_label[0, :, 2], torch.tensor([0.5, 0.5], device=device, dtype=dtype))
def test_random_mixup_p0(self, device, dtype):
torch.manual_seed(76)
@@ -141,7 +141,7 @@ class TestRandomCutMixV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label == exp_label).all()
+ assert_close(out_label, exp_label)
def test_random_mixup_beta0(self, device, dtype):
torch.manual_seed(76)
@@ -152,7 +152,7 @@ class TestRandomCutMixV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
expected = torch.tensor(
[
@@ -166,8 +166,8 @@ class TestRandomCutMixV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[0, :, 0] == label).all()
- assert (out_label[0, :, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()
+ assert_close(out_label[0, :, 0], label)
+ assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))
# cut area = 4 / 12
assert_close(out_label[0, :, 2], torch.tensor([0.33333, 0.33333], device=device, dtype=dtype))
@@ -178,7 +178,7 @@ class TestRandomCutMixV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
expected = torch.tensor(
[
@@ -192,8 +192,10 @@ class TestRandomCutMixV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[:, :, 0] == label).all()
- assert (out_label[:, :, 1] == torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]], device=device)).all()
+ assert_close(out_label[:, :, 0], label.view(1, -1).expand(5, 2))
+ assert_close(
+ out_label[:, :, 1], torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]], device=device, dtype=dtype)
+ )
assert_close(
out_label[:, :, 2],
torch.tensor(
@@ -212,7 +214,7 @@ class TestRandomCutMixV2:
input = torch.stack(
[torch.ones(1, 3, 4, device=device, dtype=dtype), torch.zeros(1, 3, 4, device=device, dtype=dtype)]
)
- label = torch.tensor([1, 0], device=device)
+ label = torch.tensor([1, 0], device=device, dtype=dtype)
expected = torch.tensor(
[
@@ -226,8 +228,8 @@ class TestRandomCutMixV2:
out_image, out_label = f(input, label)
assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
- assert (out_label[0, :, 0] == label).all()
- assert (out_label[0, :, 1] == torch.tensor([0, 1], device=device, dtype=dtype)).all()
+ assert_close(out_label[0, :, 0], label)
+ assert_close(out_label[0, :, 1], torch.tensor([0, 1], device=device, dtype=dtype))
assert_close(
out_label[0, :, 2], torch.tensor([0.5000, 0.5000], device=device, dtype=dtype), rtol=1e-4, atol=1e-4
)
diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.py
index 87218d6e7..7661223a3 100644
--- a/test/augmentation/test_container.py
+++ b/test/augmentation/test_container.py
@@ -102,15 +102,15 @@ class TestVideoSequential:
if data_format == 'BCTHW':
input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
output = aug_list(input)
- assert (output[:, :, 0] == output[:, :, 1]).all()
- assert (output[:, :, 1] == output[:, :, 2]).all()
- assert (output[:, :, 2] == output[:, :, 3]).all()
+ assert_close(output[:, :, 0], output[:, :, 1])
+ assert_close(output[:, :, 1], output[:, :, 2])
+ assert_close(output[:, :, 2], output[:, :, 3])
if data_format == 'BTCHW':
input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
output = aug_list(input)
- assert (output[:, 0] == output[:, 1]).all()
- assert (output[:, 1] == output[:, 2]).all()
- assert (output[:, 2] == output[:, 3]).all()
+ assert_close(output[:, 0], output[:, 1])
+ assert_close(output[:, 1], output[:, 2])
+ assert_close(output[:, 2], output[:, 3])
reproducibility_test(input, aug_list)
@pytest.mark.parametrize(
@@ -145,7 +145,7 @@ class TestVideoSequential:
output_2 = output_2.view(2, 4, 3, 5, 6)
if data_format == 'BCTHW':
output_2 = output_2.transpose(1, 2)
- assert (output_1 == output_2).all(), dict(aug_list_1._params)
+ assert_close(output_1, output_2)
@pytest.mark.jit
@pytest.mark.skip(reason="turn off due to Union Type")
diff --git a/test/geometry/test_boxes.py b/test/geometry/test_boxes.py
index ebb2d6324..110f17479 100644
--- a/test/geometry/test_boxes.py
+++ b/test/geometry/test_boxes.py
@@ -52,17 +52,15 @@ class TestBoxes2D:
h, w = boxes.get_boxes_shape()
assert h.ndim == 1 and w.ndim == 1
assert len(h) == 2 and len(w) == 2
- assert (h == torch.as_tensor([2.0, 3.0], device=device)).all() and (
- w == torch.as_tensor([3.0, 4.0], device=device)
- ).all()
+ assert_close(h, torch.as_tensor([2.0, 3.0], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([3.0, 4.0], device=device, dtype=dtype))
# Box batch
h, w = boxes_batch.get_boxes_shape()
assert h.ndim == 2 and w.ndim == 2
assert h.shape == (1, 2) and w.shape == (1, 2)
- assert (h == torch.as_tensor([[2.0, 3.0]], device=device)).all() and (
- w == torch.as_tensor([[3.0, 4.0]], device=device)
- ).all()
+ assert_close(h, torch.as_tensor([[2.0, 3.0]], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([[3.0, 4.0]], device=device, dtype=dtype))
def test_get_boxes_shape_batch(self, device, dtype):
t_box1 = torch.tensor([[[1.0, 1.0], [3.0, 2.0], [3.0, 1.0], [1.0, 2.0]]], device=device, dtype=dtype)
@@ -72,9 +70,8 @@ class TestBoxes2D:
h, w = batched_boxes.get_boxes_shape()
assert h.ndim == 2 and w.ndim == 2
assert h.shape == (2, 1) and w.shape == (2, 1)
- assert (h == torch.as_tensor([[2], [3]], device=device)).all() and (
- w == torch.as_tensor([[3], [4]], device=device)
- ).all()
+ assert_close(h, torch.as_tensor([[2], [3]], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([[3], [4]], device=device, dtype=dtype))
@pytest.mark.parametrize('shape', [(1, 4), (1, 1, 4)])
def test_from_tensor(self, shape, device, dtype):
@@ -450,21 +447,17 @@ class TestBbox3D:
d, h, w = boxes.get_boxes_shape()
assert h.ndim == 1 and w.ndim == 1
assert len(d) == 2 and len(h) == 2 and len(w) == 2
- assert (
- (d == torch.as_tensor([31.0, 61.0], device=device)).all()
- and (h == torch.as_tensor([21.0, 51.0], device=device)).all()
- and (w == torch.as_tensor([11.0, 41.0], device=device)).all()
- )
+ assert_close(d, torch.as_tensor([31.0, 61.0], device=device, dtype=dtype))
+ assert_close(h, torch.as_tensor([21.0, 51.0], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([11.0, 41.0], device=device, dtype=dtype))
# Box batch
d, h, w = boxes_batch.get_boxes_shape()
assert h.ndim == 2 and w.ndim == 2
assert h.shape == (1, 2) and w.shape == (1, 2)
- assert (
- (d == torch.as_tensor([[31.0, 61.0]], device=device)).all()
- and (h == torch.as_tensor([[21.0, 51.0]], device=device)).all()
- and (w == torch.as_tensor([[11.0, 41.0]], device=device)).all()
- )
+ assert_close(d, torch.as_tensor([[31.0, 61.0]], device=device, dtype=dtype))
+ assert_close(h, torch.as_tensor([[21.0, 51.0]], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([[11.0, 41.0]], device=device, dtype=dtype))
def test_get_boxes_shape_batch(self, device, dtype):
t_box1 = torch.tensor(
@@ -482,11 +475,9 @@ class TestBbox3D:
d, h, w = batched_boxes.get_boxes_shape()
assert d.ndim == 2 and h.ndim == 2 and w.ndim == 2
assert d.shape == (2, 1) and h.shape == (2, 1) and w.shape == (2, 1)
- assert (
- (d == torch.as_tensor([[31.0], [61.0]], device=device)).all()
- and (h == torch.as_tensor([[21.0], [51.0]], device=device)).all()
- and (w == torch.as_tensor([[11.0], [41.0]], device=device)).all()
- )
+ assert_close(d, torch.as_tensor([[31.0], [61.0]], device=device, dtype=dtype))
+ assert_close(h, torch.as_tensor([[21.0], [51.0]], device=device, dtype=dtype))
+ assert_close(w, torch.as_tensor([[11.0], [41.0]], device=device, dtype=dtype))
@pytest.mark.parametrize('shape', [(1, 6), (1, 1, 6)])
def test_from_tensor(self, shape, device, dtype):
diff --git a/test/geometry/test_line.py b/test/geometry/test_line.py
index e7d7c1c16..f50a1dd6a 100644
--- a/test/geometry/test_line.py
+++ b/test/geometry/test_line.py
@@ -4,7 +4,7 @@ from torch.autograd import gradcheck
from kornia.geometry.line import ParametrizedLine, fit_line
from kornia.geometry.plane import Hyperplane
-from kornia.testing import BaseTester
+from kornia.testing import BaseTester, assert_close
class TestParametrizedLine(BaseTester):
@@ -124,12 +124,12 @@ class TestFitLine(BaseTester):
assert line.origin.shape == (B, D)
assert line.direction.shape == (B, D)
- assert (line.origin == line[0]).all()
- assert (line.direction == line[1]).all()
+ assert_close(line.origin, line[0])
+ assert_close(line.direction, line[1])
origin, direction = fit_line(points)
- assert (line.origin == origin).all()
- assert (line.direction == direction).all()
+ assert_close(line.origin, origin)
+ assert_close(line.direction, direction)
def test_fit_line2(self, device, dtype):
p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)
diff --git a/test/geometry/transform/test_flip.py b/test/geometry/transform/test_flip.py
index 8aeb2c9f9..8eb00f382 100644
--- a/test/geometry/transform/test_flip.py
+++ b/test/geometry/transform/test_flip.py
@@ -21,7 +21,7 @@ class TestVflip:
[[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
def test_batch_vflip(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
@@ -35,7 +35,7 @@ class TestVflip:
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
@pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device, dtype):
@@ -85,7 +85,7 @@ class TestHflip:
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
def test_batch_hflip(self, device, dtype):
input = torch.tensor(
@@ -101,7 +101,7 @@ class TestHflip:
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
@pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device, dtype):
@@ -151,7 +151,7 @@ class TestRot180:
[[1.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
def test_batch_rot180(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
@@ -165,7 +165,7 @@ class TestRot180:
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
- assert (f(input) == expected).all()
+ assert_close(f(input), expected)
@pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device, dtype):
diff --git a/test/geometry/transform/test_homography_warper.py b/test/geometry/transform/test_homography_warper.py
index c1b1990e0..3a98ba4af 100644
--- a/test/geometry/transform/test_homography_warper.py
+++ b/test/geometry/transform/test_homography_warper.py
@@ -32,12 +32,10 @@ class TestHomographyWarper:
dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- assert (
- kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
- ).all()
+ assert_close(kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype), res)
norm_homo = kornia.geometry.conversions.normalize_homography(dst_homo_src, (height, width), (height, width))
- assert (norm_homo == dst_homo_src).all()
+ assert_close(norm_homo, dst_homo_src)
# change output scale
norm_homo = kornia.geometry.conversions.normalize_homography(
@@ -55,12 +53,10 @@ class TestHomographyWarper:
dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- assert (
- kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
- ).all()
+ assert_close(kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype), res)
denorm_homo = kornia.geometry.conversions.denormalize_homography(dst_homo_src, (height, width), (height, width))
- assert (denorm_homo == dst_homo_src).all()
+ assert_close(denorm_homo, dst_homo_src)
# change output scale
denorm_homo = kornia.geometry.conversions.denormalize_homography(
@@ -83,8 +79,10 @@ class TestHomographyWarper:
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
norm_homo = kornia.geometry.conversions.normalize_homography(dst_homo_src, (height, width), (height, width))
- res = torch.tensor([[[0.5, 0.0, 0.0], [0.0, 2.0, 5.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- assert (norm_homo == res).all()
+ res = torch.tensor([[[0.5, 0.0, 0.0], [0.0, 2.0, 5.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).expand(
+ batch_size, -1, -1
+ )
+ assert_close(norm_homo, res)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_denormalize_homography_general(self, batch_size, device, dtype):
@@ -98,8 +96,10 @@ class TestHomographyWarper:
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
denorm_homo = kornia.geometry.conversions.denormalize_homography(dst_homo_src, (height, width), (height, width))
- res = torch.tensor([[[0.5, 0.0, 3.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
- assert (denorm_homo == res).all()
+ res = torch.tensor([[[0.5, 0.0, 3.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).expand(
+ batch_size, -1, -1
+ )
+ assert_close(denorm_homo, res)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_consistency(self, batch_size, device, dtype):
@@ -116,12 +116,12 @@ class TestHomographyWarper:
norm_denorm_homo = kornia.geometry.conversions.normalize_homography(
denorm_homo, (height, width), (height, width)
)
- assert (dst_homo_src == norm_denorm_homo).all()
+ assert_close(dst_homo_src, norm_denorm_homo)
norm_homo = kornia.geometry.conversions.normalize_homography(dst_homo_src, (height, width), (height, width))
denorm_norm_homo = kornia.geometry.conversions.denormalize_homography(
norm_homo, (height, width), (height, width)
)
- assert (dst_homo_src == denorm_norm_homo).all()
+ assert_close(dst_homo_src, denorm_norm_homo)
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [(4, 5), (2, 6), (4, 3), (5, 7)])
@@ -405,8 +405,8 @@ class TestHomographyWarper3D:
[[[0.5, 0.0, 0.0, 0.0], [0.0, 0.5, 0.0, 3.5], [0.0, 0.0, 2.0, 7.0], [0.0, 0.0, 0.0, 1.0]]],
device=device,
dtype=dtype,
- )
- assert (norm_homo == res).all()
+ ).expand(batch_size, -1, -1)
+ assert_close(norm_homo, res)
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [(4, 5, 6), (2, 4, 6), (4, 3, 9), (5, 7, 8)])
diff --git a/test/test_contrib.py b/test/test_contrib.py
index cac35e011..b39427cff 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -283,7 +283,7 @@ class TestCombineTensorPatches:
m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2))
patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2))
assert m(patches).shape == (1, 1, 4, 4)
- assert (img == m(patches)).all()
+ assert_close(img, m(patches))
def test_error(self, device, dtype):
patches = kornia.contrib.extract_tensor_patches(
@@ -299,7 +299,7 @@ class TestCombineTensorPatches:
patches, original_size=(4, 3), window_size=(2, 2), stride=(2, 2), unpadding=(0, 0, 0, 1)
)
assert m.shape == (1, 1, 4, 3)
- assert (img == m).all()
+ assert_close(img, m)
def test_pad_error(self, device, dtype):
patches = kornia.contrib.extract_tensor_patches(
@@ -333,21 +333,21 @@ class TestCombineTensorPatches:
patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
m = kornia.contrib.CombineTensorPatches((4, 6), (2, 2), unpadding=1)
assert m(patches).shape == (1, 1, 4, 6)
- assert (img == m(patches)).all()
+ assert_close(img, m(patches))
def test_padding1(self, device, dtype):
img = torch.arange(16, device=device, dtype=dtype).view(1, 1, 4, 4)
patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
m = kornia.contrib.CombineTensorPatches((4, 4), (2, 2), unpadding=1)
assert m(patches).shape == (1, 1, 4, 4)
- assert (img == m(patches)).all()
+ assert_close(img, m(patches))
def test_padding2(self, device, dtype):
img = torch.arange(64, device=device, dtype=dtype).view(1, 1, 8, 8)
patches = kornia.contrib.extract_tensor_patches(img, window_size=(2, 2), stride=(2, 2), padding=1)
m = kornia.contrib.CombineTensorPatches((8, 8), (2, 2), unpadding=1)
assert m(patches).shape == (1, 1, 8, 8)
- assert (img == m(patches)).all()
+ assert_close(img, m(patches))
def test_autopadding(self, device, dtype):
img = torch.arange(104, device=device, dtype=dtype).view(1, 1, 8, 13)
@@ -358,7 +358,7 @@ class TestCombineTensorPatches:
)
m = kornia.contrib.CombineTensorPatches((8, 13), (3, 3), unpadding=padding)
assert m(patches).shape == (1, 1, 8, 13)
- assert (img == m(patches)).all()
+ assert_close(img, m(patches))
def test_gradcheck(self, device, dtype):
patches = kornia.contrib.extract_tensor_patches(
@@ -624,7 +624,7 @@ class TestHistMatch:
x = torch.tensor([4, 5, 6], device=device, dtype=dtype)
x_hat_expected = torch.tensor([-2.0, -4.0, -6.0], device=device, dtype=dtype)
x_hat = kornia.contrib.interp(x, xp, fp)
- assert (x_hat_expected == x_hat).all()
+ assert_close(x_hat_expected, x_hat)
def test_histmatch(self, device, dtype):
torch.manual_seed(44)