Reading an image

Let's start by reading an image of a dog in RStudio. The following code loads an image file and then obtains the respective output:

When using the RESNET50 network, the maximum target size that's allowed is 224 x 224 and the minimum target size that's allowed is 32 x 32.
# Read image data
setwd("~/Desktop")
img <- image_load("dog.jpg", target_size = c(224,224))
x <- image_to_array(img)
str(x)
OUTPUT
num [1:224, 1:224, 1:3] 70 69 68 73 88 79 18 22 21 20 ...

# Image plot
plot(as.raster(x, max = 255))

# Summary and histogram
summary(x)
OUTPUT
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0 89.0 150.0 137.7 190.0 255.0
hist(x)

In the preceding code, we can observe the following:

  • A picture of a Norwich terrier dog is loaded from the computer desktop that's 224 x 224 in size using the image_load() function from Keras.
  • Note that the original image may not be 224 x 224 in size. However, specifying this dimension at the time of loading the image allows us to easily resize the original image so that it has new dimensions.
  • This image is converted into an array of numbers using the image_to_array() function. The structure of this array shows a dimension of 224 x 224 x 3.
  • The summary of the array shows that it contains numbers between zero and 255.

The following is the 224 x 224 color picture of a Norwich terrier dog. This can be obtained using a plot command:

The preceding image is a picture of a Norwich terrier dog sitting and looking forward. We will make use of this picture and check whether the RESNET50 model can accurately predict the type of dog in the picture.

A histogram that was developed from the values in the array is as follows:

The preceding histogram of values in the array shows that the intensity values range from zero to 255, with most of the values concentrated around 200. Next, we will preprocess the image data. This histogram can be used to compare the resulting changes to the image data.

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

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