? RT-DETR是在DETR模型基礎(chǔ)上進行改進的,一種基于 DETR 架構(gòu)的實時端到端檢測器,它通過使用一系列新的技術(shù)和算法,實現(xiàn)了更高效的訓(xùn)練和推理,在前文我們發(fā)表了《基于 OpenVINO? Python API 部署 RT-DETR 模型 | 開發(fā)者實戰(zhàn)》,在該文章中,我們基于OpenVINO? Python API 向大家展示了包含后處理的RT-DETR模型的部署流程,但在實際工業(yè)應(yīng)用中,我們?yōu)榱伺c當(dāng)前軟件平臺集成更多會采用C++平臺,因此在本文中,我們將基于OpenVINO? C++ API 向大家展示了不包含后處理的RT-DETR模型的部署流程,并向大家展示如何導(dǎo)出不包含后處理的RT-DETR模型。
? 該項目所使用的全部代碼已經(jīng)在GitHub上開源,并且收藏在OpenVINO-CSharp-API項目里,項目所在目錄鏈接為:
https://github.com/guojin-yan/OpenVINO-CSharp-API/tree/csharp3.0/tutorial_examples
? 也可以直接訪問該項目,項目鏈接為:
https://github.com/guojin-yan/RT-DETR-OpenVINO.git
項目首發(fā)網(wǎng)址為:基于 OpenVINO? C++ API 部署 RT-DETR 模型 | 開發(fā)者實戰(zhàn)
1. RT-DETR
? 飛槳在去年 3 月份推出了高精度通用目標(biāo)檢測模型 PP-YOLOE ,同年在 PP-YOLOE 的基礎(chǔ)上提出了 PP-YOLOE+。而繼 PP-YOLOE 提出后,MT-YOLOv6、YOLOv7、DAMO-YOLO、RTMDet 等模型先后被提出,一直迭代到今年開年的 YOLOv8。
? YOLO 檢測器有個較大的待改進點是需要 NMS 后處理,其通常難以優(yōu)化且不夠魯棒,因此檢測器的速度存在延遲。DETR是一種不需要 NMS 后處理、基于 Transformer 的端到端目標(biāo)檢測器。百度飛槳正式推出了——RT-DETR (Real-Time DEtection TRansformer) ,一種基于 DETR 架構(gòu)的實時端到端檢測器,其在速度和精度上取得了 SOTA 性能。
? RT-DETR是在DETR模型基礎(chǔ)上進行改進的,它通過使用一系列新的技術(shù)和算法,實現(xiàn)了更高效的訓(xùn)練和推理。具體來說,RT-DETR具有以下優(yōu)勢:
- 1、實時性能更佳:RT-DETR采用了一種新的注意力機制,能夠更好地捕獲物體之間的關(guān)系,并減少計算量。此外,RT-DETR還引入了一種基于時間的注意力機制,能夠更好地處理視頻數(shù)據(jù)。
- 2、精度更高:RT-DETR在保證實時性能的同時,還能夠保持較高的檢測精度。這主要得益于RT-DETR引入的一種新的多任務(wù)學(xué)習(xí)機制,能夠更好地利用訓(xùn)練數(shù)據(jù)。
- 3、更易于訓(xùn)練和調(diào)參:RT-DETR采用了一種新的損失函數(shù),能夠更好地進行訓(xùn)練和調(diào)參。此外,RT-DETR還引入了一種新的數(shù)據(jù)增強技術(shù),能夠更好地利用訓(xùn)練數(shù)據(jù)。
2. OpenVINO
? 英特爾發(fā)行版 OpenVINO?工具套件基于oneAPI 而開發(fā),可以加快高性能計算機視覺和深度學(xué)習(xí)視覺應(yīng)用開發(fā)速度工具套件,適用于從邊緣到云的各種英特爾平臺上,幫助用戶更快地將更準(zhǔn)確的真實世界結(jié)果部署到生產(chǎn)系統(tǒng)中。通過簡化的開發(fā)工作流程, OpenVINO?可賦能開發(fā)者在現(xiàn)實世界中部署高性能應(yīng)用程序和算法。
? OpenVINO? 2023.1于2023年9月18日發(fā)布,該工具包帶來了挖掘生成人工智能全部潛力的新功能。生成人工智能的覆蓋范圍得到了擴展,通過PyTorch*等框架增強了體驗,您可以在其中自動導(dǎo)入和轉(zhuǎn)換模型。大型語言模型(LLM)在運行時性能和內(nèi)存優(yōu)化方面得到了提升。聊天機器人、代碼生成等的模型已啟用。OpenVINO更便攜,性能更高,可以在任何需要的地方運行:在邊緣、云中或本地。
3. 環(huán)境配置
? 在上一篇文章中我們以已經(jīng)向大家提供了RT-DETR模型導(dǎo)出蘇需要的環(huán)境,此處不再多做展示,為了大家更好的復(fù)現(xiàn)該項目代碼,此處向大家提供本次開發(fā)所使用的C++環(huán)境:
openvino: 2023.1.0
opencv: 4.5.5
? 大家在復(fù)現(xiàn)代碼時可以使用相同的環(huán)境或者與作者所使用環(huán)境發(fā)布較為接近的環(huán)境進行開發(fā),防止使用時出現(xiàn)不必要的錯誤;此外該項目提供了兩種編譯方式,大家可以使用Visual Studio進行編譯以及CMake進行編譯。
4. 模型下載與轉(zhuǎn)換
? 在上一篇文章中我們已經(jīng)向大家展示了RT-DETR預(yù)訓(xùn)練模型的導(dǎo)出方式,該模型是默認包含后處理的;因此在本文中,我們將向大家展示不包含后處理的RT-DETR模型導(dǎo)出方式以及兩種模型的差異。
4.1 模型導(dǎo)出
? PaddleDetection官方庫向我們提供了十分友好API接口,因此導(dǎo)出不包含后處理的RT-DETR模型也是十分容易的。首先修改配置文件,主要是修改RT-DETR模型的配置文件,配置文件路徑為:.\PaddleDetection\configs\rtdetr\_base_\rtdetr_r50vd.yml
,在配置文件DETR項目下增加exclude_post_process: True語句,如下圖所示:
? 然后重新運行模型導(dǎo)出指令,便可以獲取不包含后處理的模型:
python tools/export_model.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams trt=True --output_dir=output_inference
? 在模型導(dǎo)出后,我們可以轉(zhuǎn)換成ONNX格式以及IR格式,可參考上一篇文章中的模型轉(zhuǎn)換內(nèi)容。
4.2 模型信息對比
? 通過下表我們可以看出,裁剪后的模型,只包含一個輸入節(jié)點,其輸出節(jié)點也發(fā)生了變化,原本模型輸出為處理后的預(yù)測輸出,經(jīng)過裁剪后,模型輸出節(jié)點輸出內(nèi)容發(fā)生了較大變化。其中:
-
stack_7.tmp_0.slice_0
:該節(jié)點表示300種預(yù)測結(jié)果的預(yù)測框信息; -
stack_8.tmp_0.slice_0
:該節(jié)點表示300種預(yù)測結(jié)果的80種分類信息置信度,后續(xù)再處理時,需要根據(jù)預(yù)測結(jié)果獲取最終的預(yù)測分類信息。
5. C++代碼實現(xiàn)
? 為了更系統(tǒng)地實現(xiàn)RT-DETR模型的推理流程,我們采用C++特性,封裝了RTDETRPredictor模型推理類以及RTDETRProcess模型數(shù)據(jù)處理類,下面我們將對這兩個類中的關(guān)鍵代碼進行講解。
5.1 模型推理類實現(xiàn)
? C++代碼中我們定義的RTDETRPredictor模型推理類如下所示:
class RTDETRPredictor
{
public:
RTDETRPredictor(std::string model_path, std::string label_path,
std::string device_name = "CPU", bool postprcoess = true);
cv::Mat predict(cv::Mat image);
private:
void pritf_model_info(std::shared_ptr<ov::Model> model);
void fill_tensor_data_image(ov::Tensor& input_tensor, const cv::Mat& input_image);
void fill_tensor_data_float(ov::Tensor& input_tensor, float* input_data, int data_size);
private:
RTDETRProcess rtdetr_process;
bool post_flag;
ov::Core core;
std::shared_ptr<ov::Model> model;
ov::CompiledModel compiled_model;
ov::InferRequest infer_request;
};
-
- 模型推理類初始化
首先我們需要初始化模型推理類,初始化相關(guān)信息:
- 模型推理類初始化
RTDETRPredictor::RTDETRPredictor(std::string model_path, std::string label_path,
std::string device_name, bool post_flag)
:post_flag(post_flag){
INFO("Model path: " + model_path);
INFO("Device name: " + device_name);
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 = RTDETRProcess(cv::Size(640, 640), label_path, 0.5);
}
? 在該方法中主要包含以下幾個輸入:
-
model_path
:推理模型地址; -
label_path
:模型預(yù)測類別文件; -
device_name
:推理設(shè)備名稱; -
post_flag
:模型是否包含后處理,當(dāng)post_flag = true
時,包含后處理,當(dāng)post_flag = false
時,不包含后處理。 -
- 圖片預(yù)測API
這一步中主要是對輸入圖片進行預(yù)測,并將模型預(yù)測結(jié)果會知道輸入圖片上,下面是這階段的主要代碼:
- 圖片預(yù)測API
cv::Mat RTDETRPredictor::predict(cv::Mat image){
cv::Mat blob_image = rtdetr_process.preprocess(image);
if (post_flag) {
ov::Tensor image_tensor = infer_request.get_tensor("image");
ov::Tensor shape_tensor = infer_request.get_tensor("im_shape");
ov::Tensor scale_tensor = infer_request.get_tensor("scale_factor");
image_tensor.set_shape({ 1,3,640,640 });
shape_tensor.set_shape({ 1,2 });
scale_tensor.set_shape({ 1,2 });
fill_tensor_data_image(image_tensor, blob_image);
fill_tensor_data_float(shape_tensor, rtdetr_process.get_input_shape().data(), 2);
fill_tensor_data_float(scale_tensor, rtdetr_process.get_scale_factor().data(), 2);
} else {
ov::Tensor image_tensor = infer_request.get_input_tensor();
fill_tensor_data_image(image_tensor, blob_image);
}
infer_request.infer();
ResultData results;
if (post_flag) {
ov::Tensor output_tensor = infer_request.get_tensor("reshape2_95.tmp_0");
float result[6 * 300] = {0};
for (int i = 0; i < 6 * 300; ++i) {
result[i] = output_tensor.data<float>()[i];
}
results = rtdetr_process.postprocess(result, nullptr, true);
} else {
ov::Tensor score_tensor = infer_request.get_tensor(model->outputs()[1].get_any_name());
ov::Tensor bbox_tensor = infer_request.get_tensor(model->outputs()[0].get_any_name());
float score[300 * 80] = {0};
float bbox[300 * 4] = {0};
for (int i = 0; i < 300; ++i) {
for (int j = 0; j < 80; ++j) {
score[80 * i + j] = score_tensor.data<float>()[80 * i + j];
}
for (int j = 0; j < 4; ++j) {
bbox[4 * i + j] = bbox_tensor.data<float>()[4 * i + j];
}
}
results = rtdetr_process.postprocess(score, bbox, false);
}
return rtdetr_process.draw_box(image, results);
}
? 上述代碼的主要邏輯如下:首先是處理輸入圖片,調(diào)用定義的數(shù)據(jù)處理類,將輸入圖片處理成指定的數(shù)據(jù)類型;然后根據(jù)模型的輸入節(jié)點情況配置模型輸入數(shù)據(jù),如果使用的是動態(tài)模型輸入,需要設(shè)置輸入形狀;接下來就是進行模型推理;最后就是對推理結(jié)果進行處理,并將結(jié)果繪制到輸入圖片上。
5.2 模型數(shù)據(jù)處理類RTDETRProcess
-
- 定義RTDETRProcess
class RTDETRProcess
{
public:
RTDETRProcess() {}
RTDETRProcess(cv::Size target_size, std::string label_path = NULL, float threshold = 0.5,
cv::InterpolationFlags interpf = cv::INTER_LINEAR);
cv::Mat preprocess(cv::Mat image);
ResultData postprocess(float* score, float* bboxs, bool post_flag);
std::vector<float> get_im_shape() { return im_shape; }
std::vector<float> get_input_shape() { return { (float)target_size.width ,(float)target_size.height }; }
std::vector<float> get_scale_factor() { return scale_factor; }
cv::Mat draw_box(cv::Mat image, ResultData results);
private:
void read_labels(std::string label_path);
template<class T>
float sigmoid(T data) { return 1.0f / (1 + std::exp(-data));}
template<class T>
int argmax(T* data, int length) {
std::vector<T> arr(data, data + length);
return (int)(std::max_element(arr.begin(), arr.end()) - arr.begin());
}
private:
cv::Size target_size; // The model input size.
std::vector<std::string> labels; // The model classification label.
float threshold; // The threshold parameter.
cv::InterpolationFlags interpf; // The image scaling method.
std::vector<float> im_shape;
std::vector<float> scale_factor;
};
-
- 輸入數(shù)據(jù)處理方法
cv::Mat RTDETRProcess::preprocess(cv::Mat image){
im_shape = { (float)image.rows, (float)image.cols };
scale_factor = { 640.0f / (float)image.rows, 640.0f / (float)image.cols};
cv::Mat blob_image;
cv::cvtColor(image, blob_image, cv::COLOR_BGR2RGB);
cv::resize(blob_image, blob_image, target_size, 0, 0, cv::INTER_LINEAR);
std::vector<cv::Mat> rgb_channels(3);
cv::split(blob_image, rgb_channels);
for (auto i = 0; i < rgb_channels.size(); i++) {
rgb_channels[i].convertTo(rgb_channels[i], CV_32FC1, 1.0 / 255.0);
}
cv::merge(rgb_channels, blob_image);
return blob_image;
}
-
- 預(yù)測結(jié)果數(shù)據(jù)處理方法
ResultData RTDETRProcess::postprocess(float* score, float* bbox, bool post_flag)
{
ResultData result;
if (post_flag) {
for (int i = 0; i < 300; ++i) {
if (score[6 * i + 1] > threshold) {
result.clsids.push_back((int)score[6 * i ]);
result.labels.push_back(labels[(int)score[6 * i]]);
result.bboxs.push_back(cv::Rect(score[6 * i + 2], score[6 * i + 3],
score[6 * i + 4] - score[6 * i + 2],
score[6 * i + 5] - score[6 * i + 3]));
result.scores.push_back(score[6 * i + 1]);
}
}
} else {
for (int i = 0; i < 300; ++i) {
float s[80];
for (int j = 0; j < 80; ++j) {
s[j] = score[80 * i + j];
}
int clsid = argmax<float>(s, 80);
float max_score = sigmoid<float>(s[clsid]);
if (max_score > threshold) {
result.clsids.push_back(clsid);
result.labels.push_back(labels[clsid]);
float cx = bbox[4 * i] * 640.0 / scale_factor[1];
float cy = bbox[4 * i + 1] * 640.0 / scale_factor[0];
float w = bbox[4 * i + 2] * 640.0 / scale_factor[1];
float h = bbox[4 * i + 3] * 640.0 / scale_factor[0];
result.bboxs.push_back(cv::Rect((int)(cx - w / 2), (int)(cy - h / 2), w, h));
result.scores.push_back(max_score);
}
}
}
return result;
}
? 此處對輸出結(jié)果做一個解釋,由于我們提供了兩種模型的輸出,此處提供了兩種模型的輸出數(shù)據(jù)處理方式,主要區(qū)別在于是否對預(yù)測框進行還原以及對預(yù)測類別進行提取,具體區(qū)別大家可以查看上述代碼。
6. 預(yù)測結(jié)果展示
? 最后通過上述代碼,我們最終可以直接實現(xiàn)RT-DETR模型的推理部署,RT-DETR與訓(xùn)練模型采用的是COCO數(shù)據(jù)集,最終我們可以獲取預(yù)測后的圖像結(jié)果,如圖所示:
? 上圖中展示了RT-DETR模型預(yù)測結(jié)果,同時,我們對模型圖里過程中的關(guān)鍵信息以及推理結(jié)果進行了打?。?/p>
[INFO] This is an RT-DETR model deployment case using C++!
[INFO] Model path: E:\\Model\\RT-DETR\\RTDETR_cropping\\rtdetr_r50vd_6x_coco.onnx
[INFO] Device name: CPU
[INFO] Inference Model
[INFO] Model name: Model from PaddlePaddle.
[INFO] Input:
[INFO] name: image
[INFO] type: float
[INFO] shape: [?,3,640,640]
[INFO] Output:
[INFO] name: stack_7.tmp_0_slice_0
[INFO] type: float
[INFO] shape: [?,300,4]
[INFO] name: stack_8.tmp_0_slice_0
[INFO] type: float
[INFO] shape: [?,300,80]
[INFO] Infer result:
[INFO] class_id : 0, label : person, confidence : 0.928, left_top : [215, 327], right_bottom: [259, 468]
[INFO] class_id : 0, label : person, confidence : 0.923, left_top : [260, 343], right_bottom: [309, 460]
[INFO] class_id : 0, label : person, confidence : 0.893, left_top : [402, 346], right_bottom: [451, 478]
[INFO] class_id : 0, label : person, confidence : 0.796, left_top : [456, 369], right_bottom: [507, 479]
[INFO] class_id : 0, label : person, confidence : 0.830, left_top : [519, 360], right_bottom: [583, 479]
[INFO] class_id : 33, label : kite, confidence : 0.836, left_top : [323, 159], right_bottom: [465, 213]
[INFO] class_id : 33, label : kite, confidence : 0.805, left_top : [329, 64], right_bottom: [388, 85]
[INFO] class_id : 33, label : kite, confidence : 0.822, left_top : [282, 217], right_bottom: [419, 267]
[INFO] class_id : 0, label : person, confidence : 0.834, left_top : [294, 384], right_bottom: [354, 443]
[INFO] class_id : 33, label : kite, confidence : 0.793, left_top : [504, 195], right_bottom: [522, 214]
[INFO] class_id : 33, label : kite, confidence : 0.524, left_top : [233, 22], right_bottom: [242, 29]
[INFO] class_id : 33, label : kite, confidence : 0.763, left_top : [116, 178], right_bottom: [136, 190]
[INFO] class_id : 0, label : person, confidence : 0.601, left_top : [497, 380], right_bottom: [529, 479]
[INFO] class_id : 33, label : kite, confidence : 0.764, left_top : [460, 251], right_bottom: [478, 268]
[INFO] class_id : 33, label : kite, confidence : 0.605, left_top : [176, 236], right_bottom: [256, 257]
[INFO] class_id : 0, label : person, confidence : 0.732, left_top : [154, 380], right_bottom: [210, 420]
[INFO] class_id : 33, label : kite, confidence : 0.574, left_top : [221, 264], right_bottom: [342, 312]
[INFO] class_id : 33, label : kite, confidence : 0.588, left_top : [97, 316], right_bottom: [155, 359]
[INFO] class_id : 33, label : kite, confidence : 0.523, left_top : [171, 317], right_bottom: [227, 357]
[INFO] class_id : 33, label : kite, confidence : 0.657, left_top : [363, 120], right_bottom: [375, 129]
[INFO] class_id : 0, label : person, confidence : 0.698, left_top : [26, 341], right_bottom: [57, 425]
[INFO] class_id : 33, label : kite, confidence : 0.798, left_top : [242, 124], right_bottom: [263, 135]
[INFO] class_id : 33, label : kite, confidence : 0.528, left_top : [218, 178], right_bottom: [451, 241]
[INFO] class_id : 33, label : kite, confidence : 0.685, left_top : [430, 29], right_bottom: [449, 43]
[INFO] class_id : 33, label : kite, confidence : 0.640, left_top : [363, 120], right_bottom: [375, 129]
[INFO] class_id : 33, label : kite, confidence : 0.559, left_top : [161, 193], right_bottom: [171, 199]
7. 總結(jié)
? 在本項目中,我們介紹了OpenVINO C++ API 部署自帶后處理的RT-DETR模型的案例,并結(jié)合該模型的處理方式封裝完整的代碼案例,實現(xiàn)了在 Intel 平臺使用OpenVINO 加速深度學(xué)習(xí)模型,有助于大家以后落地RT-DETR模型在工業(yè)上的應(yīng)用。文章來源:http://www.zghlxwxcb.cn/news/detail-814443.html
? 在下一篇文章《基于 OpenVINO Python C# 部署 RT-DETR 模型》中,我們將基于C# API接口,實現(xiàn)RT-DETR 模型的部署,并且基于開發(fā)的代碼,對比不同平臺的推理速度。如果大家有興趣,可以先關(guān)注本項目代碼倉庫,獲取項目實現(xiàn)源碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-814443.html
到了這里,關(guān)于【OpenVINO】基于 OpenVINO C++ API 部署 RT-DETR 模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!