Is there a built in way to ensure image size match per batch?

I have a torch.utills.data.Dataset that returns images which slightly vary in size.

I produce my training batches using torch.utils.data.DataLoader. Is there an inbuilt way to resize the images such that they can be stacked into a batch on the data loader end?

If there’s not, I’ll probably write a collate_fn to do it. Or is there a better way?

PS: I don’t want to resize my whole dataset to one size. That’s why I’m specifically asking about doing it at a batch level.

If you want to resize the data in the batch only, one approach would be to apply the Resize operation in the collate_fn or return a list in a custom collate_fn and resize the data in the training loop.

Alternatively, you could also use a BatchSampler, which will pass a batch of indices to Dataset.__getitem__ and resize all images inside the Dataset.

2 Likes