DALI icon indicating copy to clipboard operation
DALI copied to clipboard

Can we have OneOf operator in Dali?

Open twmht opened this issue 3 years ago • 1 comments

Hi,

I would like to implement OneOf like function in Dali, for example, https://github.com/spytensor/pytorch-image-classification/blob/master/dataset/augmentations.py#L50.

The possible way is to implement condition like function https://docs.nvidia.com/deeplearning/dali/user-guide/docs/examples/general/expressions/expr_conditional_and_masking.html, but this is a bit complicated for implementing OneOf operator. I have no idea how to select one of the operator based on the random generator. for example, if I have four possible operator to be performed, I have to choose only one of these operators based on some probability.

Any better idea?

twmht avatar Aug 26 '22 12:08 twmht

Hi @twmht we are currently working on introducing support for conditional execution in DALI. In the current form, you would need to compute all the possible options eagerly, and than select one as the result, with the planned feature you will be able to effectively split the batch and run the operation only on the part of the input that is actually needed.

We are in progress of refactoring that would allow it, some initial functionality should be landing there in one or two releases, I will update this thread, when we have something that is usable.

klecki avatar Aug 26 '22 13:08 klecki

Hi @twmht, the support for conditional execution in DALI was recently merged and is already available in the nightly builds and will be a part of upcoming 1.23 release. The functionality is now available as experimental, we are still working on adding more features to it, but it should already allow to implement the equivalent of OneOf function.

The PR adds the documentation for this feature: https://github.com/NVIDIA/DALI/pull/4589, you can preview the tutorial under this link: https://github.com/klecki/DALI/blob/cond-intro-tutorial/docs/examples/general/conditionals.ipynb

It is possible to define the generic OneOf function and I will add it to our TODO list, but for the particular case you mentioned, this will be a sketch of the solution:

import nvidia.dali.fn as fn
@nvidia.dali.pipeline.experimental.pipeline_def(enable_conditionals=True):
def one_of_pipeline():
    input = ...
    rng = fn.random.uniform()
    if rng < p0:
        output = fn.<some_op_0>(input)
    elif rng < p0 + p1:
        output = fn.<some_op_1>(input)
    elif rng < p0+ p1+ p2:
        output = fn.<some_op_2>(input)
    else:
        output = fn.<some_op_3>(input)
    return output

Assuming the p0 + p1 + p2 + p3 = 1.0, and each of those are in the range of [0, 1], representing the normalized probabilities of applying specific operations.

klecki avatar Jan 25 '23 13:01 klecki

@klecki

you are awesome. it would be better to add the examples for RandAugment.

twmht avatar Jan 25 '23 15:01 twmht

@twmht thank you for your appreciation. We are actively working on providing a solution for the automatic augmentations, we are planning to cover at least AutoAugment, RandAugment and TrivialAugment so they can be easily used within the pipeline or customized. We are building those solution on top of the conditional execution, I will update you when we get there.

klecki avatar Jan 25 '23 16:01 klecki

Hi @twmht, DALI 1.23 contains the experimental support for conditional execution. The docs are here and the tutorial is here.

DALI 1.24 will add support for and, or and not expressions in graph definition, for now only for inputs of boolean type: https://github.com/NVIDIA/DALI/pull/4629. It should be already available in nightly and weekly builds.

As the functionality needed to handle this issue is released, I will be closing it, further updates on automatic augmentations would be provided in #2880

klecki avatar Mar 06 '23 09:03 klecki