文章地址SPNet.源码Code,讲解这篇文章的人太多了,这边就不详细讲了.主要讲讲里面很有趣的一个点子,条状池化.将它与Resnet结合,相比之前的aspp模块,精度上要有所提升!
图中展示的是作者文章中的一个改进点.主要是对输入的特征向量分别进行H * 1和1 * W的池化.看文章和作者的源码时发现,作者将strip pooling和ASPP结合了.一开始就觉得这样的结构模型过于复杂了,结果实际复现跑分割的代码,发现效果的确没有ASPP效果好!
因此单独将strip pooling模型拆开,将其与ResNet结合,应用到UNet分割模型中.发现效果挺好.因为用的分割数据集是自己在医院收集的,所以我猜测和数据集本身有关系,没时间在公共数据集上测试.就默认我的想法是对的吧.
代码如下(示例):
class StripPooling(nn.Module):
def __init__(self, in_channels, up_kwargs={'mode': 'bilinear', 'align_corners': True}):
super(StripPooling, self).__init__()
self.pool1 = nn.AdaptiveAvgPool2d((1, None))#1*W
self.pool2 = nn.AdaptiveAvgPool2d((None, 1))#H*1
inter_channels = int(in_channels / 4)
self.conv1 = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 1, bias=False),
nn.BatchNorm2d(inter_channels),
nn.ReLU(True))
self.conv2 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, (1, 3), 1, (0, 1), bias=False),
nn.BatchNorm2d(inter_channels))
self.conv3 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, (3, 1), 1, (1, 0), bias=False),
nn.BatchNorm2d(inter_channels))
self.conv4 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(inter_channels),
nn.ReLU(True))
self.conv5 = nn.Sequential(nn.Conv2d(inter_channels, in_channels, 1, bias=False),
nn.BatchNorm2d(in_channels))
self._up_kwargs = up_kwargs
def forward(self, x):
_, _, h, w = x.size()
x1 = self.conv1(x)
x2 = F.interpolate(self.conv2(self.pool1(x1)), (h, w), **self._up_kwargs)#结构图的1*W的部分
x3 = F.interpolate(self.conv3(self.pool2(x1)), (h, w), **self._up_kwargs)#结构图的H*1的部分
x4 = self.conv4(F.relu_(x2 + x3))#结合1*W和H*1的特征
out = self.conv5(x4)
return F.relu_(x + out)#将输出的特征与原始输入特征结合
在残差块定义好StripPooling模块,代码如下(示例):
self.head = StripPooling(self.hidden_channels, up_kwargs={'mode': 'bilinear', 'align_corners': True})
在每个残差块后加入StripPooling模块即可!
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.head(x)
x = self.layer2(x)
x = self.head(x)
x = self.layer3(x)
x = self.head(x)
x = self.layer4(x)
x = self.head(x)
在作者源码的基础上改了一点.可能有一点点绕,代码结合结构图一起看,才能更明白一些.有任何问题欢迎私聊我!