使用LabVIEW实现基于pytorch的DeepLabv3图像语义分割


一、什么是语义分割

图像语义分割(semantic segmentation),从字面意思上理解就是让计算机根据图像的语义来进行分割,例如让计算机在输入下面左图的情况下,能够输出右图。语义在语音识别中指的是语音的意思,在图像领域,语义指的是图像的内容,对图片意思的理解,比如下图的语义就是一个人牵着四只羊;分割的意思是从像素的角度分割出图片中的不同对象,对原图中的每个像素都进行标注,比如下图中浅黄色代表人,蓝绿色代表羊。语义分割任务就是将图片中的不同类别,用不同的颜色标记出来,每一个类别使用一种颜色。常用于医学图像,卫星图像,无人车驾驶,机器人等领域。
在这里插入图片描述

  • 如何做到将像素点上色呢?

语义分割的输出和图像分类网络类似,图像分类类别数是一个一维的one hot 矩阵。例如:三分类的[0,1,0]。语义分割任务最后的输出特征图是一个三维结构,大小与原图类似,其中通道数是类别数,每个通道所标记的像素点,是该类别在图像中的位置,最后通过argmax 取每个通道有用像素 合成一张图像,用不同颜色表示其类别位置。 语义分割任务其实也是分类任务中的一种,他不过是对每一个像素点进行细分,找到每一个像素点所述的类别。 这就是语义分割任务啦~
在这里插入图片描述

二、什么是deeplabv3

DeepLabv3是一种语义分割架构,它在DeepLabv2的基础上进行了一些修改。为了处理在多个尺度上分割对象的问题,设计了在级联或并行中采用多孔卷积的模块,通过采用多个多孔速率来捕获多尺度上下文。此外,来自 DeepLabv2 的 Atrous Spatial Pyramid Pooling模块增加了编码全局上下文的图像级特征,并进一步提高了性能。
在这里插入图片描述

三、LabVIEW调用DeepLabv3实现图像语义分割

1、模型获取及转换

  • 安装pytorch和torchvision
  • 获取torchvision中的模型:deeplabv3_resnet101(我们获取预训练好的模型):
original_model = models.segmentation.deeplabv3_resnet101(pretrained=True)
  • 转onnx
def get_pytorch_onnx_model(original_model):
    # define the directory for further converted model save
    onnx_model_path = dirname
    # define the name of further converted model
    onnx_model_name = "deeplabv3_resnet101.onnx"

    # create directory for further converted model
    os.makedirs(onnx_model_path, exist_ok=True)

    # get full path to the converted model
    full_model_path = os.path.join(onnx_model_path, onnx_model_name)

    # generate model input
    generated_input = Variable(
        torch.randn(1, 3, 448, 448)
    )

    # model export into ONNX format
    torch.onnx.export(
        original_model,
        generated_input,
        full_model_path,
        verbose=True,
        input_names=["input"],
        output_names=["output",'aux'],
        opset_version=11
    )

    return full_model_path

完整获取及模型转换python代码如下:

import os
import torch
import torch.onnx
from torch.autograd import Variable
from torchvision import models
import re

dirname, filename = os.path.split(os.path.abspath(__file__))
print(dirname)

def get_pytorch_onnx_model(original_model):
    # define the directory for further converted model save
    onnx_model_path = dirname
    # define the name of further converted model
    onnx_model_name = "deeplabv3_resnet101.onnx"

    # create directory for further converted model
    os.makedirs(onnx_model_path, exist_ok=True)

    # get full path to the converted model
    full_model_path = os.path.join(onnx_model_path, onnx_model_name)

    # generate model input
    generated_input = Variable(
        torch.randn(1, 3, 448, 448)
    )

    # model export into ONNX format
    torch.onnx.export(
        original_model,
        generated_input,
        full_model_path,
        verbose=True,
        input_names=["input"],
        output_names=["output",'aux'],
        opset_version=11
    )

    return full_model_path


def main():
    # initialize PyTorch ResNet-101 model
    original_model = models.segmentation.deeplabv3_resnet101(pretrained=True)

    # get the path to the converted into ONNX PyTorch model
    full_model_path = get_pytorch_onnx_model(original_model)
    print("PyTorch ResNet-101 model was successfully converted: ", full_model_path)


if __name__ == "__main__":
    main()

我们会发现,基于pytorch的DeepLabv3模型获取和之前的mask rcnn模型大同小异。

2、关于deeplabv3_resnet101

我们使用的模型是:deeplabv3_resnet101,该模型返回两个张量,与输入张量相同,但有21个classes。输出[“out”]包含语义掩码,而输出[“aux”]包含每像素的辅助损失值。在推理模式中,输出[‘aux]没有用处。因此,输出“out”形状为(N、21、H、W)。我们在转模型的时候设置H,W为448,N一般为1;

我们的模型是基于VOC2012数据集
VOC2012数据集分为20类,包括背景为21类,分别如下:
人 :人
动物:鸟、猫、牛、狗、马、羊
车辆:飞机、自行车、船、巴士、汽车、摩托车、火车
室内:瓶、椅子、餐桌、盆栽植物、沙发、电视/监视器
在这里插入图片描述

3、LabVIEW opencv dnn调用 deeplabv3 实现图像语义分割(deeplabv3_opencv.vi)

deeplabv3模型可以使用OpenCV dnn去加载的,也可以使用onnxruntime加载推理,所以我们分两种方式给大家介绍LabVIEW调用deeplabv3实现图像语义分割。

  • opencv dnn 调用onnx模型并选择
    在这里插入图片描述

  • 图像预处理
    最终还是采用了比较中规中矩的处理方式

在这里插入图片描述)

  • 执行推理

在这里插入图片描述

  • 后处理并实现实例分割
    因为后处理内容较多,所以直接封装为了一个子VI, deeplabv3_postprocess.vi,因为Labview没有专门的切片函数,所以会稍慢一些,所以接下来还会开发针对后处理和矩阵有关的函数,加快处理结果。

  • 整体的程序框架如下:
    在这里插入图片描述

  • 语义分割结果如下:
    在这里插入图片描述

4、LabVIEW onnxruntime调用 deeplabv3实现图像语义分割 (deeplabv3_onnx.vi)

  • 整体的程序框架如下:
    在这里插入图片描述
  • 语义分割结果如下:
    在这里插入图片描述

5、LabVIEW onnxruntime调用 deeplabv3 使用TensorRT加速模型实现图像语义分割(deeplabv3_onnx_camera.vi)

在这里插入图片描述

如上图所示,可以看到可以把人和背景完全分割开来,使用TensorRT加速推理,速度也比较快。


四、deeplabv3训练自己的数据集

训练可参考:https://github.com/pytorch/vision

总结

以上就是今天要给大家分享的内容。大家可关注微信公众号: VIRobotics,回复关键字:DeepLabv3图像语义分割源码 获取本次分享内容的完整项目源码及模型。

如果有问题可以在评论区里讨论,提问前请先点赞支持一下博主哦,如您想要探讨更多关于LabVIEW与人工智能技术,欢迎加入我们的技术交流群:705637299。

如果文章对你有帮助,欢迎关注、点赞、收藏

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

virobotics

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值