How can I know the size of data_loader when i use: torchvision.datasets.ImageFolder

Im following the example here, regarding torchvision.datasets.
In the example we have:

imagenet_data = torchvision.datasets.ImageFolder('path/to/imagenet_root/')
data_loader = torch.utils.data.DataLoader(imagenet_data,
                                          batch_size=,
                                          shuffle=True,
                                          num_workers=args.nThreads)

I am wondering if there is a way that I can know the number of images in data_loader? I want to check its size and see how many images is in it…

Thank you

4 Likes

you can do:

print(len(imagenet_data))
8 Likes

You can get the length of dataloder’s dataset like this:

print(len(dataloader.dataset))
33 Likes

How would we do the same when we use sampler=torch.utils.data.SubsetRandomSampler() when creating the dataloader?

indices = np.arange(0, len(dataset))
train_dl = DataLoader(dataset, bs, sampler=torch.utils.data.SubsetRandomSampler(indices[:300]))
test_dl  = DataLoader(dataset, bs, sampler=torch.utils.data.SubsetRandomSampler(indices[-300:]))
len(train_dl.dataset) == len(test_dl.dataset)
2 Likes

len(loader.dataset.indices) should give you the number of images in the dataloader

1 Like

You can do with:
len(train_loader.sampler.indices)

1 Like

print(len(dataloader.dataset)) worked