Show output (activations) of specific layer


In [1]:
from keras.models import load_model,Model
import dogs_vs_cats as dvc
import numpy as np

modelname = "cnn_model_trained.h5"
cnn_model = load_model(modelname)


Using Theano backend.
WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10).  Please switch to the gpuarray backend. You can get more information about how to switch at this URL:
 https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29

Using gpu device 0: GeForce GTX 960M (CNMeM is disabled, cuDNN 5105)

In [2]:
# Load some data
from keras.applications.imagenet_utils import preprocess_input
all_files = dvc.image_files()
all_files = np.array(all_files)
files_ten = all_files[np.random.choice(len(all_files),10)]
ten_img_features,ten_img_labels = dvc.load_image_set(files_ten,(3,50,50))
ten_img_features = preprocess_input(ten_img_features)


data file train.zip has already been downloaded
Data has already been extracted
loading image (1/10)

In [3]:
# Test network work as good as before
results = cnn_model.evaluate(ten_img_features,ten_img_labels)
print(" ".join(["%s: %.4f"%(metric_name,valor) for metric_name,valor in zip(cnn_model.metrics_names,results)]))


10/10 [==============================] - 0s
loss: 1.6871 acc: 0.7000

In [4]:
# https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer
model_maxpooling1 = Model(input=cnn_model.input,
                          output=cnn_model.get_layer("maxpooling2d_1").output)
print(model_maxpooling1.input_shape,
      model_maxpooling1.output_shape)


(None, 3, 50, 50) (None, 32, 23, 23)

In [5]:
feat_max_pooling = model_maxpooling1.predict(ten_img_features)
feat_max_pooling.shape


Out[5]:
(10, 32, 23, 23)

In [6]:
from IPython.display import Image, display
img_show = 3
display(Image(files_ten[img_show]))



In [7]:
import matplotlib.pyplot as plt
%matplotlib notebook


fig, axs = plt.subplots(8,4,figsize=(10,15),sharex=True,sharey=True)
axs = axs.flatten()
feat_show = feat_max_pooling[img_show]
for ax,feats in zip(axs,feat_show):
    ax.imshow(feats,cmap="gray")
    ax.axis("off")

plt.subplots_adjust(wspace=0, hspace=0)


Show convolutional filters


In [8]:
W,b = cnn_model.get_layer("convolution2d_1").get_weights()
W.shape,b.shape


Out[8]:
((32, 3, 3, 3), (32,))

In [9]:
W_show = (W-W.min())/(W.max()-W.min())
%matplotlib notebook

fig, axs = plt.subplots(8,4,figsize=(5,10),sharex=True,sharey=True)
axs = axs.flatten()
for ax,w in zip(axs,W_show):
    ax.imshow(w)



In [10]:
from keras.applications.vgg16 import VGG16
import numpy as np

# https://keras.io/applications/#vgg16
vgg16_model = VGG16(weights='imagenet')
W,b = vgg16_model.get_layer("block1_conv1").get_weights()
W.shape,b.shape


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    883             outputs =\
--> 884                 self.fn() if output_subset is None else\
    885                 self.fn(output_subset=output_subset)

ValueError: DeepCopyOp: the copy failed!

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-10-ee1c5120a6be> in <module>()
      3 
      4 # https://keras.io/applications/#vgg16
----> 5 vgg16_model = VGG16(weights='imagenet')
      6 W,b = vgg16_model.get_layer("block1_conv1").get_weights()
      7 W.shape,b.shape

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/applications/vgg16.py in VGG16(include_top, weights, input_tensor, input_shape, classes)
    111     x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x)
    112     x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x)
--> 113     x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x)
    114     x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
    115 

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, x, mask)
    570         if inbound_layers:
    571             # This will call layer.build() if necessary.
