keras-team / keras-cv

Industry-strength Computer Vision workflows with Keras
Other
999 stars 330 forks source link

Define the best API for users to apply augmentations in a probabilistic manner. #108

Closed LukeWood closed 2 years ago

LukeWood commented 2 years ago

Maybe this will look like:

RandomChance(layer, rate=0.5)

or maybe:

MaybeApply(layer, rate=0.5)

Either way, this is important for the composition of image augmentation pipelines.

bhack commented 2 years ago

I think that we need to think about this evaluating what we want to achieve at https://github.com/keras-team/keras-cv/issues/41#issuecomment-1024588287

kartik4949 commented 2 years ago

Randomise(layer, p=0.5) RandomiseApply(layer, p=0.5) RandomCall(layer, p=0.5)

innat commented 2 years ago

@LukeWood @bhack @qlzh727 @kartik4949

I think two types of functions need to be addressed regarding the processing layers. For examples:

  1. Randomly apply the layer

random_mixup  = RandomApply(Mixup(),  prob = 0.5)
random_cutmix = RandomApply(CutMix(), prob = 0.5)
random_equliz = RandomApply(Equalization(), prob = 0.5)
  1. Randomly pick a layers

random_mixup  = RandomApply(Mixup(),  prob = 0.5)
random_cutmix = RandomApply(CutMix(), prob = 0.5)

random_blur   = RandomPick( 
          [
              RandomApply(GaussianBlur(), prob = 0.5),
              RandomApply(MotionBlur(),   prob = 0.5),
              RandomApply(MediamBlur(),   prob = 0.5)
          ], 
     prob = 0.8
)
kartik4949 commented 2 years ago

@innat I think by RandomPick you mean RandAug in defined space!

innat commented 2 years ago

It's like the OneOf method that is provided in argumentation (cell 7) or img_aug libraries.

bhack commented 2 years ago

I think that we need to reason a little bit more in general about what kind of access to the augmentation layers an auto augmentation policy need to achieve. Then I suppose we could think at RandomApply as just one of the simplest policy.

I don't know if @haifeng-jin has any feedback.

bhack commented 2 years ago

Just a reminder, we had also: https://github.com/keras-team/autokeras/issues/879 https://autokeras.com/block/#imageaugmentation

LukeWood commented 2 years ago

We've decided to include a rate argument in a base augmentation layer. This provides the best user API imo:

RandomRotation(rate=0.5)

instead of:

MaybeApply(RandomRotation(), 0.5)

Scott is working on this in his base augmentation layer.

innat commented 2 years ago

@LukeWood instead of giving its name as rate, how about prob?

innat commented 2 years ago

@LukeWood @bhack @qlzh727 @kartik4949

I think two types of functions need to be addressed regarding the processing layers. For examples:

  1. Randomly apply the layer
random_mixup  = RandomApply(Mixup(),  prob = 0.5)
random_cutmix = RandomApply(CutMix(), prob = 0.5)
random_equliz = RandomApply(Equalization(), prob = 0.5)
  1. Randomly pick a layers
random_mixup  = RandomApply(Mixup(),  prob = 0.5)
random_cutmix = RandomApply(CutMix(), prob = 0.5)

random_blur   = RandomPick( 
          [
              RandomApply(GaussianBlur(), prob = 0.5),
              RandomApply(MotionBlur(),   prob = 0.5),
              RandomApply(MediamBlur(),   prob = 0.5)
          ], 
     prob = 0.8
)

@qlzh727 what do you think about the above issue (no. 2). ?

LukeWood commented 2 years ago

we went back and forth on probability and rate and picked rate`. No 2. seems like something I may implement during RandAugment, but I don't think we should offer it until there is a need.

innat commented 2 years ago

@LukeWood There is a need for no. 2 in practice. What sort of details do you think need to clarify this?

LukeWood commented 2 years ago

Historically, this is the sort of thing that isn't included in the Keras API and is delegated to users. This is pretty trivial to implement:

class RandomChoice():
    def __init__(self, layers):
        self.layers = layers
        self.n = len(layers)

    def call(self, inputs):
        return layers[tf.random.choice(range(self.n))](inputs)

with some tensorflow-friendly changes takes care of it.

LukeWood commented 2 years ago

If we're already using this internally I don't see an issue with exposing it, but I don't particularly think it's a big value add to support is all.

bhack commented 2 years ago

I still hope that we could work more in general with augmentation policy classes or that we could actively coordinate with autokeras (If It Is still an active project)

innat commented 2 years ago

@LukeWood What you're referring trivial to implement, in the breath, you can also say that implementing rate is also trivial to implement to end-users. The feature request in no. 2 is not actually a random request. It's followed in practice.

If you see other popular augmentation libraries, for example, img_aug and albumentation, all of them offers such feature to users. In addition, if you see the torch-vision, you would find the same. In torch, you can use RandomChoice to apply a single transformation randomly picked from a list with a given probability. If this info motivates you, please reconsider the request.