Yumi's Blog

Visualization of Filters with Keras

The goal of this blog post is to understand "what my CNN model is looking at". People call this visualization of the filters. But more precisely, what I will do here is to visualize the input images that maximizes (sum of the) activation map (or feature map) of the filters. I will visualize the filters of deep learning models for two different applications:

  • Facial landmark detection
  • Classification

For the facial landmark detection, I will visualize the filters of the model that was trained and described in my previous post Achieving Top 23% in Kaggle's Facial Keypoints Detection with Keras + Tensorflow. For the classification, I will use the VGG16.

Once again, I will follow the two great blog posts: Shinya's Kerasで学ぶ転移学習 and Keras's official blog.

In [3]:
import os
import matplotlib.pyplot as plt
import numpy as np
from pandas.io.parsers import read_csv
from sklearn.utils import shuffle
## These files must be downloaded from Keras website and saved under data folder

Use a single GPU

In [4]:
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
print(tf.__version__)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.95
config.gpu_options.visible_device_list = "2"
#### 1 GPU1
#### 2 GPU2
#### 0 GPU3
#### 4 GPU4
set_session(tf.Session(config=config))
1.2.1

Load the previously trained model

Model 4 was the best among all considered single models in previous analysis. I will load Model 4.

  • Create alias "input_img". This is the 96 pixcel x 96 pixcel image input for the deep learning model.
  • "layer_names" is a list of the names of layers to visualize.
  • "layer_dict" contains model layers

model.summary() shows the deep learning architecture.

In [5]:
from keras.models import model_from_json

def load_model(name):
    model = model_from_json(open(name+'_architecture.json').read())
    model.load_weights(name + '_weights.h5')
    return(model)

