一、FCN网络结构
全卷积网络(Fully Convolutional Networks),是较早用于图像语义分割的神经网络。根据名称可知,FCN主要网络结构全部由卷积层组成,在图像领域,卷积是一种非常好的特征提取方式。本质上,图像分割是一个分类任务,需要做的就是对图像上每一个像素按照人工标注进行分类。
FCN大致网络结构如下:

上图模型结构为针对VOC数据集的21个语义分割,即数据集包含21种不同分割类型。当图像进入神经网络,第一个卷积层将图像由三通道转换为96通道featuremap,第二个卷积层转换为256个通道,第三个卷积层384个通道,直到最后一个卷积层变为21个通道,每个通道对应不同分割类型。实际上,卷积层整个网络结构中卷积层的通道数可以根据不同任务进行调整,前面每经过一层会对图像进行一次宽高减半的下采样,经过5个卷积层以后,featuremap为输入的1/32,最后通过反卷积层将featuremap宽高恢复到输入图像大小。
二、FCN模型结构实现
FCN模型结构可以根据分割细粒度使用FCN32s、FCN16s、FCN8s等结构,32s即从32倍下采样的特征图恢复至输入大小,16s和8s则是从16倍和8倍下采样恢复至输入大小,当然还可以使用4s、2s结构,数字越小使用的反卷积层进行上采样越多,对应模型结构更加复杂,理论上分割的效果更精细。这里采用深度学习框架MindSpore来搭建模型结构。
FCN32s模型结构示意图:

模型构建脚本:
class FCN32s(nn.Cell):
def __init__(self, n_class=21):
super(FCN32s, self).__init__()
self.block1 = nn.SequentialCell(
nn.Conv2d(3, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.block2 = nn.SequentialCell(
nn.Conv2d(64, 128, 3),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, 3),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.block3 = nn.SequentialCell(
nn.Conv2d(128, 256, 3),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, 3),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 256, 3),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.block4 = nn.SequentialCell(
nn.Conv2d(256, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.block5 = nn.SequentialCell(
nn.Conv2d(512, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(512, 512, 3),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.block6 = nn.SequentialCell(
nn.Conv2d(512, 4096, 7),
nn.BatchNorm2d(4096),
nn.ReLU()
)
self.block7 = nn.SequentialCell(
nn.Conv2d(4096, 4096, 1),
nn.BatchNorm2d(4096),
nn.ReLU()
)
self.upscore = nn.SequentialCell(
nn.Conv2d(4096, n_class, 1),
nn.Conv2dTranspose(n_class, n_class, 4, 2, has_bias=False),

最低0.47元/天 解锁文章
847





