How to create a dataloader with variable-size input

I do not think that is possible, for a batch of images which have variable size.The output size will only be same after the AdaptiveAvgPool2d layer. You can not stack image before that layer.

Also, if you want to stack image after AdaptiveAvgPool2d, you may have to separate your network into two part, which also makes the network more complex.

I suggest that you calculate the loss for each image and accumulate the loss, after you do N forward, do one backward to update the network parameter. You can see some discussions here and here.

Another way is to just pad your images to have the same size and keep the image aspect ratio. I think it is also a viable solution if you do not want to change the image aspect ratio.

9 Likes