paddle 图像分割学习总结

2024-02-27 01:58

本文主要是介绍paddle 图像分割学习总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、图像分割综述
  • 二、FCN
  • U-Net
  • PSPNet
  • DeepLab


前言

课程链接:https://aistudio.baidu.com/aistudio/course/introduce/1767


提示:以下是本篇文章正文内容

一、图像分割综述

根据 不同的任务和数据类型:

-图像分割(image segmentation)(像素级分类)

  • 图像语义分割(image semantic segmentation)
  • 图像实例分割(image instance segmentation)
  • 图像全景分割(image panoptic segmentation)
  • 视频目标分割(video object segmentation)
  • 视频实例分割(video instance segmentation)

示例:

  • 语义分割:给每个pixel分类
  • 实例分割:给每个筐里的object分mask
  • 全景分割:背景pixel分类+框里mask
  • VOS :通常会给定目标的mask,求特定目标的mask
  • VIS :根据目标检测的框,求目标的mask
    在这里插入图片描述

分割网络的评价指标:mIoU 和 mAcc

  • mean intersection-over-unio
    分割每一类别的交并比
  • mean accuracy
    分对的像素 / 所有像素

二、FCN

网络结构:
在这里插入图片描述

代码如下(示例):

import numpy as np
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph import Conv2D
from paddle.fluid.dygraph import Conv2DTranspose
from paddle.fluid.dygraph import Dropout
from paddle.fluid.dygraph import BatchNorm
from paddle.fluid.dygraph import Pool2D
from paddle.fluid.dygraph import Linear
# from vgg import VGG16BNclass VGG(fluid.dygraph.Layer):def __init__(self, layers=16, use_bn=False, num_classes=1000):super(VGG, self).__init__()self.layers = layersself.use_bn = use_bnsupported_layers = [16, 19]assert layers in supported_layersif layers == 16:depth = [2, 2, 3, 3, 3]elif layers == 19:depth = [2, 2, 4, 4, 4]num_channels = [3, 64, 128, 256, 512]num_filters = [64, 128, 256, 512, 512]self.layer1 = fluid.dygraph.Sequential(*self.make_layer(num_channels[0], num_filters[0], depth[0], use_bn, name='layer1'))self.layer2 = fluid.dygraph.Sequential(*self.make_layer(num_channels[1], num_filters[1], depth[1], use_bn, name='layer2'))self.layer3 = fluid.dygraph.Sequential(*self.make_layer(num_channels[2], num_filters[2], depth[2], use_bn, name='layer3'))self.layer4 = fluid.dygraph.Sequential(*self.make_layer(num_channels[3], num_filters[3], depth[3], use_bn, name='layer4'))self.layer5 = fluid.dygraph.Sequential(*self.make_layer(num_channels[4], num_filters[4], depth[4], use_bn, name='layer5'))self.classifier = fluid.dygraph.Sequential(Linear(input_dim=512 * 7 * 7, output_dim=4096, act='relu'),Dropout(),Linear(input_dim=4096, output_dim=4096, act='relu'),Dropout(),Linear(input_dim=4096, output_dim=num_classes))self.out_dim = 512 * 7 * 7def forward(self, inputs):x = self.layer1(inputs)x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2)x = self.layer2(x)x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2)x = self.layer3(x)x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2)x = self.layer4(x)x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2)x = self.layer5(x)x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2)x = fluid.layers.adaptive_pool2d(x, pool_size=(7,7), pool_type='avg')x = fluid.layers.reshape(x, shape=[-1, self.out_dim])x = self.classifier(x)return xclass FCN8s(fluid.dygraph.Layer):# TODO: create fcn8s modeldef __init__(self, num_classes=59):super(FCN8s, self).__init__()self.num_classes = num_classesself.layer1 = fluid.dygraph.Sequential(Conv2D(num_channels=3, num_filters=64, filter_size=3, padding=1),BatchNorm(num_channels=64, act='relu'),Conv2D(num_channels=64, num_filters=64, filter_size=3, padding=1),BatchNorm(num_channels=64, act='relu'),Pool2D(pool_size=2, pool_stride=2, pool_type='max')) # 1/ 2self.layer2 = fluid.dygraph.Sequential(Conv2D(num_channels=64, num_filters=128, filter_size=3, padding=1),BatchNorm(num_channels=128, act='relu'),Conv2D(num_channels=128, num_filters=128, filter_size=3, padding=1),BatchNorm(num_channels=128, act='relu'),Pool2D(pool_size=2, pool_stride=2, pool_type='max')) # 1/ 4self.layer3 = fluid.dygraph.Sequential(Conv2D(num_channels=128, num_filters=256, filter_size=3, padding=1),BatchNorm(num_channels=256, act='relu'),Conv2D(num_channels=256, num_filters=256, filter_size=3, padding=1),BatchNorm(num_channels=256, act='relu'),Conv2D(num_channels=256, num_filters=256, filter_size=3, padding=1),BatchNorm(num_channels=256, act='relu'),Pool2D(pool_size=2, pool_stride=2, pool_type='max')) # 1 / 8self.layer4 = fluid.dygraph.Sequential(Conv2D(num_channels=256, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Conv2D(num_channels=512, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Conv2D(num_channels=512, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Pool2D(pool_size=2, pool_stride=2, pool_type='max')) # 1 / 16self.layer5 = fluid.dygraph.Sequential(Conv2D(num_channels=512, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Conv2D(num_channels=512, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Conv2D(num_channels=512, num_filters=512, filter_size=3, padding=1),BatchNorm(num_channels=512, act='relu'),Pool2D(pool_size=2, pool_stride=2, pool_type='max')) # 1 / 32self.conv67 = fluid.dygraph.Sequential(Conv2D(num_channels=512, num_filters=512, filter_size=1),BatchNorm(num_channels=512, act='relu'),Conv2D(num_channels=512, num_filters=512, filter_size=1),BatchNorm(num_channels=512, act='relu'))self.conv = Conv2D(num_channels=512, num_filters=self.num_classes, filter_size=1)def forward(self, inputs):x = self.layer1(inputs)x = self.layer2(x)x = self.layer3(x)pool3 = x # 1/8 256x = self.layer4(x)pool4 = x # 1/16 512x = self.layer5(x)x = self.conv67(x)x = fluid.layers.interpolate(x, pool4.shape[2:])x = fluid.layers.elementwise_add(pool4, x)x = fluid.layers.interpolate(x, pool3.shape[2:])pool3 = Conv2D(num_channels= 256, num_filters=512, filter_size=1, act='relu')(pool3)x = fluid.layers.elementwise_add(pool3, x)x = fluid.layers.interpolate(x, inputs.shape[2:])x = self.conv(x)return xdef main():with fluid.dygraph.guard():x_data = np.random.rand(2, 3, 512, 512).astype(np.float32)x = to_variable(x_data)model = FCN8s(num_classes=59)model.eval()pred = model(x)print(pred.shape)if __name__ == '__main__':main()

