Deep Learning PyTorch Course, Data Preparation

In order to build a deep learning model, the data preparation step is essential. If the correct dataset is not prepared, the model’s performance may degrade, which can ultimately have a negative impact on the quality of real applications. Therefore, this course will explain the data preparation methods using PyTorch step by step, and we will practice through example code.

1. Importance of Data Preparation

The success of deep learning often depends on the quality and quantity of data. Therefore, the data preparation and preprocessing processes have the following key purposes:

  • Accuracy: Ensures the accuracy of the data to prevent the model from being fed incorrect information during training.
  • Consistency: Maintains a consistent data format so that the model can easily understand it.
  • Balance: In classification problems, it’s important to balance the classes.
  • Data Augmentation: In case of insufficient data, data augmentation techniques can be used to increase the training data.

2. Data Preparation Using PyTorch

PyTorch provides the torch.utils.data module for data preparation. This module helps to easily create datasets and data loaders. Here are the basic steps for data preparation:

2.1 Creating a Dataset

A dataset includes the images needed for the model to learn. To create a dataset, you must inherit the torch.utils.data.Dataset class and override the __getitem__ and __len__ methods. Here is a simple example:

import torch
from torch.utils.data import Dataset

class CustomDataset(Dataset):
    def __init__(self, data, labels):
        self.data = data
        self.labels = labels

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        return self.data[idx], self.labels[idx]

# Example data
data = torch.randn(100, 3, 32, 32)  # 100 32x32 RGB images
labels = torch.randint(0, 10, (100,))  # 100 random labels (0~9)

# Creating the dataset
dataset = CustomDataset(data, labels)
print(f"Dataset size: {len(dataset)}")  # 100
    

2.2 Creating a Data Loader

A data loader is used to fetch data in batches. Using a data loader allows you to effectively split the dataset into mini-batches to pass to the model. Here’s how to create a data loader:

from torch.utils.data import DataLoader

# Creating the data loader
data_loader = DataLoader(dataset, batch_size=16, shuffle=True)

# Outputting batch data
for batch_data, batch_labels in data_loader:
    print(f"Batch data size: {batch_data.size()}")  # [16, 3, 32, 32]
    print(f"Batch label size: {batch_labels.size()}")  # [16]
    break  # Output only the first batch
    

3. Data Preprocessing

The data preprocessing step is crucial in deep learning. Taking image data as an example, common tasks that should be performed during the preprocessing stage include:

  • Normalization: Normalizing the data to enhance the training speed and enable the model to generalize better.
  • Resizing: Adjusting the image size to fit the model.
  • Data Augmentation: Augmenting data to prevent overfitting and secure a broader dataset.

3.1 Image Data Preprocessing Example

The following is an example of image data preprocessing using torchvision.transforms:

from torchvision import transforms

# Define preprocessing steps
transform = transforms.Compose([
    transforms.Resize((32, 32)),  # Resizing the image
    transforms.ToTensor(),  # Convert to tensor
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])  # Normalization
])

# Modifying the dataset class
class CustomDatasetWithTransform(Dataset):
    def __init__(self, data, labels, transform=None):
        self.data = data
        self.labels = labels
        self.transform = transform

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        image = self.data[idx]
        label = self.labels[idx]
        
        if self.transform:
            image = self.transform(image)  # Apply transformations
        
        return image, label

# Creating the modified dataset
dataset_with_transform = CustomDatasetWithTransform(data, labels, transform=transform)
data_loader_with_transform = DataLoader(dataset_with_transform, batch_size=16, shuffle=True)

# Outputting batch data
for batch_data, batch_labels in data_loader_with_transform:
    print(f"Batch data size: {batch_data.size()}")
    print(f"Batch label size: {batch_labels.size()}")
    break
    

4. Data Augmentation

Data augmentation helps the deep learning model to generalize better by providing additional data points. Here are some data augmentation techniques:

  • Rotation: Rotating the image at random angles.
  • Cropping: Cropping random parts of the image.
  • Inversion: Inverting the colors of the image.

4.1 Data Augmentation Example

The following is an example of data augmentation using torchvision:

from torchvision import transforms

# Define data augmentation steps
augment = transforms.Compose([
    transforms.RandomHorizontalFlip(),  # Random horizontal flip
    transforms.RandomRotation(20),  # Random rotation
    transforms.ToTensor(),  # Convert to tensor
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])  # Normalization
])

# Applying augmentation steps to the dataset
dataset_with_augmentation = CustomDatasetWithTransform(data, labels, transform=augment)
data_loader_with_augmentation = DataLoader(dataset_with_augmentation, batch_size=16, shuffle=True)

# Outputting batch data
for batch_data, batch_labels in data_loader_with_augmentation:
    print(f"Batch data size: {batch_data.size()}")
    print(f"Batch label size: {batch_labels.size()}")
    break
    

5. Conclusion

Data preparation is a very important step in deep learning. It is essential to generate an appropriate dataset, use a data loader to fetch data in batches, and perform necessary data preprocessing and augmentation. In this lecture, we covered the basic processes of data preparation using PyTorch.

Apply these principles to maximize your model’s performance in your deep learning projects. Data is the most critical asset for a deep learning model. Therefore, proper data preparation is the cornerstone of a successful deep learning project.

References