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

Keras is a high-level deep learning API written in Python that runs on top of TensorFlow. It provides essential abstractions and building blocks for developing machine learning solutions with a productive interface that helps solve complex problems efficiently.

The Keras functional API allows you to create flexible models by treating layers as functions that can be called on tensors. Since the functional API creates a directed acyclic graph (DAG) of layers, you can access and reuse intermediate nodes for tasks like feature extraction.

Importing Required Libraries

First, let's import the necessary libraries ?

import tensorflow as tf
from tensorflow import keras
import numpy as np

Loading Pre-trained VGG19 Model

We'll use the VGG19 model with pre-trained ImageNet weights to demonstrate node extraction ?

print("Loading VGG19 model with pre-trained weights...")
vgg19 = tf.keras.applications.VGG19(weights='imagenet')

# Display model summary
print(f"Model has {len(vgg19.layers)} layers")
print("First few layers:")
for i, layer in enumerate(vgg19.layers[:5]):
    print(f"Layer {i}: {layer.name} - {layer.__class__.__name__}")
Loading VGG19 model with pre-trained weights...
Model has 22 layers
First few layers:
Layer 0: input_1 - InputLayer
Layer 1: block1_conv1 - Conv2D
Layer 2: block1_conv2 - Conv2D
Layer 3: block1_pool - MaxPooling2D
Layer 4: block2_conv1 - Conv2D

Extracting and Reusing Intermediate Nodes

Now we'll extract outputs from all layers to create a feature extraction model ?

# Extract outputs from all layers
features_list = [layer.output for layer in vgg19.layers]

# Create a new model that outputs features from all layers
feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list)

print("Feature extraction model created successfully")
print(f"Number of outputs: {len(feat_extraction_model.outputs)}")
Feature extraction model created successfully
Number of outputs: 22

Testing Feature Extraction

Let's test the feature extraction model with a random image ?

# Create a random input image (224x224x3 for VGG19)
img = np.random.random((1, 224, 224, 3)).astype("float32")

# Extract features from all layers
print("Extracting features from all layers...")
extracted_features = feat_extraction_model(img)

# Display shapes of some extracted features
print("\nShapes of extracted features:")
for i, feature in enumerate(extracted_features[:5]):
    layer_name = vgg19.layers[i].name
    print(f"{layer_name}: {feature.shape}")
Extracting features from all layers...

Shapes of extracted features:
input_1: (1, 224, 224, 3)
block1_conv1: (1, 224, 224, 64)
block1_conv2: (1, 224, 224, 64)
block1_pool: (1, 112, 112, 64)
block2_conv1: (1, 112, 112, 128)

Extracting Specific Layer Features

You can also extract features from specific layers of interest ?

# Extract features from specific layers only
specific_layers = ['block1_pool', 'block3_pool', 'block5_pool']
specific_outputs = []

for layer_name in specific_layers:
    layer = vgg19.get_layer(layer_name)
    specific_outputs.append(layer.output)

# Create model for specific feature extraction
specific_model = keras.Model(inputs=vgg19.input, outputs=specific_outputs)

# Extract features
specific_features = specific_model(img)

print("Specific layer features extracted:")
for i, (layer_name, feature) in enumerate(zip(specific_layers, specific_features)):
    print(f"{layer_name}: {feature.shape}")
Specific layer features extracted:
block1_pool: (1, 112, 112, 64)
block3_pool: (1, 56, 56, 256)
block5_pool: (1, 14, 14, 512)

Key Benefits

Extracting and reusing nodes in Keras functional models provides several advantages:

  • Feature Extraction − Access intermediate representations for transfer learning

  • Model Visualization − Static graph structure allows plotting and inspection

  • Multi-output Models − Create models that output features from multiple layers simultaneously

  • Efficient Computation − Reuse computed features without redundant forward passes

Conclusion

Keras functional API enables easy extraction and reuse of intermediate layer outputs by treating the model as a static graph. This capability is essential for feature extraction, transfer learning, and creating multi-output models from pre-trained networks.

Updated on: 2026-03-25T14:53:10+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements