前言
今天我們一起來看一下如何使用LabVIEW實現(xiàn)語義分割。
一、什么是語義分割
**圖像語義分割(semantic segmentation),從字面意思上理解就是讓計算機根據(jù)圖像的語義來進行分割,例如讓計算機在輸入下面左圖的情況下,能夠輸出右圖。語義在語音識別中指的是語音的意思,在圖像領域,語義指的是圖像的內(nèi)容,對圖片意思的理解,比如下圖的語義就是一個人牽著四只羊;分割的意思是從像素的角度分割出圖片中的不同對象,對原圖中的每個像素都進行標注,比如下圖中淺黃色代表人,藍綠色代表羊。語義分割任務就是將圖片中的不同類別,用不同的顏色標記出來,每一個類別使用一種顏色。常用于醫(yī)學圖像,衛(wèi)星圖像,無人車駕駛,機器人等領域。 **

- 如何做到將像素點上色呢?
**語義分割的輸出和圖像分類網(wǎng)絡類似,圖像分類類別數(shù)是一個一維的one hot 矩陣。例如:三分類的[0,1,0]。語義分割任務最后的輸出特征圖是一個三維結構,大小與原圖類似,其中通道數(shù)是類別數(shù),每個通道所標記的像素點,是該類別在圖像中的位置,最后通過argmax 取每個通道有用像素 合成一張圖像,用不同顏色表示其類別位置。 語義分割任務其實也是分類任務中的一種,他不過是對每一個像素點進行細分,找到每一個像素點所述的類別。 這就是語義分割任務啦~ **

二、什么是deeplabv3
**DeepLabv3是一種語義分割架構,它在DeepLabv2的基礎上進行了一些修改。為了處理在多個尺度上分割對象的問題,設計了在級聯(lián)或并行中采用多孔卷積的模塊,通過采用多個多孔速率來捕獲多尺度上下文。此外,來自 DeepLabv2 的 Atrous Spatial Pyramid Pooling模塊增加了編碼全局上下文的圖像級特征,并進一步提高了性能。 **

三、LabVIEW調(diào)用DeepLabv3實現(xiàn)圖像語義分割
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()
?
我們會發(fā)現(xiàn),基于pytorch的DeepLabv3模型獲取和之前的mask rcnn模型大同小異。
2、關于deeplabv3_resnet101
我們使用的模型是:deeplabv3_resnet101,該模型返回兩個張量,與輸入張量相同,但有21個classes。輸出[“out”]包含語義掩碼,而輸出[“aux”]包含每像素的輔助損失值。在推理模式中,輸出[‘a(chǎn)ux]沒有用處。因此,輸出“out”形狀為(N、21、H、W)。我們在轉模型的時候設置H,W為448,N一般為1;
我們的模型是基于VOC2012數(shù)據(jù)集
VOC2012數(shù)據(jù)集分為20類,包括背景為21類,分別如下:
- 人 :人
- 動物:鳥、貓、牛、狗、馬、羊
- 車輛:飛機、自行車、船、巴士、汽車、摩托車、火車
- 室內(nèi):瓶、椅子、餐桌、盆栽植物、沙發(fā)、電視/監(jiān)視器
3、LabVIEW opencv dnn調(diào)用 deeplabv3 實現(xiàn)圖像語義分割(deeplabv3_opencv.vi)
deeplabv3模型可以使用OpenCV dnn去加載的,也可以使用onnxruntime加載推理,所以我們分兩種方式給大家介紹LabVIEW調(diào)用deeplabv3實現(xiàn)圖像語義分割。
-
opencv dnn 調(diào)用onnx模型并選擇

-
**圖像預處理 **
最終還是采用了比較中規(guī)中矩的處理方式

- **執(zhí)行推理 **

-
后處理并實現(xiàn)實例分割
因為后處理內(nèi)容較多,所以直接封裝為了一個子VI, deeplabv3_postprocess.vi,因為Labview沒有專門的切片函數(shù),所以會稍慢一些,所以接下來還會開發(fā)針對后處理和矩陣有關的函數(shù),加快處理結果。
-
整體的程序框架如下:

-
語義分割結果如下:

4、LabVIEW onnxruntime調(diào)用 deeplabv3實現(xiàn)圖像語義分割 (deeplabv3_onnx.vi)
-
整體的程序框架如下:

-
語義分割結果如下:

5、LabVIEW onnxruntime調(diào)用 deeplabv3 使用TensorRT加速模型實現(xiàn)圖像語義分割(deeplabv3_onnx_camera.vi)

如上圖所示,可以看到可以把人和背景完全分割開來,使用TensorRT加速推理,速度也比較快。
大家可關注微信公眾號: VIRobotics ,回復關鍵字:DeepLabv3圖像語義分割源碼 獲取本次分享內(nèi)容的完整項目源碼及模型。
如您想要探討更多關于LabVIEW與人工智能技術,歡迎加入我們:705637299,進群請備注暗號:LabVIEW機器學習
四、deeplabv3訓練自己的數(shù)據(jù)集
訓練可參考: https://github.com/pytorch/vision
總結
以上就是今天要給大家分享的內(nèi)容。
如果文章對你有幫助,歡迎關注、點贊、收藏
審核編輯 黃宇
-
LabVIEW
+關注
關注
2013文章
3681瀏覽量
344340 -
人工智能
+關注
關注
1813文章
49746瀏覽量
261596 -
機器學習
+關注
關注
66文章
8541瀏覽量
136236 -
深度學習
+關注
關注
73文章
5591瀏覽量
123914 -
pytorch
+關注
關注
2文章
813瀏覽量
14704
發(fā)布評論請先 登錄
AI功能(SC171開發(fā)套件V3)
van-自然和醫(yī)學圖像的深度語義分割:網(wǎng)絡結構
van-自然和醫(yī)學圖像的深度語義分割:網(wǎng)絡結構
DeepLab進行語義分割的研究分析
分析總結基于深度神經(jīng)網(wǎng)絡的圖像語義分割方法
輕松學Pytorch之Deeplabv3推理
PyTorch教程14.9之語義分割和數(shù)據(jù)集

使用LabVIEW實現(xiàn)基于pytorch的DeepLabv3圖像語義分割
評論