U-Net

网络结构:

  • 采用编码器和解码器的U形结构
  • 输入输出大小不变
  • skip结合方式:concatenation
    在这里插入图片描述

Paper Title: U-net:Convolutional networks for biomedical image segmentaion

在这里插入图片描述

PSPNet

psp模块
在这里插入图片描述

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_listnum_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 = fluid.layers.concat(out, axis=1)return outclass PSPNet(Layer):def __init__(self, num_classes=59, backbone='resnet50'):super(PSPNet, self).__init__()res = ResNet50()# stem: res.conv, res.pool2d_maxself.layer0 = fluid.dygraph.Sequential(res.conv,res.pool2d_max)self.layer1 = res.layer1self.layer2 = res.layer2self.layer3 = res.layer3self.layer4 = res.layer4num_channels = 2048# psp: 2048 -> 2048*2self.pspmodule = PSPModule(num_channels, [1,2,3,6])num_channels *= 2# cls: 2048*2 -> 512 -> num_classesself.classifier = fluid.dygraph.Sequential(Conv2D(num_channels = num_channels, num_filters=512, filter_size=3, padding=1),BatchNorm(512, act = 'relu'),Dropout(0.1),Conv2D(num_channels=512, num_filters=num_classes, filter_size=1))# aux: 1024 -> 256 -> num_classesdef forward(self, inputs):# aux: tmp_x = layer3x = self.layer0(inputs)x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)aux = xx = self.layer4(x)x = self.pspmodule(x)x = self.classifier(x)x = fluid.layers.interpolate(x, inputs.shape[2:])return x, auxdef 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()

DeepLab

aspp模块升级版
在这里插入图片描述

class ASPPPooling(Layer):# TODO: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.shapex = fluid.layers.adaptive_pool2d(inputs, 1)x = self.features(x)x = fluid.layers.interpolate(x, [h, w])return xclass ASPPConv(fluid.dygraph.Sequential):# TODO:def __init__(self, num_channels, num_filters, dilation):super(ASPPConv, self).__init__(Conv2D(num_channels=num_channels, num_filters=num_filters, filter_size=3, padding=dilation, dilation=dilation),BatchNorm(num_filters, act='relu'))class ASPPModule(Layer):# TODO: 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(1280, 256, 1),BatchNorm(256, act='relu'))def forward(self, inputs):res = []for f in self.features:res.append(f(inputs))x = fluid.layers.concat(res, axis=1)x = self.project(x)return xclass 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))class DeepLab(Layer):# TODO:def __init__(self, num_classes=59):super(DeepLab, self).__init__()bone = ResNet50(pretrained=False, duplicate_blocks=True)self.layer0 = fluid.dygraph.Sequential(bone.conv,bone.pool2d_max)self.layer1 = bone.layer1self.layer2 = bone.layer2self.layer3 = bone.layer3self.layer4 = bone.layer4#multi gridself.layer5 = bone.layer5self.layer6 = bone.layer6self.layer7 = bone.layer7# feature_dim = 2048self.classifier = DeepLabHead(2048, 59)def forward(self, inputs):n, c, h, w = inputs.shapex = 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)x = fluid.layers.interpolate(x, [h, w], align_corners=False)return x

这篇关于paddle 图像分割学习总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/750881

相关文章

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Python依赖库的几种离线安装方法总结

《Python依赖库的几种离线安装方法总结》:本文主要介绍如何在Python中使用pip工具进行依赖库的安装和管理,包括如何导出和导入依赖包列表、如何下载和安装单个或多个库包及其依赖,以及如何指定... 目录前言一、如何copy一个python环境二、如何下载一个包及其依赖并安装三、如何导出requirem

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

使用Python实现批量分割PDF文件

《使用Python实现批量分割PDF文件》这篇文章主要为大家详细介绍了如何使用Python进行批量分割PDF文件功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、架构设计二、代码实现三、批量分割PDF文件四、总结本文将介绍如何使用python进js行批量分割PDF文件的方法

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo