github地址:https://github.com/Lextal/pspnet-pytorch/blob/master/pspnet.py
PSP模块示意图如下
代码如下
class PSPModule(nn.Module):
def __init__(self, features, out_features=1024, sizes=(1, 2, 3, 6)):
super().__init__()
self.stages = []
self.stages = nn.ModuleList([self._make_stage(features, size) for size in sizes])
self.bottleneck = nn.Conv2d(features * (len(sizes) + 1), out_features, kernel_size=1)
self.relu = nn.ReLU() def _make_stage(self, features, size):
prior = nn.AdaptiveAvgPool2d(output_size=(size, size))
conv = nn.Conv2d(features, features, kernel_size=1, bias=False)
return nn.Sequential(prior, conv) def forward(self, feats):
h, w = feats.size(2), feats.size(3)
priors = [F.upsample(input=stage(feats), size=(h, w), mode='bilinear') for stage in self.stages] + [feats]
bottle = self.bottleneck(torch.cat(priors, 1))
return self.relu(bottle)
此外,我基于自己的工作稍加修改,也给出一个3D版本。改动有几处,一是3d卷积和池化,二是上采样由双线性插值切换为trilinear,不知是否翻译为三线性插值,三是我对池化部分输出尺寸的修改,上采样到输入的一半,同时与普通池化相结合,不过,这样有没有效果,我还没试过
class PSPModule(nn.Module):
def __init__(self, features, sizes=(1, 2, 3, 6)):
super(PSPModule, self).__init__()
self.stages = []
self.stages = nn.ModuleList([self._make_stage(features, size) for size in sizes])
self.bottleneck = nn.Conv3d(features * (1 + 1), features, kernel_size=1)
self.relu = nn.ReLU() def _make_stage(self, features, size):
prior = nn.AdaptiveAvgPool3d(output_size=(size, size, size))
conv = nn.Conv3d(features, features / 4, kernel_size=1, bias=False)
return nn.Sequential(prior, conv) def forward(self, x, maxpool_x):
h, w, l = x.size(2), x.size(3), x.size(4)
priors = [F.upsample(input=stage(x), size=(h / 2, w / 2, l/2), mode='trilinear') for stage in self.stages] + [maxpool_x]
bottle = self.bottleneck(torch.cat(priors, 1))
print(bottle.size())
return self.relu(bottle)