def test_nested_invert(self):
     tf = T.Compose([
         T.Compose([
             T.ToPILImage(),
             T.RandomHorizontalFlip(0),
             T.RandomHorizontalFlip(1),
         ]),
     ])
     img_pil = tf(self.img_tensor)
     self.assertIsInstance(img_pil, Image.Image)
     self.assertIsInstance(tf.invert(img_pil), torch.Tensor)
Exemple #2
0
 def test_replay(self):
     tf = T.Compose([
         T.RandomCrop(self.crop_size),
         T.RandomVerticalFlip(),
         T.RandomHorizontalFlip(),
         T.ToTensor(),
     ])
     img_tf1 = tf.replay(self.img_pil)
     img_tf2 = tf.replay(self.img_pil)
     self.assertTrue(torch.allclose(img_tf1, img_tf2))
Exemple #3
0
 def test_invert(self):
     tf = T.RandomOrder([
         invertransforms.crop_pad.CenterCrop(self.crop_size),
         T.RandomHorizontalFlip(1),
         T.RandomVerticalFlip(1),
     ])
     img_inv = tf(self.img_pil)
     img_pil = tf.invert(img_inv)
     self.assertEqual(img_pil.size, self.img_pil.size)
     center = (self.w // 2 - 10, self.h // 2 + 10)
     self.assertEqual(img_pil.getpixel(center),
                      self.img_pil.getpixel(center))
Exemple #4
0
    def test_track(self):
        tf = T.Compose([
            T.ToPILImage(),
            T.RandomVerticalFlip(p=0.5),
            # the crop will include the center pixels
            T.RandomCrop(size=tuple(
                int(0.8 * self.img_size[i]) for i in range(2))),
            T.RandomHorizontalFlip(p=0.5),
            T.ToTensor(),
        ])

        imgs_tf = [tf.track(self.img_tensor) for _ in range(10)]
        for i, img_tf in enumerate(imgs_tf):
            n = min(self.img_size) // 10
            center_pixels = (0, ) + tuple(
                slice(self.img_size[i] // 2 - n, self.img_size[i] // 2 + n)
                for i in range(2))
            self.assertTrue(
                torch.allclose(tf[i](img_tf)[center_pixels],
                               self.img_tensor[center_pixels]))
    def test_invert_before_apply(self):
        with self.assertRaises(InvertibleError):
            T.RandomVerticalFlip().inverse()

        with self.assertRaises(InvertibleError):
            T.RandomHorizontalFlip().inverse()