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)
Unpooling上池化/上采样(nn.MaxUnpool2d) (Nearest Neighbor Unpooling & Bed of Nails Unpooling)(upsampling)
于 2022-02-13 17:22:22 首次发布