5 Best Ways to Use Keras for Feature Extraction with Sequential Models in Python

Rate this post

πŸ’‘ Problem Formulation: In the world of machine learning, feature extraction is the process of using algorithms to identify and extract the most relevant information from raw data for use in model training. With Keras, a high-level neural networks API, Python developers can leverage sequential models for efficient feature extraction. If given a dataset of images, the input is the raw pixel data, and the desired outputs are high-level features that can be used for training classification models.

Method 1: Using Pre-trained Convolutional Neural Networks

Pre-trained Convolutional Neural Networks (CNNs) from Keras applications provide a quick and powerful way to extract features without the expense of training from scratch. Models such as VGG16 have been trained on a large corpus of images and can be repurposed for feature extraction on new datasets.

Here’s an example:

from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing import image
import numpy as np

# Load the VGG model without the top classification layer
model = VGG16(weights='imagenet', include_top=False)

# Load a sample image and preprocess it
img = image.load_img('path_to_image.jpg', target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)

# Extract features
features = model.predict(img_array)

Output: A Numpy array of extracted features with shape according to the last pooling layer of VGG16.

This snippet uses the VGG16 model pre-loaded with weights trained on ImageNet. By setting include_top=False, we can fetch the model without its fully connected output layers, making it perfect for feature extraction. The sample image is loaded, preprocessed accordingly, and passed through the model to obtain the features.

Method 2: Custom Feature Extractor Layers

A custom feature extraction can be performed by building a Sequential model with specific layers configured for the type of data being handled. Convolutional layers followed by pooling layers can serve as a basic yet effective structure for feature extraction.

Here’s an example:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten

# Build the model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
    MaxPooling2D(pool_size=(2, 2)),

# Assume `data` variable is a preprocessed batch of images
features = model.predict(data)

Output: A Numpy array of features reshaped into a vector suitable for training or further processing.

This code defines a Sequential model with convolutional and pooling layers to build a basic feature extractor. The Flatten() layer then converts the two-dimensional features into a one-dimensional vector. When model.predict() is called with image data, the output is a feature vector.

Method 3: Feature Extraction with Data Augmentation

Data augmentation techniques can improve the quality of feature extraction by introducing variance that helps make the model more robust. This can be accomplished within Keras by configuring an ImageDataGenerator instance.

Here’s an example:

from keras.preprocessing.image import ImageDataGenerator

# Create an instance of ImageDataGenerator with augmentation parameters
datagen = ImageDataGenerator(rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)

# Assume `model` is a pre-built Keras Sequential model for feature extraction
features_generator = datagen.flow(data, batch_size=32)
features = model.predict_generator(features_generator, steps=len(data)/32)

Output: Augmented Numpy array of features.

This example demonstrates how to employ an ImageDataGenerator to apply random transformations such as rotations and flips. The generator then produces these augmented images which are fed into the model for feature extraction. This approach helps in achieving better generalization by simulating a more diverse training dataset.

Method 4: Transfer Learning with Fine-Tuning

Feature extraction can also benefit from transfer learning where a pre-trained model is fine-tuned on a new dataset. This involves replacing the top layer and training only these new layers on the target dataset while keeping the pre-trained base layers frozen.

Here’s an example:

from keras.applications.resnet import ResNet50
from keras.layers import Dense, GlobalAveragePooling2D
from keras.models import Model

# Load the ResNet50 base model without the classification layers
base_model = ResNet50(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)  # New FC layer for feature extraction

# Append a new softmax layer with the number of classes in the target dataset
predictions = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

# Freeze base model layers and compile
for layer in base_model.layers:
    layer.trainable = False
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# Continue with fine-tuning or feature extraction...

Output: A model with new layers suitable for feature extraction on the target dataset.

This code customizes the ResNet50 model for a different task by adding a global average pooling layer and a fully connected layer with ReLU activation before the final classification layer. The base model’s layers are frozen to maintain their pre-trained weights, which allows for effective feature extraction with the newly added layers during fine-tuning.

Bonus One-Liner Method 5: Lambda Layers for Quick Custom Features

For quick inline feature extraction without the need for building complex models, Keras Lambda layers can be used. These layers allow you to apply any function to the input data as it passes through the network.

Here’s an example:

from keras.layers import Lambda
from keras.models import Sequential

# Build the model with a Lambda layer for simple feature extraction
model = Sequential([
    Lambda(lambda x: x ** 2, input_shape=(10,))

# Assume `data` is input data that needs to be transformed
features = model.predict(data)

Output: Transformed features as per the Lambda layer’s function.

This simplistic example uses a Lambda layer to square the input data, which serves as a naive form of feature extraction. The model can be expanded with additional Lambda layers for more complex operations depending on the use-case.


  • Method 1: Pre-trained CNNs. Strengths: Quick setup, robust feature extraction using established architectures. Weaknesses: May not be optimized for very specific tasks or novel data types.
  • Method 2: Custom Feature Extractor Layers. Strengths: Complete control over the architecture, tailored to specific data. Weaknesses: Requires in-depth knowledge of model architecture and may need extensive training.
  • Method 3: Data Augmentation. Strengths: Improves model generalization, simulates a more diverse dataset. Weaknesses: Computationally more expensive, can lead to overfitting if not managed well.
  • Method 4: Transfer Learning with Fine-Tuning. Strengths: Balances between pre-trained efficiency and customization for specific tasks. Weaknesses: Fine-tuning process may still require a sizable dataset and computation resources.
  • Method 5: Lambda Layers. Strengths: Quick, inline feature transformations. Weaknesses: Limited by the simplicity of functions that can be applied; not suitable for complex feature extraction needs.