Skip to content

Instantly share code, notes, and snippets.

Last active August 27, 2023 18:43
Star You must be signed in to star a gist
What would you like to do?
PyTorch Image File Paths With Dataset Dataloader
import torch
from torchvision import datasets
class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths. Extends
# override the __getitem__ method. this is the method that dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
path = self.imgs[index][0]
# make a new tuple that includes original and the path
tuple_with_path = (original_tuple + (path,))
return tuple_with_path
# instantiate the dataset and dataloader
data_dir = "your/data_dir/here"
dataset = ImageFolderWithPaths(data_dir) # our custom dataset
dataloader = torch.utils.DataLoader(dataset)
# iterate over data
for inputs, labels, paths in dataloader:
# use the above variables freely
print(inputs, labels, paths)
Copy link

`import torch
from torchvision import *

transforms = transforms.Compose([
class ImageFolderWithPaths(datasets.ImageFolder):

def getitem(self, index):
# this is what ImageFolder normally returns
original_tuple = super(ImageFolderWithPaths, self).getitem(index)
# the image file path
path = self.imgs[index][0]
tuple_with_path = (original_tuple + (path,))
return tuple_with_path
data_dir = "./sig_datasets/"
dataset = ImageFolderWithPaths(data_dir, transform=transforms)
dataloader =

iterate over data
for i, data in enumerate(dataloader):
images,labels,paths = data

This code worked for me.

Copy link

jshtok commented Mar 19, 2021

Works out of the box. Thanks!

Copy link

You are my hero! thank you!

Copy link

eformx commented Dec 16, 2021

How would I modify this to isolate files with a wildcard? For example if I wanted to isolate all image files that start with vid_1234.

Copy link

Wondeeful! You save my day!

Copy link

Thanks; hard to imagine that ImageFolder doesn't have this function / flag

Copy link

May I ask under what license this snippet is released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment