当前位置:   article > 正文

百度深度学习框架Paddleseg图像分割出入门_百度 深度学习分割

百度 深度学习分割

百度深度学习框架Paddleseg图像分割初入门

百度深度学习框架Paddleseg分割初入门

最近跟随百度的老师,完成了7天的语义分割课程,终于手把手带我搭建了框架,欢饮雀跃哈哈
想参与的详见课程链接百度7天分割课程

这是我的UNET实现作业

作业内容

  1. pspnet.py
import numpy as np
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph import Layer
from paddle.fluid.dygraph import Conv2D
from paddle.fluid.dygraph import BatchNorm
from paddle.fluid.dygraph import Dropout
from resnet_dilated import ResNet50

# pool with different bin_size
# interpolate back to input size
# concat
class PSPModule(Layer):
    def __init__(self,num_channels,bin_size_list):
        super(PSPModule,self).__init__()
        self.bin_size_list = bin_size_list
        num_filters = num_channels //len(bin_size_list)
        self.features = []
        #融合部分
        for i in range(len(bin_size_list)):
            self.features.append(
                fluid.dygraph.Sequential(
                    Conv2D(num_channels,num_filters,1),
                    BatchNorm(num_filters,act='relu')
                )
            )
    
    def forward(self,inputs):
        out = [inputs]
        for idx,f in enumerate(self.features):
            x = fluid.layers.adaptive_pool2d(inputs,self.bin_size_list[idx])
            x = f(x)
            x = fluid.layers.interpolate(x,inputs.shape[2::],align_corners=True)
            out.append(x)
        #out is list
        out = fluid.layers.concat(out,axis=1) #NCHW

        return out



class PSPNet(Layer):
    def __init__(self, num_classes=59, backbone='resnet50'):
        super(PSPNet, self).__init__()
        res = ResNet50(False)
        # stem: res.conv, res.pool2d_max
        self.layer0 = fluid.dygraph.Sequential(
                        res.conv,
                        res.pool2d_max
        )
        self.layer1 =res.layer1
        self.layer2 =res.layer2
        self.layer3 =res.layer3
        self.layer4 =res.layer4

        # psp: 2048 -> 2048*2
        num_channels = 2048
        self.pspmodule = PSPModule(num_channels,[1,2,3,6])
        num_channels *= 2
        # cls: 2048*2 -> 512 -> num_classes
        self.classifier = fluid.dygraph.Sequential(
            Conv2D(num_channels=num_channels,num_filters=512,filter_size=3,padding=1),
            BatchNorm(512,'relu'),
            Dropout(0.1),
            Conv2D(num_channels= 512,num_filters=num_classes,filter_size=1),

        )
        # aux: 1024 -> 256 -> num_classes      
    def forward(self, inputs):
        x = self.layer0(inputs)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.pspmodule(x)
        x = self.classifier(x)
        x = fluid.layers.interpolate(x,inputs.shape[2::],align_corners=True)
        return x
        # aux: tmp_x = layer3
            



def main():
    with fluid.dygraph.guard(fluid.CPUPlace()):
        x_data=np.random.rand(2,3, 473, 473).astype(np.float32)
        x = to_variable(x_data)
        model = PSPNet(num_classes=59)
        model.train()
        pred, aux = model(x)
        print(pred.shape, aux.shape)

if __name__ =="__main__":
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  1. transform.py
    完成模型预测代码。
import cv2
import numpy as np
import random
import numbers
import collections
import sys
from basic_dataloader import BasicDataLoader
import os


if sys.version_info < (3, 3):
    Sequence = collections.Sequence
    Iterable = collections.Iterable
else:
    Sequence = collections.abc.Sequence
    Iterable = collections.abc.Iterable


class Compose(object):
    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, image, label=None):
        for t in self.transforms:
            image, label = t(image, label)
        return image, label


class Normalize(object):
    def __init__(self, mean_val, std_val, val_scale=1):
        # set val_scale = 1 if mean and std are in range (0,1)
        # set val_scale to other value, if mean and std are in range (0,255)
        self.mean = np.array(mean_val, dtype=np.float32)
        self.std = np.array(std_val, dtype=np.float32)
        self.val_scale = 1 / 255.0 if val_scale == 1 else 1

    def __call__(self, image, label=None):
        image = image.astype(np.float32)
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/510319
推荐阅读
相关标签
  

闽ICP备14008679号