pytorch里面的maxpool,有一个属性叫ceil_mode,这个属性在api里面的解释是
ceil_mode: when True, will use ceil instead of floor to compute the output shape
也就是说,在计算输出的shape的时候,如果ceil_mode的值为True,那么则用天花板模式,否则用地板模式。
pytorch中的默认模式为floor模式
???
举两个例子就明白了。
# coding:utf-8
import torch
import torch.nn as nn
from torch.autograd import Variable
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.maxp = nn.MaxPool2d(kernel_size=2, ceil_mode=False)
def forward(self, x):
x = self.maxp(x)
return x
square_size = 6
inputs = torch.randn(1, 1, square_size, square_size)
for i in range(square_size):
inputs[0][0][i] = i * torch.ones(square_size)
inputs = Variable(inputs)
print(inputs)
net = Net()
outputs = net(inputs)
print(outputs.size())
print(outputs)
在上面的代码中,无论ceil_mode是True or False,结果都是一样
但是如果设置square_size=5,那么
当ceil_mode=True
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
[torch.FloatTensor of size 1x1x5x5]
torch.Size([1, 1, 3, 3])
Variable containing:
(0 ,0 ,.,.) =
1 1 1
3 3 3
4 4 4
[torch.FloatTensor of size 1x1x3x3]
当ceil_mode=False
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
[torch.FloatTensor of size 1x1x5x5]
torch.Size([1, 1, 2, 2])
Variable containing:
(0 ,0 ,.,.) =
1 1
3 3
[torch.FloatTensor of size 1x1x2x2]
所以ceil模式就是会把不足square_size的边给保留下来,单独另算,或者也可以理解为在原来的数据上补充了值为-NAN的边。而floor模式则是直接把不足square_size的边给舍弃了。
原文链接:https://blog.csdn.net/GZHermit/article/details/79351803
caffe中的ceil模式也是可以选择的,但是默认为ceil模式
先说两种方式的源码实现:
位于caffe的src/layers/pooling_layer.cpp中:
switch (round_mode_) {
case PoolingParameter_RoundMode_CEIL:
pooled_height_ = static_cast(ceil(static_cast(
height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
pooled_width_ = static_cast(ceil(static_cast(
width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
break;
case PoolingParameter_RoundMode_FLOOR:
pooled_height_ = static_cast(floor(static_cast(
height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
pooled_width_ = static_cast(floor(static_cast(
width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
break;
可见caffe也是ceil和floor都有的,暂时未查到pytorch中nn.max_pool2d的源码,查资料发现pytorch和caffe在计算边长的实现上应该是一样的(此处待考证)
然后如何确定默认的模式是哪种呢?
顺着round_mode_参数找到了它的调用:
round_mode_ = pool_param.round_mode();
发现它的参数名字叫做round_mode,据此在caffe.proto里找这个注册后的参数:
optional RoundMode round_mode = 13 [default = CEIL];
发现默认是ceil的
当然若caffe版本不同也可能没有可选的参数,最原始的貌似是只有ceil的,这就造成了在pytorch转caffe模型时,这一层会出现caffe的结果比pytorch大的问题,解决的方案是,在caffe中手动添加slice层,把多出来的最后一行一列删掉即可,maxpool没有权重,在我自己的实验中,删去几乎不会影响模型结果。