-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Port random affine, rotate, perspective and to_grayscale to pytest #4000
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
NicolasHug
merged 6 commits into
pytorch:master
from
vivekkumar7089:port-test_transformtensor_to_pytest
Jun 9, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4b457d
Port random affine, rotate, perspective and to_grayscale to pytest
vivekkumar7089 19047cb
Defined separate save function
vivekkumar7089 e0f60f0
Merging to update it from master
vivekkumar7089 aa64908
Merge branch 'master' of github.com:pytorch/vision into port-test_tra…
NicolasHug c1141f4
pep8
NicolasHug aa4a1ae
again
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 hidden or 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
_create_data_batch, | ||
_assert_equal_tensor_to_pil, | ||
_assert_approx_equal_tensor_to_pil, | ||
cpu_and_gpu | ||
cpu_and_gpu, | ||
) | ||
from _assert_utils import assert_equal | ||
|
||
|
@@ -286,100 +286,6 @@ def test_resized_crop(self): | |
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_resized_crop.pt")) | ||
|
||
def test_random_affine(self): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) | ||
|
||
def _test(**kwargs): | ||
transform = T.RandomAffine(**kwargs) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
return s_transform | ||
|
||
for interpolation in [NEAREST, BILINEAR]: | ||
for shear in [15, 10.0, (5.0, 10.0), [-15, 15], [-10.0, 10.0, -11.0, 11.0]]: | ||
_test(degrees=0.0, interpolation=interpolation, shear=shear) | ||
|
||
for scale in [(0.7, 1.2), [0.7, 1.2]]: | ||
_test(degrees=0.0, interpolation=interpolation, scale=scale) | ||
|
||
for translate in [(0.1, 0.2), [0.2, 0.1]]: | ||
_test(degrees=0.0, interpolation=interpolation, translate=translate) | ||
|
||
for degrees in [45, 35.0, (-45, 45), [-90.0, 90.0]]: | ||
_test(degrees=degrees, interpolation=interpolation) | ||
|
||
for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: | ||
_test(degrees=0.0, interpolation=interpolation, fill=fill) | ||
|
||
s_transform = _test(degrees=0.0) | ||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_random_affine.pt")) | ||
|
||
def test_random_rotate(self): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) | ||
|
||
for center in [(0, 0), [10, 10], None, (56, 44)]: | ||
for expand in [True, False]: | ||
for degrees in [45, 35.0, (-45, 45), [-90.0, 90.0]]: | ||
for interpolation in [NEAREST, BILINEAR]: | ||
for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: | ||
transform = T.RandomRotation( | ||
degrees=degrees, interpolation=interpolation, expand=expand, center=center, fill=fill | ||
) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")) | ||
|
||
def test_random_perspective(self): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=self.device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=self.device) | ||
|
||
for distortion_scale in np.linspace(0.1, 1.0, num=20): | ||
for interpolation in [NEAREST, BILINEAR]: | ||
for fill in [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]: | ||
transform = T.RandomPerspective( | ||
distortion_scale=distortion_scale, | ||
interpolation=interpolation, | ||
fill=fill | ||
) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_perspective.pt")) | ||
|
||
def test_to_grayscale(self): | ||
|
||
meth_kwargs = {"num_output_channels": 1} | ||
tol = 1.0 + 1e-10 | ||
_test_class_op( | ||
T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, | ||
tol=tol, agg_method="max" | ||
) | ||
|
||
meth_kwargs = {"num_output_channels": 3} | ||
_test_class_op( | ||
T.Grayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, | ||
tol=tol, agg_method="max" | ||
) | ||
|
||
meth_kwargs = {} | ||
_test_class_op( | ||
T.RandomGrayscale, meth_kwargs=meth_kwargs, test_exact_match=False, device=self.device, | ||
tol=tol, agg_method="max" | ||
) | ||
|
||
def test_normalize(self): | ||
fn = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) | ||
tensor, _ = _create_data(26, 34, device=self.device) | ||
|
@@ -736,5 +642,125 @@ def setUp(self): | |
self.device = "cuda" | ||
|
||
|
||
def _test_random_affine_helper(device, **kwargs): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) | ||
transform = T.RandomAffine(**kwargs) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
def test_random_affine(device): | ||
transform = T.RandomAffine(degrees=45.0) | ||
s_transform = torch.jit.script(transform) | ||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_random_affine.pt")) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('shear', [15, 10.0, (5.0, 10.0), [-15, 15], [-10.0, 10.0, -11.0, 11.0]]) | ||
def test_random_affine_shear(device, interpolation, shear): | ||
_test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, shear=shear) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('scale', [(0.7, 1.2), [0.7, 1.2]]) | ||
def test_random_affine_scale(device, interpolation, scale): | ||
_test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, scale=scale) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('translate', [(0.1, 0.2), [0.2, 0.1]]) | ||
def test_random_affine_translate(device, interpolation, translate): | ||
_test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, translate=translate) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('degrees', [45, 35.0, (-45, 45), [-90.0, 90.0]]) | ||
def test_random_affine_degrees(device, interpolation, degrees): | ||
_test_random_affine_helper(device, degrees=degrees, interpolation=interpolation) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) | ||
def test_random_affine_fill(device, interpolation, fill): | ||
_test_random_affine_helper(device, degrees=0.0, interpolation=interpolation, fill=fill) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('center', [(0, 0), [10, 10], None, (56, 44)]) | ||
@pytest.mark.parametrize('expand', [True, False]) | ||
@pytest.mark.parametrize('degrees', [45, 35.0, (-45, 45), [-90.0, 90.0]]) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) | ||
def test_random_rotate(device, center, expand, degrees, interpolation, fill): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) | ||
|
||
transform = T.RandomRotation( | ||
degrees=degrees, interpolation=interpolation, expand=expand, center=center, fill=fill | ||
) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
|
||
def test_random_rotate_save(): | ||
transform = T.RandomRotation(degrees=45.0) | ||
s_transform = torch.jit.script(transform) | ||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")) | ||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('distortion_scale', np.linspace(0.1, 1.0, num=20)) | ||
@pytest.mark.parametrize('interpolation', [NEAREST, BILINEAR]) | ||
@pytest.mark.parametrize('fill', [85, (10, -10, 10), 0.7, [0.0, 0.0, 0.0], [1, ], 1]) | ||
def test_random_perspective(device, distortion_scale, interpolation, fill): | ||
tensor = torch.randint(0, 256, size=(3, 44, 56), dtype=torch.uint8, device=device) | ||
batch_tensors = torch.randint(0, 256, size=(4, 3, 44, 56), dtype=torch.uint8, device=device) | ||
|
||
transform = T.RandomPerspective( | ||
distortion_scale=distortion_scale, | ||
interpolation=interpolation, | ||
fill=fill | ||
) | ||
s_transform = torch.jit.script(transform) | ||
|
||
_test_transform_vs_scripted(transform, s_transform, tensor) | ||
_test_transform_vs_scripted_on_batch(transform, s_transform, batch_tensors) | ||
|
||
|
||
def test_random_perspective_save(): | ||
transform = T.RandomPerspective() | ||
s_transform = torch.jit.script(transform) | ||
with get_tmp_dir() as tmp_dir: | ||
s_transform.save(os.path.join(tmp_dir, "t_perspective.pt")) | ||
Comment on lines
+746
to
+747
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. let's move this out as well |
||
|
||
|
||
@pytest.mark.parametrize('device', cpu_and_gpu()) | ||
@pytest.mark.parametrize('Klass, meth_kwargs', [ | ||
(T.Grayscale, {"num_output_channels": 1}), | ||
(T.Grayscale, {"num_output_channels": 3}), | ||
(T.RandomGrayscale, {}) | ||
]) | ||
def test_to_grayscale(device, Klass, meth_kwargs): | ||
|
||
tol = 1.0 + 1e-10 | ||
_test_class_op( | ||
Klass, meth_kwargs=meth_kwargs, test_exact_match=False, device=device, | ||
tol=tol, agg_method="max" | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
Let's move this into a separate test: with all the parametrization, it's called many many times and adds up about 4 seconds to the execution, which is no negligible. We just need to call it once in a test called e.g.
test_random_rotate_save
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.
Hi @NicolasHug !! I have changed everything, the rest of this. Actually, I couldn't find a way to reuse
s_transform
outside of its parent function. Could you please help me to solve this problem?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 made a comment above, hopefully this helps? LMK!
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.
It means here I can define a function.
And also in a similar manner in the function
test_random_perspective
.@NicolasHug
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.
Yes this sounds like the right way here!
Uh oh!
There was an error while loading. Please reload this page.
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.
Hello @NicolasHug !!
s_transform = torch.jit.script(T.RandomAffine())
and
s_transform = torch.jit.script(T.RandomRotation())
both are giving same error -: TypeError: init() missing 1 required positional argument: 'degrees'
while
s_transform = torch.jit.script(T.RandomPerspective())
is working fine.Can we fix some degrees here? (in above cases)
Uh oh!
There was an error while loading. Please reload this page.
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.
yes we can just pass degree=0
EDIT - or 45 as you did!