pytorch---之pytorch与tensorflow的pad不同

pytorch和tensorflow所含的maxpool,虽然名字相同,但是功能是不一样。之前在用pytorch复现darknet里面的yolo-v2时才发现这个问题。在yolov2的第六个maxpool的时候,kernel为2,stride为1,所以
按道理来说呢,输出的边size应该比输入的边size少1,但是yolo的设计是输入和输出的shape要相同。所以才发现了这个问题。

不同之处在于padding的补全方式

pytorch里的maxpool,padding的方式是四周都补0,如果padding等于x,那么输入的feature map就会比原来大上x圈。并且还支持dilation模式Σ( ° △ °|||)︴

而tensorflow里的maxpool,padding方式是分为SAMEVALID,在SAME模式下,padding有时候只会在图像右侧和下方补0,而左侧和上方并不会。不支持dilation。

举个例子
如果输入一个3X3的图片,
1,2,3
4,5,6
7,8,9
对其做kernel_size=2,stride=1的maxpool,那么本应该得到
5,6
8,9

# pytorch版本
import torch
import torch.nn.functional as F
from torch.autograd import Variable

data = torch.FloatTensor([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]])
i = Variable(data, requires_grad=True)
res = F.max_pool2d(i, kernel_size=(2, 2), stride=1)
print(res)
  •  
# tensorflow版本
import tensorflow as tf

data = np.array([[[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]]])
i = tf.placeholder(dtype=tf.int32, shape=(1, 3, 3, 1))

net = tf.nn.max_pool(i, ksize=(1, 2, 2, 1), strides=(1, 1, 1, 1), padding='VALID')

sess = tf.Session()
sess.run(tf.global_variables_initializer())

out = sess.run([net, ], {i: data})
print(out)
  •  

但是如果我们想要保证feature map在输入前后尺寸不变,tensorflow很好实现,把padding模式改成’SAME’就行,但是pytorch就做不到了(有办法么?)因为pytorch里面如果padding=1,那么输出的feature map的尺寸就是4X4的。

Variable containing:
(0 ,0 ,.,.) =
1 2 3 3
4 5 6 6
7 8 9 9
7 8 9 9
[torch.FloatTensor of size 1x1x4x4]

而tensorflow的输出是

[[[[5 6 6]
[8 9 9]
[8 9 9]]]]

另附上pytorch的maxpool计算公式
这里写图片描述

pytorch实现单边加0的padding方式

pytorch里有pad()方法,可以控制在feature map的边界加上0。

import torch
import torch.nn.functional as F
from torch.autograd import Variable

#
data = torch.FloatTensor([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]])
i = Variable(data, requires_grad=True)
pd = (0, 1, 0, 1)
out = F.pad(i, pd, 'constant', 0)
print(out)
res = F.max_pool2d(out, kernel_size=(2, 2), stride=1, padding=0)
print(res)
  •  

输出:

Variable containing:
(0 ,0 ,.,.) =
1 2 3 0
4 5 6 0
7 8 9 0
0 0 0 0
[torch.FloatTensor of size 1x1x4x4]

Variable containing:
(0 ,0 ,.,.) =
5 6 6
8 9 9
8 9 9
[torch.FloatTensor of size 1x1x3x3]

--------------------- 本文来自 GZHermit 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/gzhermit/article/details/79236056?utm_source=copy

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值