# Dynamic padding based on input shape

Hi,
For my model my input (image) needs to be divisible by 32 and I would like to pad my input dynamically to fit this requirement. Meaning if the input would be for example 520x520x3, I want it to be padded to 544x544x3.

torch.nn.functional.pad() requires the pad to be list of ints, but my paddings would be a tensor computed dynamically depending on the shape of the input.
The model (including the padding) is supposed to be exported to onnx so it has to be traceable.

Does anybody have a clue on how to approach this problem?
Andreas

Hey, the reply is a little late, but I was struggling with the same problem. So I hope this will help somebody in the future. I solved my problem according to this example:

``````from torchvision import transforms
from functools import partial
from math import ceil

p = "path/to/img.png"

print("Shape:", i.size)
image_size = 544

h, w = image.size
h_diff = h - min_size
w_diff = w - min_size
h_pad = ceil(abs(h_diff) / 2) if h_diff < 0 else 0
w_pad = ceil(abs(w_diff) / 2) if w_diff < 0 else 0

return image
else:

transform = transforms.Compose([
transforms.RandomCrop(image_size)
])

i_transformed = transform(i)
print("Shape after transform:", i_transformed.size)
``````

Output:

``````Shape: (512, 383)
Shape after transform: (544, 544)
``````

This should dynamically pad any image to the given image_size. Same code could be used for dynamic scaling as well.

Cheers!

1 Like