-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making Vflip and Hflip in Tensor format #1465
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd prefer using |
||
|
||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you specify in the documentation what is the expected format of the tensor? Is it |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make the offset relative to the end of the dimension? 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. | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment, can you do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
self.assertEqual
here? I'd prefer to move away from the raw asserts and useself.assert*
methods from unittest for newer test