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

How to implement OneOf ? #19

Open
Optimox opened this issue Jan 12, 2023 · 0 comments
Open

How to implement OneOf ? #19

Optimox opened this issue Jan 12, 2023 · 0 comments

Comments

@Optimox
Copy link

Optimox commented Jan 12, 2023

Hello,

Thank you very much for this repo, it's very useful.

I wanted to add a new compostion OneOf where you can give a list of augmentations and only one of them is executed according to a random weight.

I tried this but it does not seem to work, could you help me please?

class OneOf:
    def __init__(self, transforms, p=1., weights=[], targets=[['image'],['mask']]):
        self.transforms = transforms
        self.weights = weights
        self.targets = targets
        self.p = p
        
    def __call__(self, force_apply=False, **data):
        need_to_run = force_apply or random.random() < self.p
        which_to_run = random.choices(range(len(self.transforms)),
                                        weights=self.weights)[0]
        transforms = self.transforms[which_to_run] if need_to_run else self.get_always_apply_transforms()
        data = transforms(force_apply, self.targets, **data)
       
        return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant