Skip to content

Make crop scriptable #1379

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

Merged
merged 16 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion test/test_functional_tensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import torch
import torchvision.transforms as transforms
import torchvision.transforms.functional_tensor as F_t
import torchvision.transforms.functional as F
import unittest
import torch
import random


class Tester(unittest.TestCase):
Expand All @@ -19,6 +22,21 @@ def test_hflip(self):
self.assertEqual(hflipped_img.shape, img_tensor.shape)
self.assertTrue(torch.equal(img_tensor, hflipped_img_again))

def test_crop(self):
img_tensor = torch.FloatTensor(3, 16, 16).uniform_(0, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use torch.rand(3, 16, 16) instead? torch.FloatTensor is a legacy API and is deprecated

top = random.randint(0, 15)
left = random.randint(0, 15)
height = random.randint(1, 16-top)
width = random.randint(1, 16-left)
img_cropped = F_t.crop(img_tensor, top, left, height, width)
img_PIL = transforms.ToPILImage()(img_tensor)
img_PIL_cropped = F.crop(img_PIL, top, left, height, width)
img_cropped_GT = transforms.ToTensor()(img_PIL_cropped)

max_diff = (img_cropped_GT - img_cropped).abs().max()

assert max_diff < 5e-3, "Functional crop not working"
Copy link
Contributor

@surgan12 surgan12 Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed out by @fmassa here in #1465 and also #1483 , let's change to self.assertTrue instead of normal assert.



if __name__ == '__main__':
unittest.main()
21 changes: 19 additions & 2 deletions torchvision/transforms/functional_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def vflip(img_tensor):
Tensor: Vertically flipped image Tensor.
"""
if not F._is_tensor_image(img_tensor):
raise TypeError('tensor is not a torch image.')
raise TypeError('Input image is not a tensor.')

return img_tensor.flip(-2)

Expand All @@ -28,6 +28,23 @@ def hflip(img_tensor):
"""

if not F._is_tensor_image(img_tensor):
raise TypeError('tensor is not a torch image.')
raise TypeError('Input image is not a tensor.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message was the same as supplied here https://github.com/pytorch/vision/blob/53b062ca58932bbf387b96f2dd3397c4495b735b/torchvision/transforms/functional.py#L209 which already works on input tensor.
The above message would be confusing for the cases in which you supply a grayscale img tensor of the form HxW to the function. In this case the image is a tensor but not an accepted torch image but the error msg seem to mislead the user.
@ekagra-ranjan thoughts ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link you provided isn't working but I get your concern. It's a valid point and I will revert the change.


return img_tensor.flip(-1)


def crop(img, top, left, height, width):
"""Crop the given Image Tensor.
Args:
img (Tensor): Image to be cropped in the form [C, H, W]. (0,0) denotes the top left corner of the image.
top (int): Vertical component of the top left corner of the crop box.
left (int): Horizontal component of the top left corner of the crop box.
height (int): Height of the crop box.
width (int): Width of the crop box.
Returns:
Tensor: Cropped image.
"""
if not F._is_tensor_image(img):
raise TypeError('Input image is not a tensor.')

return img[..., top:top + height, left:left + width]