user8152821
user8152821

Reputation: 197

How to Get n-Number of Images from torchvision.datasets.ImageFolder

I am currently experimenting on CNN using PyTorch, and the task I want the model to accomplish is to classify images.

I understand that using torchvision.datasets.ImageFolder can help with loading all images from my training folder, according to each subfolders' names as the labels.

I plan on taking only n-number of images from the ImageFolder randomly, but as far as I know, there is no mechanism for ImageFolder to load n-number of images randomly, where n is any number ranging from 1 to all available images.

How can I do that? Thanks for any help

Upvotes: 6

Views: 6621

Answers (1)

jodag
jodag

Reputation: 22284

You can create a subset of ImageFolder using PyTorch's Subset class. We can generate random indices using numpy or some other way if you want.

dataset = torchvision.datasets.ImageFolder(...)
dataset_subset = torch.utils.data.Subset(dataset, numpy.random.choice(len(dataset), n, replace=False))

Upvotes: 10

Related Questions