How can Keras be used to extract features from only one layer of the model using Python?

Keras is a high-level deep learning API that runs on top of TensorFlow. Sometimes you need to extract features from a specific intermediate layer of a trained model, rather than using the final output. This is useful for transfer learning, feature visualization, or building feature extractors.

Installation

TensorFlow (which includes Keras) can be installed using ?

pip install tensorflow

Importing Required Libraries

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

Creating a Sequential Model

First, let's create a CNN model with multiple layers. We'll name one layer specifically so we can extract features from it ?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

print("Creating initial model with named intermediate layer")

initial_model = keras.Sequential([
    keras.Input(shape=(250, 250, 3)),
    layers.Conv2D(32, 5, strides=2, activation="relu"),
    layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
    layers.Conv2D(32, 3, activation="relu"),
])

print("Model created successfully")
print(f"Total layers: {len(initial_model.layers)}")
Creating initial model with named intermediate layer
Model created successfully
Total layers: 4

Extracting Features from Specific Layer

Now we create a new model that takes the same inputs but outputs features from our named intermediate layer ?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Create initial model
initial_model = keras.Sequential([
    keras.Input(shape=(250, 250, 3)),
    layers.Conv2D(32, 5, strides=2, activation="relu"),
    layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
    layers.Conv2D(32, 3, activation="relu"),
])

print("Creating feature extractor from intermediate layer")

# Create feature extractor
feature_extractor = keras.Model(
    inputs=initial_model.inputs,
    outputs=initial_model.get_layer(name="my_intermediate_layer").output,
)

print("Feature extractor created")
print(f"Input shape: {feature_extractor.input_shape}")
print(f"Output shape: {feature_extractor.output_shape}")
Creating feature extractor from intermediate layer
Feature extractor created
Input shape: (None, 250, 250, 3)
Output shape: (None, 124, 124, 32)

Using the Feature Extractor

Let's test our feature extractor with sample data ?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Create model and feature extractor
initial_model = keras.Sequential([
    keras.Input(shape=(250, 250, 3)),
    layers.Conv2D(32, 5, strides=2, activation="relu"),
    layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
    layers.Conv2D(32, 3, activation="relu"),
])

feature_extractor = keras.Model(
    inputs=initial_model.inputs,
    outputs=initial_model.get_layer(name="my_intermediate_layer").output,
)

print("Testing feature extractor with sample data")

# Create test data
x = tf.ones((1, 250, 250, 3))
print(f"Input data shape: {x.shape}")

# Extract features
features = feature_extractor(x)
print(f"Extracted features shape: {features.shape}")
print(f"Feature values range: {tf.reduce_min(features):.3f} to {tf.reduce_max(features):.3f}")
Testing feature extractor with sample data
Input data shape: (1, 250, 250, 3)
Extracted features shape: (1, 124, 124, 32)
Feature values range: 0.000 to 4.691

Alternative Method Using Layer Index

You can also extract features using the layer index instead of name ?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Create model
initial_model = keras.Sequential([
    keras.Input(shape=(250, 250, 3)),
    layers.Conv2D(32, 5, strides=2, activation="relu"),
    layers.Conv2D(32, 3, activation="relu"),
    layers.Conv2D(32, 3, activation="relu"),
])

print("Extracting features using layer index")

# Extract from layer at index 1 (second Conv2D layer)
feature_extractor = keras.Model(
    inputs=initial_model.inputs,
    outputs=initial_model.layers[1].output,  # Index 1 = second layer
)

# Test the extractor
x = tf.ones((1, 250, 250, 3))
features = feature_extractor(x)
print(f"Features from layer index 1 shape: {features.shape}")
Extracting features using layer index
Features from layer index 1 shape: (1, 124, 124, 32)

Key Benefits

  • Transfer Learning: Use pre-trained features for new tasks
  • Feature Visualization: Analyze what different layers learn
  • Dimensionality Reduction: Extract lower-dimensional representations
  • Multi-output Models: Create models with multiple prediction heads

Conclusion

Keras makes it easy to extract features from specific layers using keras.Model with custom inputs/outputs. You can reference layers by name or index to create powerful feature extractors for various machine learning tasks.

Updated on: 2026-03-25T14:45:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements