Skip to main content
Welcome to this comprehensive guide on PyTorch Datasets and Dataloaders. In this lesson, you will learn how to load preexisting datasets from TorchAudio and TorchVision, as well as build and visualize custom datasets. These techniques allow you to efficiently stream data to your model during training while ensuring your data is organized and accessible.

Preloaded Audio Datasets

Preloaded datasets are excellent for research and experimentation. In this section, we use TorchAudio’s built-in DR_VCTK (Device Recorded VCTK) dataset. The snippet below downloads the test subset of the dataset into a folder named “audio”. Note that the dataset is sizable (approximately 1.6 GB) and may take a minute or two to download.
Once the download is complete, you can inspect the ./audio folder to explore the dataset.

Preloaded Image Datasets

Next, we explore preloaded image datasets using TorchVision. In this example, we use the FashionMNIST classification dataset. A transformation is applied to convert images to tensors for further processing.
After downloading, the dataset is stored in the “fashion” directory. You can inspect the class labels and index mapping as shown below:
For FashionMNIST, the classes are:
with indices ranging from 0 to 9.

Visualizing the FashionMNIST Dataset

Visualizing a subset of the dataset helps to better understand the data. The following code randomly displays a grid of 9 images along with their labels:
Alternatively, you can visualize the dataset with a different grid layout:
The grid maps numerical labels to human-friendly class names, making it easier to interpret the visualized data.
The image shows a grid of heatmap-style visualizations of clothing items, including a dress, bag, T-shirt, and trousers, labeled accordingly. It appears to be part of a Jupyter Notebook interface, likely related to PyTorch DataLoaders.

Working with DataLoaders

A DataLoader handles the batching and shuffling of your dataset during training. Below is an example that demonstrates how to create a DataLoader for the FashionMNIST dataset with a batch size of 64, ensuring that the data is shuffled during training.
To evaluate a single batch, iterate over the DataLoader once:
For example, the output might be:
This confirms that each batch contains 64 grayscale images of size 28x28 along with their corresponding labels. To further visualize a random image from the batch and display its human-readable label:
Executing this code snippet repeatedly will display various images and their correct labels from the dataset.

Creating a Custom Dataset

If you have your own image collection and corresponding labels, you can define a custom dataset using PyTorch’s Dataset class. In this example, we assume that image file paths and labels are stored in a CSV file named labels.csv.
Ensure your CSV file is formatted correctly, as shown in the example below.

Defining the Custom Dataset

Import the necessary modules and create a custom dataset class as follows:
Assume your labels.csv is structured as follows:
Create an instance of your custom dataset with:
To inspect the dataset details, use:
Since the custom dataset does not automatically generate a mapping from class names to indices, you can create one manually:

Visualizing the Custom Dataset

The following code snippet visualizes 9 random images from your custom dataset:
Each execution displays different images along with their labels (e.g., 0 for cat, 1 for dog).

Custom Dataset DataLoader

Similar to preloaded datasets, you can create a DataLoader for your custom dataset. Even if the dataset contains fewer images than the specified batch size (64 in this example), the DataLoader will return all available samples.
For example, the output might be:
To visualize a random image from this batch with its corresponding label:
Running this snippet multiple times will help validate that your custom dataset and its label mapping work correctly.

Using TorchVision’s ImageFolder

An efficient alternative for organizing images is to use TorchVision’s ImageFolder. When your images are arranged such that each class has its own subdirectory, ImageFolder automatically assigns labels based on these subdirectory names.
Load this dataset with a DataLoader:
To visualize a batch of images from the ImageFolder dataset:
This approach leverages the directory structure to automatically generate class labels, simplifying dataset creation when working with well-organized image folders.
The image shows a grid of animal photos, including cats and dogs, with labels "0" and "1" above each image. It appears to be part of a dataset used in a coding environment.

Conclusion

In this guide, we demonstrated techniques for working with preloaded datasets and DataLoaders in PyTorch, as well as methods for creating and visualizing custom datasets. These approaches help streamline data loading and preprocessing for model training, whether you’re using built-in libraries or your own data collections. Happy coding and exploring with PyTorch!
For more details on PyTorch data handling, visit the PyTorch Documentation.

Watch Video

Practice Lab