Using variable sized input - Is padding required?

This does not work with the default dataloader but in general you could handle the loading by yourself and simply add a batch dimension to your data and use torch.cat to stack them to a batch:

batch_elements = []

for i in range(curr_batch_size):
    # generate some sample data
    tmp = torch.rand((1, 50, 50))
    # add batch dimension
    tmp = tmp.unsqueeze(0)
    batch_elements.append(tmp)
batch = torch.cat(batch_elements, 0)

Replace tmp = torch.rand((1, 50, 50)) by your own data samples. In This case I used 50x50 pixel images with one channel as sample data. To show the workflow with general data I did not integrate the batch dimension into the shape of the random tensor but added it afterwards.

EDIT: Alternatively you could use something like this. But note that this will pad your input and (depending on the maximal difference of your input sizes) you could end up with padding an enormous amount of zeros (or constants or whatever).

1 Like