Batching Inferencing on Images of varying size

This code is working

for k, image_path in enumerate(image_list):
    image = imgproc.loadImage(image_path)
    print(image.shape)
    with torch.no_grad():
        y, feature = net(x)        
    result = image.cuda()

However, when I try to use data loader for the same having batch size more than 1, the code is failing as the size of images are different. I know I can resize the image but not doing it as it might change the aspect ratio of the image and might impact my results.

What alternative solution am i having?

Can we use any bucketing technique here?

Hi @abhiksark,

You are almost there!

According to my pass projects, I still haven’t been able to train batch cnn with different image size.
What I did is to suggest you to do the following:

Step 1. Resize the image into at least one same dimension (aka, you could try to make the width/height into the same size like width=800, while leaving the other w.r.t their resized-aspect ratio size).
Step 2. Pick the highest aspect ratio that might be possible for your case (in my case it is w:h -> 16:9). Hence, w:h -> 800:450).
Step 3. While doing step. 1, you will find that there is a little bit of missing pixels to match them into your highest aspect ratio. Then, we will fill it with zero padding for the rest.

Note:

  • Some hack for step 3, I will create a new zero image with w:h according to your aspect ratio, which in this case is 800:450, fill the 0:800 width and 0:X height with your image, then leave the rest as zero.
  • Don’t forget to adjust your mask or annotations w.r.t. your changes.

Hope it helps, cheers~

1 Like