def test_forward_3d(self):
        x = torch.rand(2, 1, 4, 4, 4)
        theta = torch.Tensor([[[0, 0, -1, 0], [1, 0, 0, 0],
                               [0, 0, 1, 0]]]).repeat(2, 1, 1)
        grid = torch.nn.functional.affine_grid(theta,
                                               x.size(),
                                               align_corners=False)
        expected = torch.nn.functional.grid_sample(x,
                                                   grid,
                                                   align_corners=False)
        expected = expected.detach().cpu().numpy()

        actual = AffineTransform(normalized=True,
                                 reverse_indexing=False)(x, theta)
        actual = actual.detach().cpu().numpy()
        np.testing.assert_allclose(actual, expected)
        np.testing.assert_allclose(list(theta.shape), [2, 3, 4])

        theta = torch.Tensor([[0, 0, -1, 0], [1, 0, 0, 0], [0, 0, 1, 0]])
        actual = AffineTransform(normalized=True,
                                 reverse_indexing=False)(x, theta)
        actual = actual.detach().cpu().numpy()
        np.testing.assert_allclose(actual, expected)
        np.testing.assert_allclose(list(theta.shape), [3, 4])

        theta = torch.Tensor([[[0, 0, -1, 0], [1, 0, 0, 0], [0, 0, 1, 0]]])
        actual = AffineTransform(normalized=True,
                                 reverse_indexing=False)(x, theta)
        actual = actual.detach().cpu().numpy()
        np.testing.assert_allclose(actual, expected)
        np.testing.assert_allclose(list(theta.shape), [1, 3, 4])
Example #2
0
 def test_affine_shift_2(self):
     affine = torch.as_tensor([[1.0, 0.0, -1.0], [0.0, 1.0, 0.0]])
     image = torch.as_tensor([[[[4.0, 1.0, 3.0, 2.0], [7.0, 6.0, 8.0, 5.0], [3.0, 5.0, 3.0, 6.0]]]])
     out = AffineTransform()(image, affine)
     out = out.detach().cpu().numpy()
     expected = [[[[0, 0, 0, 0], [4, 1, 3, 2], [7, 6, 8, 5]]]]
     np.testing.assert_allclose(out, expected, atol=1e-5)
Example #3
0
 def test_affine_shift_1(self):
     affine = torch.as_tensor([[1, 0, -1], [0, 1, -1]])
     image = torch.as_tensor([[[[4, 1, 3, 2], [7, 6, 8, 5], [3, 5, 3, 6]]]])
     out = AffineTransform()(image, affine)
     out = out.detach().cpu().numpy()
     expected = [[[[0, 0, 0, 0], [0, 4, 1, 3], [0, 7, 6, 8]]]]
     np.testing.assert_allclose(out, expected, atol=1e-5)
 def test_affine_transform_minimum(self):
     t = np.pi / 3
     affine = [[np.cos(t), -np.sin(t), 0], [np.sin(t),
                                            np.cos(t), 0], [0, 0, 1]]
     affine = torch.as_tensor(affine,
                              device=torch.device("cpu:0"),
                              dtype=torch.float32)
     image = torch.arange(24.0).view(1, 1, 4,
                                     6).to(device=torch.device("cpu:0"))
     out = AffineTransform()(image, affine)
     out = out.detach().cpu().numpy()
     expected = [[[
         [0.0, 0.06698727, 0.0, 0.0, 0.0, 0.0],
         [3.8660254, 0.86602557, 0.0, 0.0, 0.0, 0.0],
         [7.732051, 3.035899, 0.73205125, 0.0, 0.0, 0.0],
         [11.598076, 6.901923, 2.7631402, 0.0, 0.0, 0.0],
     ]]]
     np.testing.assert_allclose(out, expected, atol=1e-3, rtol=_rtol)