Skip to content

Conversation

vivekkumar7089
Copy link
Contributor

Refactor Group D as mentioned in #3987

  • group D
    • test_random_affine -- split this into different parametrized functions (one for shear, one for scale, etc.)
    • test_random_rotate -- parametrize over all loop variables
    • test_random_perspective -- parametrize over all loop variables
    • test_to_grayscale -- parametrize over meth_kwargs

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @vivekkumar7089 ! a few comments but this looks good

self.device = "cuda"


def _test(device, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

let's call this _test_random_affine_helper to be more specific

Comment on lines +701 to +702
with get_tmp_dir() as tmp_dir:
s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt"))
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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!

Copy link
Contributor Author

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.

def test_random_rotate_save():
    s_transform = torch.jit.scriptT.RandomAffine(...)
    with get_tmp_dir() as tmp_dir:     
         s_transform.save(os.path.join(tmp_dir, "t_random_rotate.pt")

And also in a similar manner in the function test_random_perspective.
@NicolasHug

Copy link
Member

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!

Copy link
Contributor Author

@vivekkumar7089 vivekkumar7089 Jun 8, 2021

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)

Copy link
Member

@NicolasHug NicolasHug Jun 9, 2021

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!

Comment on lines +723 to +724
with get_tmp_dir() as tmp_dir:
s_transform.save(os.path.join(tmp_dir, "t_perspective.pt"))
Copy link
Member

Choose a reason for hiding this comment

The 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('tol', [1.0 + 1e-10])
Copy link
Member

Choose a reason for hiding this comment

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

as there's only one value, we don't need to parametrize over it. Let's just write tol = ... in the function body

@pytest.mark.parametrize('device', cpu_and_gpu())
@pytest.mark.parametrize('tol', [1.0 + 1e-10])
@pytest.mark.parametrize('num_output_channels', [0, 1, 3])
def test_to_grayscale(device, num_output_channels, tol):
Copy link
Member

Choose a reason for hiding this comment

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

here maybe it woudl make more sense to parametrize over

@pytest.mark.parametrize('Klass, meth_kwargs', [
   (T.Grayscale, {"num_output_channels": 1}),
   (T.Grayscale, {"num_output_channels": 3}),
   (T.RandomGrayscale, {}),

so that we can remove the if/else in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @NicolasHug !! I will take care of all comments which you have made.


@pytest.mark.parametrize('device', cpu_and_gpu())
def test_random_affine(device):
s_transform = _test(device, degrees=0.0)
Copy link
Member

Choose a reason for hiding this comment

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

I realize this was already present, but we don't need to get s_transform from _test(). _test() doesn't need to return anything and we can just declare s_transform here as s_transform = torch.jit.scriptT.RandomAffine(...)

(where ... may actually be empty)

@NicolasHug NicolasHug merged commit 4db4d15 into pytorch:master Jun 9, 2021
@NicolasHug
Copy link
Member

I took care of a minor pep8 issue, thanks a lot @vivekkumar7089 !

@vivekkumar7089 vivekkumar7089 deleted the port-test_transformtensor_to_pytest branch June 9, 2021 11:20
facebook-github-bot pushed a commit that referenced this pull request Jun 14, 2021
…pytest (#4000)

Reviewed By: fmassa

Differential Revision: D29097740

fbshipit-source-id: c8303ca0665d6ee7e2420b7be9ce6eabe483a010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants