Hello,
I have an imbalanced dataset in 6 classes, and I’m using the “WeightedRandomSampler”, but when I load the dataset, the train doesn’t work. My code is here:
train_transforms = transforms.Compose([
transforms.Resize((sz, sz)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
valid_transforms = transforms.Compose([
transforms.Resize((sz, sz)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
train_ds = datasets.ImageFolder(train path, train_transforms)
valid_ds = datasets.ImageFolder(valid path, valid_transforms)
sample_count = [224, 477, 5027, 4497, 483, 247]
weight = 1 / torch.Tensor(sample_count)
sampler = WeightedRandomSampler(weight, batch_size)
train_dl = torch.utils.data.DataLoader(train_ds, batch_size=batch_size, sampler=sampler)
valid_dl = torch.utils.data.DataLoader(valid_ds, batch_size=batch_size, shuffle=True)
train_ds_sz = len(train_ds)
valid_ds_sz = len(valid_ds)
print('Train size: {}\nValid size: {} ({:.2f})'.format(train_ds_sz, valid_ds_sz, valid_ds_sz/(train_ds_sz +
valid_ds_sz)))
class_names = train_ds.classes
Any help would be appreciated.