• [技术干货] 使用LabVIEW实现 DeepLabv3+ 语义分割含源码【转】
    前言图像分割可以分为两类:语义分割(Semantic Segmentation)和实例分割(Instance Segmentation),前面已经给大家介绍过两者的区别,并就如何在labview上实现相关模型的部署也给大家做了讲解,今天和大家分享如何使用labview 实现deeplabv3+的语义分割,并就 Pascal VOC2012 (DeepLabv3Plus-MobileNet) 上的分割结果和城市景观的分割结果(DeepLabv3Plus-MobileNet)给大家做一个分享。一、什么是deeplabv3+Deeplabv3+是一个语义分割网络,使用DeepLabv3作为Encoder模块,并添加一个简单且有效的Decoder模块来获得更清晰的分割。即网络主要分为两个部分:Encoder和Decoder;论文中采用的是Xception作为主干网络(在代码中也可以根据需求替换成MobileNet,本文示例中即使用的MobileNet),然后使用了ASPP结构,解决多尺度问题;为了将底层特征与高层特征融合,提高分割边界准确度,引入Decoder部分。Encoder-Decoder网络已经成功应用于许多计算机视觉任务,通常,Encoder-Decoder网络包含:逐步减少特征图并提取更高语义信息的Encoder模块逐步恢复空间信息的Decoder模块二、LabVIEW调用DeepLabv3+实现图像语义分割1、模型获取及转换下载预训练好的.pth模型文件,下载链接: cid:link_2 ,我们选择主干网络为Mobilenet的模型 git上下载开源的整个项目文件,链接为: cid:link_0根据requirements.txt 安装所需要的库pip install -r requirements.txt原项目中使用的模型为.pth,我们将其转onnx模型,将best_deeplabv3plus_mobilenet_voc_os16.pth转化为deeplabv3plus_mobilenet.onnx,具体转化模型代码如下:import network import numpy as np import torch from torch.autograd import Variable from torchvision import models import os 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 = "deeplabv3plus_mobilenet.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, 513, 513) ) # model export into ONNX format torch.onnx.export( original_model, generated_input, full_model_path, verbose=True, input_names=["input"], output_names=["output"], opset_version=11 ) return full_model_path model = network.modeling.__dict__["deeplabv3plus_mobilenet"](num_classes=21, output_stride=8) checkpoint = torch.load("best_deeplabv3plus_mobilenet_voc_os16.pth", map_location=torch.device('cpu')) model.load_state_dict(checkpoint["model_state"]) full_model_path = get_pytorch_onnx_model(model)将best_deeplabv3plus_mobilenet_cityscapes_os16.pth转化为deeplabv3plus_mobilenet_cityscapes.onnx,具体转化模型代码如下:import network import numpy as np import torch from torch.autograd import Variable from torchvision import models import os 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 = "deeplabv3plus_mobilenet_cityscapes.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, 513, 513) ) # model export into ONNX format torch.onnx.export( original_model, generated_input, full_model_path, verbose=True, input_names=["input"], output_names=["output"], opset_version=11 ) return full_model_path model = network.modeling.__dict__["deeplabv3plus_mobilenet"](num_classes=19, output_stride=8) checkpoint = torch.load("best_deeplabv3plus_mobilenet_cityscapes_os16.pth", map_location=torch.device('cpu')) model.load_state_dict(checkpoint["model_state"]) full_model_path = get_pytorch_onnx_model(model)注意:我们需要将以上两个脚本保存并与network文件夹同路径2、LabVIEW 调用基于 Pascal VOC2012训练的deeplabv3+实现图像语义分割 (deeplabv3+_onnx.vi)经过实验发现,opencv dnn因缺少一些算子,所以无法加载deeplabv3+ onnx模型,所以我们选择使用LabVIEW开放神经网络交互工具包【ONNX】来加载并推理整个模型,实现语义分割,程序源码如下: 3、LabVIEW Pascal VOC2012上的分割结果(deeplabv3+_onnx.vi)4、LabVIEW 调用基于 Cityscapes 训练的deeplabv3+实现图像语义分割 (deeplabv3+_onnx_cityscape.vi)如下图所示即为程序源码,我们对比deeplabv3+_onnx.vi,发现其实只需要把模型和待检测的图片更换,图片尺寸比例也做一个修改即可 5、LabVIEW 城市景观的分割结果(deeplabv3+_onnx_cityscape.vi)转载自:cid:link_1
  • [问题求助] 80分类coco128数据集子训练yolov5,成功安装App,但检测不出来结果
    已经成功嵌入到相机里面了, 就是检测不出结果。是不是中间有什么坑!请问是为什么?
  • [问题求助] 在python3.8下面应该如何使用nvidia-smi?
    在python3.8下面应该如何使用nvidia-smi?