Loading Keras

Loading the pretrained model

library(keras)
library(tensorflow)
 model <- load_model_hdf5("cats_and_dogs_small_2.h5")

Vizualizing the model

 model
## Model
## Model: "sequential_2"
## ________________________________________________________________________________
## Layer (type)                        Output Shape                    Param #     
## ================================================================================
## conv2d_7 (Conv2D)                   (None, 148, 148, 32)            896         
## ________________________________________________________________________________
## max_pooling2d_6 (MaxPooling2D)      (None, 74, 74, 32)              0           
## ________________________________________________________________________________
## conv2d_8 (Conv2D)                   (None, 72, 72, 64)              18496       
## ________________________________________________________________________________
## max_pooling2d_7 (MaxPooling2D)      (None, 36, 36, 64)              0           
## ________________________________________________________________________________
## conv2d_9 (Conv2D)                   (None, 34, 34, 128)             73856       
## ________________________________________________________________________________
## max_pooling2d_8 (MaxPooling2D)      (None, 17, 17, 128)             0           
## ________________________________________________________________________________
## conv2d_10 (Conv2D)                  (None, 15, 15, 128)             147584      
## ________________________________________________________________________________
## max_pooling2d_9 (MaxPooling2D)      (None, 7, 7, 128)               0           
## ________________________________________________________________________________
## flatten_2 (Flatten)                 (None, 6272)                    0           
## ________________________________________________________________________________
## dropout (Dropout)                   (None, 6272)                    0           
## ________________________________________________________________________________
## dense_4 (Dense)                     (None, 512)                     3211776     
## ________________________________________________________________________________
## dense_5 (Dense)                     (None, 1)                       513         
## ================================================================================
## Total params: 3,453,121
## Trainable params: 3,453,121
## Non-trainable params: 0
## ________________________________________________________________________________

Selecting the image

It is the image of a cat - vizualized later

img_path <- "C:/Users/gocoo/Downloads/cats_and_dogs_small/test/cats/cat.1700.jpg"

Importing the image through the directory

The size of the image is 150, 150

img <- image_load(img_path, target_size = c(150, 150)) 

Converting the image to an array

img_tensor <- image_to_array(img)

Reshaping the array into a 4dimension tensor

1st is the number of images, second the height of image, third the width and fourth the depth or channel

img_tensor <- array_reshape(img_tensor, c(1, 150, 150, 3))

Rescaling the image

Models like smaller numbers

img_tensor <- img_tensor / 255
dim(img_tensor)
## [1]   1 150 150   3

Rasterizing the imported image

plot(as.raster(img_tensor[1,,,])) 

Selecting 8 layers from the model to create the output

layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)

keras_model

keras_model_sequential() function takes one input and produces a singular output while keras_model can take single input to produce output on each layer

activation_model <- keras_model(inputs = model$input, outputs = layer_outputs)

The followng returns a list of 5 arrays 1 array per layer activation

activations <- activation_model %>% predict(img_tensor)

This is the first layer of activation

first_layer_activation <- activations[[1]]
dim(first_layer_activation)
## [1]   1 148 148  32

this function plots the channel

Remember after activation the channels are various filters

 plot_channel <- function(channel) {
   rotate <- function(x) t(apply(x, 2, rev))
   image(rotate(channel), axes = FALSE, asp = 1, col = terrain.colors(12))
 }

plotting the 2nd channel and the 32nd channel produced by the first layer of activation (the channel here is presented by the filters)

plot_channel(first_layer_activation[1,,,2])

plot_channel(first_layer_activation[1,,,32])

Plotting all intermediate images produced by the activation using the above principle

image_size <- 58
images_per_row <- 16

Let’s write a function

for (i in 1:8) {
   layer_activation <- activations[[i]]
   layer_name <- model$layers[[i]]$name
   n_features <- dim(layer_activation)[[4]]
   n_cols <- n_features %/% images_per_row
   print(paste("Intermediate activation layer for", i))
   op <- par(mfrow = c(n_cols, images_per_row), mai = rep_len(0.02, 4))
   for (col in 0:(n_cols-1)) {
           for (row in 0:(images_per_row-1)) {
                   channel_image <- layer_activation[1,,,(col*images_per_row) + row + 1]
                   plot_channel(channel_image)
           }
         } 
  par(op)
 }
## [1] "Intermediate activation layer for 1"

## [1] "Intermediate activation layer for 2"

## [1] "Intermediate activation layer for 3"

## [1] "Intermediate activation layer for 4"

## [1] "Intermediate activation layer for 5"

## [1] "Intermediate activation layer for 6"

## [1] "Intermediate activation layer for 7"

## [1] "Intermediate activation layer for 8"