How to do it...

The Fashion-MNIST dataset contains images of 10 different types of items of clothing and accessories. It consists of 60,000 examples in the training set and 10,000 examples in the testing dataset. Each example is a 28 × 28 grayscale image, associated with a label from the 10 classes. 

  1. We import the Fashion-MNIST dataset in our environment:
fashion <- dataset_fashion_mnist()
x_train <- fashion$train$x
y_train <- fashion$train$y
x_test <- fashion$test$x
y_test <- fashion$test$y

We can check the dimensions of the train and test datasets using the commands:

dim(x_train)
dim(x_test)

Now let's take a look at the data for a sample image:

 x_test[1,,]

In the following screenshot, we can see that the sample image data is in the form of a matrix:

We check the label of the preceding screenshot using the following code:

paste("label of first image is:  " ,y_train[1])

In the following screenshot, we can see that the sample image belongs to class 9:

Now we define the label names for the different classes in the data:

label_names = c('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',  'Sandal',
'Shirt', 'Sneaker', 'Bag', 'Ankle boot')
If you are using the Jupyter notebook, you can use the repr library to set the plot window size using the following code: options(repr.plot.width=5, repr.plot.height=3)

Let's visualize a few sample images from the different classes:

# Visualize images

par(mfcol=c(3,3))
par(mar=c(2,2,2,2),xaxs = "i",yaxs = "i")
for (idx in 1:9) {
img <- x_train[idx,,]
img <- t(apply(img, 2, rev))
image(1:28,1:28,img, main=paste(label_names[y_train[idx]+1]),xaxt = 'n',yaxt = 'n',col= gray((0:255)/255))
}

In the following screenshot, we can see sample images along with their label names:

  1. Next, we reshape the data, normalize it, and convert the target label to a binary class matrix:
# Resize the shape of inputs
x_train <- array_reshape(x_train, c(nrow(x_train), 28, 28, 1))
x_test <- array_reshape(x_test, c(nrow(x_test), 28, 28, 1))

# Transform RGB values into [0,1] range
x_train <- x_train / 255
x_test <- x_test / 255

# Convert class vectors to binary class matrices
y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)
  1. Once we are done with data preparation, we build, compile, and train our CNN model:
# Define model
cnn_model <- keras_model_sequential() %>%
layer_conv_2d(filters = 8, kernel_size = c(4,4), activation = 'relu',
input_shape = c(28,28,1)) %>%
layer_conv_2d(filters = 16, kernel_size = c(3,3), activation = 'relu') %>%
layer_flatten() %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')

Let's look at the summary of the model:

cnn_model %>% summary()

The following screenshot shows the details about the model:

Before compiling the model, let's define its loss function:

loss_entropy <- function(y_pred, y_true) {
loss_categorical_crossentropy(y_pred, y_true)
}

Now we compile the model:

# Compile model
cnn_model %>% compile(
loss = loss_entropy,
optimizer = optimizer_sgd(),
metrics = c('accuracy')
)

After compiling the model, we train it with a batch size of 128, number of epochs set to 5, and a validation split of 20%:

# train the model
cnn_model %>% fit(
x_train, y_train,
batch_size = 128,
epochs = 5,
validation_split = 0.2
)
  1. Finally, we evaluate the performance of the trained model and print the evaluation metrics:
scores <- cnn_model %>% evaluate(x_test,
y_test,
verbose = 0
)
# Output metrics
paste('Test loss:', scores[[1]])
paste('Test accuracy:', scores[[2]])

In the following screenshot, we can see the evaluation metrics of the model on test data:

Once we are satisfied with the model's accuracy, we can use it for predicting classes for the testing dataset:

#prediction
predicted_label <- cnn_model %>% predict_classes(x_test)

In the preceding screenshot, we can see our model achieves a good accuracy of 81.63% on test data. 

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

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