MNIST fashion data

We will continue to use the Keras and EBImage libraries. The code for reading the fashion-MNIST data is as follows:

# Libraries
library
(keras) library(EBImage)

# Fashion-MNIST data
mnist <- dataset_fashion_mnist()
str(mnist)
List of 2
$ train:List of 2
..$ x: int [1:60000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ...
..$ y: int [1:60000(1d)] 9 0 0 3 0 2 7 2 5 5 ...
$ test :List of 2
..$ x: int [1:10000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ...
..$ y: int [1:10000(1d)] 9 2 1 1 6 1 4 6 5 7 ...

Here, the training data has 60,000 images and the test data has 10,000 images of fashion items. Since we will be using an unsupervised learning approach for this example, we will not use the labels that are available for the train and test data.

We store the training image data in trainx and test image data in testx, as shown in the following code:

# Train and test data
trainx <- mnist$train$x testx <- mnist$test$x

# Plot of 64 images par(mfrow = c(8,8), mar = rep(0, 4)) for (i in 1:64) plot(as.raster(trainx[i,,], max = 255))

The first 64 images of fashion items can be seen in the following image:

Next, we will reshape the image data into a suitable format, as shown in the following code:

# Reshape images
trainx <- array_reshape(trainx, c(nrow(trainx), 28, 28, 1)) testx <- array_reshape(testx, c(nrow(testx), 28, 28, 1)) trainx <- trainx / 255 testx <- testx / 255

Here, we have also divided trainx and testx by 255 to change the range of values that are between 0-255 to a range between 0 and 1.

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

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