Loading the libraries and data files

We will begin by using the following steps:

  1. First, we load all the libraries that we will use. We will use just one function from each of these libraries but we need each one to get our data in the proper format. The jpeg library will be used to read in the image data and store it as a matrix. The purrr package will be used to apply a function to our list of arrays. The abind package will be used to convert the list of arrays into one array. Finally, OpenImageR will be used to resize our data. We load all the libraries needed to bring in images and convert them to the proper format using the following code:
 library(jpeg)
library(purrr)
library(abind)
library(OpenImageR)
  1. After loading the libraries, the next step is to bring over all the image files. The first step in this process is to change the working directory, for convenience, to the folder containing all the image files.
  2. Once you have navigated to this folder, use the list.files function to bring over a vector of all the filenames. Lastly, we use the map() function from the purrr package to perform functions on every element in our vector and pass the results to a list.
  3. In this case, every element in our vector is a file path. We pass each file path as an argument to the readJPEG function from the jpeg package. This function returns an array for every image, with all the pixel values represented as normalized values between 0 and 1. This is convenient because this is the format we want for our neural networks. As noted before, the pixel values are ordinarily stored as integers between 0 and 255; however, values bound between 0 and 1 work better when planning to pass data through a neural network. We import our images, convert all the pixel values to normalized values between 0 and 1, and store all the formatted image data in a list of arrays using the following code:
 setwd('data/faces')
filename_vector = list.files(pattern="*.jpg")
image_list <- purrr::map(filename_vector, jpeg::readJPEG)
  1. After running the code, we now have the list of arrays in our data environment. If we expand the object, we can see a sample of pixel values for the images in this set. After expanding the data object in your environment, it will look as in the following screenshot: