Beispiel #1
0
    def test_batch_random_hflip(self):
        batch_size = 5
        flip_param_0 = {'batch_prob': torch.tensor([False] * 5)}
        flip_param_1 = {'batch_prob': torch.tensor([True] * 5)}

        input = torch.tensor([[[[0., 0., 0.], [0., 0., 0.],
                                [0., 1., 1.]]]])  # 1 x 1 x 3 x 3

        expected = torch.tensor([[[[0., 0., 0.], [0., 0., 0.],
                                   [1., 1., 0.]]]])  # 1 x 1 x 3 x 3

        expected_transform = torch.tensor([[[-1., 0., 3.], [0., 1., 0.],
                                            [0., 0., 1.]]])  # 1 x 3 x 3

        identity = torch.tensor([[[1., 0., 0.], [0., 1., 0.],
                                  [0., 0., 1.]]])  # 1 x 3 x 3

        input = input.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected = expected.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected_transform = expected_transform.repeat(batch_size, 1,
                                                       1)  # 5 x 3 x 3
        identity = identity.repeat(batch_size, 1, 1)  # 5 x 3 x 3

        assert (F.apply_hflip(input,
                              params=flip_param_0,
                              return_transform=True)[0] == input).all()
        assert (F.apply_hflip(input,
                              params=flip_param_0,
                              return_transform=True)[1] == identity).all()
        assert (F.apply_hflip(input,
                              params=flip_param_1,
                              return_transform=True)[0] == expected).all()
        assert (F.apply_hflip(
            input, params=flip_param_1,
            return_transform=True)[1] == expected_transform).all()
Beispiel #2
0
    def test_random_hflip(self):
        flip_param_0 = {'batch_prob': torch.tensor(False)}
        flip_param_1 = {'batch_prob': torch.tensor(True)}

        input = torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.],
                              [0., 0., 1., 2.]])  # 3 x 4

        expected = torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.],
                                 [2., 1., 0., 0.]])  # 3 x 4

        assert (F.apply_hflip(input, params=flip_param_0) == input).all()
        assert (F.apply_hflip(input, params=flip_param_1) == expected).all()
Beispiel #3
0
    def test_batch_random_hflip(self):
        batch_size = 5
        flip_param_0 = {'batch_prob': torch.tensor([False] * 5)}
        flip_param_1 = {'batch_prob': torch.tensor([True] * 5)}

        input = torch.tensor([[[[0., 0., 0.], [0., 0., 0.],
                                [0., 1., 1.]]]])  # 1 x 1 x 3 x 3

        expected = torch.tensor([[[[0., 0., 0.], [0., 0., 0.],
                                   [1., 1., 0.]]]])  # 1 x 1 x 3 x 3

        input = input.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected = expected.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3

        assert (F.apply_hflip(input, params=flip_param_0) == input).all()
        assert (F.apply_hflip(input, params=flip_param_1) == expected).all()
Beispiel #4
0
    def test_random_hflip(self, device, dtype):
        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

        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

        assert (F.apply_hflip(input[None, None]) == expected).all()
Beispiel #5
0
    def test_random_hflip(self, device, dtype):
        input = torch.tensor(
            [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 1., 2.]],
            device=device,
            dtype=dtype)  # 3 x 4

        expected = torch.tensor(
            [[0., 0., 0., 0.], [0., 0., 0., 0.], [2., 1., 0., 0.]],
            device=device,
            dtype=dtype)  # 3 x 4

        assert (F.apply_hflip(input) == expected).all()
Beispiel #6
0
    def test_random_hflip(self):
        flip_param_0 = {'batch_prob': torch.tensor(False)}
        flip_param_1 = {'batch_prob': torch.tensor(True)}

        input = torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.],
                              [0., 0., 1., 2.]])  # 3 x 4

        expected = torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.],
                                 [2., 1., 0., 0.]])  # 3 x 4

        expected_transform = torch.tensor([[-1., 0., 4.], [0., 1., 0.],
                                           [0., 0., 1.]])  # 3 x 3

        identity = torch.tensor([[1., 0., 0.], [0., 1., 0.], [0., 0.,
                                                              1.]])  # 3 x 3

        assert (F.apply_hflip(input,
                              params=flip_param_0,
                              return_transform=True)[0] == input).all()
        assert (F.apply_hflip(input,
                              params=flip_param_0,
                              return_transform=True)[1] == identity).all()
        assert (F.apply_hflip(input,
                              params=flip_param_1,
                              return_transform=True)[0] == expected).all()
        assert (F.apply_hflip(
            input, params=flip_param_1,
            return_transform=True)[1] == expected_transform).all()
        assert (F.apply_hflip(input,
                              params=flip_param_0,
                              return_transform=False) == input).all()
        assert (F.apply_hflip(input,
                              params=flip_param_1,
                              return_transform=False) == expected).all()
Beispiel #7
0
    def test_batch_random_hflip(self, device, dtype):
        batch_size = 5
        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
        )  # 1 x 1 x 3 x 3

        expected = torch.tensor(
            [[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]]]], device=device, dtype=dtype
        )  # 1 x 1 x 3 x 3

        input = input.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected = expected.repeat(batch_size, 3, 1, 1)  # 5 x 3 x 3 x 3
        assert (F.apply_hflip(input) == expected).all()
Beispiel #8
0
def h_flip(x, b):
    # b is bool
    if b == True:
        x = F_k.apply_hflip((x))

    return x