Skip to content

Rotation transformation #566

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

Open
noeagles opened this issue Aug 3, 2018 · 15 comments
Open

Rotation transformation #566

noeagles opened this issue Aug 3, 2018 · 15 comments

Comments

@noeagles
Copy link

noeagles commented Aug 3, 2018

I noticed that RandomRotation range only from (min, max); however it is more realistic to rotate from the set (angle1, angle2, angle3) randomly?

@noeagles noeagles changed the title Rotation transfomation Rotation transformation Aug 3, 2018
@fmassa
Copy link
Member

fmassa commented Aug 3, 2018

Could you be a bit more clear on what you mean by those three angles?

@noeagles
Copy link
Author

noeagles commented Aug 4, 2018

When images are being randomly rotated, they should be rotated angle1 or angle2 or angle3 degrees, not from (mix, max) degrees

@fmassa
Copy link
Member

fmassa commented Aug 13, 2018

If I understand correctly, you are saying that we should propose the rotations to be randomly sampled from a discrete subset, instead of having them being sampled from a continuous interval, is that right?

If that's the case, I'd say that having it being sampled from an interval is more generic, and should probably be kept.

@sotte
Copy link
Contributor

sotte commented Sep 9, 2018

I agree with @fmassa. That being said, @noeagles, you could use the functional transforms and build that functionality yourself.

import torchvision.transforms.functional as TF
import random

angle = random.choice([-30, -15, 0, 15, 30])
img = TF.rotate(img, angle)

@noeagles
Copy link
Author

noeagles commented Sep 9, 2018

@fmassa @sotte Thank you both very much!! I came up with this issue mainly because that many papers i encoutered use discrete rotation as data augmentation. Thanks again!

@noeagles noeagles closed this as completed Sep 9, 2018
@VesninEN
Copy link

How it can be used inside the transforms.Compose([...])?

@sotte
Copy link
Contributor

sotte commented Sep 27, 2019

Something like this would work:

import torchvision.transforms.functional as TF
import random
from typing import Sequence

class MyRotateTransform:
    def __init__(self, angles: Sequence[int]):
        self.angles = angles

    def __call__(self, x):
        angle = random.choice(self.angles)
        return TF.rotate(x, angle)

@fmassa
Copy link
Member

fmassa commented Sep 30, 2019

@sotte do you think this snippet could be added somewhere in torchvision? Maybe in the documentation of rotate?

@sotte
Copy link
Contributor

sotte commented Sep 30, 2019

@fmassa Um, if you think it's helpful, sure :)

If we add this we should maybe just document the general procedure how you can use TF to build your own transform classes (and use the example from above).

I guess I could add it to https://pytorch.org/docs/stable/torchvision/transforms.html#functional-transforms.

I can create a PR (let me know if want something specific), but you can also just add the snippet. Both is fine with me.

@fmassa
Copy link
Member

fmassa commented Sep 30, 2019

I think a note in the documentation of the functional interface, explaining that it gives full flexibility sounds like best thing to do, with an example for a particular case, like the rotation that you proposed just above

@sotte
Copy link
Contributor

sotte commented Oct 1, 2019

I'll send the PR later today.

@crypdick
Copy link

@fmassa I have a use-case which prohibits the use of "generic interval" rotations because it destroys information I need in the corners. In particular, I can only use order-4 symmetry transforms (image attached). Using only reflections (RandomHorizontalFlip and RandomVerticalFlip) I can only get 4 of the 8 possible transforms.


wiki/File:Dih4_cycle_graph.svg

These order-4 symmetry transforms are ubiquitous, for instance, in computer vision for satellite imagery research. I'm surprised that it isn't a built-in.

@fmassa
Copy link
Member

fmassa commented Jul 30, 2020

@crypdick that is a valid point.

@vfdev-5 thoughts on this?

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Aug 2, 2020

@crypdick yes, rotations by 90 degrees as you suggest are definitely make sense for satellite, medical imagery and as data augmentations in torchvision. IMO, the reason why this is not yet built-in in torchvision, probably, due to "photography" type of imagery intended to be processed (like ImageNet) where upside-down rotation does not make much sense...

@fmassa I agree that this can be an enhancement for transforms and functional operations.

@YannDubs
Copy link

YannDubs commented Apr 6, 2021

I agree that having the D_4 group augmentation as shown by @crypdick is very useful for many application ( biological, chemical and satellite data comes to mind).

It is very easy to get it though:

from torchvision.transforms import (
    Compose,
    RandomApply,
    RandomHorizontalFlip,
    RandomRotation,
    RandomVerticalFlip,
)

transform = Compose(
            [
                RandomHorizontalFlip(p=0.5),
                RandomVerticalFlip(p=0.5),
                RandomApply([RandomRotation((90, 90))], p=0.5),
            ]
        )

But a shorthand and quicker version might be useful.

rajveerb pushed a commit to rajveerb/vision that referenced this issue Nov 30, 2023
* [LLM] Initial commit of the PaxML implementation

* [LLM] Update paxml link

* [LLM] checkpoint loading script and document

* [LLM] SPM generation steps

* [LLM] SPM generation step correction

* [LLM] Add script to select example from dataset

* [LLM PaxML] Sync model with PaxML 00156e6

* [LLM PaxML] update readme

* [GPT-3 PaxML] Update model to the latest version.

* [LLM] update c4_mllog.py to latest APIs

* [LLM] Update README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants