基于百度飞浆PaddlePaddle的图像分割学习心得

基于百度飞浆PaddlePaddle的图像分割学习心得

学习的步骤:

       a.百度AI Studio平台上notebook的使用规则;
       b.PaddlePaddle的建构神经网络模型的基本流程与所需调用的基本函数,并进行简单的模型的构建与训练;
       c.学习相关课程,图像分割。百度高级工程师手敲代码,帮助理解。附课程链接:https://aistudio.baidu.com/aistudio/education/group/info/1767
       d.实践调整代码,利用API实现自己的完整代码。前期基本包括,数据导入(basic_dataload)、数据预处理(basic_transform)以及模型构建(basic_modle)。最后,进行网络的完整构建,包括有VGG、U-Net、PSPNet以及DeepLab等的构建。

使用notebook的一些体验:

      在使用notebook时,是以cell为基本执行单位,一个cell就是一个执行主体。cell有两种模式,一种为代码运行模式(Code模式)、一种为文本类模式(Markdown模式)。如下图所示,两种cell自身直接也可以切换,但注意,执行代码时需要采用Code模式。
      ![Code模式](https://img-blog.csdnimg.cn/20201025123633186.png#pic_center)
![Markdown模式](https://img-blog.csdnimg.cn/20201025123756767.png#pic_center)
      在使用AI Studio过程中,掌握数据的导入也是常规操作。在导入文件夹的时候需要传入压缩文件,如你导入格式为.zip文件,则需要在cell上写!unzip + 'work/dummy_data'(绝对路径),并执行才成功将其导入。
      在notebook右下方有变量监控、运行历史、性能监控,可以方便查看运行过程以及CPU/GPU的使用情况。
      ![变量监控](https://img-blog.csdnimg.cn/20201025124110432.png#pic_center)
      ![运行历史](https://img-blog.csdnimg.cn/20201025124133443.png#pic_center)
      ![性能监控](https://img-blog.csdnimg.cn/20201025124155157.png#pic_center)

前期基本代码的构建(都已经运行检测,可成功run)

basic_dataload代码,可在百度AI Studio上使用并进行基本测试。

import os
import random
import numpy as np
import cv2
import paddle.fluid as fluid

class Transform(object):
    def __init__(self, size=256):
        self.size = size

    def __call__(self, input, label):
        input = cv2.resize(input, (self.size, self.size), interpolation=cv2.INTER_LINEAR)
        label = cv2.resize(label, (self.size, self.size), interpolation=cv2.INTER_LINEAR)
        return input,label

class BasicDataLoader():
    def __init__(self,
                 image_folder,
                 image_list_file,
                 transform=None,
                 shuffle=True):
        self.image_folder = image_folder
        self.image_list_file = image_list_file
        self.transform = transform
        self.shuffle = shuffle
        self.data_list = self.read_list()

    def read_list(self):
        data_list = []
        with open(self.image_list_file,'r') as infile:
            for line in infile.readlines():
                data_path = os.path.join(self.image_folder, line.split()[0])
                label_path = os.path.join(self.image_folder, line.split()[1])
                data_list.append((data_path, label_path))
        random.shuffle(data_list)
        return data_list
    def preprocess(self, data, label):
        h, w, c = data.shape
        h_gt, w_gt = label.shape
        assert h == h_gt, "Error"
        assert w == w_gt, 'Error'

        if self.transform:
            data,label = self.transform(data, label)

        label = label[:, :, np.newaxis]

        return data, label
    def __len__(self):
        return len(self.data_list)
    def __call__(self):
        for data_path, label_path in self.data_list:
            data = cv2.imread(data_path, cv2.IMREAD_COLOR)
            data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
            label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE)
            print(data.shape, label.shape)
            data, label = self.preprocess(data, label)
            yield data, label



def main():
    batch_size = 5
    place = fluid.CPUPlace()
    with fluid.dygraph.guard(place):
        # TODO: craete BasicDataloder instance
        # image_folder="./dummy_data"
        # image_list_file="./dummy_data/list.txt"

        # TODO: craete fluid.io.DataLoader instance

        # TODO: set sample generator for fluid dataloader
        transform = Transform(256)
        # create BasicDataloder instance
        basic_dataloader = BasicDataLoader(
            image_folder=r'work/dummy_data',
            image_list_file=r'work/dummy_data/list.txt',
            transform=transform,
            shuffle=True
        )
        # create fluid.io.DataLoader instance
        dataloader = fluid.io.DataLoader.from_generator(capacity=1, use_multiprocess=False)
        # set sample generator for fluid dataloader
        dataloader.set_sample_generator(basic_dataloader,
                                        batch_size=batch_size,
                                        places=place)

        num_epoch = 2
        for epoch in range(1, num_epoch+1):
            print(f'Epoch [{epoch}/{num_epoch}]:')
            for idx, (data, label) in enumerate(dataloader):
                print(f'Iter {idx}, Data shape: {data.shape}, Label shape: {label.shape}')

if __name__ == "__main__":
    main()

basic_modle代码:

import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import Pool2D #TODO
from paddle.fluid.dygraph import Conv2D #TODO
from paddle.fluid.dygraph import to_variable #TODO
import numpy as np
np.set_printoptions(precision=2)


class BasicModel(fluid.dygraph.Layer):
    # BasicModel contains:
    # 1. pool:   4x4 max pool op, with stride 4
    # 2. conv:   3x3 kernel size, takes RGB image as input and output num_classes channels,
    #            note that the feature map size should be the same
    # 3. upsample: upsample to input size
    #
    # TODOs:
    # 1. The model takes an random input tensor with shape (1, 3, 8, 8)
    # 2. The model outputs a tensor with same HxW size of the input, but C = num_classes
    # 3. Print out the model output in numpy format 

    def __init__(self, num_classes=59):
        super(BasicModel, self).__init__()
        self.Pool=Pool2D(pool_size=4,pool_stride=4,pool_type='max')#TODO
        self.Conv=Conv2D(num_channels=3,num_filters=num_classes,filter_size=1)

    def forward(self, inputs):
        x = self.Pool(inputs)#TODO 
        x = fluid.layers.interpolate(x, out_shape=(inputs.shape[2], inputs.shape[3]))
        x = self.Conv(x)#TODO 
        return x

def main():
    place = paddle.fluid.CPUPlace()
    with fluid.dygraph.guard(place):
        model = BasicModel(num_classes=59)
        model.eval()
        input_data = np.random.rand(1,3,8,8).astype(np.float32)# TODO
        print('Input data shape: ', input_data.shape)
        input_data = to_variable(input_data)# TODO
        output_data = model.forward(input_data)# TODO
        output_data = output_data.numpy()# TODO
        print('Output data shape: ', output_data.shape)

if __name__ == "__main__":
    main()

(需要训练的基本数据可以评论区留言!)
VGG、PSPNet、U-Net均在此基础上进行添加修改,未完待续!

你可能感兴趣的:(深度学习,笔记,深度学习,paddlepaddle)