learnables / learn2learn

A PyTorch Library for Meta-learning Research
http://learn2learn.net
MIT License
2.61k stars 350 forks source link

What does l2l.vision.transforms.RandomClassRotation(dataset, [0.0, 90.0, 180.0, 270.0]) do? #372

Closed brando90 closed 1 year ago

brando90 commented 1 year ago

I noticed omniglot has this. What is it doing and why do we need it?

I'm reading:

class RandomClassRotation(object):
    """

    [[Source]]()

    **Description**

    Samples rotations from a given list uniformly at random, and applies it to
    all images from a given class.

    **Arguments**

    * **degrees** (list) - The rotations to be sampled.

    **Example**
    ~~~python
    transform = RandomClassRotation([0, 90, 180, 270])
    ~~~

    """

    def __init__(self, dataset, degrees):
        self.degrees = degrees
        self.dataset = dataset

    def __call__(self, task_description):
        rotations = {}
        for data_description in task_description:
            c = self.dataset.indices_to_labels[data_description.index]
            if c not in rotations:
                rot = random.choice(self.degrees)
                if float(tv.__version__.split('.')[1]) >= 11:
                    rotations[c] = transforms.RandomRotation((rot, rot))
                else:
                    rotations[c] = transforms.Compose(
                        [
                            transforms.ToPILImage(),
                            transforms.RandomRotation((rot, rot)),
                            transforms.ToTensor(),
                        ]
                    )
            rotation = rotations[c]
            data_description.transforms.append(lambda x: (rotation(x[0]), x[1]))
        return task_description

but it's not clear what it does, since it seems the rotation affects the classes? Does it affect the classes or the images?

A high level description is enough.

(a better example that prints the image and class inde if it changes both would be nice too)

brando90 commented 1 year ago

https://datascience.stackexchange.com/questions/116207/what-is-a-random-class-rotation-in-a-few-shot-learning-task

seba-1511 commented 1 year ago

Hello @brando90,

It randomly rotates all of the images in a class by either 0, 90, 180, or 280 degrees. See the original MAML paper for more details.

Hope this helps!