前言
圖像分割可以分為兩類:語(yǔ)義分割(Semantic Segmentation)和實(shí)例分割(Instance Segmentation),前面已經(jīng)給大家介紹過(guò)兩者的區(qū)別,并就如何在labview上實(shí)現(xiàn)相關(guān)模型的部署也給大家做了講解,今天和大家分享如何使用labview 實(shí)現(xiàn)deeplabv3+的語(yǔ)義分割,并就 Pascal VOC2012 (DeepLabv3Plus-MobileNet) 上的分割結(jié)果和城市景觀的分割結(jié)果(DeepLabv3Plus-MobileNet)給大家做一個(gè)分享。
一、什么是deeplabv3+
Deeplabv3+是一個(gè)語(yǔ)義分割網(wǎng)絡(luò),使用DeepLabv3作為Encoder模塊,并添加一個(gè)簡(jiǎn)單且有效的Decoder模塊來(lái)獲得更清晰的分割。即網(wǎng)絡(luò)主要分為兩個(gè)部分:Encoder和Decoder;論文中采用的是Xception作為主干網(wǎng)絡(luò)(在代碼中也可以根據(jù)需求替換成MobileNet,本文示例中即使用的MobileNet),然后使用了ASPP結(jié)構(gòu),解決多尺度問(wèn)題;為了將底層特征與高層特征融合,提高分割邊界準(zhǔn)確度,引入Decoder部分。
Encoder-Decoder網(wǎng)絡(luò)已經(jīng)成功應(yīng)用于許多計(jì)算機(jī)視覺(jué)任務(wù),通常,Encoder-Decoder網(wǎng)絡(luò)包含:
- 逐步減少特征圖并提取更高語(yǔ)義信息的Encoder模塊
- 逐步恢復(fù)空間信息的Decoder模塊
二、LabVIEW調(diào)用DeepLabv3+實(shí)現(xiàn)圖像語(yǔ)義分割
1、模型獲取及轉(zhuǎn)換
- 下載預(yù)訓(xùn)練好的.pth模型文件,下載鏈接:https://share.weiyun.com/qqx78Pv5 ,我們選擇主干網(wǎng)絡(luò)為Mobilenet的模型
- git上下載開(kāi)源的整個(gè)項(xiàng)目文件,鏈接為:https://github.com/VainF/DeepLabV3Plus-Pytorch
- 根據(jù)requirements.txt 安裝所需要的庫(kù)
pip install -r requirements.txt
- 原項(xiàng)目中使用的模型為.pth,我們將其轉(zhuǎn)onnx模型,
- 將best_deeplabv3plus_mobilenet_voc_os16.pth轉(zhuǎn)化為deeplabv3plus_mobilenet.onnx,具體轉(zhuǎn)化模型代碼如下:
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轉(zhuǎn)化為deeplabv3plus_mobilenet_cityscapes.onnx,具體轉(zhuǎn)化模型代碼如下:
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)
注意:我們需要將以上兩個(gè)腳本保存并與network文件夾同路徑
2、LabVIEW 調(diào)用基于 Pascal VOC2012訓(xùn)練的deeplabv3+實(shí)現(xiàn)圖像語(yǔ)義分割 (deeplabv3+_onnx.vi)
經(jīng)過(guò)實(shí)驗(yàn)發(fā)現(xiàn),opencv dnn因缺少一些算子,所以無(wú)法加載deeplabv3+ onnx模型,所以我們選擇使用LabVIEW開(kāi)放神經(jīng)網(wǎng)絡(luò)交互工具包【ONNX】來(lái)加載并推理整個(gè)模型,實(shí)現(xiàn)語(yǔ)義分割,程序源碼如下:
3、LabVIEW Pascal VOC2012上的分割結(jié)果(deeplabv3+_onnx.vi)
4、LabVIEW 調(diào)用基于 Cityscapes 訓(xùn)練的deeplabv3+實(shí)現(xiàn)圖像語(yǔ)義分割 (deeplabv3+_onnx_cityscape.vi)
如下圖所示即為程序源碼,我們對(duì)比deeplabv3+_onnx.vi,發(fā)現(xiàn)其實(shí)只需要把模型和待檢測(cè)的圖片更換,圖片尺寸比例也做一個(gè)修改即可
5、LabVIEW 城市景觀的分割結(jié)果(deeplabv3+_onnx_cityscape.vi)
三、項(xiàng)目源碼及模型下載
歡迎關(guān)注微信公眾號(hào): VIRobotics,回復(fù)關(guān)鍵字:deepLabv3+ 語(yǔ)義分割源碼 獲取本次分享內(nèi)容的完整項(xiàng)目源碼及模型。
附加說(shuō)明
操作系統(tǒng):Windows10
python:3.6及以上
LabVIEW:2018及以上 64位版本
視覺(jué)工具包:techforce_lib_opencv_cpu-1.0.0.73.vip
LabVIEW開(kāi)放神經(jīng)網(wǎng)絡(luò)交互工具包【ONNX】:virobotics_lib_onnx_cpu-1.0.0.13.vip文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-545686.html
總結(jié)
以上就是今天要給大家分享的內(nèi)容。如果有問(wèn)題可以在評(píng)論區(qū)里討論,提問(wèn)前請(qǐng)先點(diǎn)贊支持一下博主哦,如您想要探討更多關(guān)于LabVIEW與人工智能技術(shù),歡迎加入我們的技術(shù)交流群:705637299。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-545686.html
到了這里,關(guān)于使用LabVIEW實(shí)現(xiàn) DeepLabv3+ 語(yǔ)義分割含源碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!