-
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
Port to_tensor tests in test_transforms to pytest #3966
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca374b3
Port test to tensor in test_transforms to pytest
DevPranjal f2d52b4
Port test to tensor in test_transforms to pytest with parametrize
DevPranjal 654aa9b
Add code formatting
DevPranjal cdf3e6c
Merge branch 'pytorch:master' into refactor-test-ToTensor
DevPranjal ed42460
Fix issue: Extract errors into seperate functions
DevPranjal 9b7fba8
Merge branch 'refactor-test-ToTensor' of https://github.com/DevPranja…
DevPranjal e3deff2
Merge branch 'master' into refactor-test-ToTensor
DevPranjal bcd4777
Merge branch 'master' of github.com:pytorch/vision into refactor-test…
NicolasHug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,54 +491,6 @@ def test_random_order(self): | |
# Checking if RandomOrder can be printed as string | ||
random_order_transform.__repr__() | ||
|
||
def test_to_tensor(self): | ||
test_channels = [1, 3, 4] | ||
height, width = 4, 4 | ||
trans = transforms.ToTensor() | ||
|
||
with self.assertRaises(TypeError): | ||
trans(np.random.rand(1, height, width).tolist()) | ||
|
||
with self.assertRaises(ValueError): | ||
trans(np.random.rand(height)) | ||
trans(np.random.rand(1, 1, height, width)) | ||
|
||
for channels in test_channels: | ||
input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
torch.testing.assert_close(output, input_data, check_stride=False) | ||
|
||
ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) | ||
output = trans(ndarray) | ||
expected_output = ndarray.transpose((2, 0, 1)) / 255.0 | ||
torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) | ||
|
||
ndarray = np.random.rand(height, width, channels).astype(np.float32) | ||
output = trans(ndarray) | ||
expected_output = ndarray.transpose((2, 0, 1)) | ||
torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) | ||
|
||
# separate test for mode '1' PIL images | ||
input_data = torch.ByteTensor(1, height, width).bernoulli_() | ||
img = transforms.ToPILImage()(input_data.mul(255)).convert('1') | ||
output = trans(img) | ||
torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) | ||
|
||
def test_to_tensor_with_other_default_dtypes(self): | ||
current_def_dtype = torch.get_default_dtype() | ||
|
||
t = transforms.ToTensor() | ||
np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) | ||
img = Image.fromarray(np_arr) | ||
|
||
for dtype in [torch.float16, torch.float, torch.double]: | ||
torch.set_default_dtype(dtype) | ||
res = t(img) | ||
self.assertTrue(res.dtype == dtype, msg=f"{res.dtype} vs {dtype}") | ||
|
||
torch.set_default_dtype(current_def_dtype) | ||
|
||
def test_max_value(self): | ||
for dtype in int_dtypes(): | ||
self.assertEqual(F_t._max_value(dtype), torch.iinfo(dtype).max) | ||
|
@@ -676,39 +628,6 @@ def test_accimage_to_tensor(self): | |
|
||
torch.testing.assert_close(output, expected_output) | ||
|
||
def test_pil_to_tensor(self): | ||
test_channels = [1, 3, 4] | ||
height, width = 4, 4 | ||
trans = transforms.PILToTensor() | ||
|
||
with self.assertRaises(TypeError): | ||
trans(np.random.rand(1, height, width).tolist()) | ||
trans(np.random.rand(1, height, width)) | ||
|
||
for channels in test_channels: | ||
input_data = torch.ByteTensor(channels, height, width).random_(0, 255) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
torch.testing.assert_close(input_data, output, check_stride=False) | ||
|
||
input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
expected_output = input_data.transpose((2, 0, 1)) | ||
torch.testing.assert_close(output.numpy(), expected_output) | ||
|
||
input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) | ||
img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() | ||
output = trans(img) # HWC -> CHW | ||
expected_output = (input_data * 255).byte() | ||
torch.testing.assert_close(output, expected_output, check_stride=False) | ||
|
||
# separate test for mode '1' PIL images | ||
input_data = torch.ByteTensor(1, height, width).bernoulli_() | ||
img = transforms.ToPILImage()(input_data.mul(255)).convert('1') | ||
output = trans(img).view(torch.uint8).bool().to(torch.uint8) | ||
torch.testing.assert_close(input_data, output, check_stride=False) | ||
|
||
@unittest.skipIf(accimage is None, 'accimage not available') | ||
def test_accimage_pil_to_tensor(self): | ||
trans = transforms.PILToTensor() | ||
|
@@ -1403,6 +1322,88 @@ def test_random_erasing(self): | |
t.__repr__() | ||
|
||
|
||
@pytest.mark.parametrize('channels', [1, 3, 4]) | ||
def test_pil_to_tensor(channels): | ||
height, width = 4, 4 | ||
trans = transforms.PILToTensor() | ||
|
||
with pytest.raises(TypeError): | ||
trans(np.random.rand(1, height, width).tolist()) | ||
trans(np.random.rand(1, height, width)) | ||
|
||
input_data = torch.ByteTensor(channels, height, width).random_(0, 255) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
torch.testing.assert_close(input_data, output, check_stride=False) | ||
|
||
input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
expected_output = input_data.transpose((2, 0, 1)) | ||
torch.testing.assert_close(output.numpy(), expected_output) | ||
|
||
input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) | ||
img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() | ||
output = trans(img) # HWC -> CHW | ||
expected_output = (input_data * 255).byte() | ||
torch.testing.assert_close(output, expected_output, check_stride=False) | ||
|
||
# separate test for mode '1' PIL images | ||
input_data = torch.ByteTensor(1, height, width).bernoulli_() | ||
img = transforms.ToPILImage()(input_data.mul(255)).convert('1') | ||
output = trans(img).view(torch.uint8).bool().to(torch.uint8) | ||
torch.testing.assert_close(input_data, output, check_stride=False) | ||
|
||
|
||
@pytest.mark.parametrize('channels', [1, 3, 4]) | ||
def test_to_tensor(channels): | ||
height, width = 4, 4 | ||
trans = transforms.ToTensor() | ||
|
||
with pytest.raises(TypeError): | ||
trans(np.random.rand(1, height, width).tolist()) | ||
|
||
with pytest.raises(ValueError): | ||
trans(np.random.rand(height)) | ||
trans(np.random.rand(1, 1, height, width)) | ||
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 here about having 1 |
||
|
||
input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) | ||
img = transforms.ToPILImage()(input_data) | ||
output = trans(img) | ||
torch.testing.assert_close(output, input_data, check_stride=False) | ||
|
||
ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) | ||
output = trans(ndarray) | ||
expected_output = ndarray.transpose((2, 0, 1)) / 255.0 | ||
torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) | ||
|
||
ndarray = np.random.rand(height, width, channels).astype(np.float32) | ||
output = trans(ndarray) | ||
expected_output = ndarray.transpose((2, 0, 1)) | ||
torch.testing.assert_close(output.numpy(), expected_output, check_stride=False, check_dtype=False) | ||
|
||
# separate test for mode '1' PIL images | ||
input_data = torch.ByteTensor(1, height, width).bernoulli_() | ||
img = transforms.ToPILImage()(input_data.mul(255)).convert('1') | ||
output = trans(img) | ||
torch.testing.assert_close(input_data, output, check_dtype=False, check_stride=False) | ||
|
||
|
||
@pytest.mark.parametrize('dtype', [torch.float16, torch.float, torch.double]) | ||
def test_to_tensor_with_other_default_dtypes(dtype): | ||
current_def_dtype = torch.get_default_dtype() | ||
|
||
t = transforms.ToTensor() | ||
np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) | ||
img = Image.fromarray(np_arr) | ||
|
||
torch.set_default_dtype(dtype) | ||
res = t(img) | ||
assert res.dtype == dtype, f"{res.dtype} vs {dtype}" | ||
|
||
torch.set_default_dtype(current_def_dtype) | ||
|
||
|
||
class TestPad: | ||
|
||
def test_pad(self): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think we should have 2
raises
blocks here, one per line. This is because we want to make sure that both lines below raise a TypeError. With a single block, we're only asserting that one of them raises the error. In fact I think the original test code was incorrect.Also, we don't need those assertions to be part of the parametrization, it's a bit overkill as they don't need the
channel
parameter. So I'd suggest to extract them into a separatetest_pil_to_tensor_errors()
test function