Resizing and reshaping

To prepare the data for developing a classification model, we start by resizing the dimensions of all 18 images to the same size using the following code:

# Resizing
for (i in 1:length(temp)) {mypic[[i]] <- resize(mypic[[i]], 28, 28)}

As can be seen from the preceding code, all images are now resized to 28 x 28 x 3. Let's plot all the images again to see the impact of resizing using the following code:

# Plot images
par(mfrow = c(3,6))
for (i in 1:length(temp)) plot(mypic[[i]])
par(mfrow = c(1,1)

When we reduce the dimensions of a picture, it will lead to a lower number of pixels, which in turn will cause pictures to have lower quality, as can be seen in the following screenshot:

Next, we will reshape the dimensions of 28 x 28 x 3 into a single dimension of 28 x 28 x 3 (or 2,352 vectors) using the following code:

# Reshape
for (i in 1:length(temp)) {mypic[[i]] <- array_reshape(mypic[[i]], c(28, 28,3))}
str(mypic)

OUTPUT

List of 18
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 0.953 0.953 0.953 0.953 0.953 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 0.328 ...
$ : num [1:28, 1:28, 1:3] 0.26 0.294 0.312 0.309 0.289 ...
$ : num [1:28, 1:28, 1:3] 0.49 0.49 0.49 0.502 0.502 ...
$ : num [1:28, 1:28, 1:3] 1 1 1 1 1 1 1 1 1 1 ..

By observing the structure of the preceding data using str(mypic), we can see that there are 18 different items in the list that correspond to the 18 images that we started with.

Next, we will create training, validation, and test data.

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

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