ConstantPad2d but for an inner border?

Hi, is there something similar to ConstantPad2d but for the inner border, in a way that the image is still bordered with a constant value but its shape stays the same?

I’m asking because I have a batch of n images and I want to color 2 of them in white and the rest in black. So I used ConstantPad2d to have a black outer border for all images. Now I want to select the two images that I’ll replace their border color in white, I found a solution by looping over indexes or using numpy\matlab slicing convention, but I was wondering if there’s a more “torch” way to do it.

How about doing crop + pad?

What do you mean by crop? I’m using ConstantPad2d to pad my tensors, and CenterCrop is for PIL images, not tensors

I meant just doing slicing yourself, e.g., F.pad(x[:, :, 2:-2, 2:-2], 2, 'constant', -1)

1 Like