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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

git使用的说明总结

Git使用说明 下载安装(下载地址) macOS: Git - Downloading macOS Windows: Git - Downloading Windows Linux/Unix: Git (git-scm.com) 创建新仓库 本地创建新仓库:创建新文件夹,进入文件夹目录,执行指令 git init ,用以创建新的git 克隆仓库 执行指令用以创建一个本地仓库的

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识