百度PaddlePaddle图像分割7日训练营之DeepLab V3
·
百度PaddlePaddle图像分割7日训练营之DeepLab V3
百度PaddlePaddle图像分割7日训练营之DeepLab V3
课程链接:https://aistudio.baidu.com/aistudio/course/introduce/1767
一、引言
DeepLab语义分割系列网络的最新作是DeepLabv3+ ,其前作有 DeepLabv1,DeepLabv2, DeepLabv3, 在最新作中,DeepLab的作者通过encoder-decoder进行多尺度信息的融合,同时保留了原来的空洞卷积和ASSP层, 其骨干网络使用了Xception模型,提高了语义分割的健壮性和运行速率,在 PASCAL VOC 2012 dataset取得新的state-of-art performance,89.0mIOU。

DeepLab V3+ 论文链接 :Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation
DeepLab V3 论文链接 :Rethinking Atrous Convolution for Semantic Image Segmentation
二、DeepLab V3 网络结构

图中DeepLab V3使用的是ResNet的backbone,百度朱老师建议换成使用MobileNet作为骨干网络比较好。
三、实现代码
1.ASPPPooling
class ASPPPooling(Layer):
def __init__(self, num_channels, num_filters):
super(ASPPPooling, self).__init__()
self.features = fluid.dygraph.Sequential(
Conv2D(num_channels, num_filters, 1),
BatchNorm(num_filters, act='relu')
)
def forward(self, inputs):
n, c, h, w = inputs.shape
x = fluid.layers.adaptive_pool2d(inputs,1)
x = self.features(x)
x = fluid.layers.interpolate(x, (h, w), align_corners=False)
return x
2.ASPPModule
class ASPPModule(Layer):
def __init__(self, num_channels, num_filters, rates):
super(ASPPModule, self).__init__()
self.features = []
self.features.append(
fluid.dygraph.Sequential(
Conv2D(num_channels, num_filters, 1),
BatchNorm(num_filters, act='relu')
)
)
self.features.append(ASPPPooling(num_channels, num_filters))
for r in rates:
self.features.append(
ASPPConv(num_channels, num_filters, r)
)
self.project = fluid.dygraph.Sequential(
Conv2D(num_filters*(2 + len(rates) ), num_filters, 1),
BatchNorm(num_filters, act='relu'),
)
def forward(self, inputs):
res = []
for op in self.features:
res.append(op(inputs))
x = fluid.layers.concat(res,axis=1)
x = self.project(x)
return x
3.DeepLab
class DeepLab(Layer):
def __init__(self, num_classes=59):
super(DeepLab, self).__init__()
back = ResNet50(pretrained=False, duplicate_blocks=True)
self.layer0 = fluid.dygraph.Sequential(
back.conv,
back.pool2d_max,
)
self.layer1 = back.layer1
self.layer2 = back.layer2
self.layer3 = back.layer3
# multigrid
self.layer4 = back.layer4
self.layer5 = back.layer5
self.layer6 = back.layer6
self.layer7 = back.layer7
feature_dim = 2048
self.classifier = DeepLabHead(feature_dim, num_classes)
def forward(self, inputs):
n, c, h, w = inputs.shape
x = self.layer0(inputs)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.layer6(x)
x = self.layer7(x)
x = self.classifier(x) # ASPP & classifier
x = fluid.layers.interpolate(x, (h, w), align_corners=False) # resize
return x
4.DeepLabHead
class DeepLabHead(fluid.dygraph.Sequential):
def __init__(self, num_channels, num_classes):
super(DeepLabHead, self).__init__(
ASPPModule(num_channels, 256, [12, 24, 36]),
Conv2D(256, 256, 3, padding=1),
BatchNorm(256, act='relu'),
Conv2D(256, num_classes, 1)
)
5.ASPPConv
class ASPPConv(fluid.dygraph.Sequential):
def __init__(self, num_channels, num_filters, dilation):
super(ASPPConv, self).__init__(
Conv2D(num_channels, num_filters, filter_size=3, padding=dilation, dilation=dilation),
BatchNorm(num_filters, act='relu')
)
6.main
def main():
place = paddle.fluid.CUDAPlace(0)
with fluid.dygraph.guard(place):
x_data = np.random.rand(2, 3, 512, 512).astype(np.float32)
x = to_variable(x_data)
model = DeepLab(num_classes=59)
model.eval()
pred = model(x)
print(pred.shape)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)