? RT-DETR是在DETR模型基礎(chǔ)上進(jìn)行改進(jìn)的,一種基于 DETR 架構(gòu)的實(shí)時(shí)端到端檢測(cè)器,它通過使用一系列新的技術(shù)和算法,實(shí)現(xiàn)了更高效的訓(xùn)練和推理,在前文我們發(fā)表了《基于 OpenVINO? Python API 部署 RT-DETR 模型 | 開發(fā)者實(shí)戰(zhàn)》和《基于 OpenVINO? C++ API 部署 RT-DETR 模型 | 開發(fā)者實(shí)戰(zhàn)》,在該文章中,我們基于OpenVINO? Python 和 C++ API 向大家展示了的RT-DETR模型的部署流程,并分別展示了是否包含后處理的模型部署流程,為大家使用RT-DETR模型提供了很好的范例。
在實(shí)際工業(yè)應(yīng)用時(shí),有時(shí)我們需要在C#環(huán)境下使用該模型應(yīng)用到工業(yè)檢測(cè)中,因此在本文中,我們將向大家展示使用OpenVINO Csharp API 部署RT-DETR模型,并對(duì)比不同編程平臺(tái)下模型部署的速度。
該項(xiàng)目所使用的全部代碼已經(jīng)在GitHub上開源,并且收藏在OpenVINO-CSharp-API項(xiàng)目里,項(xiàng)目所在目錄鏈接為:
https://github.com/guojin-yan/OpenVINO-CSharp-API/tree/csharp3.0/tutorial_examples
也可以直接訪問該項(xiàng)目,項(xiàng)目鏈接為:
https://github.com/guojin-yan/RT-DETR-OpenVINO.git
項(xiàng)目首發(fā)網(wǎng)址為:基于 OpenVINO? C# API 部署 RT-DETR 模型 | 開發(fā)者實(shí)戰(zhàn)
1. RT-DETR
? 飛槳在去年 3 月份推出了高精度通用目標(biāo)檢測(cè)模型 PP-YOLOE ,同年在 PP-YOLOE 的基礎(chǔ)上提出了 PP-YOLOE+。而繼 PP-YOLOE 提出后,MT-YOLOv6、YOLOv7、DAMO-YOLO、RTMDet 等模型先后被提出,一直迭代到今年開年的 YOLOv8。
? YOLO 檢測(cè)器有個(gè)較大的待改進(jìn)點(diǎn)是需要 NMS 后處理,其通常難以優(yōu)化且不夠魯棒,因此檢測(cè)器的速度存在延遲。DETR是一種不需要 NMS 后處理、基于 Transformer 的端到端目標(biāo)檢測(cè)器。百度飛槳正式推出了——RT-DETR (Real-Time DEtection TRansformer) ,一種基于 DETR 架構(gòu)的實(shí)時(shí)端到端檢測(cè)器,其在速度和精度上取得了 SOTA 性能。
? RT-DETR是在DETR模型基礎(chǔ)上進(jìn)行改進(jìn)的,它通過使用一系列新的技術(shù)和算法,實(shí)現(xiàn)了更高效的訓(xùn)練和推理。具體來說,RT-DETR具有以下優(yōu)勢(shì):
- 1、實(shí)時(shí)性能更佳:RT-DETR采用了一種新的注意力機(jī)制,能夠更好地捕獲物體之間的關(guān)系,并減少計(jì)算量。此外,RT-DETR還引入了一種基于時(shí)間的注意力機(jī)制,能夠更好地處理視頻數(shù)據(jù)。
- 2、精度更高:RT-DETR在保證實(shí)時(shí)性能的同時(shí),還能夠保持較高的檢測(cè)精度。這主要得益于RT-DETR引入的一種新的多任務(wù)學(xué)習(xí)機(jī)制,能夠更好地利用訓(xùn)練數(shù)據(jù)。
- 3、更易于訓(xùn)練和調(diào)參:RT-DETR采用了一種新的損失函數(shù),能夠更好地進(jìn)行訓(xùn)練和調(diào)參。此外,RT-DETR還引入了一種新的數(shù)據(jù)增強(qiáng)技術(shù),能夠更好地利用訓(xùn)練數(shù)據(jù)。
2. OpenVINO
? 英特爾發(fā)行版 OpenVINO?工具套件基于oneAPI 而開發(fā),可以加快高性能計(jì)算機(jī)視覺和深度學(xué)習(xí)視覺應(yīng)用開發(fā)速度工具套件,適用于從邊緣到云的各種英特爾平臺(tái)上,幫助用戶更快地將更準(zhǔn)確的真實(shí)世界結(jié)果部署到生產(chǎn)系統(tǒng)中。通過簡(jiǎn)化的開發(fā)工作流程, OpenVINO?可賦能開發(fā)者在現(xiàn)實(shí)世界中部署高性能應(yīng)用程序和算法。
? OpenVINO? 2023.1于2023年9月18日發(fā)布,該工具包帶來了挖掘生成人工智能全部潛力的新功能。生成人工智能的覆蓋范圍得到了擴(kuò)展,通過PyTorch*等框架增強(qiáng)了體驗(yàn),您可以在其中自動(dòng)導(dǎo)入和轉(zhuǎn)換模型。大型語言模型(LLM)在運(yùn)行時(shí)性能和內(nèi)存優(yōu)化方面得到了提升。聊天機(jī)器人、代碼生成等的模型已啟用。OpenVINO更便攜,性能更高,可以在任何需要的地方運(yùn)行:在邊緣、云中或本地。
3. 環(huán)境配置
本文中主要使用的項(xiàng)目環(huán)境可以通過NuGet Package包進(jìn)行安裝,Visual Studio 提供了NuGet Package包管理功能,可以通過其進(jìn)行安裝,主要使用下圖兩個(gè)程序包,C#平臺(tái)安裝程序包還是十分方便的,直接安裝即可使用:
除了通過Visual Studio 安裝,也可以通過 dotnet 指令進(jìn)行安裝,安裝命令為:
dotnet add package OpenVINO.CSharp.Windows --version 2023.1.0.2
dotnet add package OpenCvSharp4.Windows --version 4.8.0.20230708
4. 模型下載與轉(zhuǎn)換
? 在之前的文章中我們已經(jīng)講解了模型的到處方式,大家可以參考下面兩篇文章實(shí)現(xiàn)模型導(dǎo)出:《基于 OpenVINO? Python API 部署 RT-DETR 模型 | 開發(fā)者實(shí)戰(zhàn)》和《基于 OpenVINO? C++ API 部署 RT-DETR 模型 | 開發(fā)者實(shí)戰(zhàn)》。
5. C#代碼實(shí)現(xiàn)
? 為了更系統(tǒng)地實(shí)現(xiàn)RT-DETR模型的推理流程,我們采用C#特性,封裝了RTDETRPredictor模型推理類以及RTDETRProcess模型數(shù)據(jù)處理類,下面我們將對(duì)這兩個(gè)類中的關(guān)鍵代碼進(jìn)行講解。
5.1 模型推理類實(shí)現(xiàn)
? C# 代碼中我們定義的RTDETRPredictor模型推理類如下所示:
public class RTDETRPredictor
{
public RTDETRPredictor(string model_path, string label_path,
string device_name = "CPU", bool postprcoess = true)
{}
public Mat predict(Mat image)
{}
private void pritf_model_info(Model model)
{}
private void fill_tensor_data_image(Tensor input_tensor, Mat input_image)
{}
private void fill_tensor_data_float(Tensor input_tensor, float[] input_data, int data_size)
{}
RTDETRProcess rtdetr_process;
bool post_flag;
Core core;
Model model;
CompiledModel compiled_model;
InferRequest infer_request;
}
1. 模型推理類初始化
? 首先我們需要初始化模型推理類,初始化相關(guān)信息:
public RTDETRPredictor(string model_path, string label_path, string device_name = "CPU", bool postprcoess = true)
{
INFO("Model path: " + model_path);
INFO("Device name: " + device_name);
core = new Core();
model = core.read_model(model_path);
pritf_model_info(model);
compiled_model = core.compile_model(model, device_name);
infer_request = compiled_model.create_infer_request();
rtdetr_process = new RTDETRProcess(new Size(640, 640), label_path, 0.5f);
this.post_flag = postprcoess;
}
? 在該方法中主要包含以下幾個(gè)輸入:
- model_path:推理模型地址;
- label_path:模型預(yù)測(cè)類別文件;
- device_name:推理設(shè)備名稱;
- post_flag:模型是否包含后處理,當(dāng)post_flag = true時(shí),包含后處理,當(dāng)post_flag = false時(shí),不包含后處理。
2. 圖片預(yù)測(cè)API
? 這一步中主要是對(duì)輸入圖片進(jìn)行預(yù)測(cè),并將模型預(yù)測(cè)結(jié)果會(huì)知道輸入圖片上,下面是這階段的主要代碼:
public Mat predict(Mat image)
{
Mat blob_image = rtdetr_process.preprocess(image.Clone());
if (post_flag)
{
Tensor image_tensor = infer_request.get_tensor("image");
Tensor shape_tensor = infer_request.get_tensor("im_shape");
Tensor scale_tensor = infer_request.get_tensor("scale_factor");
image_tensor.set_shape(new Shape(new List<long> { 1, 3, 640, 640 }));
shape_tensor.set_shape(new Shape(new List<long> { 1, 2 }));
scale_tensor.set_shape(new Shape(new List<long> { 1, 2 }));
fill_tensor_data_image(image_tensor, blob_image);
fill_tensor_data_float(shape_tensor, rtdetr_process.get_input_shape().ToArray(), 2);
fill_tensor_data_float(scale_tensor, rtdetr_process.get_scale_factor().ToArray(), 2);
} else {
Tensor image_tensor = infer_request.get_input_tensor();
image_tensor.set_shape(new Shape(new List<long> { 1, 3, 640, 640 }));
fill_tensor_data_image(image_tensor, blob_image);
}
infer_request.infer();
ResultData results;
if (post_flag)
{
Tensor output_tensor = infer_request.get_tensor("reshape2_95.tmp_0");
float[] result = output_tensor.get_data<float>(300 * 6);
results = rtdetr_process.postprocess(result, null, true);
} else {
Tensor score_tensor = infer_request.get_tensor(model.outputs()[1].get_any_name());
Tensor bbox_tensor = infer_request.get_tensor(model.outputs()[0].get_any_name());
float[] score = score_tensor.get_data<float>(300 * 80);
float[] bbox = bbox_tensor.get_data<float>(300 * 4);
results = rtdetr_process.postprocess(score, bbox, false);
}
return rtdetr_process.draw_box(image, results);
}
? 上述代碼的主要邏輯如下:首先是處理輸入圖片,調(diào)用定義的數(shù)據(jù)處理類,將輸入圖片處理成指定的數(shù)據(jù)類型;然后根據(jù)模型的輸入節(jié)點(diǎn)情況配置模型輸入數(shù)據(jù),如果使用的是動(dòng)態(tài)模型輸入,需要設(shè)置輸入形狀;接下來就是進(jìn)行模型推理;最后就是對(duì)推理結(jié)果進(jìn)行處理,并將結(jié)果繪制到輸入圖片上。
? 在模型數(shù)據(jù)加載時(shí),此處重新設(shè)置了輸入節(jié)點(diǎn)形狀,因此此處支持動(dòng)態(tài)模型輸入;并且根據(jù)模型是否包含后處理分別封裝了不同的處理方式,所以此處代碼支持所有導(dǎo)出的預(yù)測(cè)模型。
5.2 模型數(shù)據(jù)處理類RTDETRProcess
1. 定義RTDETRProcess
? C# 代碼中我們定義的RTDETRProcess模型推理類如下所示:
public class RTDETRProcess
{
public RTDETRProcess(Size target_size, string label_path = null, float threshold = 0.5f, InterpolationFlags interpf = InterpolationFlags.Linear)
{}
public Mat preprocess(Mat image)
{}
public ResultData postprocess(float[] score, float[] bbox, bool post_flag)
{}
public List<float> get_input_shape()
{}
public List<float> get_scale_factor() { }
public Mat draw_box(Mat image, ResultData results)
{}
private void read_labels(string label_path)
{}
private float sigmoid(float data)
{}
private int argmax(float[] data, int length)
{}
private Size target_size; // The model input size.
private List<string> labels; // The model classification label.
private float threshold; // The threshold parameter.
private InterpolationFlags interpf; // The image scaling method.
private List<float> im_shape;
private List<float> scale_factor;
}
2. 輸入數(shù)據(jù)處理方法
? 輸入數(shù)據(jù)處理這一塊需要獲取圖片形狀大小以及圖片縮放比例系數(shù),最后直接調(diào)用OpenCV提供的數(shù)據(jù)處理方法,對(duì)輸入數(shù)據(jù)進(jìn)行處理。
public Mat preprocess(Mat image)
{
im_shape = new List<float> { (float)image.Rows, (float)image.Cols };
scale_factor = new List<float> { 640.0f / (float)image.Rows, 640.0f / (float)image.Cols };
Mat input_mat = CvDnn.BlobFromImage(image, 1.0 / 255.0, target_size, 0, true, false);
return input_mat;
}
3. 預(yù)測(cè)結(jié)果數(shù)據(jù)處理方法
public ResultData postprocess(float[] score, float[] bbox, bool post_flag)
{
ResultData result = new ResultData();
if (post_flag)
{
for (int i = 0; i < 300; ++i)
{
if (score[6 * i + 1] > threshold)
{
result.clsids.Add((int)score[6 * i]);
result.labels.Add(labels[(int)score[6 * i]]);
result.bboxs.Add(new Rect((int)score[6 * i + 2], (int)score[6 * i + 3],
(int)(score[6 * i + 4] - score[6 * i + 2]),
(int)(score[6 * i + 5] - score[6 * i + 3])));
result.scores.Add(score[6 * i + 1]);
}
}
}
else
{
for (int i = 0; i < 300; ++i)
{
float[] s = new float[80];
for (int j = 0; j < 80; ++j)
{
s[j] = score[80 * i + j];
}
int clsid = argmax(s, 80);
float max_score = sigmoid(s[clsid]);
if (max_score > threshold)
{
result.clsids.Add(clsid);
result.labels.Add(labels[clsid]);
float cx = (float)(bbox[4 * i] * 640.0 / scale_factor[1]);
float cy = (float)(bbox[4 * i + 1] * 640.0 / scale_factor[0]);
float w = (float)(bbox[4 * i + 2] * 640.0 / scale_factor[1]);
float h = (float)(bbox[4 * i + 3] * 640.0 / scale_factor[0]);
result.bboxs.Add(new Rect((int)(cx - w / 2), (int)(cy - h / 2), (int)w, (int)h));
result.scores.Add(max_score);
}
}
}
return result;
}
? 此處對(duì)輸出結(jié)果做一個(gè)解釋,由于我們提供了兩種模型的輸出,此處提供了兩種模型的輸出數(shù)據(jù)處理方式,主要區(qū)別在于是否對(duì)預(yù)測(cè)框進(jìn)行還原以及對(duì)預(yù)測(cè)類別進(jìn)行提取,具體區(qū)別大家可以查看上述代碼。
6. 預(yù)測(cè)結(jié)果展示
? 最后通過上述代碼,我們最終可以直接實(shí)現(xiàn)RT-DETR模型的推理部署,RT-DETR與訓(xùn)練模型采用的是COCO數(shù)據(jù)集,最終我們可以獲取預(yù)測(cè)后的圖像結(jié)果,如圖所示:
? 上圖中展示了RT-DETR模型預(yù)測(cè)結(jié)果,同時(shí),我們對(duì)模型圖里過程中的關(guān)鍵信息以及推理結(jié)果進(jìn)行了打?。?/p>
[INFO] Hello, World!
[INFO] Model path: E:\Model\RT-DETR\RTDETR\rtdetr_r50vd_6x_coco.xml
[INFO] Device name: CPU
[INFO] Inference Model
[INFO] Model name: Model from PaddlePaddle.
[INFO] Input:
[INFO] name: im_shape
[INFO] type: float
[INFO] shape: Shape : {1,2}
[INFO] name: image
[INFO] type: float
[INFO] shape: Shape : {1,3,640,640}
[INFO] name: scale_factor
[INFO] type: float
[INFO] shape: Shape : {1,2}
[INFO] Output:
[INFO] name: reshape2_95.tmp_0
[INFO] type: float
[INFO] shape: Shape : {300,6}
[INFO] name: tile_3.tmp_0
[INFO] type: int32_t
[INFO] shape: Shape : {1}
[INFO] Infer result:
[INFO] class_id : 0, label : person, confidence : 0.9437, left_top : [504.0, 504.0], right_bottom: [596.0, 429.0]
[INFO] class_id : 0, label : person, confidence : 0.9396, left_top : [414.0, 414.0], right_bottom: [506.0, 450.0]
[INFO] class_id : 0, label : person, confidence : 0.8740, left_top : [162.0, 162.0], right_bottom: [197.0, 265.0]
[INFO] class_id : 0, label : person, confidence : 0.8715, left_top : [267.0, 267.0], right_bottom: [298.0, 267.0]
[INFO] class_id : 0, label : person, confidence : 0.8663, left_top : [327.0, 327.0], right_bottom: [346.0, 127.0]
[INFO] class_id : 0, label : person, confidence : 0.8593, left_top : [576.0, 576.0], right_bottom: [611.0, 315.0]
[INFO] class_id : 0, label : person, confidence : 0.8578, left_top : [104.0, 104.0], right_bottom: [126.0, 148.0]
[INFO] class_id : 0, label : person, confidence : 0.8272, left_top : [363.0, 363.0], right_bottom: [381.0, 180.0]
[INFO] class_id : 0, label : person, confidence : 0.8183, left_top : [349.0, 349.0], right_bottom: [365.0, 155.0]
[INFO] class_id : 0, label : person, confidence : 0.8167, left_top : [378.0, 378.0], right_bottom: [394.0, 132.0]
[INFO] class_id : 56, label : chair, confidence : 0.6448, left_top : [98.0, 98.0], right_bottom: [118.0, 250.0]
[INFO] class_id : 56, label : chair, confidence : 0.6271, left_top : [75.0, 75.0], right_bottom: [102.0, 245.0]
[INFO] class_id : 24, label : backpack, confidence : 0.6196, left_top : [64.0, 64.0], right_bottom: [84.0, 243.0]
[INFO] class_id : 0, label : person, confidence : 0.6016, left_top : [186.0, 186.0], right_bottom: [199.0, 97.0]
[INFO] class_id : 0, label : person, confidence : 0.5715, left_top : [169.0, 169.0], right_bottom: [178.0, 95.0]
[INFO] class_id : 33, label : kite, confidence : 0.5623, left_top : [162.0, 162.0], right_bottom: [614.0, 539.0]
7. 平臺(tái)推理時(shí)間測(cè)試
? 為了評(píng)價(jià)不同平臺(tái)的模型推理性能,在C++、C#平臺(tái)分別部署了RT-DETR不同Backbone結(jié)構(gòu)的模型,如下表所示:
? 通過該表可以看出,不同Backbone結(jié)構(gòu)的RT-DETR模型在C++、C#不同平臺(tái)上所表現(xiàn)出來的模型推理性能基本一致,說明我們所推出的OpenVINO C# API 對(duì)模型推理性能并沒有產(chǎn)生較大的影響。下圖為模型推理時(shí)CPU使用以及內(nèi)存占用情況,可以看出在本機(jī)設(shè)備上,模型部署時(shí)CPU占用在40%~45%左右,內(nèi)存穩(wěn)定在10G左右,所測(cè)試結(jié)果CPU以及內(nèi)存占用未減去其他軟件開銷。
8. 總結(jié)
? 在本項(xiàng)目中,我們介紹了OpenVINO C# API 部署RT-DETR模型的案例,并結(jié)合該模型的處理方式封裝完整的代碼案例,實(shí)現(xiàn)了在 Intel 平臺(tái)使用OpenVINO C# API加速深度學(xué)習(xí)模型,有助于大家以后落地RT-DETR模型在工業(yè)上的應(yīng)用。
? 最后我們對(duì)比了不同Backbone結(jié)構(gòu)的RT-DETR模型在C++、C#不同平臺(tái)上所表現(xiàn)出來的模型推理性能,在C++與C#平臺(tái)上,OpenVINO所表現(xiàn)出的性能基本一致。但在CPU平臺(tái)下,RT-DETR模型推理時(shí)間依舊達(dá)不到理想效果,后續(xù)我們會(huì)繼續(xù)研究該模型的量化技術(shù),通過量化技術(shù)提升模型的推理速度。文章來源:http://www.zghlxwxcb.cn/news/detail-817783.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-817783.html
到了這里,關(guān)于【OpenVINO】基于 OpenVINO C# API 部署 RT-DETR 模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!