Skip to content

Allow degrees=None in RandomAffine to disable rotation#9444

Closed
sunnycho100 wants to merge 1 commit intopytorch:mainfrom
sunnycho100:allow-degrees-none-randomaffine
Closed

Allow degrees=None in RandomAffine to disable rotation#9444
sunnycho100 wants to merge 1 commit intopytorch:mainfrom
sunnycho100:allow-degrees-none-randomaffine

Conversation

@sunnycho100
Copy link

Closes #5241

Motivation

Right now degrees is a required positional arg in RandomAffine, which forces users to pass degrees=0 just to disable rotation — even when they only care about translation, scale, or shear. Every other parameter already accepts None to opt out, so degrees is the odd one out.

This has been bugging me while working on an augmentation pipeline where I only needed translate + scale. Having to write RandomAffine(0, translate=(...), scale=(...)) felt like a papercut every time.

Changes

Both v1 (transforms.py) and v2 (v2/_geometry.py):

  • Changed degrees from a required positional arg to an optional kwarg defaulting to None
  • When degrees=None, rotation is disabled internally by setting self.degrees = [0.0, 0.0]
  • Added a guard: if degrees=None and none of translate, scale, shear are set either, a ValueError is raised — a no-op RandomAffine() with zero args does not make sense
  • Updated the docstring to reflect the new behavior

Tests (test_transforms.py, test_transforms_v2.py):

  • RandomAffine(degrees=None, translate=(...)) works and sets degrees to [0.0, 0.0]
  • RandomAffine(translate=(...)) works (positional omission)
  • RandomAffine() and RandomAffine(degrees=None) both raise ValueError

Usage

# Before — must explicitly pass 0
RandomAffine(0, translate=(0.2, 0.3))

# After — degrees can be omitted or set to None
RandomAffine(translate=(0.2, 0.3))
RandomAffine(degrees=None, translate=(0.2, 0.3))

Backward-compatible: existing code that passes degrees positionally still works exactly the same.

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 18, 2026

🔗 Helpful Links

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

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

❌ 2 New Failures, 2 Unrelated Failures

As of commit 6b33f6a with merge base b358d66 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@meta-cla
Copy link

meta-cla bot commented Mar 18, 2026

Hi @sunnycho100!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@sunnycho100 sunnycho100 marked this pull request as ready for review March 18, 2026 07:25
@meta-cla meta-cla bot added the cla signed label Mar 18, 2026
@meta-cla
Copy link

meta-cla bot commented Mar 18, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@zy1git
Copy link
Contributor

zy1git commented Mar 19, 2026

@sunnycho100 Thanks a lot for opening this PR! After discussion, we don’t think this is a current priority for TorchVision, so we won’t be merging it at this time.

@zy1git zy1git closed this Mar 19, 2026
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.

Allow more consistent degrees=None in RandomAffine constructor

2 participants