How can Keras be used to extract and reuse nodes in graph of layers using Python?

Keras was developed as a part of research for the project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System). Keras is a deep learning API, which is written in Python. It is a high-level API that has a productive interface that helps solve machine learning problems. It runs on top of the Tensorflow framework. It was built to help experiment in a quick manner. It provides essential abstractions and building blocks that are essential in developing and encapsulating machine learning solutions.

It is highly scalable, and comes with cross-platform abilities. This means Keras can be run on TPU or clusters of GPUs. Keras models can also be exported to run in a web browser or a mobile phone as well.

Keras is already present within the Tensorflow package. It can be accessed using the below line of code.

import tensorflow
from tensorflow import keras

The Keras functional API helps create models that are more flexible in comparison to models created using sequential API. The functional API can work with models that have non-linear topology, can share layers and work with multiple inputs and outputs. A deep learning model is usually a directed acyclic graph (DAG) that contains multiple layers. The functional API helps build the graph of layers.

We are using Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook. Following is the code snippet wherein Kera is used to extract and reuse nodes in graph of layers −


print("VGG19 model with pre-trained weights")
vgg19 = tf.keras.applications.VGG19()
features_list = [layer.output for layer in vgg19.layers]
feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list)

img = np.random.random((1, 224, 224, 3)).astype("float32")
print("Create feature-extraction model")
extracted_features = feat_extraction_model(img)

Code credit −


VGG19 model with pre-trained weights
Downloading data from
574717952/574710816 [==============================] - 6s 0us/step
Create feature-extraction model


  • Since the graph of layers is a static data structure, it can be accessed.

  • This is the reason why functional models can be plotted as images.

  • The activations of intermediate layers (nodes) can also be accessed and reused.

  • This is very useful for feature extraction purposes.

  • We will be using the VGG19 model which has pre-trained weights with the help of ImageNet.

  • These intermediate activations can be obtained by querying graph data structure.

  • These features can be used to create a new feature-extraction model that returns values of intermediate layer activations.