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.