Deep Learning PyTorch Course, Feature Extraction Techniques

Deep learning is a powerful technology that enables problem-solving by automatically learning useful features from various data. Today, we will address feature extraction techniques using the PyTorch library. This plays a crucial role in extracting attributes from various forms of data such as images, text, and audio to enhance the performance of machine learning models.

What is Feature Extraction?

Feature extraction refers to the process of transforming original data into a lower dimension to extract useful information. This process helps reduce noise in the data and alleviates the difficulties encountered by the model during learning. For example, in an image classification problem, instead of directly using the pixel values of the images, we can utilize CNN (Convolutional Neural Network) to extract only the important features.

1. Extracting Features from Image Data

We will look at an example of using CNN to extract features in the field of image processing. CNN is structured favorably to capture local information in images.

1.1 Data Preparation

import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch
import torch.nn as nn
import torchvision.models as models

# Download and preprocess dataset
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
])

# Example of CIFAR10 dataset
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=32, shuffle=True)

1.2 Define CNN Model

We will use a model based on ResNet to extract features.

# Load ResNet model
model = models.resnet18(pretrained=True)  # Pre-trained model
model.fc = nn.Identity()  # Remove the last layer to output features only

# Set GPU usage
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

1.3 Extract Features

To check the shape of the extracted features, let’s pass the data through the model.

def extract_features(data_loader):
    features = []
    
    model.eval()  # Switch to evaluation mode

    with torch.no_grad():  # Disable gradient calculation
        for images, labels in data_loader:
            images = images.to(device)
            feature = model(images)
            features.append(feature.cpu())

    return torch.cat(features)

# Execute feature extraction
features = extract_features(train_loader)
print("Size of extracted features:", features.size())

2. Extracting Features from Text Data

To handle text data, we will explore how to extract features using RNN (Recurrent Neural Network). This is commonly used in natural language processing (NLP).

2.1 Data Preparation

from torchtext.datasets import AG_NEWS
from torchtext.data import Field, BucketIterator

TEXT = Field(tokenize='spacy', lower=True)
LABEL = Field(sequential=False)

# Load AG News dataset
train_data, test_data = AG_NEWS splits=(TEXT, LABEL))
TEXT.build_vocab(train_data)
LABEL.build_vocab(train_data)

# Build data loaders
train_iterator, test_iterator = BucketIterator.splits((train_data, test_data), batch_size=64, device=device)

2.2 Define RNN Model

class RNN(nn.Module):
    def __init__(self, input_dim, embed_dim, hidden_dim, output_dim):
        super().__init__()
        self.embedding = nn.Embedding(input_dim, embed_dim)
        self.rnn = nn.RNN(embed_dim, hidden_dim)
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, text):
        embedded = self.embedding(text)
        output, hidden = self.rnn(embedded)
        return hidden

# Instantiate the model
input_dim = len(TEXT.vocab)
embed_dim = 100
hidden_dim = 256
output_dim = len(LABEL.vocab)

model = RNN(input_dim, embed_dim, hidden_dim, output_dim).to(device)

2.3 Extract Features

Now we will use the RNN model to extract features from the text data.

def extract_text_features(data_loader):
    text_features = []

    model.eval()

    with torch.no_grad():
        for batch in data_loader:
            text, labels = batch.text
            text = text.to(device)
            hidden = model(text)
            text_features.append(hidden.cpu())

    return torch.cat(text_features)

# Execute feature extraction
text_features = extract_text_features(train_iterator)
print("Size of extracted text features:", text_features.size())

Conclusion

In this post, we explored how to extract features from image and text data using PyTorch. We confirmed that we can implement feature extraction methods suitable for each data type using structures such as CNN and RNN. Feature extraction is an essential step in improving the performance of machine learning models and enabling smooth data analysis. We encourage further exploration of various models and techniques!

If you have any questions related to feature extraction or need more information, please leave a comment. Thank you!