DEEP LEARNING • LESSON 14

Feature Extraction

Feature extraction is a Transfer Learning technique where we use a pretrained model to extract useful features from our data and train a new model using those features.

SIMPLE IDEA

Use the pretrained model as a feature extractor.

The pretrained model has already learned useful patterns. We keep those learned features and use them to solve a new problem.

01

What Is Feature Extraction?

A neural network can learn different features from data. In an image model, early layers can learn simple patterns such as edges and lines, while deeper layers can learn shapes and more complex visual patterns.

With feature extraction, we take a pretrained model and reuse the features it has already learned.

Input Image
      ↓
Pretrained Model
      ↓
Learned Features
      ↓
New Classifier
      ↓
Prediction

Instead of asking the model to learn everything again, we reuse the existing feature-learning part.

02

Why Do We Use Feature Extraction?

Training a deep neural network from scratch can require a large amount of data and computing power.

A pretrained model has already learned many useful patterns, so we can reuse those patterns for our new problem.

Training From Scratch

Random Model
      ↓
Learn Edges
      ↓
Learn Shapes
      ↓
Learn Objects
      ↓
Learn Your Task


Feature Extraction

Pretrained Model
      ↓
Already Learned Features
      ↓
Your Dataset
      ↓
New Classifier
      ↓
Prediction

This can make training faster and can be especially useful when our new dataset is relatively small.

03

What Happens to the Pretrained Model?

In feature extraction, we normally freeze the pretrained layers. Freezing means that their weights are not updated during training on the new dataset.

Pretrained Layers
        ↓
    FROZEN
        ↓
Extract Features
        ↓
New Classification Layer
        ↓
    TRAINED
        ↓
Prediction

The pretrained layers keep their existing knowledge while the new classification layer learns the new task.

04

Simple Example — Flower Classification

Imagine we have a pretrained image model that already understands many general visual patterns.

We want to build a model that identifies different types of flowers.

Flower Image
      ↓
Pretrained Model
      ↓
Edges
Shapes
Textures
Visual Features
      ↓
New Classifier
      ↓
Rose / Tulip / Lily

We don't need to teach the pretrained model basic visual features again. We only need to train the new classifier for our flower categories.

05

Example — Cats and Dogs

Suppose we have a pretrained image model and want to classify images as either cats or dogs.

             Pretrained Model
                    │
                    │
          ┌─────────┴─────────┐
          │                   │
     Learned Features     Frozen Layers
          │
          ↓
     New Classifier
          │
          ↓
     Cat or Dog

The pretrained model provides general visual features. The new classifier learns how those features relate to cats and dogs.

06

Feature Extraction vs Training From Scratch

TRAINING FROM SCRATCH

Random Weights
      ↓
Train All Layers
      ↓
Learn Features
      ↓
Learn New Task
      ↓
Final Model


FEATURE EXTRACTION

Pretrained Weights
      ↓
Freeze Pretrained Layers
      ↓
Extract Features
      ↓
Train New Classifier
      ↓
Final Model

The biggest difference is that feature extraction starts with knowledge that has already been learned.

07

Feature Extraction in Python

TensorFlow and Keras provide pretrained models that can be used for feature extraction.

For example, we can use MobileNetV2 as the pretrained feature extractor.

import tensorflow as tf

base_model = tf.keras.applications.MobileNetV2(
    weights="imagenet",
    include_top=False,
    pooling="avg"
)

base_model.trainable = False

The model is loaded with pretrained ImageNet weights. Setting trainable to False freezes the pretrained layers.

08

Add a New Classifier

After loading the pretrained feature extractor, we can add our own classification layer.

model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.Dense(2, activation="softmax")
])

The final layer has two outputs because our example has two classes.

Output 0 → Cat
Output 1 → Dog

The pretrained model extracts features, and the new classifier uses those features to make the final prediction.

09

Complete Simple Example

import tensorflow as tf

# Load pretrained model
base_model = tf.keras.applications.MobileNetV2(
    weights="imagenet",
    include_top=False,
    pooling="avg"
)

# Freeze pretrained layers
base_model.trainable = False

# Build new model
model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.Dense(2, activation="softmax")
])

# Compile model
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

model.summary()

MobileNetV2 provides the pretrained features, while the final Dense layer is used for the new classification task.

10

The Main Idea to Remember

Pretrained Model
        ↓
Freeze Its Layers
        ↓
Use It To Extract Features
        ↓
Add New Classifier
        ↓
Train New Classifier
        ↓
Make Predictions

Feature extraction does not mean manually finding features. The neural network extracts the useful features for us.

KEY TAKEAWAY

Reuse what the model has already learned.

Feature extraction uses a pretrained model as a fixed feature extractor. Its learned layers are frozen, and a new classifier is trained for the new task.

Quick Check

What is feature extraction?

Using features learned by a pretrained model for a new machine learning task.

Why are pretrained layers frozen?

To keep the useful knowledge already learned by the pretrained model while training the new classifier.

What is trained?

In basic feature extraction, the new classification layer is trained while the pretrained layers remain frozen.