In this tutorial, we would obtain the dataset we need for image recognition. We would cover the following:
- Download the Training and Test Datasets
- View the Images
- Create the DataLoaders
- Examine the Shape of the Data
1. Download the Training and Test Datasets
Download the training dataset
training_data = datasets.FashionMNIST( root="data", train=True, download=True, transform=ToTensor(), )
Download the test dataset
test_data = datasets.FashionMNIST( root="data", train=False, download=True, transform=ToTensor(), )
2. View the Images
To see the images, use the code below:
import matplotlib.pyplot as plt image, label = training_data[1] plt.imshow(image.squeeze(), cmap="gray") plt.title(f"Label: {label}") plt.show()
The image displays as shown below:

3. Create the DataLoaders
Dataloaders helps in efficiently loading and managing datasets for training and evaluating deep learning models. It is a key component in the data preprocessing pipeline.
We would need to pass the Dataset argument to the DataLoader. With it, we have a wrapper for our dataset which we can use to iterate over the dataset in defined batch sizes such as 64. This means that each element in the dataset would return a batch of 64 features and classes.
The dataloader would load the data in batches of 64.
batch_size = 64 train_dataloader = DataLoader(training_data, batch_size=batch_size) test_dataloader = DataLoader(test_data, batch_size=batch_size)
4. Examine the Shape of the Data
The the dataloader provides the features (X) and the outputs (y). We can use the shape() method to examine the shape of the data
shape() returns a tuple of 4 items
N: batch_size
c: channels, this is the needed to encode each image. For black and white images, the value is 1. this means we only need one color, white and we enocode the intensity. For colored images it would be 3
This means we must encode 3 colors, Red, Green and Blue (RGB)
H: height of the image
W: width of the image
You can see the shape of the data.
for X, y in test_dataloader: print(f"Shape of X [N, C, H, W]: {X.shape}") print(f"Shape of y: {y.shape}, {y.dtype}") break
In the next tutorial, we would build the model.