Loading the pretrained model
library(keras)
library(tensorflow)
model <- load_model_hdf5("cats_and_dogs_small_2.h5")
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
## ________________________________________________________________________________
It is the image of a cat - vizualized later
img_path <- "C:/Users/gocoo/Downloads/cats_and_dogs_small/test/cats/cat.1700.jpg"
The size of the image is 150, 150
img <- image_load(img_path, target_size = c(150, 150))
img_tensor <- image_to_array(img)
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))
Models like smaller numbers
img_tensor <- img_tensor / 255
dim(img_tensor)
## [1] 1 150 150 3
plot(as.raster(img_tensor[1,,,]))
layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)
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)
activations <- activation_model %>% predict(img_tensor)
first_layer_activation <- activations[[1]]
dim(first_layer_activation)
## [1] 1 148 148 32
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))
}
plot_channel(first_layer_activation[1,,,2])
plot_channel(first_layer_activation[1,,,32])
image_size <- 58
images_per_row <- 16
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"