Image Datasets, Dataloaders, and Transforms
We will be implementing them on a sample dataset which can be downloaded from this link. You can download this dataset and follow along with this article to understand the concept better.
Import the necessary libraries
We will first import the libraries we will be using in this article.
Python3
import os import numpy as np from PIL import Image import matplotlib.pyplot as plt import torch import torchvision |
Image Dataset
An image dataset can be created by defining the class which inherits the properties of torch.utils.data.Dataset class. This class has two abstract methods which have to be present in the derived class:
- __len__(): returns the number of samples present in the dataset.
- __getitem__(): returns the sample at the ith index from the dataset.
We can load the image dataset in Pytorch as follows:
Python3
# Creating a custom dataset class class ImageDataset(torch.utils.data.Dataset): def __init__( self , dir , transform = None ): self .data_dir = dir self .images = os.listdir( dir ) self .transform = transform # Defining the length of the dataset def __len__( self ): return len ( self .images) # Defining the method to get an item from the dataset def __getitem__( self , index): image_path = os.path.join( self .data_dir, self .images[index]) image = np.array(Image. open (image_path)) # Applying the transform if self .transform: image = self .transform(image) return image |
Now let us use this class on our sample dataset.
Python3
# Replace the path with the path to your dataset data_path = './maps/train' # Creating a dataset object with the path to the dataset dataset = ImageDataset(data_path) # Getting the length of the dataset dataset_length = len (dataset) # Printing the length of the dataset print ( 'Number of training examples:' ,dataset_length) # Generating a random index within the dataset length random_index = random.randint( 0 , dataset_length - 1 ) # Plotting the randomly selected image plt.imshow(dataset[random_index]) plt.show() |
Output:
Number of training examples: 1096
Image Datasets, Dataloaders, and Transforms in Pytorch
Deep learning in Pytorch is becoming increasingly popular due to its ease of use, support for multiple hardware platforms, and efficient processing. Image datasets, dataloaders, and transforms are essential components for achieving successful results with deep learning models using Pytorch.
In this article, we will discuss Image datasets, dataloaders, and transforms in Python using the Pytorch library. Image datasets store collections of images that can be used in deep-learning models for training, testing, or validation. These images are collected from a variety of sources such as online websites, physical controllers, user-generated content, etc. Dataloaders are responsible for loading the image datasets and providing them in batches to the models. Transforms are algorithms used to alter certain aspects of the images such as color, size, shape, brightness, etc. In Pytorch, these components can be used to create deep learning models for tasks such as object recognition, image classification, and image segmentation.
Popular datasets such as ImageNet, CIFAR-10, and MNIST can be used as the basis for creating image datasets and Dataloaders. Popular image transforms such as random rotation, random crop, random horizontal or vertical flipping, normalization, and color augmentation can be used to create model-ready data. Dataloaders can be used to efficiently load batches of data from the dataset for model training.
Contact Us