Is there any way to make the specific class get into the minibatch from dataloader?

Hi, I’d like to make the dataloader that samples the minibatch consists of specific class all the time.

For example, if the batch size 32, and the class label is 4(cat, dog, truck, ship), I want to make the minibatch as 4examples of cat, 12 examples of dog, 4 examples of truck, 12 examples of ship.

Is there any solutions to make dataloader like above?

You could create a custom sampler, which is responsible to create the indices passed to Dataset.__getitem__. This custom sampler could use the target to create the valid indices for each batch such that the desired classes will be used.

1 Like