Пример #1
0
    def test_affine_rotate_translate(self, device, dtype):
        # TODO: Remove when #666 is implemented
        if device.type == 'cuda':
            pytest.skip("Currently breaks in CUDA." "See https://github.com/kornia/kornia/issues/666")
        batch_size = 2

        input = torch.tensor(
            [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
            device=device,
            dtype=dtype,
        ).repeat(batch_size, 1, 1, 1)

        angle = torch.tensor(180.0, device=device, dtype=dtype).repeat(batch_size)
        translation = torch.tensor([1.0, 0.0], device=device, dtype=dtype).repeat(batch_size, 1)

        expected = torch.tensor(
            [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]]],
            device=device,
            dtype=dtype,
        ).repeat(batch_size, 1, 1, 1)

        transform = kornia.Affine(angle=angle, translation=translation, align_corners=True).to(
            device=device, dtype=dtype
        )
        actual = transform(input)
        assert_close(actual, expected, atol=1e-4, rtol=1e-4)
Пример #2
0
    def test_affine_shear(self, device):
        torch.manual_seed(0)
        shear = torch.rand(1, 2, device=device)
        input = torch.rand(1, 2, 3, 4, device=device)

        transform = kornia.Affine(shear=shear).to(device)
        actual = transform(input)
        expected = kornia.shear(input, shear)
        assert_allclose(actual, expected)
Пример #3
0
    def test_affine_shear(self, device, dtype):
        torch.manual_seed(0)
        shear = torch.rand(1, 2, device=device, dtype=dtype)
        input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)

        transform = kornia.Affine(shear=shear, device=device, dtype=dtype)
        actual = transform(input)
        expected = kornia.shear(input, shear)
        assert_close(actual, expected, atol=1e-4, rtol=1e-4)
Пример #4
0
    def test_affine_translate(self, device):
        torch.manual_seed(0)
        translation = torch.rand(1, 2, device=device) * 2.0
        input = torch.rand(1, 2, 3, 4, device=device)

        transform = kornia.Affine(translation=translation).to(device)
        actual = transform(input)
        expected = kornia.translate(input, translation)
        assert_allclose(actual, expected)
Пример #5
0
    def test_affine_rotate(self, device):
        torch.manual_seed(0)
        angle = torch.rand(1, device=device) * 90.0
        input = torch.rand(1, 2, 3, 4, device=device)

        transform = kornia.Affine(angle=angle).to(device)
        actual = transform(input)
        expected = kornia.rotate(input, angle)
        assert_allclose(actual, expected)
Пример #6
0
    def test_affine_scale(self, device):
        torch.manual_seed(0)
        scale_factor = torch.rand(1, device=device) * 2.0
        input = torch.rand(1, 2, 3, 4, device=device)

        transform = kornia.Affine(scale_factor=scale_factor).to(device)
        actual = transform(input)
        expected = kornia.scale(input, scale_factor)
        assert_allclose(actual, expected)
Пример #7
0
    def test_affine_translate(self, device, dtype):
        # TODO: Remove when #666 is implemented
        if device.type == 'cuda':
            pytest.skip("Currently breaks in CUDA." "See https://github.com/kornia/kornia/issues/666")
        torch.manual_seed(0)
        translation = torch.rand(1, 2, device=device, dtype=dtype) * 2.0
        input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)

        transform = kornia.Affine(translation=translation).to(device=device, dtype=dtype)
        actual = transform(input)
        expected = kornia.translate(input, translation)
        assert_close(actual, expected, atol=1e-4, rtol=1e-4)
Пример #8
0
    def test_affine_scale(self, device, dtype):
        # TODO: Remove when #666 is implemented
        if device.type == 'cuda':
            pytest.skip("Currently breaks in CUDA." "See https://github.com/kornia/kornia/issues/666")
        torch.manual_seed(0)
        _scale_factor = torch.rand(1, device=device, dtype=dtype) * 2.0
        scale_factor = torch.stack([_scale_factor, _scale_factor], dim=1)
        input = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)

        transform = kornia.Affine(scale_factor=scale_factor).to(device=device, dtype=dtype)
        actual = transform(input)
        expected = kornia.scale(input, scale_factor)
        assert_close(actual, expected, atol=1e-4, rtol=1e-4)
Пример #9
0
    def test_affine_rotate(self, device):
        # TODO: Remove when #666 is implemented
        if device.type == 'cuda':
            pytest.skip("Currently breaks in CUDA."
                        "See https://github.com/kornia/kornia/issues/666")
        torch.manual_seed(0)
        angle = torch.rand(1, device=device) * 90.0
        input = torch.rand(1, 2, 3, 4, device=device)

        transform = kornia.Affine(angle=angle).to(device)
        actual = transform(input)
        expected = kornia.rotate(input, angle)
        assert_allclose(actual, expected)
Пример #10
0
    def test_affine_rotate_translate(self, device):
        batch_size = 2

        input = torch.tensor(
            [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]], device=device
        ).repeat(batch_size, 1, 1, 1)

        angle = torch.tensor(180.0, device=device).repeat(batch_size)
        translation = torch.tensor([1.0, 0.0]).repeat(batch_size, 1)

        expected = torch.tensor(
            [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]]], device=device,
        ).repeat(batch_size, 1, 1, 1)

        transform = kornia.Affine(angle=angle, translation=translation, align_corners=True).to(device)
        actual = transform(input)
        assert_allclose(actual, expected)
Пример #11
0
 def test_affine_batch_size_mismatch(self, device, dtype):
     with pytest.raises(RuntimeError):
         angle = torch.rand(1, device=device, dtype=dtype)
         translation = torch.rand(2, 2, device=device, dtype=dtype)
         kornia.Affine(angle, translation)
Пример #12
0
 def test_affine_no_args(self):
     with pytest.raises(RuntimeError):
         kornia.Affine()