×

The following line adds a max-pooling layer with a pool size of (2, 2), which reduces the spatial dimensions of the input by taking the maximum value within each pool:
model.add(MaxPooling2D(pool_size=(2, 2)))

The following line adds a dropout layer with a rate of 0.25, which randomly sets 25% of the input units to 0 during training. Dropout helps prevent overfitting by introducing randomness and reducing the reliance on specific features:
model.add(Dropout(0.25))

The code continues adding more convolutional layers, max-pooling layers, and dropout layers, and finally ends with fully connected (dense) layers:
model.add(Conv2D(64, (3, 3), activation=’relu’, padding=’same’))
model.add(Conv2D(64, (3, 3), activation=’relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

The following line flattens the previous layer’s output to a 1D tensor, preparing it to be connected to a dense layer:
model.add(Flatten())

The following line adds a dense layer with 512 units and ReLU activation:
model.add(Dense(512, activation=’relu’))

The following line adds a dropout layer with a rate of 0.5:
model.add(Dropout(0.5))

The following line adds a final dense layer with 10 units and softmax activation, which produces a probability distribution over the 10 classes for classification:
model.add(Dense(10, activation=’softmax’))

The following code initializes an instance of the ImageDataGenerator class from Keras, which is used for data augmentation in image datasets:
# Define the data augmentation parameters
datagen = ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True
)
# Compile the model
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’,\
    metrics=[‘accuracy’])
# Train the model with data augmentation
history = model.fit(datagen.flow(x_train, y_train, \
    batch_size=64), epochs=100, \
    validation_data=(x_test, y_test))
# Evaluate the model on the test set
score = model.evaluate(x_test, y_test, verbose=0)
print(‘Test loss:’, score[0])
print(‘Test accuracy:’, score[1])

This code defines a CNN with two convolutional layers, two max-pooling layers, and three fully connected layers. Data augmentation is performed using the ImageDataGenerator class, which randomly applies various transformations to the training images to generate more training data. The model is trained for 100 epochs using the fit method with the data generator as the input. Finally, the model is evaluated on the test set using the evaluate method.

Summary

In this chapter, we covered a variety of image data augmentation techniques. We learned how to implement an SVM with data augmentation in Python using the scikit-learn and Keras libraries. We first implemented SVM with the default hyperparameters and evaluated the performance of the classifier on the original dataset. We then implemented an SVM with data augmentation and trained the classifier on each batch of training data generated by the ImageDataGenerator object. Finally, we evaluated the performance of the classifier on the augmented dataset.

We also saw how to implement a CNN using augmentation with the CIFAR-10 dataset. Using data augmentation, we were able to improve the accuracy of the classifier on the augmented dataset. This demonstrates the effectiveness of data augmentation in improving the performance of machine learning models, especially in cases where the available dataset is limited.

Data augmentation can reduce the need for manual annotation by creating variations of existing labeled data. Instead of labeling each transformed image separately, augmentation techniques allow for the generation of additional labeled samples without the need for additional human annotation efforts.

In the next chapter, we will explore how to label text data using generative models.

Leave a Reply

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

Related Posts

Example of video data labeling using k-means clustering with a color histogram – Exploring Video Data

Let us see example code for performing k-means clustering on video data using the open source scikit-learn Python package and the Kinetics...

Read out all

Frame visualization – Exploring Video Data

We create a line plot to visualize the frame intensities over the frame indices. This helps us understand the variations in intensity...

Read out all

Appearance and shape descriptors – Exploring Video Data

Extract features based on object appearance and shape characteristics. Examples include Hu Moments, Zernike Moments, and Haralick texture features. Appearance and shape...

Read out all

Optical flow features – Exploring Video Data

We will extract features based on the optical flow between consecutive frames. Optical flow captures the movement of objects in video. Libraries...

Read out all

Extracting features from video frames – Exploring Video Data

Another useful technique for the EDA of video data is to extract features from each frame and analyze them. Features are measurements...

Read out all

Loading video data using cv2 – Exploring Video Data

Exploratory Data Analysis (EDA) is an important step in any data analysis process. It helps you understand your data, identify patterns and...

Read out all