6

I am currently using a model from tf.keras.applications for training. And a data augmentation layer along with it. Wierdly, after I import the model from applications, the augmentation layer does not work. The augmentation layer does work before I import it. What is going on?

Also, this has only started happening recently after the new version of TF 2.8.0 was released. Before it was working all fine.

The code for the augmentation layer is

data_augmentation = tf.keras.Sequential([
  tf.keras.layers.RandomFlip("horizontal_and_vertical"),
  tf.keras.layers.RandomRotation(0.5),
])

And I am importing the model using

base_model = tf.keras.applications.MobileNetV3Small(
                 input_shape=(75, 50, 3), alpha=1.0, 
                 weights='imagenet', pooling='avg', include_top=False,
                 dropout_rate=0.1, include_preprocessing=False)

Please help me understand what is going on. You can reproduce the code here on this notebook https://colab.research.google.com/drive/13Jd3l2CxbvIWQv3Y7CtryOdrv2IdKNxD?usp=sharing

1
  • Confirmed not working in colab, that is weird... Commented Feb 17, 2022 at 23:52

3 Answers 3

11

I noticed same issue with tf 2.8. It can be solved by add training =True , when you test the augmentation layer:

aug = data_augmentation(image,training=True)

The reason is that the augmentation layer behaves differently in training and predicting (inference), i.e. it will do augmentation in training but do nothing in predicting. Ideally, the layer should set the training= argument smartly according the situation. Apparently, it is not smart in above code: it does not know your intention is to test the layer.

But I think you should still leave training argument as default when you build the full model, letting the augmentation layer do the job.

1
  • 1
    This is actually very helpful. When trying to view augmented images before adding the layer to your model, adding training=True allows you to do so.
    – SmiffyKmc
    Commented May 20, 2022 at 15:49
0

You cannot see the effect of augmentation from a single output. Please set a range to see the effect of augmentation.

data_augmentation = tf.keras.Sequential([
  tf.keras.layers.RandomFlip("horizontal_and_vertical",seed=5),
  tf.keras.layers.RandomRotation(0.5),
])
for i in range(4):
  aug = data_augmentation(image)
  plt.imshow(aug.numpy().astype('uint8'))
  plt.show()
2
  • ofcourse i tried it multiple times. not working
    – Kriz Moses
    Commented Mar 22, 2022 at 19:32
  • This also doesn't work for me. Random flip does not work. Only random cropping works even with training=True.
    – Ron
    Commented Apr 13, 2023 at 20:46
0

Let me tell you Here is a big correction to do for data augmentation the data is not being augmented. You only have to write

`data_augmentation = tf.keras.Sequential([
  tf.keras.layers.RandomFlip("horizontal_and_vertical",seed=5),
  tf.keras.layers.RandomRotation(0.5),
])
for i in range(4):
  aug = data_augmentation(image, training = True)
  plt.imshow(aug.numpy().astype('uint8'))
  plt.show()`

Now it should work and your images should be augmented. It is an error and it should be fixed in the next update I think so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.