Data preparation

We will keep the data size smaller by using only the first 2,000 images in the training and test data from CIFAR10. This will allow the image classification model to be run on a regular computer or laptop. We will also resize the training and test images from 32 x 32 dimensions to 224 x 224 dimensions to be able to compare classification performance with the pretrained model. The following code includes the necessary preprocessing that we went over earlier in this chapter:

# Selecting first 2000 images
trainx <- data$train$x[1:2000,,,]
testx <- data$test$x[1:2000,,,]

# One-hot encoding
trainy <- to_categorical(data$train$y[1:2000,], num_classes = 10)
testy <- to_categorical(data$test$y[1:2000,] , num_classes = 10)

# Resizing train images to 224x224
x <- array(rep(0, 2000 * 224 * 224 * 3), dim = c(2000, 224, 224, 3))
for (i in 1:2000) { x[i,,,] <- resize(trainx[i,,,], 224, 224) }

# Plot of before/after resized image
par(mfrow = c(1,2), mar = rep(0, 4))
plot(as.raster(trainx[2,,,], max = 255))
plot(as.raster(x[2,,,], max = 255))
par(mfrow = c(1,1))

trainx <- imagenet_preprocess_input(x)

# Resizing test images to 224x224
x <- array(rep(0, 2000 * 224 * 224 * 3), dim = c(2000, 224, 224, 3))
for (i in 1:2000) { x[i,,,] <- resize(testx[i,,,], 224, 224) }
testx <- imagenet_preprocess_input(x)

In the preceding code, while resizing dimensions from 32 x 32 to 224 x 224, we use bilinear interpolation, which is included as part of the EBImage package. Bilinear interpolation extends linear interpolation to two variables, which in this case is the height and width of an image. The effect of bilinear interpolation can be observed from the before and after images of the truck shown in the following image:

Here, we can see that the after image (second image) looks smoother as it contains more pixels compared to the original image (first image).

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

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