Hi everyone,
I have a custom data set for which I used torch.utils.data.Dataset. The getitem(self, index) method returns [torch.Tensor, label] for a specific index. The data is highly unbalanced.
For this I used the weighted random sampler and have the following function in order to obtain the weights :
def get_sample_weights(dataset_instance):
    label_dict = dataset_instance.labels
    classes = []
    for key in label_dict:
        idx = label_dict[key]
        classes.append(idx)

    class_counts = torch.tensor(classes)
    class_sample_count = torch.tensor([(class_counts == t).sum() for t in torch.unique(class_counts, sorted=True)])
    weight = 1. / class_sample_count.float()
    sample_weights = torch.tensor([weight[t] for t in class_counts])
    return sample_weights

Afterwards I use
test = CustomDataSet(...)
weights = get_sample_weights(test)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, len(weights))
dl = DataLoader(test, batch_size=100, collate_fn=stack_collate, sampler=sampler)

But the data I get from the DataLoader is still unbalanced. Do you guys have any idea what is wrong or have any tips how to solve this issue ?
Thank you in advance