BEST IMAGE AUGMENTATION CODE YOU WILL FIND ON INTERNET | by Sudhanshu

BEST IMAGE AUGMENTATION CODE YOU WILL FIND ON INTERNET | by Sudhanshu

Link : https://colab.research.google.com/drive/1e4sppYddlRZvI6izuDPJPJmYTvVC17Ao?usp=sharing

What is Data/Image Augmentation ?

Data augmentation is a technique commonly used in machine learning and computer vision, particularly in the context of deep learning, to artificially increase the size of a dataset by applying various transformations and modifications to the existing data. The primary goal of data augmentation is to create new training examples that are variations of the original data, without changing the underlying characteristics or labels of the data.

Why is Data/Image Augmentation important ?

1. Increased Data Diversity : Data augmentation techniques introduce diversity into the training dataset. By applying various transformations, you generate new, unique data points from the existing ones.

2. Preventing Overfitting : Overfitting occurs when a machine learning model learns to memorize the training data rather than generalize from it. Augmentation reduces overfitting by providing the model with more training examples .

3. Enhanced Model Robustness : Data augmentation helps make machine learning models more robust to variations in the input data. This is especially important when dealing with real-world data, which may exhibit variations in lighting, orientation, scale, or other factors. Augmentation helps models handle these variations gracefully.

4. Improved Performance with Small Datasets : In many real-world scenarios, collecting a large labeled dataset can be challenging or costly. Data augmentation allows you to artificially increase the effective size of your dataset, making it possible to train effective models with limited data.

5. Increased Model Stability : Augmentation can lead to more stable and reliable training. Since the model has seen many variations of the data, it’s less likely to get stuck in local optima during the optimization process.

6. Transfer Learning : Data augmentation is often used when fine-tuning pretrained models on new tasks. By augmenting the new data, the pretrained model can adapt more effectively to the specifics of the target problem while retaining the valuable knowledge it gained from the original training.

Data augmentation is a crucial technique that enhances the performance and reliability of machine learning models, especially in situations where data is limited or the real-world data environment is complex and varied. It is widely used across various domains, including computer vision, natural language processing, and many other machine learning applications.

When to use Data Augmentation ?

  1. Limited Training Data
  2. Imbalanced Dataset
  3. Reducing Overfitting ( most important reason )
  4. Fine-Tuning Pretrained Models/Transformers
  5. Increasing Model Performance

DATA AUGMENTATION CODE

  1. Import the necessary libraries
import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator

2. Define Train and Test transformations you want :

batch_size = 16  train_datagen = ImageDataGenerator(      rescale = 1.0/255,      # Rescale image     shear_range=0.2,        # Shear transformations     rotation_range=40,      # Rotate images up to 40 degrees     width_shift_range=0.2,  # Shift images horizontally by up to 20%     height_shift_range=0.2, # Shift images vertically by up to 20%     zoom_range=0.2,         # Zoom in/out by up to 20%     horizontal_flip=True,   # Flip images horizontally     fill_mode='nearest'    )  test_datagen = ImageDataGenerator(rescale=1.0/255)  #Don't use transformation on test dataset except rescaling

3. Define the file path and image size for Training and Test data

train_generator = train_datagen.flow_from_directory(     "your_file_path_for_train_data",     target_size=(300,300),     batch_size = batch_size,     class_mode = "binary" )   test_generator = test_datagen.flow_from_directory(     "your_file_path_for_test_data",     target_size=(300,300),      # resize the shape of image     batch_size = batch_size,      class_mode = "binary"       # class mode depends on type of problem you are solving here it is a binary classification     # class_mode="categorical"  # Use "categorical" for multi-class classification )

4. Define you CNN Model

from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense,Flatten, Conv2D,MaxPooling2D,Dropout,Activation  # MODEL  model = Sequential() model.add(Conv2D(32 , (3,3) , input_shape=(300,300,3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2)))   model.add(Conv2D(32 , (3,3) )) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2)))  model.add(Conv2D(64 , (3,3) )) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2,2)))   model.add(Flatten()) model.add(Dense(64)) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation("sigmoid"))   # Compile the Model  model.compile(loss="binary_crossentropy" , optimizer="adam" , metrics=['accuracy'])

5. Here comes the main step that is ” TRAINING THE MODEL

# TRAINING THE MODEL ( Don't use fit , use fit_generator )  model.fit_generator(     train_generator,                    # training data     epochs=30,                          # number of times training is done     validation_data = test_generator,   # test data )

PRACTICAL EXAMPLE TO UNDERSTAND THE IMPORTANCE OF IMAGE AUGMENTATION

Without Image Augmentation

None

Here you can see :

  • Train accuracy : 99.69%
  • Test accuracy : 92.62%

Altough accuracy is high , but the model is certainly overfittinng the training data .

With Image Augmentation

None

Here you can see :

  • Train accuracy : 83% ( approx)
  • Test accuracy : 82–85%

Altough here accuracy is lower than above model , but this model is not overfitting . Hence it is a more reliable model for new data .

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top