In deep learning, overfitting is a common problem that occurs when a model learns the training data too well, to the point that it performs poorly on unseen data. One way to address overfitting is through data augmentation, which involves creating new training samples by applying transformations to existing data. In this tutorial, we will explore various data augmentation techniques using TensorFlow, Keras, and Python.
- Importing Libraries
First, we need to import the necessary libraries for our project. We will be using TensorFlow and Keras for deep learning, as well as NumPy for numerical operations.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
- Loading Dataset
For this tutorial, we will use the Fashion MNIST dataset, which consists of 60,000 training samples and 10,000 test samples of grayscale images in 10 different categories.
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
- Data Augmentation
Next, we will create an ImageDataGenerator object to perform data augmentation. We can specify various transformation parameters such as rotation, zoom, shift, and flip.
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
- Visualizing Augmented Data
To visualize the effect of data augmentation, we will generate augmented images from a single input image.
img = train_images[0]
img = img.reshape((1,) + img.shape + (1,))
i = 0
for batch in datagen.flow(img, batch_size=1):
plt.figure(i)
plt.imshow(batch[0].reshape(28, 28), cmap='gray')
i += 1
if i % 4 == 0:
break
plt.show()
- Training with Augmented Data
Finally, we will train a CNN model on the augmented data to see if it helps in reducing overfitting.
model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
datagen.fit(train_images.reshape(-1, 28, 28, 1))
model.fit(datagen.flow(train_images.reshape(-1, 28, 28, 1), train_labels, batch_size=32),
epochs=10,
validation_data=(test_images.reshape(-1, 28, 28, 1), test_labels))
By using data augmentation, we can effectively address overfitting in deep learning models. Experiment with different augmentation techniques and parameters to find the best combination for your dataset.
Check out our premium machine learning course with 2 Industry projects: https://codebasics.io/courses/machine-learning-for-data-science-beginners-to-advanced
The experimental and preprocessing modules are deprecated. Use this code
data_augmentation = keras.Sequential([ layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
])
20:28
best teacher ever
use layers.RandomFlip(), layers.RandomRotation(), instead of using random.experimental.preprocessing . This fixes the issue of module not found
i have done argumentation but my accuracy have been decrease from 55% to 22% can anyone help
hi in this one error coming for me when I start to do data augmentation. That is " AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental' " . please sir help me to solve this issue. There is no any issue with the libararies. All the libraries are upto date.
hello I am facing some error tried all the solutions on stackoverflow and github even changed code to import tensorflow as tf
from tensorflow.keras import layers
data_augmentation = tf.keras.Sequential(
[
tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal",
input_shape=(img_height,
img_width,
3)),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.1),
tf.keras.layers.experimental.preprocessing.RandomZoom(0.1),
]
) but erros is same, AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental' Help i am stuck on this error
I having an error on the following code : —————————————————————————
error Traceback (most recent call last)
Cell In[24], line 6
4 for image in images:
5 img = cv2.imread(str(image))
—-> 6 resized_img = cv2.resize(img,(180,180))
7 X.append(resized_img)
8 y.append(flowers_labels_dict[flower_name])
error: OpenCV(4.9.0) D:aopencv-pythonopencv-pythonopencvmodulesimgprocsrcresize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' Please help me to resolve it.
num_classes = 5
model = keras.Sequential([
keras.layers.Conv2D(16, kernel_size=(3,3), padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(32, kernel_size=(3,3), padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Conv2D(64, kernel_size=(3,3), padding='same', activation='relu'),
keras.layers.MaxPooling2D(),
keras.layers.Flatten(),
keras.layers.Dense(1024, activation='relu'),
keras.layers.Dense(512, activation='relu'),
keras.layers.Dense(num_classes)
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
model.fit(X_train_scaled, y_train, epochs=30)
With this u can get 1.0 accuracy
Why don't you use normalization instead of dividing it to 255?
thank you sir
What about the augmentation of tabular data ?
Very well explained sir…🤗
😍😍😍😍😍😍😍😍
I am noticing that no one is talking about the loss before augmentation and loss after augmentation and how to reduce it
Seems it would be better to encode image using measures that are scale and translation invariant.
incredible Video. Hey how can I get the Slides you used in this video?
I was trying to use loss='SparseCategoricalCrossentropy' in the cnn model and i got the same accuracy after each epochs. Can someone explain me why it's happening??
I have faced some errors…..I don't fit x and y…..any one help to me???