Hi,
I am loading a custom data and I’ve read the post here:

  
    
    
    Balanced Sampling between classes with torchvision DataLoader vision
  
  
    Hi all, 
I’m trying to find a way to make a balanced sampling using ImageFolder and DataLoader with a imbalanced dataset. I suppose that I should build a new sampler. 
I’m not sure if I’m missing something. Is there an already implemented way of do it? 
Thanks 
Code: 
train_loader = torch.utils.data.DataLoader( 
datasets.ImageFolder(traindir, transforms.Compose([ 
transforms.Scale(600), 
transforms.RandomSizedCrop(512), 
transforms.ToTensor(), 
normalize 
])), 
batch_size=args.batch_size, shuffl…
  


I am using the same code as follows:
class_sample_count = np.array([len(np.where(y_train==t)[0]) for t in np.unique(y_train)])
weight = 1. / class_sample_count
samples_weight = np.array([weight[t] for t in y_train])

samples_weight = torch.from_numpy(samples_weight)
sampler = WeightedRandomSampler(samples_weight.type('torch.DoubleTensor'), len(samples_weight))

mb_size = 13
trainDataset = torch.utils.data.TensorDataset(torch.FloatTensor(X_train), torch.FloatTensor(y_train.astype(int)))

trainLoader = torch.utils.data.DataLoader(dataset = trainDataset, batch_size=mb_size, num_workers=1, sampler = sampler)

However, when I iterate for some epochs, there are cases of target vectors with all from the same class (either 0 or 1) which generates an error in my triplet loss function. What is the best way to handle this issue?
Is there a way to force the trainLoader to always load from both classes?
Thanks!