There's more...

Overfitting is a common challenge in scenarios where we have only a few data samples to learn from. This prevents our model from performing robustly on unseen data. There are a few techniques that help us deal with this issue:

Data augmentation: It is a technique that reduces overfitting by generating more training data from existing samples in the data and augmenting the samples via several random transformations that produce believable-looking images. It creates modified versions by applying operations like shifting, flipping, zooming, and so on. It also enriches our data, which helps to generalize our model and make it more robust. Data augmentation is done only on the training set.

The keras library in R provides the image_data_generator() function to implement real-time data augmentation in batches. The following example shows how we can augment data from the Fruits 360 dataset. The augmented images will be rotated randomly by between 0 and 50 degrees, and their width/height will vary between 0% and 10% of the total width/height. We have specified a zoom range of 0.2:

train_data_generator <- image_data_generator(rotation_range = 50,
width_shift_range = 0.1,
height_shift_range = 0.1,
zoom_range = 0.2,
horizontal_flip = TRUE,
fill_mode = "nearest")

The following code block demonstrates how to load images from a directory with data augmentation on the fly:

train_data <- flow_images_from_directory(directory = train_path,
generator = train_data_generator,
target_size = img_size,
color_mode = "rgb",
class_mode = "categorical",
classes = class_label,
batch_size = 20)

Note that we have used the rgb color mode; other color modes include grayscale.

Batch normalization: While training deep neural networks, the distribution of each layer's inputs changes and slows down the training since we need to keep lower learning rates and do careful parameter initialization to train the model correctly. This phenomenon is referred to as internal covariate shift and is addressed by performing the normalization for each training mini-batch. Batch normalization normalizes each batch by both mean and variance reference, allowing us to use higher learning rates. It also acts as a regularizer, in some cases eliminating the need for dropout. Batch normalization makes weight initializations easy. For more details about the layer_batch_normalization() function and its arguments, please refer to https://keras.rstudio.com/reference/layer_batch_normalization.html.

If we were to use batch normalization in our CNN model, here is how we could have done it:

cnn_model_batch_norm <- keras_model_sequential() %>% 
layer_conv_2d(filters = 32, kernel_size = c(4,4),input_shape = c(img_width,img_height,3),padding = "same") %>%
layer_batch_normalization()%>%
layer_activation("relu") %>%
layer_conv_2d(filters = 16, kernel_size = c(3,3))%>%
layer_batch_normalization()%>%
layer_activation("relu")%>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_flatten() %>%
layer_dense(units = 50, activation = 'relu') %>%
layer_dense(units = 23, activation = 'softmax')

Let's see the summary of the model with batch normalization:

summary(cnn_model_batch_norm)

The screenshot shows the description of the model with batch normalization:

Thus, in this recipe, we focused on max pooling and average pooling. Another famous technique of pooling is known as global average pooling, which performs extreme dimensionality reduction. In the See also section of this recipe, a link to more information about global average pooling is provided.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset