#Exercices
#Exercise: Create a Custom Dataset with Transformations
Objective: Create a custom dataset class for a set of images stored in a local directory, apply transformations to the images, and visualize a few samples with the transformations applied.
Steps:
Create a Custom Dataset Class:
- Write a PyTorch
Dataset
class to load images from a directory. - Each image should have a corresponding label from a CSV file (format:
filename,label
).
- Write a PyTorch
Apply Transformations:
- Resize the images to 128x128 pixels.
- Convert the images to PyTorch tensors.
- Normalize the images with a mean of 0.5 and standard deviation of 0.5.
Load the Dataset with DataLoader:
- Use
DataLoader
to load the dataset and prepare it for training.
- Use
Visualize Transformed Images:
- Display a few images from the dataset with the transformations applied.
#Explanation of the Solution
Custom Dataset Class:
- A custom dataset class
CustomImageDataset
is created to load images and labels from a specified directory and CSV file. - The
__getitem__
method loads an image and its label and applies transformations if provided.
- A custom dataset class
Define Transformations:
- Images are resized to 128x128 pixels.
- Images are converted to PyTorch tensors.
- Images are normalized using a mean of 0.5 and a standard deviation of 0.5 for all channels.
DataLoader:
DataLoader
is used to load the dataset in batches and shuffle the data for training.
Visualization:
- A helper function
show_images
is used to display the first 8 images from a batch with the transformations applied, showing the effect of resizing, normalization, and tensor conversion.
- A helper function