Unpooling上池化/上采样(nn.MaxUnpool2d) (Nearest Neighbor Unpooling & Bed of Nails Unpooling)(upsampling)

Unpooling作为一种上采样(upsampling)的方法,与pooling看起来像是相反的操作,我们有三种方法,第一种是Nearest Neighbor,就是把相同的数据复制4个达到扩大四倍的效果,第二种是”Bed of Nails”,把数据防止在对应位置的左上角,然后其余的地方补0,如下图所示。

 第三种方法是Max Unpooling,对于一部分网络模型来讲,上采样和下采样的结构往往是对称的,我们可以在下采样做Max Pooling的时候记录下来最大值所在的位置,当做上采样的时候把最大值还原到其对应的位置,然后其余的位置补0,如下图所示。

 这样可以最大限度的还原信息。

import torch
from torch import nn

pool = nn.MaxPool2d(2, stride=2, return_indices=True)
unpool = nn.MaxUnpool2d(2, stride=2)
input = torch.tensor([[[[ 1.,  2,  3,  4],
                        [ 5,  6,  7,  8],
                        [ 9, 10, 11, 12],
                        [13, 14, 15, 16]]]])

output, indices = pool(input)
result = unpool(output, indices)
print(result)

#也可以unpool的结果的size和input不同
result = unpool(output, indices, output_size=torch.Size([1, 1, 5, 5]))
print(result)

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值