From 9664ec87f84e3db2974486016b9fe4a2c39f9e0d Mon Sep 17 00:00:00 2001 From: surgan12 Date: Tue, 15 Oct 2019 19:36:58 +0530 Subject: [PATCH 1/4] vflip and hflip tensor --- torchvision/transforms/functional_tensor.py | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 torchvision/transforms/functional_tensor.py diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py new file mode 100644 index 00000000000..6ce84e063da --- /dev/null +++ b/torchvision/transforms/functional_tensor.py @@ -0,0 +1,32 @@ +import torch +import torchvision.transforms.functional as F + +def vflip(img_tensor): + """Vertically flip the given the Image Tensor. + + Args: + img_tensor (Tensor): Image Tensor to be flipped. + + Returns: + Tensor: Vertically flipped image Tensor. + """ + if not F._is_tensor_image(img_tensor): + raise TypeError('tensor is not a torch image.') + + return img_tensor.flip(1) + + +def hflip(img_tensor): + """Horizontally flip the given the Image Tensor. + + Args: + img_tensor (Tensor): Image Tensor to be flipped. + + Returns: + Tensor: Horizontally flipped image Tensor. + """ + + if not F._is_tensor_image(img_tensor): + raise TypeError('tensor is not a torch image.') + + return img_tensor.flip(2) From 4a3946d4f8b7523892654fa5c781616a10b1fad4 Mon Sep 17 00:00:00 2001 From: surgan12 Date: Tue, 15 Oct 2019 19:38:14 +0530 Subject: [PATCH 2/4] vflip and hflip tensor --- test/test_functional_tensor.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/test_functional_tensor.py diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py new file mode 100644 index 00000000000..e68eed0e9a1 --- /dev/null +++ b/test/test_functional_tensor.py @@ -0,0 +1,24 @@ +import torchvision.transforms.functional_tensor as F_t +import unittest +import torch + +class Tester(unittest.TestCase): + + def test_vflip(self): + img_tensor = torch.randn(3,16,16) + vflipped_img = F_t.vflip(img_tensor) + vflipped_img_again = F_t.vflip(vflipped_img) + + assert vflipped_img.shape == img_tensor.shape + assert torch.equal(img_tensor, vflipped_img_again) + + def test_hflip(self): + img_tensor = torch.randn(3,16,16) + hflipped_img = F_t.hflip(img_tensor) + hflipped_img_again = F_t.hflip(hflipped_img) + + assert hflipped_img.shape == img_tensor.shape + assert torch.equal(img_tensor, hflipped_img_again) + +if __name__ == '__main__': + unittest.main() From 444a69f84df7d08bba07d87b19b15e684fee0aae Mon Sep 17 00:00:00 2001 From: surgan12 Date: Tue, 15 Oct 2019 20:08:53 +0530 Subject: [PATCH 3/4] changes made --- test/test_functional_tensor.py | 29 ++++++++++----------- torchvision/transforms/functional_tensor.py | 17 ++++++------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index e68eed0e9a1..02ce19a6462 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -2,23 +2,22 @@ import unittest import torch -class Tester(unittest.TestCase): - - def test_vflip(self): - img_tensor = torch.randn(3,16,16) - vflipped_img = F_t.vflip(img_tensor) - vflipped_img_again = F_t.vflip(vflipped_img) - assert vflipped_img.shape == img_tensor.shape - assert torch.equal(img_tensor, vflipped_img_again) +class Tester(unittest.TestCase): - def test_hflip(self): - img_tensor = torch.randn(3,16,16) - hflipped_img = F_t.hflip(img_tensor) - hflipped_img_again = F_t.hflip(hflipped_img) + def test_vflip(self): + img_tensor = torch.randn(3,16,16) + vflipped_img = F_t.vflip(img_tensor) + vflipped_img_again = F_t.vflip(vflipped_img) + self.assertEqual(vflipped_img.shape, img_tensor.shape) + self.assertTrue(torch.equal(img_tensor, vflipped_img_again)) - assert hflipped_img.shape == img_tensor.shape - assert torch.equal(img_tensor, hflipped_img_again) + def test_hflip(self): + img_tensor = torch.randn(3,16,16) + hflipped_img = F_t.hflip(img_tensor) + hflipped_img_again = F_t.hflip(hflipped_img) + self.assertEqual(hflipped_img.shape, img_tensor.shape) + self.assertTrue(torch.equal(img_tensor, hflipped_img_again)) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index 6ce84e063da..abb802fc9bf 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -1,32 +1,33 @@ import torch import torchvision.transforms.functional as F + def vflip(img_tensor): """Vertically flip the given the Image Tensor. Args: - img_tensor (Tensor): Image Tensor to be flipped. + img_tensor (Tensor): Image Tensor to be flipped in the form CXHXW. Returns: Tensor: Vertically flipped image Tensor. """ if not F._is_tensor_image(img_tensor): - raise TypeError('tensor is not a torch image.') - - return img_tensor.flip(1) + raise TypeError('tensor is not a torch image.') + + return img_tensor.flip(-2) def hflip(img_tensor): """Horizontally flip the given the Image Tensor. Args: - img_tensor (Tensor): Image Tensor to be flipped. + img_tensor (Tensor): Image Tensor to be flipped in the form CXHXW. Returns: Tensor: Horizontally flipped image Tensor. """ if not F._is_tensor_image(img_tensor): - raise TypeError('tensor is not a torch image.') - - return img_tensor.flip(2) + raise TypeError('tensor is not a torch image.') + + return img_tensor.flip(-1) \ No newline at end of file From 75d1396ad8aee08654724c052acb2a666a9a60a8 Mon Sep 17 00:00:00 2001 From: surgan12 Date: Tue, 15 Oct 2019 20:16:51 +0530 Subject: [PATCH 4/4] lint --- test/test_functional_tensor.py | 5 +++-- torchvision/transforms/functional_tensor.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test_functional_tensor.py b/test/test_functional_tensor.py index 02ce19a6462..4bdab11f6be 100644 --- a/test/test_functional_tensor.py +++ b/test/test_functional_tensor.py @@ -6,18 +6,19 @@ class Tester(unittest.TestCase): def test_vflip(self): - img_tensor = torch.randn(3,16,16) + img_tensor = torch.randn(3, 16, 16) vflipped_img = F_t.vflip(img_tensor) vflipped_img_again = F_t.vflip(vflipped_img) self.assertEqual(vflipped_img.shape, img_tensor.shape) self.assertTrue(torch.equal(img_tensor, vflipped_img_again)) def test_hflip(self): - img_tensor = torch.randn(3,16,16) + img_tensor = torch.randn(3, 16, 16) hflipped_img = F_t.hflip(img_tensor) hflipped_img_again = F_t.hflip(hflipped_img) self.assertEqual(hflipped_img.shape, img_tensor.shape) self.assertTrue(torch.equal(img_tensor, hflipped_img_again)) + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/torchvision/transforms/functional_tensor.py b/torchvision/transforms/functional_tensor.py index abb802fc9bf..bc12327f39b 100644 --- a/torchvision/transforms/functional_tensor.py +++ b/torchvision/transforms/functional_tensor.py @@ -30,4 +30,4 @@ def hflip(img_tensor): if not F._is_tensor_image(img_tensor): raise TypeError('tensor is not a torch image.') - return img_tensor.flip(-1) \ No newline at end of file + return img_tensor.flip(-1)