question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add augmentation probability to tf.keras.layers.BaseImageAugmentationLayer augmentation rate

See original GitHub issue

System information.

TensorFlow version (you are using): 2.9.1 Are you willing to contribute it (Yes/No): Yes

Describe the feature and the current behavior/state.

Currently, all image augmentation layers inherit from an abstract base layer, BaseImageAugmentationLayer. This layer has a variable rate in its constructor; however, this variable is never used in any of its subclasses.

I think it would be nice if this variable was used, as its name implies, to control the probability of a specific augmentation technique being applied. This could be useful in cases where there is a significant number of images for training/fine-tuning a model, and there is only the need to apply a little bit of data augmentation, without having to augment every image in the dataset, with the computational cost it would imply.

Will this change the current api? How? It shouldn’t change the API: all augmentation layers currently accept the rate variable as part of **kwargs, and thus rate is set in the constructor of BaseImageAugmentationLayer. The only issue is that this variable is currently unused.

Who will benefit from this feature? As mentioned before, this can reduce the computational cost implied in augmenting all the images in a dataset, in cases where there is no real need to augment all the images.

Contributing

  • Do you want to contribute a PR? (yes/no): yes

  • If yes, please read this page for instructions: read and understood 👍

  • Briefly describe your candidate solution(if contributing): My solution would be to add another if statement, just at the beginning of the _augment function of BaseImageAugmentationLayer, which would use the random number generator and the rate variable to determine whether or not to augment each image (whether individual images or each image in a batch). However, even if the straightforward solution would be to use the already existing random number generator to determine if a specific image would be augmented or not, this would cause reproducibility issues when trying to retrain models, since the same RNG would be called more times. The way I see it, there are two ways of fixing this:

    1. Adding a special if self.rate == 1.0 condition, to avoid calling the RNG. Assuming that nobody has used the rate variable in the constructor of image augmentation layers, this should avoid reproducibility issues.
    2. Adding another RNG to control image augmentation probability, using the same seed to initialize both generators. As before, if nobody has used the rate variable, this shouldn’t create reproducibility issues.

Furthermore, I don’t know if there would be concurrency issues if many “threads” try using the same RNG. If you could clarify me this, I would be relieved of knowing that reproducibility would be maintained.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
LukeWoodcommented, Sep 15, 2022

Oh, first notice I have of that function!

I couldn’t find any information about this function neither on the TensorFlow page, nor in the Keras CV page. Maybe it should be documented somewhere?

yeah, KerasCV docs are out of date. Lets definitely add it to KerasCV’s API docs.

0reactions
lgleznahcommented, Sep 16, 2022

Perfect then! Is there any way I could help with that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data augmentation | TensorFlow Core
This tutorial demonstrates data augmentation: a technique to increase the diversity of your ... Use the Keras preprocessing layers, such as tf.keras.layers.
Read more >
Image Augmentation with Keras Preprocessing Layers and tf ...
In this post, you will discover how you can use the Keras preprocessing layer as well as the tf.image module in TensorFlow for...
Read more >
Custom Image Augmentations with ... - Keras
This guide will show you how to implement your own custom augmentation layers using BaseImageAugmentationLayer . As an example, we will ...
Read more >
Data Augmentation in Python: Everything You Need to Know
Write our own augmentation pipelines or layers using tf.image. ... rate, hardware consumption, and more) using Neptune-Keras integration.
Read more >
Randomly apply Keras `PreprocessingLayer` - Stack Overflow
Currently, there is no such probability parameter to control the occurrence of the keras built-in augmentation layer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found