That’s very helpful here’s the solution with a datafolder:
def sample_weight(data_folder):
    
    class_sample_count = np.array([len([i for i in data_folder.targets if i == t]) for t in range(0, len(data_folder.classes))])
    weight = 1 / class_sample_count
    samples_weight = np.array([weight[t] for t in data_folder.targets])
    samples_weight = torch.from_numpy(samples_weight)
    samples_weight = samples_weight.double()
    sampler = WeightedRandomSampler(samples_weight, len(samples_weight))
    return sampler

train_dataset = DatasetFolder(root=train_dir, loader=csv_loader, extensions=".csv")
train_sample_weight = sample_weight(train_dataset)