CNN model

We will start by using a not-so-deep convolutional neural network to develop an image classification model. We will use the following code for this:

# Model architecture
model <- keras_model_sequential()
model %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu',
input_shape = c(224,224,3)) %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu') %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_dropout(rate = 0.25) %>%
layer_flatten() %>%
layer_dense(units = 256, activation = 'relu') %>%
layer_dropout(rate = 0.25) %>%
layer_dense(units = 10, activation = 'softmax')
summary(model)
_________________________________________________________________________
Layer (type) Output Shape Param #
=========================================================================
conv2d_6 (Conv2D) (None, 222, 222, 32) 896
_________________________________________________________________________
conv2d_7 (Conv2D) (None, 220, 220, 32) 9248
_________________________________________________________________________
max_pooling2d_22 (MaxPooling2D) (None, 110, 110, 32) 0
_________________________________________________________________________
dropout_6 (Dropout) (None, 110, 110, 32) 0
_________________________________________________________________________
flatten_18 (Flatten) (None, 387200) 0
__________________________________________________________________________
dense_35 (Dense) (None, 256) 99123456
__________________________________________________________________________
dropout_7 (Dropout) (None, 256) 0
__________________________________________________________________________
dense_36 (Dense) (None, 10) 2570
==========================================================================
Total params: 99,136,170
Trainable params: 99,136,170
Non-trainable params: 0
___________________________________________________________________________________________

# Compile
model %>% compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = 'accuracy')

# Fit
model_one <- model %>% fit(trainx,
trainy,
epochs = 10,
batch_size = 10,
validation_split = 0.2)

From the preceding code, we can observe the following:

  • The total number of parameters in this network is 99,136,170.
  • When compiling the model, we use categorical_crossentropy as the loss function since the response has 10 categories.
  • For the optimizer, we specify rmsprop, which is a gradient-based optimization method and is a popular choice that provides reasonably good performance.
  • We train the model with 10 epochs and with a batch size of 10.
  • Out of 2,000 images in the training data, 20% (or 400 images) is used for assessing validation errors and the remaining 80% (or 1,600 images) is used for training.

A plot of the accuracy and loss values after training the model is as follows for model_one:

From the preceding plot, the following observations can be made:

  • The plot of the accuracy and loss values shows that, after about 4 epochs, the loss and accuracy values for both training and validation data remain more or less constant.
  • Although the accuracy for the training data reaches high values closer to 100%, there seems to be no impact on the accuracy based on the images in the validation data.
  • In addition, the gap between the accuracy of the training and validation data seems to be high, suggesting the presence of overfitting. When assessing the model's performance, we expect to see low accuracy in terms of image classification by the model.

Note that developing a decent image classification model using CNN requires a large number of images for training, and therefore more time and resources. Later in this chapter, we will learn how to use pretrained networks to help us overcome this problem. For now, though, let's proceed by assessing the image classification model's performance.

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

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