I am using the Weighted random sampler function of PyTorch to sample my classes equally, But while checking the samples of each class in a batch, it seems to sample randomly.
here is a snippet of my code.
def cal_sample_weight(files):
print("file length ",len(files))
labels = [int(f[-5])-1 for f in files]
class_count = [labels.count© for c in np.unique(labels)]
print("class count is ",class_count)
# weights_class = [(1.0/len(np.unique(labels)))/(cc) for cc in class_count]
weights_class = [1.0/(cc) for cc in class_count]
print("Weight class is ", weights_class)
weight_array = np.array([weights_class[l] for l in labels])
print(weight_array.shape)
return torch.from_numpy(weight_array)
np.random.seed(0)
random.shuffle(Train_files)
random.shuffle(Val_files)
print([(int(image_path[-5])-1) for image_path in Train_files[0:32]])
Train_labels = [(int(image_path[-5])-1) for image_path in Train_files]
Val_labels = [(int(image_path[-5])-1) for image_path in Val_files]

Train_sample_weight = cal_sample_weight(Train_files)
Val_sample_weight = cal_sample_weight(Val_files)
training_set = Dataset(list_IDs=Train_files,labels=Train_labels, sample_weight=Train_sample_weight, shuffle=True, augment=True)
print("length of train is ", len(training_set))
sampler_train = torch.utils.data.sampler.WeightedRandomSampler(weights = Train_sample_weight, num_samples=len(training_set))
trainloader = DataLoader(torch.utils.data.Subset(training_set,Train_labels), batch_size=batch_size, sampler=sampler_train,shuffle=False, num_workers=24, pin_memory=True)