Feeding the model with different sized crops

I understand there are many posts about how to handle different sized images, but what I want to do is a bit different.

For each image, I want to crop a random location and create a batch of N crops in each iteration. The size of crops of course will have to be same per batch but I want to vary it randomly across different batches.
I want to avoid resizing.

I believe I can do this outside of the dataloader, like get a prepared batch and the do the random cropping before I feed the batch into the model, however, this won’t take advantage of the “num_workers” and the parralelization in the dataloader and hence will be a bit slow.

I am wondering if there is a way to do this inside the dataloader. Is there a way to randomly generate a size for each batch and share this size variable such that it is accessible to the transform function for each image inside a single batch?

I think there are different valid approaches for this use case, but I think an easy one would be to use a BatchSampler and pass the indices for the complete batch to the Dataset.__getitem__.
This would allow you so sample the new crop size for a complete batch as well as load and process all images from this batch inside the __getitem__ in a loop.
Have a look at this post for more information and an example.