Reputation: 197
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
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