Using the saved deep learning model

Now that we have built our deep learning model, we can restart R and reload the model from disk. The code for this section is in the Chapter11/use_cifar10_model.R folder. We will load the model that was created in the previous section by using the following code:

library(keras)

# load model trained in build_cifar10_model.R
model <- load_model_hdf5("cifar_model.h5")

We will use this model to generate a prediction for an image file from the validation set. We will pick the first directory in the validation folder and then pick the 7th file from that folder. We load the image and apply the same preprocessing to it as we did when preprocessing the images during training, which is to normalize the data by dividing the pixel values by 255.0. Here is the code that loads the image and generates the prediction:

> valid_dir <-"../data/cifar_10_images/data1/valid/"
> first_dir <- list.dirs(valid_dir, full.names=FALSE, recursive=FALSE)[1]
> valid_dir <- paste(valid_dir,first_dir,sep="")
> img_path <- paste(valid_dir,list.files(valid_dir)[7],sep="/")

# load image and convert to shape we can use for prediction
> img <- image_load(img_path, target_size = c(32,32))
> x <- image_to_array(img)
> x <- array_reshape(x, c(1, dim(x)))
> x <- x / 255.0
> preds <- model %>% predict(x)
> preds <- round(preds,3)
> preds
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0.997 0 0 0 0 0 0 0.003

The model predicts that the input is from the first class with 99.7% certainty. Since we chose the first directory in the validation set, the prediction is correct.

The final thing we will do with our model is to evaluate it on a directory of image files. We will also show how to generate predictions for an entire directory of image files. This code loads the images from the directories using data generators, similar to how we trained the model. Here is the code that evaluates and predicts categories by using the model for the validation images we saved to disk:

> valid_dir <-"../data/cifar_10_images/data1/valid/"
> flow_batch_size <- 50
> num_valid <- 8000
>
> valid_gen <- image_data_generator(rescale=1.0/255)
> valid_flow <- flow_images_from_directory(
valid_dir,
valid_gen,
target_size=c(32,32),
batch_size=flow_batch_size,
class_mode="categorical"
)
>
> evaluate_generator(model,valid_flow,
steps=as.integer(num_valid/flow_batch_size))
$`loss`
[1] 0.5331386

$acc
[1] 0.808625

The validation accuracy is 80.86%, which is similar to what we observed during model training, this confirms that the model was saved correctly to disk. Here is the code that generates predictions for all 8,000 validation images:

> preds <- predict_generator(model,valid_flow,
steps=as.integer(num_valid/flow_batch_size))
> dim(preds)
[1] 8000 8

> # view the predictions,
> preds <- round(preds,3)
> head(preds)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0.000 0.000 0.000 0.000 0.000 0.000 0.999 0.001
[2,] 0.000 0.007 0.001 0.002 0.990 0.000 0.000 0.000
[3,] 0.000 0.855 0.069 0.032 0.021 0.017 0.002 0.002
[4,] 0.134 0.001 0.000 0.000 0.000 0.000 0.001 0.864
[5,] 0.011 0.064 0.057 0.226 0.051 0.515 0.004 0.073
[6,] 0.108 0.277 0.135 0.066 0.094 0.091 0.052 0.179

We can see that our prediction output has 8,000 rows and 8 columns, so for each validation image, there is a probability for each category. We can see that the sum for each row is 1.0 and that there is usually one class that has a significant probability. For example, the model is predicting that the first image is in class 7 with a probability of 99.9%.

We have now built a complete image classification solution using image files. This template can be reused for other tasks once the image data is stored in the same directory structure. If the new task had a different number of categories, then all you would need to change is the number of nodes in the last dense layer and possibly the softmax activation. However, if you did have a new image classification task that involved real-life images, then you probably would get better results by using an existing model and using transfer learning. Before I explain how to do that, I will provide some background on the ImageNet dataset, which is often used to train complex models which are then used in transfer learning.