--> 572             self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
    573             # Outputs were already computed when calling self.add_inbound_node.
    574             outputs = self.inbound_nodes[-1].output_tensors

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/engine/topology.py in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
    633         # creating the node automatically updates self.inbound_nodes
    634         # as well as outbound_nodes on inbound layers.
--> 635         Node.create_node(self, inbound_layers, node_indices, tensor_indices)
    636 
    637     def get_output_shape_for(self, input_shape):

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/engine/topology.py in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
    164 
    165         if len(input_tensors) == 1:
--> 166             output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
    167             output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0]))
    168             # TODO: try to auto-infer shape

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/layers/convolutional.py in call(self, x, mask)
    473                           border_mode=self.border_mode,
    474                           dim_ordering=self.dim_ordering,
--> 475                           filter_shape=self.W_shape)
    476         if self.bias:
    477             if self.dim_ordering == 'th':

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/keras/backend/theano_backend.py in conv2d(x, kernel, strides, border_mode, dim_ordering, image_shape, filter_shape, filter_dilation)
   1496     kernel = _preprocess_conv2d_kernel(kernel, dim_ordering)
   1497     th_border_mode = _preprocess_border_mode(border_mode)
-> 1498     np_kernel = kernel.eval()
   1499     image_shape = _preprocess_conv2d_image_shape(dim_ordering, image_shape)
   1500     filter_shape = _preprocess_conv2d_filter_shape(dim_ordering, filter_shape)

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/theano/gof/graph.py in eval(self, inputs_to_values)
    517         args = [inputs_to_values[param] for param in inputs]
    518 
--> 519         rval = self._fn_cache[inputs](*args)
    520 
    521         return rval

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    896                     node=self.fn.nodes[self.fn.position_of_error],
    897                     thunk=thunk,
--> 898                     storage_map=getattr(self.fn, 'storage_map', None))
    899             else:
    900                 # old-style linkers raise their own exceptions

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/theano/gof/link.py in raise_with_op(node, thunk, exc_info, storage_map)
    323         # extra long error message in that case.
    324         pass
--> 325     reraise(exc_type, exc_value, exc_trace)
    326 
    327 

/home/gonzalo/.local/lib/python3.5/site-packages/six.py in reraise(tp, value, tb)
    683             value = tp()
    684         if value.__traceback__ is not tb:
--> 685             raise value.with_traceback(tb)
    686         raise value
    687 

/home/gonzalo/anaconda3/envs/deep_learning/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    882         try:
    883             outputs =\
--> 884                 self.fn() if output_subset is None else\
    885                 self.fn(output_subset=output_subset)
    886         except Exception:

ValueError: DeepCopyOp: the copy failed!
Apply node that caused the error: DeepCopyOp(block5_conv3_W)
Toposort index: 0
Inputs types: [CudaNdarrayType(float32, 4D)]
Inputs shapes: [(512, 512, 3, 3)]
Inputs strides: [(4608, 9, 3, 1)]
Inputs values: ['not shown']
Outputs clients: [['output']]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

In [12]:
%matplotlib notebook

W_show = (W-W.min())/(W.max()-W.min())
fig, axs = plt.subplots(8,8,figsize=(10,10),sharex=True,sharey=True)
axs = axs.flatten()
for ax,w in zip(axs,W_show):
    ax.imshow(w)
plt.subplots_adjust(wspace=0, hspace=0)


Show activations with quiver

Install quiver:

pip install --no-deps git+git://github.com/jakebian/quiver.git

In [ ]:
import os
import shutil

# copy some images in other dir to avoid saturate quiqver:
dirname = "trains_subset"
if not os.path.exists(dirname):
    os.mkdir(dirname)
    all_files = dvc.image_files()
    for img in all_files[np.random.choice(len(all_files),15)]:
        shutil.copy(img,
                    os.path.join(dirname,os.path.basename(img)))

In [ ]:
import quiver_engine.server as server
server.launch(cnn_model,classes=["dog","cat"],input_folder=dirname)

In [ ]:
!ls