PYTORCH DATA LOADERS — 4 Types
In this article I will show you how to setup Data loaders and Transformers in Pytorch, You need to import below for the same exercise
import torchvision
import torch
import os
import matplotlib.pyplot as plt
import numpy as np
MNIST DATASET
1. Define the Transform
Image Resize (256,256) or Any other size
Convert to Pytorch Tensors
Normalize the Image by calling torchvision.transform.Normalize
transform_img = torchvision.transforms.Compose([torchvision.transforms.Resize((256, 256)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=[0.485],std=[0.229])])
2. Create the DataSet from torchvision.datasets
Set some Directory Path, download = True will download the data into the directory specified, transform should be set to transform defined above
dir_path= ‘C:\\Users\\Asus\\pytorch-basics-part2’
dataset_mnist_train = torchvision.datasets.MNIST(dir_path, train=True, transform=transform_img,
target_transform=None, download=True)
You can index this Dataset, dataset_mnist_train[i] will contain the Tuple of (Image, Label).
3. Create the DataLoader
Important things here is that batch_size should be set to 8,16 or 32 depending on the application, Batch_size=1 will make the model very slow, Where as higher batch sizes would require more GPU memory. Set the Shuffle = True.
dataloader_mnist_train= torch.utils.data.DataLoader(dataset_mnist_train, batch_size=32,
shuffle=True, num_workers=1)
4. Check the Data
You can now use this Data loader as follows
images, classes = next(iter(dataloader_mnist_train))
OR
for images, classes in dataloader_mnist_train:
print(image.shape)
print(class)
Note that Images have Batch size of 32 as we created dataloader of 32 batch size, Also note that Labels will have 32 values for each image
torch.Size([32, 1, 256, 256])
tensor([7, 0, 9, 6, 2, 5, 6, 3, 3, 2, 7, 6, 7, 9, 2, 6, 0, 2, 1, 6, 8, 0, 0, 8,
3, 4, 3, 8, 5, 0, 8, 9])
torch.Size([32, 1, 256, 256])
tensor([9, 8, 0, 5, 4, 4, 0, 1, 4, 0, 2, 6, 3, 0, 4, 0, 2, 0, 1, 6, 0, 8, 6, 1,
2, 4, 0, 7, 3, 5, 1, 0])
torch.Size([32, 1, 256, 256])
tensor([1, 2, 4, 5, 6, 3, 5, 1, 8, 5, 9, 6, 9, 2, 5, 5, 1, 1, 8, 2, 2, 1, 3, 3,
1, 1, 0, 3, 4, 2, 7, 2])
torch.Size([32, 1, 256, 256])
tensor([5, 8, 7, 2, 8, 1, 7, 4, 1, 0, 8, 3, 1, 1, 5, 1, 1, 4, 0, 1, 0, 2, 6, 1,
2, 6, 9, 9, 5, 1, 4, 4])
Print the Images
Define a small function to convert Torch tensor to numpy array
def convert_to_numpy(inp):
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
return inp
Make a grid of the 32 images in a batch
images, classes = next(iter(dataloader_mnist_train))
images.shape
torch.Size([32, 1, 256, 256])
grid_image= torchvision.utils.make_grid(images)
out.shape
torch.Size([3, 1034, 2066])
images_numpy=convert_to_numpy(out)
plt.imshow(images_numpy)
FashionMNIST DataSet
Very similar procedure as MNIST Data, (1) Create Image transformer (2) Create fashion mnist data from torchvision.datasets.FashionMnist (3) Create a Data Loader. This time we have chosen a batch size of 8
transform_img = torchvision.transforms.Compose([torchvision.transforms.Resize((256, 256)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=[0.485],std=[0.229])])
dataset_fashion_mnist_train = torchvision.datasets.FashionMNIST(dir_path, train=True, transform=transform_img,
target_transform=None, download=True)
dataloader_fashionmnist_train= torch.utils.data.DataLoader(dataset_fashion_mnist_train, batch_size=8,
shuffle=True, num_workers=4)
Print the Sample Images
images, classes = next(iter(dataloader_fashionmnist_train))
grid_images= torchvision.utils.make_grid(images)
images_numpy=convert_to_numpy(grid_images)
fig = plt.figure(figsize=(20, 8))
plt.imshow(images_numpy)
SBU DataSet
SBU Dataset has Images with Target/Labels as Image Captions (Text)
Data Download/Transform and Data Loader creation is very similar to MNIST and FASHION MNIST, Only difference is that SBU Data has colored images and each image will have 3 channels(R,G,B)
dir_path= ‘C:\\Users\\Asus\\pytorch-basics-part2’
transform_img = torchvision.transforms.Compose([
torchvision.transforms.Resize(256),
torchvision.transforms.CenterCrop(224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])] )
dataset_sbu_train=torchvision.datasets.SBU(dir_path, transform=transform_img, target_transform=None, download=True)
dataloader_sbu_train= torch.utils.data.DataLoader(dataset_sbu_train, batch_size=8,
shuffle=True, num_workers=4)
Check the Data and Print the Images
images, classes = next(iter(dataloader_sbu_train))
grid_images= torchvision.utils.make_grid(images)
images_numpy=convert_to_numpy(grid_images)
fig = plt.figure(figsize=(20, 8))
plt.imshow(images_numpy)
print(classes) ### These are Image Captions
(' I love that little white hippie van in front of us.', " The first-floor women's bathroom in Canaday library is covered with pun-filled graffiti like this. I cannot resist a pun!", ' There is a black cat in my back yard', ' Tools and bearings all over the floor a sure sign of work being done!', ' we stopped to check on the geese (who are off in the lake to the left of the frame) on our morning bike ride.', ' bowl of fruit (that was consumed by me soon after this was taken)', ' Stained glass window in the Ridderzaal', ' Rachel up in the tree house that is still being built.')
You can view my Youtube Video for detailed explanation of these Data Loaders
Youtube channel: https://www.youtube.com/c/nagarajbmachinelearning