I am trying to write a custom DataLoader to combine two image datasets. These datasets have different lengths but the same label classes. Dataset A is relatively small, while Dataset B is relatively big. The procedure would look like this:

Sample a batch from Dataset A with label x1.
Sample a batch from Dataset B with label x1.
Repeat the above steps for all remaining labels x2, x3…
Sample another batch from Dataset A with label x1.
Sample another batch from Dataset B with label x1.
And so on.
Once all the images from Dataset A are exhausted, I would like to start over for Dataset A.
Once all the images from Dataset B are exhausted, the procedure stops.

I am aware of ConcatDataset, but it doesn’t really suit my purpose. One idea would be to use Subset to construct different iterators for different classes. This is obviously not practical. Is there another way to go about this I am missing?