使用飞桨实现图像分割模型U-net的心得

使用飞桨实现图像分割模型U-net的心得
简介

最近参加了百度的图像分割打卡营,学习了如何使用飞桨实现图像分割模型U-net,收获很大。
U-net

U-net是U-net语义分割系列网络的最新作,其前作有U-net。该系列模型的比较见下图:
使用飞桨实现图像分割模型U-net的心得_第1张图片

U-net Series 关键结构对比
模型结构图示比较如下所示:
使用飞桨实现图像分割模型U-net的心得_第2张图片

模型结构图示比较
代码实现
class UNet(Layer):
def init(self, num_classes=59):
super(UNet, self).init()
# encoder: 3->64->128->256->512
# mid: 512->1024->1024

    #TODO: 4 encoders, 4 decoders, and mid layers contains 2 1x1conv+bn+relu
    self.down1 = Encoder(num_channels=3, num_filters=64)
    self.down2 = Encoder(num_channels=64, num_filters=128)
    self.down3 = Encoder(num_channels=128, num_filters=256)
    self.down4 = Encoder(num_channels=256, num_filters=512)

    self.mid_conv1 = Conv2D(512, 1024, filter_size=1, padding=0, stride=1)
    self.mid_bn1 = BatchNorm(1024, act='relu')
    self.mid_conv2 = Conv2D(1024, 1024, filter_size=1, padding=0, stride=1)
    self.mid_bn2 = BatchNorm(1024, act='relu')

    self.up4 = Decoder(1024, 512)
    self.up3 = Decoder(512, 256)
    self.up2 = Decoder(256, 128)
    self.up1 = Decoder(128, 64)

    self.last_conv = Conv2D(num_channels=64, num_filters=num_classes, filter_size=1)


def forward(self, inputs):
    x1, x = self.down1(inputs)
    print(x1.shape, x.shape)
    x2, x = self.down2(x)
    print(x2.shape, x.shape)
    x3, x = self.down3(x)
    print(x3.shape, x.shape)
    x4, x = self.down4(x)
    print(x4.shape, x.shape)

    # middle layers
    x = self.mid_conv1(x)
    x = self.mid_bn1(x)
    x = self.mid_conv2(x)
    x = self.mid_bn2(x)

    print(x4.shape, x.shape)
    x = self.up4(x4, x)
    print(x3.shape, x.shape)
    x = self.up3(x3, x)
    print(x2.shape, x.shape)
    x = self.up2(x2, x)
    print(x1.shape, x.shape)
    x = self.up1(x1, x)
    print(x.shape)

    x = self.last_conv(x)

    return x

代码实现比较简单,得益于PaddlePaddle的简单易用。部分代码如下所示:

部分代码
总结

U-net作为图像语义分割的一种经典模型具有结构清晰、效果好的优点。在飞桨中实现起来较容易,效果较好。百度图像分割七日打卡营收获很大,推荐大家学习。
参考文献:

飞桨官网

你可能感兴趣的:(使用飞桨实现图像分割模型U-net的心得)