Ceil Mode in pooling

For the following parameter setting:
input size 112x112
kernel 3x3
stride 2
padding = 0
Ceil Mode =True
The output size is 56x56.

If the ceil mode is true, will there be any one-sided padding for input image at right and bottom before pooling?.
With 108th column of input, outsize will be 55, in order to get 56 output size we will move to 110th column and since filter size is 3 we need an extra column. So there should be single-column padding, right?

Hi,

Usually the output size is calculated as follow.

O = floor((W-K+2P)/S) + 1

If the ceil mode is on, ceil used instead of floor operation in the output. That is what said in the documentation. You can refer here.
https://pytorch.org/docs/stable/_modules/torch/nn/modules/pooling.html

In the case of ceil mode, additional columns and rows are added at the right as well as at the down. (Not top and not left). It does not need to be one extra column. It depends on the stride value as well. I just wrote small code snippet where you can check how the populated values are pooled in either modes.

import torch.nn as nn
import torch

test = torch.randn(2,3,32,32)
x = torch.tensor([[-2, 1, 2, 6, 4], [-3, 1, 7, 2, -2], [-4, 2, 3, -1 , -3], [-7, 1, 2, 3, 11], [5, -7, 8, 12, -9]]).float()
x = x.unsqueeze(0)
y_1 = nn.MaxPool2d(kernel_size=2,stride=2, padding=0)
y_2 = nn.MaxPool2d(kernel_size=2,stride=2, padding=0, ceil_mode=True)
print(y_1(x))
print(y_2(x))

Thanks

1 Like

Thanks,clear now :blush:

In this doc
MaxPool2d — PyTorch 2.1 documentation[torch nn MaxPool2D], why the output size is calculated differently
image