Skip to content
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

ADD: ApplySomeOf transforms to Random Choice #7586

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ambujpawar
Copy link
Contributor

@ambujpawar ambujpawar commented May 13, 2023

Extended RandomChoice to include ApplySomeOf transforms.
Will add tests if you're happy with the functional implementation

@pytorch-bot
Copy link

pytorch-bot bot commented May 13, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/7586

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures, 1 Unrelated Failure

As of commit e737d47 with merge base f69eee6 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@ambujpawar
Copy link
Contributor Author

@NicolasHug can you have a look please

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 for the PR @ambujpawar and sorry for such a late review - thanks for the reminder. The PR looks good so far, I left a few comments below, LMK what you think.

@@ -129,16 +130,24 @@ class RandomChoice(Transform):
p (list of floats or None, optional): probability of each transform being picked.
If ``p`` doesn't sum to 1, it is automatically normalized. If ``None``
(default), all transforms have the same probability.
max_transforms(int, optional): The maximum number of transforms that can be applied.
If specified, ``p`` is ignored and a random number of transforms sampled from
[1, ``max_transforms``] is applied.
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 sample from [0, max_transforms] as in the original feature requets

Comment on lines 164 to 165
idx = int(torch.multinomial(torch.tensor(self.p), 1))
transform = self.transforms[idx]
Copy link
Member

Choose a reason for hiding this comment

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

These 2 lines here should probably be part of the if self.p block below

Comment on lines +171 to +173
selected_transforms = random.sample(self.transforms, k=random.randint(1, self.max_transforms))
random.shuffle(selected_transforms)
return Compose(selected_transforms)(*inputs)
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 try to implement that without using the builtin random module and instead just rely on pytorch's RNG. torch.randperm can probably be used for that

Comment on lines +5327 to +5329
def test_assertions_p_and_max_transforms(self):
with pytest.raises(ValueError, match="Only one of `p` and `max_transforms` should be specified."):
transforms.RandomChoice([transforms.Pad(2), transforms.RandomCrop(28)], p=[1], max_transforms=3)
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this is due to the fact that this file has change a lot in the past, but this test is now part of test_pure_tensor_heuristic() and it shouldn't be there. We have a def test_random_choice() somewhere, maybe we should make it a class and put those tests together.
Also, it'd be nice to test the behaviour of the new max_transforms parameter

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.

[Transforms] ApplySomeOf
3 participants