I am working with an unbalanced dataset and I’m trying to use pytorch wonderful tools to deal with this way too common problem. I am having trouble using the WeightedRandomSampler. I will write my approach so it’s easier to give feedback.
##
## Necessary imports
##
transform = transforms.Compose([
        transforms.RandomResizedCrop(
            size=224, scale=(0.4, 1.6), ratio=(0.9, 1.1)),
         transforms.ColorJitter(brightness=0.1, saturation =0.1,
                               contrast= 0.1, hue=0.3),
        transforms.RandomVerticalFlip(),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor()
    ])

weights = 1 / (torch.FloatTensor([970, 3308, 2407, 212, 4422, 11424, 286, 594, 272])+1e-5)
sampler = torch.utils.data.WeightedRandomSampler(weights=weights, num_samples=len(weights), replacement=True)
# Load the dataset
train_dir = base_dir + "/train"

train_dataset = torchvision.datasets.ImageFolder(train_dir, transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=hparams['batch_size'],                                             drop_last=True, num_workers=nw, sampler=sampler)

Now if I run:
>>> len(train_loader)
>>> 0

If I don’t use the sampler the result is not 0 but the number of images divided by the batch size which would be correct result.
Do you see anything that I’m doing wrong? Also, I don’t fully understand the use of the “replacement” argument, if you could spare a moment to explain it I’d be very grateful.