model = load_model("model4")
model.summary()
input_img = model.layers[0].input
layer_names = ["conv2d_22","conv2d_23","conv2d_24"]
layer_dict = dict([(layer.name, layer) for layer in model.layers])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_22 (Conv2D)           (None, 94, 94, 32)        320       
_________________________________________________________________
activation_37 (Activation)   (None, 94, 94, 32)        0         
_________________________________________________________________
max_pooling2d_22 (MaxPooling (None, 47, 47, 32)        0         
_________________________________________________________________
conv2d_23 (Conv2D)           (None, 46, 46, 64)        8256      
_________________________________________________________________
activation_38 (Activation)   (None, 46, 46, 64)        0         
_________________________________________________________________
max_pooling2d_23 (MaxPooling (None, 23, 23, 64)        0         
_________________________________________________________________
conv2d_24 (Conv2D)           (None, 22, 22, 128)       32896     
_________________________________________________________________
activation_39 (Activation)   (None, 22, 22, 128)       0         
_________________________________________________________________
max_pooling2d_24 (MaxPooling (None, 11, 11, 128)       0         
_________________________________________________________________
flatten_8 (Flatten)          (None, 15488)             0         
_________________________________________________________________
dense_31 (Dense)             (None, 500)               7744500   
_________________________________________________________________
activation_40 (Activation)   (None, 500)               0         
_________________________________________________________________
dense_32 (Dense)             (None, 500)               250500    
_________________________________________________________________
activation_41 (Activation)   (None, 500)               0         
_________________________________________________________________
dense_33 (Dense)             (None, 30)                15030     
=================================================================
Total params: 8,051,502
Trainable params: 8,051,502
Non-trainable params: 0
_________________________________________________________________

Functions to maximize the activation layer

In [121]:
import numpy as np
from keras import backend as K

class VisualizeImageMaximizeFmap(object):
    def __init__(self,pic_shape):
        '''
        pic_shape : a dimention of a single picture e.g., (96,96,1)
        '''
        self.pic_shape = pic_shape
        
    def find_n_feature_map(self,layer_name,max_nfmap):
        '''
        shows the number of feature maps for this layer
        only works if the layer is CNN
        '''
        n_fmap = None
        for layer in model.layers:
            if layer.name == layer_name:
                weights = layer.get_weights()
                n_fmap=weights[1].shape[0]
        if n_fmap is None:
            print(layer_name + " is not one of the layer names..")
            n_fmap = 1
        n_fmap = np.min([max_nfmap,n_fmap])
        return(int(n_fmap))

    def find_image_maximizing_activation(self,iterate,input_img_data,
                                         picorig=False,
                                         n_iter = 30):
        '''
        The input image is scaled to range between 0 and 1
        picorig  : True  if the picture image for input is original scale
                         ranging between 0 and 225
                   False if the picture image for input is ranging [0,1]
        '''
            
        input_img_data = np.random.random((1, 
                                           self.pic_shape[0],
                                           self.pic_shape[1],
                                           self.pic_shape[2]))
        if picorig:
            ## if the original picture is unscaled and ranging between (0,225),
            ## then the image values are centered around 123 with STD=25
            input_img_data = input_img_data*25 + 123 
        ## I played with this step value but the final image looks to be robust
        step = 500 

        
        
        # gradient ascent
        loss_values = []
        for i in range(n_iter):
            loss_value, grads_value = iterate([input_img_data, 0])
            input_img_data += grads_value * step
            loss_values.append(loss_value) 
        return(input_img_data,loss_values)

    def create_iterate(self,input_img, layer_output,filter_index):
        '''
        layer_output[:,:,:,0] is (Nsample, 94, 94) tensor contains:
        W0^T [f(image)]_{i,j}], i = 1,..., 94, j = 1,..., 94
        
        layer_output[:,:,:,1] contains:
        W1^T [f(image)]_{i,j}], i = 1,..., 94, j = 1,..., 94
        
        W0 and W1 are different kernel!
        '''
        ## loss is a scalar 
        if len(layer_output.shape) == 4:
            ## conv layer 
            loss = K.mean(layer_output[:,  :, :, filter_index])
        elif len(layer_output.shape) ==2:
            ## fully connected layer
            loss = K.mean(layer_output[:, filter_index])
         
        # calculate the gradient of the loss evaluated at the provided image
        grads = K.gradients(loss, input_img)[0]
        # normalize the gradients
        grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

        # iterate is a function taking (input_img, scalar) and output [loss_value, gradient_value]
        iterate = K.function([input_img, K.learning_phase()], [loss, grads])
        return(iterate)

    def deprocess_image(self,x):
        # standardize to have a mean 0 and std  0.1 
        x -= x.mean()
        x /= (x.std() + 1e-5)
        x *= 0.1

        # Shift x to have a mean 0.5 and std 0.1
        # This means 95% of the x should be in between 0 and 1
        # if x is normal
        x += 0.5
        x = np.clip(x, 0, 1)

        # resclar the values to range between 0 and 255
        x *= 255
        x = np.clip(x, 0, 255).astype('uint8')

        return x

    def find_images(self,input_img,layer_names,layer_dict, max_nfmap,
                    picorig=False,n_iter=30):
        '''
        Input :

        input_img   : the alias of the input layer from the deep learning model
        layer_names : list containing the name of the layers whose feature maps to be used
        layer_dict  : symbolic outputs of each "key" layer (we gave them unique names).
        max_nfmap   : the maximum number of feature map to be used for each layer.
        pic_shape   : For example pic_shape = (96,96,1)

        Output : 
        dictionary 

        key = layer name 
        value = a list containing the tuple of (images, list of loss_values) that maximize each feature map
        '''
        argimage = {}
        ## Look for the image for each feature map of each layer one by one
        for layer_name in layer_names: ## the layer to visualize
            n_fmap = self.find_n_feature_map(layer_name,max_nfmap)
            layer_output = layer_dict[layer_name].output
            result = self.find_images_for_layer(input_img,
                                                layer_output,
                                                range(n_fma),
                                                picorig=picorig,
                                                n_iter=n_iter)

            argimage[layer_name] = result
        return(argimage)

    def find_images_for_layer(self,input_img,layer_output,indecies,
                              picorig=False,n_iter=30):
        '''
        indecies : list containing index of 
                      --> filtermaps of CNN or 
                      --> nodes of fully-connected layer
        Output

        a list containing the tuple of (images, list of loss_values) 
        that maximize each feature map


        '''
        result_temp = []
        for filter_index in indecies: # filtermap to visualize
                iterate = self.create_iterate(input_img, layer_output,filter_index)
                input_img_data, loss_values = self.find_image_maximizing_activation(
                    iterate,input_img,
                    picorig=picorig,
                    n_iter=n_iter)
                result_temp.append((input_img_data,loss_values))
        return(result_temp)

    def plot_images_wrapper(self,argimage,n_row = 8, scale = 1):
        '''
        scale : scale up or down the plot size
        '''
        pic_shape = self.pic_shape
        if pic_shape[2] == 1:
            pic_shape = self.pic_shape[:2]
        layer_names = np.sort(argimage.keys())


        for layer_name in layer_names:
            n_fmap = len(argimage[layer_name])
            n_col = np.ceil(n_fmap/float(n_row))
            fig = plt.figure(figsize=(n_col*scale,
                                      n_row*scale))
            fig.subplots_adjust(hspace=0.001,wspace=0.001)
            plt.title(layer_name + " n_featuremap=" + str(n_fmap))
            count = 1
            for value in argimage[layer_name]:
                input_img_data = value[0][0]
                img = self.deprocess_image(input_img_data)
                ax = fig.add_subplot(n_row,n_col,count,
                                    xticks=[],yticks=[])
                ax.imshow(img.reshape(*pic_shape),cmap="gray")
                count += 1
            plt.show()

For each feature map from each CNN layer, we look for the image that maximizes the sum of the feature maps.

Look for the 96 pixcel x 96 pixcel image that maximize:

$ \textrm{argmax}_{image} \sum_{\textrm{kernel}} \boldsymbol{W}^T \left[ f(image) \right]_{\textrm{kernel}} $

  • $\boldsymbol{W}$ is a "filter", vector of length kernel_size[0] * kernel_size[1]. For example, for conv1, kernel_size=(4,4).
  • $\sum_{\textrm{kernel}}$ The sum goes over 94 windows, running over the picture for conv1.

max_nfmap determines the number of feature maps from each layer to use for analysis.

Observations

  • First layer distinguish colours.
  • Some of the first images seem to have duplicated infomation (same colour). So maybe we can reduce the number of featuremap while keeping the same model performance.
  • The 2nd and the 3rd layer get excited with more complex images.
In [8]:
max_nfmap = np.Inf ## print ALL the images

visualizer = VisualizeImageMaximizeFmap(pic_shape = (96,96,1))
print("find images that maximize feature maps")
argimage = visualizer.find_images(input_img,
                                  layer_names,
                                  layer_dict, 
                                  max_nfmap)
print("plot them...")
visualizer.plot_images_wrapper(argimage,n_row = 8, scale = 1)
find images that maximize feature maps
plot them...

Did the gradient ascent converge?

In Kaggle's official blog, the number of iterations for the gradient ascent is set to as low as 20. Do I really find the best image that maximizes the sum of the feature map? To answer to this question, I plotted the sum of the feature map over iterations.

Not suprisingly, the gradient ascent did not converge at all!! I run the codes several times and look at different best images found by the gradient ascent (i.e., run the codes in the previous cell several times). It seems that the best image obtained and plotted above are virtually the same even when the algorithm did not coverge. I guess there are various numerical solutions to this optimization but they are vertually the same image after rescaling.

In [9]:
const  = 1
n_row = 8 
for layer_name in layer_names:
    n_fmap = len(argimage[layer_name])
    n_col = np.ceil(n_fmap/float(n_row))
    fig = plt.figure(figsize=(n_col*const,n_row*const))
    fig.subplots_adjust(hspace=0.001,wspace=0.001)
                        
    plt.title(layer_name + " n_featuremap=" + str(n_fmap))
    for count , value in enumerate(argimage[layer_name]):
        objective = value[1]
        ax = fig.add_subplot(n_row,n_col,count+1,
                            xticks=[],yticks=[])
        ax.plot(objective)
    plt.show()

Visualizing VGG16

Let's learn to visualize layers of deep learning model for classification problem. Here I use VGG model following the discussion of Keras's official blog. VGG16 (also called OxfordNet) is a convolutional neural network architecture named after the Visual Geometry Group from Oxford, who developed it. It was used to win the ILSVR (ImageNet) competition in 2014.

Due to the proxy problem I cannot download and bulid model using keras.applications.VGG16 so here I take manual approach.

Step 1: Downloading data from Github. This is a massive .h5 file (57MB).

Step 2: The source code of keras.applications.VGG16 is available. It seems that we can manually set the weights to be the one locally available.

In [10]:
ls "vgg16"*
vgg16_weights_tf_dim_ordering_tf_kernels.h5*
vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5*

Some observations:

  • Input shape is (None, None, None, 3).

This (probablly) means that the sample size and the frame sizes can be specified later. But the channel (RGB) is specified at the 3rd dimention and it must be 3? Keras's official blog sets the 1st dimention for the RGB specification i.e., (None,3,None,None). According to the Keras's documentation about VGG16, you are allowed to chose the channel to come to the last dimention or the first dimention. But where can I set the dimention? I found my answer in K.image_data_format(). It shows that the default setting is 'channels_last'. Therefore, it makes sense that the VGG16 has 3 as the last dimention.

In [11]:
import keras.backend as K
K.image_data_format()
Out[11]:
'channels_last'
In [12]:
from keras.applications import VGG16

model = VGG16(include_top=False,weights=None)

## load the locally saved weights 
model.load_weights("vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5")

## show the deep learning model
model.summary()

input_img = model.layers[0].input

# get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, None, None, 3)     0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, None, None, 64)    1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, None, None, 64)    36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, None, None, 64)    0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, None, None, 128)   73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, None, None, 128)   147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, None, None, 128)   0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, None, None, 256)   295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, None, None, 256)   590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, None, None, 256)   590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, None, None, 256)   0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, None, None, 512)   1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, None, None, 512)   0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, None, None, 512)   2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, None, None, 512)   0         
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________

Here the max_nfmap is set to a low number because the number of feature maps is as large as 512 for block5, and this is too many to plot. Notice that although the frame size is set to (96, 96, 3), the frame size can be different: as long as the width and height are larger than 48. Great flexibility of deep learning model :D. This flexilibity is also discussed in the Kaggle's blog.

"Note that we only go up to the last convolutional layer --we don't include fully-connected layers. The reason is that adding the fully connected layers forces you to use a fixed input size for the model (224x224, the original ImageNet format). By only keeping the convolutional modules, our model can be adapted to arbitrary input sizes."

I observe similar results as Keras's official blog:

  • The first layer encode direction (looks diagonal lines) and color.
  • Later layers mix the direction and colors together.
  • Later layers look more complex and very staticy.
In [13]:
layer_names = ["block1_conv1","block1_conv2",
               "block2_conv2",
               "block3_conv3",
               "block4_conv3",
               "block5_conv3"]

## (196, 196, 3) , (96,96,3)
visualizer = VisualizeImageMaximizeFmap(pic_shape = (48,48,3))
max_nfmap = 3
argimage = visualizer.find_images(input_img,
                                  layer_names,
                                  layer_dict, 
                                  max_nfmap)
visualizer.plot_images_wrapper(argimage,n_row = 1, scale = 3)

Use the full VGG16 models

Finding an input that maximizes a specific class

In the facial landmark detection application, I did not visualize the final output layer. This is because knowing which image can maximize the x (or y) coordinate of the eye center does not give much insite about the model. On the other hand, visualizing the final output layer gives interesting insights in the classification application, because knowing the which image can maximize the probability of being in one of the specific class is interesting.

Following Keras's official blog, we will find the images that maximize specific classes. Once again, I download weights from here. This weight is 10 times larger! (528 MB). This makes sense because the weights with top fully connected layer contains 138,357,544 parameters while the weights without the top layers contains 10 times less parameters (14,714,688 parameters).

In [20]:
ls "vgg16_weights_tf_dim"*
vgg16_weights_tf_dim_ordering_tf_kernels.h5*
vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5*

Notice that the input dimention is set to (None, 224, 224, 3), indicating that our input image needs to have this size

In [63]:
model = VGG16(include_top=True,weights=None)
model.summary()
model.load_weights("vgg16_weights_tf_dim_ordering_tf_kernels.h5")
input_img = model.layers[0].input
layer_dict = dict([(layer.name, layer) for layer in model.layers])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
_________________________________________________________________

It would be interesting to learn what kind of image can maximize the probability of picture having cats or dogs. The visualization becomes more interesting if we know the actual label/meaning of each of the 1000 classes.

So before going into the details of the visualization, I extract the labels of each 1000 classes. These classes are saved as a json object at here. This json object is used internally from the method keras.applications.vgg16.decode_predictions().

In [16]:
ls imagenet_class_index.json
imagenet_class_index.json*
In [66]:
import json
CLASS_INDEX = json.load(open("imagenet_class_index.json"))
classlabel = []
for i in range(1000):
    classlabel.append(CLASS_INDEX[str(i)][1])
classlabel = np.array(classlabel)
print(len(classlabel))
1000

List all 1000 classes in order

  • It is interesting to know that many of the class labels are related to dogs.
  • As Keras's public blog says the 65th class is sea snake class and the 18th magpie.
In [67]:
for i, lab in enumerate(classlabel):
    print("{:4.0f}: {:30}".format(i,str(lab))),
    if i % 3 == 2:
        print("")
        
   0: tench                             1: goldfish                          2: great_white_shark              
   3: tiger_shark                       4: hammerhead                        5: electric_ray                   
   6: stingray                          7: cock                              8: hen                            
   9: ostrich                          10: brambling                        11: goldfinch                      
  12: house_finch                      13: junco                            14: indigo_bunting                 
  15: robin                            16: bulbul                           17: jay                            
  18: magpie                           19: chickadee                        20: water_ouzel                    
  21: kite                             22: bald_eagle                       23: vulture                        
  24: great_grey_owl                   25: European_fire_salamander         26: common_newt                    
  27: eft                              28: spotted_salamander               29: axolotl                        
  30: bullfrog                         31: tree_frog                        32: tailed_frog                    
  33: loggerhead                       34: leatherback_turtle               35: mud_turtle                     
  36: terrapin                         37: box_turtle                       38: banded_gecko                   
  39: common_iguana                    40: American_chameleon               41: whiptail                       
  42: agama                            43: frilled_lizard                   44: alligator_lizard               
  45: Gila_monster                     46: green_lizard                     47: African_chameleon              
  48: Komodo_dragon                    49: African_crocodile                50: American_alligator             
  51: triceratops                      52: thunder_snake                    53: ringneck_snake                 
  54: hognose_snake                    55: green_snake                      56: king_snake                     
  57: garter_snake                     58: water_snake                      59: vine_snake                     
  60: night_snake                      61: boa_constrictor                  62: rock_python                    
  63: Indian_cobra                     64: green_mamba                      65: sea_snake                      
  66: horned_viper                     67: diamondback                      68: sidewinder                     
  69: trilobite                        70: harvestman                       71: scorpion                       
  72: black_and_gold_garden_spider     73: barn_spider                      74: garden_spider                  
  75: black_widow                      76: tarantula                        77: wolf_spider                    
  78: tick                             79: centipede                        80: black_grouse                   
  81: ptarmigan                        82: ruffed_grouse                    83: prairie_chicken                
  84: peacock                          85: quail                            86: partridge                      
  87: African_grey                     88: macaw                            89: sulphur-crested_cockatoo       
  90: lorikeet                         91: coucal                           92: bee_eater                      
  93: hornbill                         94: hummingbird                      95: jacamar                        
  96: toucan                           97: drake                            98: red-breasted_merganser         
  99: goose                           100: black_swan                      101: tusker                         
 102: echidna                         103: platypus                        104: wallaby                        
 105: koala                           106: wombat                          107: jellyfish                      
 108: sea_anemone                     109: brain_coral                     110: flatworm                       
 111: nematode                        112: conch                           113: snail                          
 114: slug                            115: sea_slug                        116: chiton                         
 117: chambered_nautilus              118: Dungeness_crab                  119: rock_crab                      
 120: fiddler_crab                    121: king_crab                       122: American_lobster               
 123: spiny_lobster                   124: crayfish                        125: hermit_crab                    
 126: isopod                          127: white_stork                     128: black_stork                    
 129: spoonbill                       130: flamingo                        131: little_blue_heron              
 132: American_egret                  133: bittern                         134: crane                          
 135: limpkin                         136: European_gallinule              137: American_coot                  
 138: bustard                         139: ruddy_turnstone                 140: red-backed_sandpiper           
 141: redshank                        142: dowitcher                       143: oystercatcher                  
 144: pelican                         145: king_penguin                    146: albatross                      
 147: grey_whale                      148: killer_whale                    149: dugong                         
 150: sea_lion                        151: Chihuahua                       152: Japanese_spaniel               
 153: Maltese_dog                     154: Pekinese                        155: Shih-Tzu                       
 156: Blenheim_spaniel                157: papillon                        158: toy_terrier                    
 159: Rhodesian_ridgeback             160: Afghan_hound                    161: basset                         
 162: beagle                          163: bloodhound                      164: bluetick                       
 165: black-and-tan_coonhound         166: Walker_hound                    167: English_foxhound               
 168: redbone                         169: borzoi                          170: Irish_wolfhound                
 171: Italian_greyhound               172: whippet                         173: Ibizan_hound                   
 174: Norwegian_elkhound              175: otterhound                      176: Saluki                         
 177: Scottish_deerhound              178: Weimaraner                      179: Staffordshire_bullterrier      
 180: American_Staffordshire_terrier  181: Bedlington_terrier              182: Border_terrier                 
 183: Kerry_blue_terrier              184: Irish_terrier                   185: Norfolk_terrier                
 186: Norwich_terrier                 187: Yorkshire_terrier               188: wire-haired_fox_terrier        
 189: Lakeland_terrier                190: Sealyham_terrier                191: Airedale                       
 192: cairn                           193: Australian_terrier              194: Dandie_Dinmont                 
 195: Boston_bull                     196: miniature_schnauzer             197: giant_schnauzer                
 198: standard_schnauzer              199: Scotch_terrier                  200: Tibetan_terrier                
 201: silky_terrier                   202: soft-coated_wheaten_terrier     203: West_Highland_white_terrier    
 204: Lhasa                           205: flat-coated_retriever           206: curly-coated_retriever         
 207: golden_retriever                208: Labrador_retriever              209: Chesapeake_Bay_retriever       
 210: German_short-haired_pointer     211: vizsla                          212: English_setter                 
 213: Irish_setter                    214: Gordon_setter                   215: Brittany_spaniel               
 216: clumber                         217: English_springer                218: Welsh_springer_spaniel         
 219: cocker_spaniel                  220: Sussex_spaniel                  221: Irish_water_spaniel            
 222: kuvasz                          223: schipperke                      224: groenendael                    
 225: malinois                        226: briard                          227: kelpie                         
 228: komondor                        229: Old_English_sheepdog            230: Shetland_sheepdog              
 231: collie                          232: Border_collie                   233: Bouvier_des_Flandres           
 234: Rottweiler                      235: German_shepherd                 236: Doberman                       
 237: miniature_pinscher              238: Greater_Swiss_Mountain_dog      239: Bernese_mountain_dog           
 240: Appenzeller                     241: EntleBucher                     242: boxer                          
 243: bull_mastiff                    244: Tibetan_mastiff                 245: French_bulldog                 
 246: Great_Dane                      247: Saint_Bernard                   248: Eskimo_dog                     
 249: malamute                        250: Siberian_husky                  251: dalmatian                      
 252: affenpinscher                   253: basenji                         254: pug                            
 255: Leonberg                        256: Newfoundland                    257: Great_Pyrenees                 
 258: Samoyed                         259: Pomeranian                      260: chow                           
 261: keeshond                        262: Brabancon_griffon               263: Pembroke                       
 264: Cardigan                        265: toy_poodle                      266: miniature_poodle               
 267: standard_poodle                 268: Mexican_hairless                269: timber_wolf                    
 270: white_wolf                      271: red_wolf                        272: coyote                         
 273: dingo                           274: dhole                           275: African_hunting_dog            
 276: hyena                           277: red_fox                         278: kit_fox                        
 279: Arctic_fox                      280: grey_fox                        281: tabby                          
 282: tiger_cat                       283: Persian_cat                     284: Siamese_cat                    
 285: Egyptian_cat                    286: cougar                          287: lynx                           
 288: leopard                         289: snow_leopard                    290: jaguar                         
 291: lion                            292: tiger                           293: cheetah                        
 294: brown_bear                      295: American_black_bear             296: ice_bear                       
 297: sloth_bear                      298: mongoose                        299: meerkat                        
 300: tiger_beetle                    301: ladybug                         302: ground_beetle                  
 303: long-horned_beetle              304: leaf_beetle                     305: dung_beetle                    
 306: rhinoceros_beetle               307: weevil                          308: fly                            
 309: bee                             310: ant                             311: grasshopper                    
 312: cricket                         313: walking_stick                   314: cockroach                      
 315: mantis                          316: cicada                          317: leafhopper                     
 318: lacewing                        319: dragonfly                       320: damselfly                      
 321: admiral                         322: ringlet                         323: monarch                        
 324: cabbage_butterfly               325: sulphur_butterfly               326: lycaenid                       
 327: starfish                        328: sea_urchin                      329: sea_cucumber                   
 330: wood_rabbit                     331: hare                            332: Angora                         
 333: hamster                         334: porcupine                       335: fox_squirrel                   
 336: marmot                          337: beaver                          338: guinea_pig                     
 339: sorrel                          340: zebra                           341: hog                            
 342: wild_boar                       343: warthog                         344: hippopotamus                   
 345: ox                              346: water_buffalo                   347: bison                          
 348: ram                             349: bighorn                         350: ibex                           
 351: hartebeest                      352: impala                          353: gazelle                        
 354: Arabian_camel                   355: llama                           356: weasel                         
 357: mink                            358: polecat                         359: black-footed_ferret            
 360: otter                           361: skunk                           362: badger                         
 363: armadillo                       364: three-toed_sloth                365: orangutan                      
 366: gorilla                         367: chimpanzee                      368: gibbon                         
 369: siamang                         370: guenon                          371: patas                          
 372: baboon                          373: macaque                         374: langur                         
 375: colobus                         376: proboscis_monkey                377: marmoset                       
 378: capuchin                        379: howler_monkey                   380: titi                           
 381: spider_monkey                   382: squirrel_monkey                 383: Madagascar_cat                 
 384: indri                           385: Indian_elephant                 386: African_elephant               
 387: lesser_panda                    388: giant_panda                     389: barracouta                     
 390: eel                             391: coho                            392: rock_beauty                    
 393: anemone_fish                    394: sturgeon                        395: gar                            
 396: lionfish                        397: puffer                          398: abacus                         
 399: abaya                           400: academic_gown                   401: accordion                      
 402: acoustic_guitar                 403: aircraft_carrier                404: airliner                       
 405: airship                         406: altar                           407: ambulance                      
 408: amphibian                       409: analog_clock                    410: apiary                         
 411: apron                           412: ashcan                          413: assault_rifle                  
 414: backpack                        415: bakery                          416: balance_beam                   
 417: balloon                         418: ballpoint                       419: Band_Aid                       
 420: banjo                           421: bannister                       422: barbell                        
 423: barber_chair                    424: barbershop                      425: barn                           
 426: barometer                       427: barrel                          428: barrow                         
 429: baseball                        430: basketball                      431: bassinet                       
 432: bassoon                         433: bathing_cap                     434: bath_towel                     
 435: bathtub                         436: beach_wagon                     437: beacon                         
 438: beaker                          439: bearskin                        440: beer_bottle                    
 441: beer_glass                      442: bell_cote                       443: bib                            
 444: bicycle-built-for-two           445: bikini                          446: binder                         
 447: binoculars                      448: birdhouse                       449: boathouse                      
 450: bobsled                         451: bolo_tie                        452: bonnet                         
 453: bookcase                        454: bookshop                        455: bottlecap                      
 456: bow                             457: bow_tie                         458: brass                          
 459: brassiere                       460: breakwater                      461: breastplate                    
 462: broom                           463: bucket                          464: buckle                         
 465: bulletproof_vest                466: bullet_train                    467: butcher_shop                   
 468: cab                             469: caldron                         470: candle                         
 471: cannon                          472: canoe                           473: can_opener                     
 474: cardigan                        475: car_mirror                      476: carousel                       
 477: carpenter's_kit                 478: carton                          479: car_wheel                      
 480: cash_machine                    481: cassette                        482: cassette_player                
 483: castle                          484: catamaran                       485: CD_player                      
 486: cello                           487: cellular_telephone              488: chain                          
 489: chainlink_fence                 490: chain_mail                      491: chain_saw                      
 492: chest                           493: chiffonier                      494: chime                          
 495: china_cabinet                   496: Christmas_stocking              497: church                         
 498: cinema                          499: cleaver                         500: cliff_dwelling                 
 501: cloak                           502: clog                            503: cocktail_shaker                
 504: coffee_mug                      505: coffeepot                       506: coil                           
 507: combination_lock                508: computer_keyboard               509: confectionery                  
 510: container_ship                  511: convertible                     512: corkscrew                      
 513: cornet                          514: cowboy_boot                     515: cowboy_hat                     
 516: cradle                          517: crane                           518: crash_helmet                   
 519: crate                           520: crib                            521: Crock_Pot                      
 522: croquet_ball                    523: crutch                          524: cuirass                        
 525: dam                             526: desk                            527: desktop_computer               
 528: dial_telephone                  529: diaper                          530: digital_clock                  
 531: digital_watch                   532: dining_table                    533: dishrag                        
 534: dishwasher                      535: disk_brake                      536: dock                           
 537: dogsled                         538: dome                            539: doormat                        
 540: drilling_platform               541: drum                            542: drumstick                      
 543: dumbbell                        544: Dutch_oven                      545: electric_fan                   
 546: electric_guitar                 547: electric_locomotive             548: entertainment_center           
 549: envelope                        550: espresso_maker                  551: face_powder                    
 552: feather_boa                     553: file                            554: fireboat                       
 555: fire_engine                     556: fire_screen                     557: flagpole                       
 558: flute                           559: folding_chair                   560: football_helmet                
 561: forklift                        562: fountain                        563: fountain_pen                   
 564: four-poster                     565: freight_car                     566: French_horn                    
 567: frying_pan                      568: fur_coat                        569: garbage_truck                  
 570: gasmask                         571: gas_pump                        572: goblet                         
 573: go-kart                         574: golf_ball                       575: golfcart                       
 576: gondola                         577: gong                            578: gown                           
 579: grand_piano                     580: greenhouse                      581: grille                         
 582: grocery_store                   583: guillotine                      584: hair_slide                     
 585: hair_spray                      586: half_track                      587: hammer                         
 588: hamper                          589: hand_blower                     590: hand-held_computer             
 591: handkerchief                    592: hard_disc                       593: harmonica                      
 594: harp                            595: harvester                       596: hatchet                        
 597: holster                         598: home_theater                    599: honeycomb                      
 600: hook                            601: hoopskirt                       602: horizontal_bar                 
 603: horse_cart                      604: hourglass                       605: iPod                           
 606: iron                            607: jack-o'-lantern                 608: jean                           
 609: jeep                            610: jersey                          611: jigsaw_puzzle                  
 612: jinrikisha                      613: joystick                        614: kimono                         
 615: knee_pad                        616: knot                            617: lab_coat                       
 618: ladle                           619: lampshade                       620: laptop                         
 621: lawn_mower                      622: lens_cap                        623: letter_opener                  
 624: library                         625: lifeboat                        626: lighter                        
 627: limousine                       628: liner                           629: lipstick                       
 630: Loafer                          631: lotion                          632: loudspeaker                    
 633: loupe                           634: lumbermill                      635: magnetic_compass               
 636: mailbag                         637: mailbox                         638: maillot                        
 639: maillot                         640: manhole_cover                   641: maraca                         
 642: marimba                         643: mask                            644: matchstick                     
 645: maypole                         646: maze                            647: measuring_cup                  
 648: medicine_chest                  649: megalith                        650: microphone                     
 651: microwave                       652: military_uniform                653: milk_can                       
 654: minibus                         655: miniskirt                       656: minivan                        
 657: missile                         658: mitten                          659: mixing_bowl                    
 660: mobile_home                     661: Model_T                         662: modem                          
 663: monastery                       664: monitor                         665: moped                          
 666: mortar                          667: mortarboard                     668: mosque                         
 669: mosquito_net                    670: motor_scooter                   671: mountain_bike                  
 672: mountain_tent                   673: mouse                           674: mousetrap                      
 675: moving_van                      676: muzzle                          677: nail                           
 678: neck_brace                      679: necklace                        680: nipple                         
 681: notebook                        682: obelisk                         683: oboe                           
 684: ocarina                         685: odometer                        686: oil_filter                     
 687: organ                           688: oscilloscope                    689: overskirt                      
 690: oxcart                          691: oxygen_mask                     692: packet                         
 693: paddle                          694: paddlewheel                     695: padlock                        
 696: paintbrush                      697: pajama                          698: palace                         
 699: panpipe                         700: paper_towel                     701: parachute                      
 702: parallel_bars                   703: park_bench                      704: parking_meter                  
 705: passenger_car                   706: patio                           707: pay-phone                      
 708: pedestal                        709: pencil_box                      710: pencil_sharpener               
 711: perfume                         712: Petri_dish                      713: photocopier                    
 714: pick                            715: pickelhaube                     716: picket_fence                   
 717: pickup                          718: pier                            719: piggy_bank                     
 720: pill_bottle                     721: pillow                          722: ping-pong_ball                 
 723: pinwheel                        724: pirate                          725: pitcher                        
 726: plane                           727: planetarium                     728: plastic_bag                    
 729: plate_rack                      730: plow                            731: plunger                        
 732: Polaroid_camera                 733: pole                            734: police_van                     
 735: poncho                          736: pool_table                      737: pop_bottle                     
 738: pot                             739: potter's_wheel                  740: power_drill                    
 741: prayer_rug                      742: printer                         743: prison                         
 744: projectile                      745: projector                       746: puck                           
 747: punching_bag                    748: purse                           749: quill                          
 750: quilt                           751: racer                           752: racket                         
 753: radiator                        754: radio                           755: radio_telescope                
 756: rain_barrel                     757: recreational_vehicle            758: reel                           
 759: reflex_camera                   760: refrigerator                    761: remote_control                 
 762: restaurant                      763: revolver                        764: rifle                          
 765: rocking_chair                   766: rotisserie                      767: rubber_eraser                  
 768: rugby_ball                      769: rule                            770: running_shoe                   
 771: safe                            772: safety_pin                      773: saltshaker                     
 774: sandal                          775: sarong                          776: sax                            
 777: scabbard                        778: scale                           779: school_bus                     
 780: schooner                        781: scoreboard                      782: screen                         
 783: screw                           784: screwdriver                     785: seat_belt                      
 786: sewing_machine                  787: shield                          788: shoe_shop                      
 789: shoji                           790: shopping_basket                 791: shopping_cart                  
 792: shovel                          793: shower_cap                      794: shower_curtain                 
 795: ski                             796: ski_mask                        797: sleeping_bag                   
 798: slide_rule                      799: sliding_door                    800: slot                           
 801: snorkel                         802: snowmobile                      803: snowplow                       
 804: soap_dispenser                  805: soccer_ball                     806: sock                           
 807: solar_dish                      808: sombrero                        809: soup_bowl                      
 810: space_bar                       811: space_heater                    812: space_shuttle                  
 813: spatula                         814: speedboat                       815: spider_web                     
 816: spindle                         817: sports_car                      818: spotlight                      
 819: stage                           820: steam_locomotive                821: steel_arch_bridge              
 822: steel_drum                      823: stethoscope                     824: stole                          
 825: stone_wall                      826: stopwatch                       827: stove                          
 828: strainer                        829: streetcar                       830: stretcher                      
 831: studio_couch                    832: stupa                           833: submarine                      
 834: suit                            835: sundial                         836: sunglass                       
 837: sunglasses                      838: sunscreen                       839: suspension_bridge              
 840: swab                            841: sweatshirt                      842: swimming_trunks                
 843: swing                           844: switch                          845: syringe                        
 846: table_lamp                      847: tank                            848: tape_player                    
 849: teapot                          850: teddy                           851: television                     
 852: tennis_ball                     853: thatch                          854: theater_curtain                
 855: thimble                         856: thresher                        857: throne                         
 858: tile_roof                       859: toaster                         860: tobacco_shop                   
 861: toilet_seat                     862: torch                           863: totem_pole                     
 864: tow_truck                       865: toyshop                         866: tractor                        
 867: trailer_truck                   868: tray                            869: trench_coat                    
 870: tricycle                        871: trimaran                        872: tripod                         
 873: triumphal_arch                  874: trolleybus                      875: trombone                       
 876: tub                             877: turnstile                       878: typewriter_keyboard            
 879: umbrella                        880: unicycle                        881: upright                        
 882: vacuum                          883: vase                            884: vault                          
 885: velvet                          886: vending_machine                 887: vestment                       
 888: viaduct                         889: violin                          890: volleyball                     
 891: waffle_iron                     892: wall_clock                      893: wallet                         
 894: wardrobe                        895: warplane                        896: washbasin                      
 897: washer                          898: water_bottle                    899: water_jug                      
 900: water_tower                     901: whiskey_jug                     902: whistle                        
 903: wig                             904: window_screen                   905: window_shade                   
 906: Windsor_tie                     907: wine_bottle                     908: wing                           
 909: wok                             910: wooden_spoon                    911: wool                           
 912: worm_fence                      913: wreck                           914: yawl                           
 915: yurt                            916: web_site                        917: comic_book                     
 918: crossword_puzzle                919: street_sign                     920: traffic_light                  
 921: book_jacket                     922: menu                            923: plate                          
 924: guacamole                       925: consomme                        926: hot_pot                        
 927: trifle                          928: ice_cream                       929: ice_lolly                      
 930: French_loaf                     931: bagel                           932: pretzel                        
 933: cheeseburger                    934: hotdog                          935: mashed_potato                  
 936: head_cabbage                    937: broccoli                        938: cauliflower                    
 939: zucchini                        940: spaghetti_squash                941: acorn_squash                   
 942: butternut_squash                943: cucumber                        944: artichoke                      
 945: bell_pepper                     946: cardoon                         947: mushroom                       
 948: Granny_Smith                    949: strawberry                      950: orange                         
 951: lemon                           952: fig                             953: pineapple                      
 954: banana                          955: jackfruit                       956: custard_apple                  
 957: pomegranate                     958: hay                             959: carbonara                      
 960: chocolate_sauce                 961: dough                           962: meat_loaf                      
 963: pizza                           964: potpie                          965: burrito                        
 966: red_wine                        967: espresso                        968: cup                            
 969: eggnog                          970: alp                             971: bubble                         
 972: cliff                           973: coral_reef                      974: geyser                         
 975: lakeside                        976: promontory                      977: sandbar                        
 978: seashore                        979: valley                          980: volcano                        
 981: ballplayer                      982: groom                           983: scuba_diver                    
 984: rapeseed                        985: daisy                           986: yellow_lady's_slipper          
 987: corn                            988: acorn                           989: hip                            
 990: buckeye                         991: coral_fungus                    992: agaric                         
 993: gyromitra                       994: stinkhorn                       995: earthstar                      
 996: hen-of-the-woods                997: bolete                          998: ear                            
 999: toilet_tissue                 

Is VGG16 doing its job?

Before visualizing the fully-connected layer's filters, I check if VGG16 is doing its highly-reputated job. I took a picture of a dog from Google image, and see if the VGG16 can predict the picture correctly.

VGG16 correctly guesses that the picture has a dog! Amazingly, it also guesses the type of the dog correctly!

In [111]:
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array

image = load_img('./dog.jpg', target_size=(224, 224,3))
plt.imshow(image)
plt.show()
image = img_to_array(image)  #output Numpy-array
y_pred = model.predict(image.reshape(1,image.shape[0],image.shape[1],image.shape[2])).flatten()

## top 5 selected classes
top = 5
chosen_classes = classlabel[  np.argsort( y_pred )[::-1][:top] ]

print("The top {} labels".format(top))
print("-----------------------------------------------------")
for myclass in chosen_classes:
    myprob = y_pred[classlabel==myclass][0]
    print("{:30} prob={:4.3}".format(myclass,myprob))
The top 5 labels
-----------------------------------------------------
Maltese_dog                    prob=0.474
Dandie_Dinmont                 prob=0.0834
soft-coated_wheaten_terrier    prob=0.0734
toy_poodle                     prob=0.0686
Lhasa                          prob=0.0521

Visualization of the filters of the final output layers

Finally, I visualize the filters of output layers. Here I choose the following 4 output classes for visualization.

The following codes look a bit complicated because of the double loop. The first loop goes over the four classes. The next while loop iterates until the gradient-ascent algorithm "converges". In comparisons to my Model 4, VGG16 is more complex (with about 17 times more parameters (17=138357544/8051502)). Therefore the simple gradient ascent algorithm yield quite different pictures every time it runs with different initial start image. As the solution to this optimization problem is not unique, it is expected to have a different images as the solution. However, the resulting picture should yield the probability of belonging to the corresponding class to be very high, as high as 1. Otherwise, it is most likely that the algorithm did not converge.

From the trial and error, I found that the convergence (in terms of the probability of belonging to the class = 1) depends highly on the initial start image. For these reasons, my code runs until it finds the image that yieled high probability of belonging to the class.

Observations

I was able to find pictures with 100% probability of being sea snake, magpie, cliff or siamang!

  • The pictures that I found look a bit different from the one found by Keras's official blog. Expectedly.

  • The sea snake image seems to have some curvy texture?

  • The magpie image looks to have a texture of feather ... ? some beaks? If you say so.
  • Do I see the colourful bee eater at the center?
  • The pillow image looks soft and fluffy..

I would not classify any of these pictures into sea snake or magpie! I wonder what would happen if I label them as not-sea-snake or not-magie, include as parts of the training data and retrain the models. (Is this the idea of GAN? Something I would love to learn.) But I guess CNN look at pictures differently. I will conclude this blog by quoting the comments from Keras's official blog:

we should refrain from our natural tendency to anthropomorphize them and believe that they "understand", say, the concept of dog, or the appearance of a magpie, just because they are able to classify these objects with high accuracy. They don't, at least not to any any extent that would make sense to us humans.

In [132]:
layer_output = layer_dict["predictions"].output


out_index = [65, 18, 92, 721]
for i in out_index:
    visualizer = VisualizeImageMaximizeFmap(pic_shape = (224,224,3))
    images = []
    probs  = []
    myprob = 0
    n_alg = 0
    while(myprob < 0.9):
        myimage = visualizer.find_images_for_layer(input_img,layer_output,[i],
                                                   picorig=True,n_iter=20)
        y_pred = model.predict(myimage[0][0]).flatten()
        myprob = y_pred[i]
        n_alg += 1

    print("The total number of times the gradient ascent needs to run: {}".format(n_alg))
    
    argimage = {"prediction":[myimage]}
    print("{} probability:".format(classlabel[i])),
    print("{:4.3}".format(myprob)),
    
    visualizer.plot_images_wrapper(argimage,n_row = 1, scale = 4)
The total number of times the gradient ascent needs to run: 11
sea_snake probability:  1.0
 The total number of times the gradient ascent needs to run: 50
magpie probability:  1.0
 The total number of times the gradient ascent needs to run: 4
bee_eater probability:  1.0
 The total number of times the gradient ascent needs to run: 10
pillow probability:  1.0

Comments