一、BlurPool
论文地址:https://arxiv.org/abs/1904.11486
论文:BlurPool:Making Convolutional Networks Shift-Invariant Again
论文中给出的原因,是因为stride=2的时候卷积和pool等下采样操作时,违反了采样定理,会导致信号走样,其实这个问题在很久之前就有所说明,就比如我们在构建高斯、拉布普斯金字塔的时候,下采样之前都需要先使用高斯模糊对图像进行处理,就是为了防止下采样出现走样的情况,也就是anti-aliasing by low-pass filtering before downsampling.
传统的max pool可以分解为两部分,stride = 1的max + subsample(下采样) 。第一部分stride=1的max具有平移不变性,引起混叠的过程是subsample
作者提出的MaxBlurPool = max + blur + subsample
max 和 blur操作都是平移不变的但是,低通滤波blur的加入并不能完全消除混叠,只是减小了混叠。其使用方法下:
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
class BlurPool(nn.Module):
def __init__(self, channels, pad_type='reflect', filt_size=4, stride=2, pad_off=0):
super(BlurPool, self).__init__()
self.filt_size = filt_size
self.pad_off = pad_off
self.pad_sizes = [int(1. * (filt_size - 1) / 2), int(np.ceil(1. * (filt_size - 1) / 2)),
int(1. * (filt_size - 1) / 2), int(np.ceil(1. * (filt_size - 1) / 2))]
self.pad_sizes = [pad_size + pad_off for pad_size in self.pad_sizes]
self.stride = stride
self.off = int((self.stride - 1) / 2.)
self.channels = channels
if self.filt_size == 1:
a = np.array([1., ])
elif self.filt_size == 2:
a = np.array([1., 1.])
elif self.filt_size == 3:
a = np.array([1., 2., 1.])
elif self.filt_size == 4:
a = np.array([1., 3., 3., 1.])
elif self.filt_size == 5:
a = np.array([1., 4., 6., 4., 1.])
elif self.filt_size == 6:
a = np.array([1., 5., 10., 10., 5., 1.])
elif self.filt_size == 7:
a = np.array([1., 6., 15., 20., 15., 6., 1.])
filt = torch.Tensor(a[:, None] * a[None, :])
filt = filt / torch.sum(filt)
self.register_buffer('filt', filt[None, None, :, :].repeat((self.channels, 1, 1, 1)))
self.pad = get_pad_layer(pad_type)(self.pad_sizes)
def forward(self, inp):
if self.filt_size == 1:
if self.pad_off == 0:
return inp[:, :, ::self.stride, ::self.stride]
else:
return self.pad(inp)[:, :, ::self.stride, ::self.stride]
else:
return F.conv2d(self.pad(inp), self.filt, stride=self.stride, groups=inp.shape[1])
def get_pad_layer(pad_type):
if pad_type in ['refl', 'reflect']:
PadLayer = nn.ReflectionPad2d
elif pad_type in ['repl', 'replicate']:
PadLayer = nn.ReplicationPad2d
elif pad_type == 'zero':
PadLayer = nn.ZeroPad2d
else:
print('Pad type [%s] not recognized' % pad_type)
return PadLayer