pytorch / pytorch/vision

[Transforms] ApplySomeOf

Open
#7,544 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module: transforms needs discussion new feature
Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🚀 The feature

Hey,

I found it useful to have a someof class e.g. in imgaug to randomly apply a subset of transformations out of a set of transformation with a parameter "max_transforms". The number of transformations in the subset is randomly sampled with random.randint(0, max_number).

I could not found something similar in the repo so here is an example class for v2:

import torchvision
from typing import Sequence, Callable, Any, Optional, List
import random

class ApplySomeOf(torchvision.transforms.v2.Transform):
    def __init__(
        self,
        transforms: Sequence[Callable],
        max_transforms: Optional[int] = None,
        rand_shuffle: Optional[bool] = False,
    ) -> None:
        if not isinstance(transforms, Sequence):
            raise TypeError("Argument transforms should be a sequence of callables")

        if max_transforms is None:
            max_transforms = len(transforms)

        super().__init__()

        self.transforms = transforms
        self.max_transforms = max_transforms
        self.rand_shuffle =rand_shuffle

    def forward(self, *inputs: Any) -> Any:
        sample = inputs if len(inputs) > 1 else inputs[0]
        selected_transforms = random.sample(self.transforms, k=random.randint(0, self.max_transforms))

        if self.rand_shuffle:
            random.shuffle(selected_transforms)

        for transform in selected_transforms:
            sample = transform(sample)

        return sample


What do you think? Could this be integrated? Or did I miss a similar functionality?

Motivation, pitch

Imgaug has it. It is quite useful.

Alternatives

No response

Additional context

No response

cc @vfdev-5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the torchvision.transforms.v2.Transform entry point and the proposed ApplySomeOf example; no specific files or tests are named in the issue. Done should provide an agreed integration of randomly selecting and optionally shuffling a subset of transforms, with behavior and tests defined by the project.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.