Hey all. I’m trying to create a weighted sampler to do balanced sampling on my training set, and I created a sampler based off of the response here (Is there a better way to split data and deal with an unbalanced dataset? 5). Therefore, my code to generate a weighted sampler is very similar:
def get_weighted_sampler(dataset):
    sampler = None
    # Create weight array for each training sample
    sorted_label_counts = dataset.label_counts.sort_index()
    label_weights = sum(sorted_label_counts.iloc[:]) / np.array(sorted_label_counts.iloc[:])
    sampling_weights = []
    for i in range(len(dataset)):
        print(i)
        _, image_label = camera_catalogue_training[i]
        sampling_weights.append(label_weights[image_label])
    sampler = torch.utils.data.sampler.WeightedRandomSampler(sampling_weights , len(sampling_weights))
    print(len(sampling_weights))
    return sampler

I then tried to use this sampler with a DataLoader as follows:
training_sampler = get_weighted_sampler(camera_catalogue_training)
training_loader = torch.utils.data.DataLoader(camera_catalogue_training, batch_size=8, shuffle=True)
validation_loader = torch.utils.data.DataLoader(camera_catalogue_validation, batch_size=8, shuffle=True)
test_loader = torch.utils.data.DataLoader(camera_catalogue_test, batch_size=8, shuffle=True)

I didn’t get any errors with initializing the DataLoader itself, but when I try to iterate over a batch of the DataLoader, I get a “TypeError: len() of unsized object” error. I believe this has to do specifically with this weighted sampler I’m trying to work with, because when I remove sampler and just use a normal DataLoader, I’m able to iterate and examine the contents of a batch perfectly fine. Any ideas?