How to pad with a (CNN) custom value

Hi everyone,

This could be a silly question. I want to know how to pad my 3dCNN with a given (custom) number, not just zeros.

Also, if someone can refer me to a resource which gives a comprehensive explanation of padding modes would be very helpful.

padding_mode (string , optional ) – 'zeros' , 'reflect' , 'replicate' or 'circular'

To pad a given with a custom number, you will have to use the functionality torch.nn.functional.pad() mentioned in the docs here.
A simple implementation can be:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # pads with custom float value and then performs 3d convolution
        # no padding at convolution step
        self.conv1 = nn.Conv3d(3, 16, 3, stride=2, padding=0)
        self.p3d = (1, 1, 1, 1, 1, 1) # pad by (1, 1), (1, 1), and (1, 1)
    def forward(self, x):
        #p3d is dimensions to pad, constant means constant value padding, and 0 is the number you want to pad with
        x = torch.nn.functional.pad(x, self.p3d, "constant", 0)
        x = self.conv1(x)
        return x

This way you can pad with any custom number you want.

1 Like

Thanks a lot :slight_smile:

It is interesting how things are done in PyTorch