RandAugment for Image Classification for Improved Robustness

Hi folks,

I always wanted to use RandAugment to improve the robustness of my vision models. But never found a straightforward example that showed how to use it in the context of TensorFlow and Keras.

Now, together with the community, we have one such example showing a clear advantage of RandAugment for improving the robustness of vision models:

The example shows how to use the imgaug library together with tf.py_function inside tf.data pipelines. Of course, it has its own demerits. But hey, that’s life!

6 Likes

Nice but It could be useful to expose to the user what we have at https://github.com/tensorflow/models/blob/master/official/vision/beta/ops/augment.py
https://github.com/tensorflow/models/blob/master/official/vision/image_classification/augment.py

Probably TF ops could have a better performance then tf.py_function with imageaug.

1 Like

Certainly yes and I note that in the example itself. However, the example is more focused on readability and ease of use. I didn’t find the official one that you shared to not that easy-to-use.

But I am interested to know more. Feel free to provide a minimal example of using the official one if you have time.

1 Like

You can find a code snippet in preprocess

1 Like

Yeah, I am aware of it. I would take your suggestion and include a link to it so that readers are aware.

1 Like

These objects are exposed with tf-models-nightly · PyPI

1 Like

Both of these are pretty cool.

augment.py

That seems like something we could mention in the image data augmentation tutorial. I’m sure some users end up on that page looking for RandAugment or another higher level function and are disappointed.

2 Likes

I think the main issue there Is that externally, to the users, Is not clear if we want that tf-models could be used also as library or if we are waiting that keras, keras-cv and keras-nlp will be standalone.

1 Like

That is a really good idea. I have seen that tutorial evolve over time and I really appreciate the effort.

Yes, exactly, that seems to be the case.

1 Like

Here’s how we can take advantage of the RandAugment class from tf-models.

First, install the nightly build: !pip install -q tf-models-nightly.

Then -

from official.vision.beta.ops import augment

# Recommended is m=2, n=9
augmenter = augment.RandAugment(num_layers=3, magnitude=10)

dataset = load_dataset(filenames)
dataset = dataset.shuffle(batch_size*10)
dataset = dataset.map(augmenter.distort, num_parallel_calls=AUTO)
...

load_dataset() takes image filepaths and reads images from those paths and creates a tf.data.Dataset object.

5 Likes

I’m one of those people!

+1 for RandAugment being added to the data augmentation tutorial

2 Likes

On a related note, here’s a tutorial that shows the use of RandAugment from TensorFlow Model Garden, thereby allowing for faster training with TPUs (since it is implemented using native TF ops):

3 Likes