Environment Setup

In [15]:
%matplotlib inline
import os
import numpy as np
import matplotlib.pyplot as plt

from keras import models
from keras.preprocessing import image

base_dir = 'jo_feline'
feline_dir = os.path.join(base_dir,'feline')
jo_dir = os.path.join(base_dir,'jo')

Retrieving Model

In [20]:
model = models.load_model('cats_and_dogs_small_1.h5')
print(model.summary())
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 72, 72, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 36, 36, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 34, 34, 128)       73856     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 17, 17, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 15, 15, 128)       147584    
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 128)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 6272)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               3211776   
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 513       
=================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0
_________________________________________________________________
None

Preprocessing a Single Image

Retrieving Image and Converting to Tensor

In [13]:
img_path = os.path.join(feline_dir,os.listdir(feline_dir)[0])
print(img_path)
img = image.load_img(img_path, target_size =(150,150))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis = 0)
img_tensor /= 255.
jo_feline/feline/feline4.jpg

Displaying the Image

In [17]:
plt.imshow(img_tensor[0])
Out[17]:
<matplotlib.image.AxesImage at 0x7fc3b803b128>

Vizualizing a Channel

First, I will instantiate a model from an input tensor and a list of output tensor

In [22]:
layer_outputs = [layer.output for layer in model.layers[:8]]
activation_model = models.Model(inputs = model.input, outputs = layer_outputs)

This is what our activation_model looks like:

  • One input (150,150,3)
  • 8 outputs, one for each of first 8 layers

Now, I will send my test image in the model and vizualize one channel (e.g. the fourth channel)

In [24]:
activations = activation_model.predict(img_tensor)
In [39]:
first_layer_activation = activations[1]
print(first_layer_activation.shape)
plt.matshow(first_layer_activation[0,:,:,4], cmap = 'viridis')
(1, 74, 74, 32)
Out[39]:
<matplotlib.image.AxesImage at 0x7fc37cbcca58>

Vizualizing Every Channels of Every Layers

In [56]:
layer_names = []
for layer in model.layers[:8]:
    layer_names.append(layer.name)
    
images_per_row = 16

for layer_name, layer_activation in zip (layer_names, activations):
    n_features = layer_activation.shape[-1]
    
    size = layer_activation.shape[1]
    
    n_cols = n_features // images_per_row
    display_grid = np.zeros((size * n_cols, images_per_row * size))
    
    for col in range(n_cols):
        for row in range(images_per_row):
            channel_image = layer_activation[0,:,:,col * images_per_row + row]
            #Normalize tensors
            channel_image -= channel_image.mean()
            channel_image /= channel_image.std()
            channel_image *= 64
            channel_image += 128
            channel_image = np.clip(channel_image, 0, 255).astype('uint8')
            display_grid[col * size : (col + 1) * size,
                        row * size: (row + 1) * size] = channel_image

    scale = 1. /size
    plt.figure(figsize = (scale * display_grid.shape[1],
                         scale * display_grid.shape[0]))

    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap = 'viridis')