Data augmentation is the process of increasing the amount and diversity of data. We do not collect new data, rather we transform the already present data. Data augmentation is an integral process in deep learning, as in deep learning we need large amounts of data and in some cases it is not feasible to collect thousands or millions of images, so data augmentation comes to the rescue.
It helps us to increase the size of the dataset and introduce variability in the dataset.
The most commonly used operations are-
- Rotation: Rotation operation as the name suggests, just rotates the image by a certain specified degree. In the example below, I specified the rotation degree as 40.
- Shearing: Shearing is also used to transform the orientation of the image.
- Zooming: Zooming operation allows us to either zoom in or zoom out.
- Cropping: Cropping allows us to crop the image or select a particular area from an image.
- Flipping: Flipping allows us to flip the orientation of the image. We can use horizontal or vertical flip.You should use this feature carefully as there will be scenarios where this operation might not make much sense e.g. suppose you are designing a facial recognition system, then it is highly unlikely that a person will stand upside down in front of a camera, so you can avoid using the vertical flip operation.
- Changing the brightness level: This feature helps us to combat illumination changes.You can encounter a scenario where most of your dataset comprises of images having a similar brightness level e.g. collecting the images of employees entering the office, by augmenting the images we make sure that our model is robust and is able to detect the person even in different surroundings
Flowers Dataset
Checkout Dataset features
The flowers dataset has five classes.
Libraries: NumPy
pandas
matplotlib
tensorflow
keras
### Keras preprocessing layer:
You can use the Keras preprocessing layers to resize your images to a consistent shape with tf.keras.layers.Resizing
, and to rescale pixel values with tf.keras.layers.Rescaling
.
You can use the Keras preprocessing layers for data augmentation as well, such as tf.keras.layers.RandomFlip
and tf.keras.layers.RandomRotation
.
There are a variety of preprocessing layers you can use for data augmentation including tf.keras.layers.RandomContrast
, tf.keras.layers.RandomCrop
, tf.keras.layers.RandomZoom
, and others.
There are two ways you can use these preprocessing layers, with important trade-offs.
- Make the preprocessing layers part of your model
model = tf.keras.Sequential([
# Add the preprocessing layers you created earlier.
resize_and_rescale,
data_augmentation,
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
# Rest of your model.
])
- Apply the preprocessing layers to your dataset
aug_ds = train_ds.map(
lambda x, y: (resize_and_rescale(x, training=True), y))
There are two ways:
- First, you will create a
tf.keras.layers.Lambda layer
. This is a good way to write concise code.
def random_invert(factor=0.5):
return layers.Lambda(lambda x: random_invert_img(x, factor))
random_invert = random_invert()
- Next, you will write a new layer via subclassing, which gives you more control.
class RandomInvert(layers.Layer):
def __init__(self, factor=0.5, **kwargs):
super().__init__(**kwargs)
self.factor = factor
def call(self, x):
return random_invert_img(x)
The above Keras preprocessing utilities are convenient. But, for finer control, you can write your own data augmentation pipelines or layers using tf.data
and tf.image
.
-
flip an image:
flipped = tf.image.flip_left_right(image) visualize(image, flipped)
-
Grayscale an image:
grayscaled = tf.image.rgb_to_grayscale(image) visualize(image, tf.squeeze(grayscaled)) _ = plt.colorbar()
-
Image Saturation:
saturated = tf.image.adjust_saturation(image, 3) visualize(image, saturated)
-
Image brightness:
bright = tf.image.adjust_brightness(image, 0.4) visualize(image, bright)
-
Center cropping:
cropped = tf.image.central_crop(image, central_fraction=0.5) visualize(image,cropped)
Applying random transformations to the images can further help generalize and expand the dataset. The current tf.image API provides eight such random image operations (ops):
- tf.image.stateless_random_brightness
- tf.image.stateless_random_contrast
- tf.image.stateless_random_crop
- tf.image.stateless_random_flip_left_right
- tf.image.stateless_random_flip_up_down
- tf.image.stateless_random_hue
- tf.image.stateless_random_jpeg_quality
- tf.image.stateless_random_saturation
Random brightness and contrast:
Data Augmentation
Subclassing
Data Augmentation | Tensorflow
Subclassing
If you have any feedback, please reach out at pradnyapatil671@gmail.com
I am an AI Enthusiast and Data science & ML practitioner