1. 項(xiàng)目介紹
?? NVIDIA? TensorRT? 是一款用于高性能深度學(xué)習(xí)推理的 SDK,包括深度學(xué)習(xí)推理優(yōu)化器和運(yùn)行時(shí),可為推理應(yīng)用程序提供低延遲和高吞吐量?;?NVIDIA TensorRT 的應(yīng)用程序在推理過程中的執(zhí)行速度比純 CPU 平臺(tái)快 36 倍,使您能夠優(yōu)化在所有主要框架上訓(xùn)練的神經(jīng)網(wǎng)絡(luò)模型,以高精度校準(zhǔn)低精度,并部署到超大規(guī)模數(shù)據(jù)中心、嵌入式平臺(tái)或汽車產(chǎn)品平臺(tái)。
? TensorRT 基于 NVIDIA CUDA? 并行編程模型構(gòu)建,使您能夠在 NVIDIA GPU 上使用量化、層和張量融合、內(nèi)核調(diào)整等技術(shù)來優(yōu)化推理。TensorRT 提供 INT8 使用量化感知訓(xùn)練和訓(xùn)練后量化和浮點(diǎn) 16 (FP16) 優(yōu)化,用于部署深度學(xué)習(xí)推理應(yīng)用程序,例如視頻流、推薦、欺詐檢測和自然語言處理。低精度推理可顯著降低延遲,這是許多實(shí)時(shí)服務(wù)以及自主和嵌入式應(yīng)用所必需的。TensorRT 與 PyTorch 和 TensorFlow 集成,因此只需一行代碼即可實(shí)現(xiàn) 6 倍的推理速度。TensorRT 提供了一個(gè) ONNX 解析器,因此您可以輕松地將 ONNX 模型從常用框架導(dǎo)入 TensorRT。它還與 ONNX 運(yùn)行時(shí)集成,提供了一種以 ONNX 格式實(shí)現(xiàn)高性能推理的簡單方法。
? 基于這些優(yōu)勢,TensorRT目前在深度模型部署應(yīng)用越來越廣泛。但是TensorRT目前只提供了C++與Python接口,對(duì)于跨語言使用十分不便。目前C#語言已經(jīng)成為當(dāng)前編程語言排行榜上前五的語言,也被廣泛應(yīng)用工業(yè)軟件開發(fā)中。為了能夠?qū)崿F(xiàn)在C#中調(diào)用TensorRT部署深度學(xué)習(xí)模型,我們在之前的開發(fā)中開發(fā)了TensorRT C# API。雖然實(shí)現(xiàn)了該接口,但由于數(shù)據(jù)傳輸存在問題,當(dāng)時(shí)開發(fā)的版本在應(yīng)用時(shí)存在較大的問題。
? 基于此,我們開發(fā)了TensorRT C# API 2.0版本,該版本在開發(fā)時(shí)充分考慮了上一版本應(yīng)用時(shí)出現(xiàn)的問題,并進(jìn)行了改進(jìn)。為了更加方便開發(fā)者使用,在本次更新中增加了對(duì)動(dòng)態(tài)輸入模型的支持,將在本技術(shù)文中詳細(xì)介紹本次更新內(nèi)容以及應(yīng)用案例。
- TensorRT C# API 項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API.git
- TensorRT C# API 項(xiàng)目應(yīng)用源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples.git
2. 更新回顧
? 由于該項(xiàng)目目前還沒有完全開發(fā)完成,為了更好的方便大家使用,因此會(huì)在最新更新后提供給大家最新的資訊。如果大家在使用時(shí)有任何疑問,可以閱讀之前發(fā)布的技術(shù)博客:
-
技術(shù)博客一:《最新發(fā)布!TensorRT C# API :基于C#與TensorRT部署深度學(xué)習(xí)模型》
分享了更新版的TensorRT C# API 相關(guān)信息,并對(duì)擴(kuò)展接口進(jìn)行改進(jìn),優(yōu)化哦了模型推理數(shù)據(jù)加載方式,很大程度上壓縮了數(shù)據(jù)處理時(shí)間;同時(shí)分型了最新版本的詳細(xì)使用流程,以及提供了配套的使用案例,方便開發(fā)者們進(jìn)行使用。
3. 動(dòng)態(tài)輸入模型支持
? 在上一版本中,支持了多Bath推理,單其實(shí)現(xiàn)方式是導(dǎo)出的推理模型是多Bath的,因此模型推理的Bath是不可更改的。但是目前TensorRT已經(jīng)支持了動(dòng)態(tài)模型輸入,所以更新了對(duì)動(dòng)態(tài)輸入模型的支持。下面將對(duì)更新的API接口以及推理流程進(jìn)行簡單的介紹:
3.1 新增API
-
public static void OnnxToEngine(string modelPath, int memorySize, string nodeName, Dims minShapes, Dims optShapes, Dims maxShapes)
- 模型轉(zhuǎn)換接口:可以調(diào)用封裝的TensorRT中的ONNX 解釋器,對(duì)ONNX模型進(jìn)行轉(zhuǎn)換,并根據(jù)本機(jī)設(shè)備信息,編譯本地模型,將模型轉(zhuǎn)換為TensorRT 支持的engine格式,該接口支持動(dòng)態(tài)輸入模型。
- string modelPath: 本地ONNX模型地址,只支持ONNX格式,且ONNX模型必須為確定的輸入輸出,暫不支持動(dòng)態(tài)輸入。
- int memorySize: 模型轉(zhuǎn)換時(shí)分配的內(nèi)存大小。
- string nodeName: 模型輸入節(jié)點(diǎn)名稱,該節(jié)點(diǎn)維度確定但是形狀是動(dòng)態(tài)的,一般為: [-1, 3 640, 640],某一維度或其中幾個(gè)維度大小為“-1”。
- Dims minShapes: 動(dòng)態(tài)尺寸的最小允許值
- Dims optShapes: 優(yōu)化(內(nèi)核選擇)中使用的值、動(dòng)態(tài)尺寸的最優(yōu)值
- Dims maxShapes: 動(dòng)態(tài)尺寸等的最大允許值
-
public Nvinfer(string modelPath, int maxBatahSize)
-
Nvinfer 初始化接口: 初始化Nvinfer類,主要初始化封裝的推理引擎,該推理引擎中封裝了比較重要的一些類和指針。
-
string modelPath: engine模型路徑。
-
int maxBatahSize: 推理推理支持的最大的Bath。
-
-
public void SetBindingDimensions(int index, Dims dims)/SetBindingDimensions(string nodeName, Dims dims)
- 設(shè)置節(jié)點(diǎn)維度接口: 通過端口編號(hào)或者端口名稱,獲取綁定的端口的形狀信息。
- int index: 綁定端口的編號(hào)。
- string nodeName: 綁定端口的名稱。
- Dims dims: 需要設(shè)置綁定端口的維度。
3.2 推理流程
? 對(duì)于固定輸入模型的推理流程,主要包括以下四個(gè)步驟:
- Nvinfer初始化
- 加載推理數(shù)據(jù)
- 模型推理
- 獲取推理結(jié)果
? 而當(dāng)我們使用動(dòng)態(tài)輸入模型時(shí),其推理流程發(fā)生了變化,如下圖所示:
? 當(dāng)部署動(dòng)態(tài)輸入模型時(shí),推理流程為:
- Nvinfer初始化
- 設(shè)置本次推理模型輸入大小
- 加載推理數(shù)據(jù)
- 模型推理
- 獲取推理結(jié)果
? 與常規(guī)的規(guī)定輸入模型的推理流程相比,主要是增加了設(shè)置本次推理模型輸入大小這一步,其他步驟并未發(fā)生較大的變化。此外,如果下一次推理輸入數(shù)據(jù)形狀大小發(fā)生了改變,就需要重新進(jìn)行設(shè)置,如果輸入形狀大小并未對(duì)發(fā)生變化,則無需進(jìn)行再次設(shè)置。
4. 接口應(yīng)用
? 關(guān)于該項(xiàng)目的調(diào)用方式在上一篇文章中已經(jīng)進(jìn)行了詳細(xì)介紹,具體使用可以參考《最新發(fā)布!TensorRT C# API :基于C#與TensorRT部署深度學(xué)習(xí)模型》,下面結(jié)合Yolov8-cls模型詳細(xì)介紹一下更新的接口使用方法。
4.1 創(chuàng)建并配置C#項(xiàng)目
? 首先創(chuàng)建一個(gè)簡單的C#項(xiàng)目,然后添加項(xiàng)目配置。
? 首先是添加TensorRT C# API 項(xiàng)目引用,如下圖所示,添加上文中C#項(xiàng)目生成的dll文件即可。
? 接下來添加OpenCvSharp,此處通過NuGet Package安裝即可,此處主要安裝以下兩個(gè)程序包即可:
? 配置好項(xiàng)目后,項(xiàng)目的配置文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>TensorRT_CSharp_API_demo</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.9.0.20240103" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.9.0.20240103" />
</ItemGroup>
<ItemGroup>
<Reference Include="TensorRtSharp">
<HintPath>E:\GitSpace\TensorRT-CSharp-API\src\TensorRtSharp\bin\Release\net6.0\TensorRtSharp.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
4.2 添加推理代碼
? 此處演示一個(gè)簡單的圖像分類項(xiàng)目,以Yolov8-cls項(xiàng)目為例:
(1) 轉(zhuǎn)換engine模型
? 動(dòng)態(tài)輸入的模型在進(jìn)行格式轉(zhuǎn)換時(shí),需要指定模型推理形狀至此的范圍,minShapes
表示模型推理支持的最小形狀,optShapes
表示模型推理支持的最佳形狀,maxShapes
表示模型推理支持的最大形狀,模型轉(zhuǎn)換需要消耗較多時(shí)間,最終轉(zhuǎn)換成功后會(huì)在模型同級(jí)目錄下生成相同名字的.engine
文件。
Dims minShapes = new Dims(1, 3, 224, 224);
Dims optShapes = new Dims(10, 3, 224, 224);
Dims maxShapes = new Dims(20, 3, 224, 224);
Nvinfer.OnnxToEngine(onnxPath, 20, "images", minShapes, optShapes, maxShapes);
(2) 定義模型預(yù)測方法
? 下面代碼是定義的Yolov8-cls模型的預(yù)測方法,該方法支持動(dòng)態(tài)Bath輸入模型推理,可以根據(jù)用戶輸入圖片數(shù)量,自動(dòng)設(shè)置輸入Bath,然后進(jìn)行推理。
? 下面代碼與上一篇文章中的代碼差異主要是增加了predictor.SetBindingDimensions("images", new Dims(batchNum, 3, 224, 224));
這一句代碼。同時(shí)在初始化時(shí),設(shè)置最大支持20Bath,這與上文模型轉(zhuǎn)換時(shí)設(shè)置的一致。
public class Yolov8Cls
{
public Dims InputDims;
public int BatchNum;
private Nvinfer predictor;
public Yolov8Cls(string enginePath)
{
predictor = new Nvinfer(enginePath, 20);
InputDims = predictor.GetBindingDimensions("images");
}
public void Predict(List<Mat> images)
{
BatchNum = images.Count;
for (int begImgNo = 0; begImgNo < images.Count; begImgNo += BatchNum)
{
DateTime start = DateTime.Now;
int endImgNo = Math.Min(images.Count, begImgNo + BatchNum);
int batchNum = endImgNo - begImgNo;
List<Mat> normImgBatch = new List<Mat>();
int imageLen = 3 * 224 * 224;
float[] inputData = new float[BatchNum * imageLen];
for (int ino = begImgNo; ino < endImgNo; ino++)
{
Mat input_mat = CvDnn.BlobFromImage(images[ino], 1.0 / 255.0, new OpenCvSharp.Size(224, 224), 0, true, false);
float[] data = new float[imageLen];
Marshal.Copy(input_mat.Ptr(0), data, 0, imageLen);
Array.Copy(data, 0, inputData, ino * imageLen, imageLen);
}
predictor.SetBindingDimensions("images", new Dims(batchNum, 3, 224, 224));
predictor.LoadInferenceData("images", inputData);
DateTime end = DateTime.Now;
Console.WriteLine("[ INFO ] Input image data processing time: " + (end - start).TotalMilliseconds + " ms.");
predictor.infer();
start = DateTime.Now;
predictor.infer();
end = DateTime.Now;
Console.WriteLine("[ INFO ] Model inference time: " + (end - start).TotalMilliseconds + " ms.");
start = DateTime.Now;
float[] outputData = predictor.GetInferenceResult("output0");
for (int i = 0; i < batchNum; ++i)
{
Console.WriteLine(string.Format("[ INFO ] Classification Top {0} result : ", 2));
float[] data = new float[1000];
Array.Copy(outputData, i * 1000, data, 0, 1000);
List<int> sortResult = Argsort(new List<float>(data));
for (int j = 0; j < 2; ++j)
{
string msg = "";
msg += ("index: " + sortResult[j] + "\t");
msg += ("score: " + data[sortResult[j]] + "\t");
Console.WriteLine("[ INFO ] " + msg);
}
}
end = DateTime.Now;
Console.WriteLine("[ INFO ] Inference result processing time: " + (end - start).TotalMilliseconds + " ms.\n");
}
}
public static List<int> Argsort(List<float> array)
{
int arrayLen = array.Count;
List<float[]> newArray = new List<float[]> { };
for (int i = 0; i < arrayLen; i++)
{
newArray.Add(new float[] { array[i], i });
}
newArray.Sort((a, b) => b[0].CompareTo(a[0]));
List<int> arrayIndex = new List<int>();
foreach (float[] item in newArray)
{
arrayIndex.Add((int)item[1]);
}
return arrayIndex;
}
}
(3) 預(yù)測方法調(diào)用
? 下面是上述定義的預(yù)測方法,為了測試不同Bath性能,此處讀取了多張圖片,并分別預(yù)測不同張數(shù)圖片,如下所示:
Yolov8Cls yolov8Cls = new Yolov8Cls("E:\\Model\\yolov8\\yolov8s-cls_b.engine");
Mat image1 = Cv2.ImRead("E:\\ModelData\\image\\demo_4.jpg");
Mat image2 = Cv2.ImRead("E:\\ModelData\\image\\demo_5.jpg");
Mat image3 = Cv2.ImRead("E:\\ModelData\\image\\demo_6.jpg");
Mat image4 = Cv2.ImRead("E:\\ModelData\\image\\demo_7.jpg");
Mat image5 = Cv2.ImRead("E:\\ModelData\\image\\demo_8.jpg");
yolov8Cls.Predict(new List<Mat> { image1, image2 });
yolov8Cls.Predict(new List<Mat> { image1, image2, image3 });
yolov8Cls.Predict(new List<Mat> { image1, image2, image3, image4 });
yolov8Cls.Predict(new List<Mat> { image1, image2, image3, image4, image5 });
4.3 項(xiàng)目演示
? 配置好項(xiàng)目并編寫好代碼后,運(yùn)行該項(xiàng)目,項(xiàng)目輸出如下所示:
[ INFO ] Input image data processing time: 5.5277 ms.
[ INFO ] Model inference time: 1.3685 ms.
[ INFO ] Classification Top 2 result :
[ INFO ] index: 386 score: 0.8754883
[ INFO ] index: 385 score: 0.08013916
[ INFO ] Classification Top 2 result :
[ INFO ] index: 293 score: 0.89160156
[ INFO ] index: 276 score: 0.05480957
[ INFO ] Inference result processing time: 3.0823 ms.
[ INFO ] Input image data processing time: 2.7356 ms.
[ INFO ] Model inference time: 1.4435 ms.
[ INFO ] Classification Top 2 result :
[ INFO ] index: 386 score: 0.8754883
[ INFO ] index: 385 score: 0.08013916
[ INFO ] Classification Top 2 result :
[ INFO ] index: 293 score: 0.89160156
[ INFO ] index: 276 score: 0.05480957
[ INFO ] Classification Top 2 result :
[ INFO ] index: 14 score: 0.99853516
[ INFO ] index: 88 score: 0.0006980896
[ INFO ] Inference result processing time: 1.5137 ms.
[ INFO ] Input image data processing time: 3.7277 ms.
[ INFO ] Model inference time: 1.5285 ms.
[ INFO ] Classification Top 2 result :
[ INFO ] index: 386 score: 0.8754883
[ INFO ] index: 385 score: 0.08013916
[ INFO ] Classification Top 2 result :
[ INFO ] index: 293 score: 0.89160156
[ INFO ] index: 276 score: 0.05480957
[ INFO ] Classification Top 2 result :
[ INFO ] index: 14 score: 0.99853516
[ INFO ] index: 88 score: 0.0006980896
[ INFO ] Classification Top 2 result :
[ INFO ] index: 294 score: 0.96533203
[ INFO ] index: 269 score: 0.0124435425
[ INFO ] Inference result processing time: 2.7328 ms.
[ INFO ] Input image data processing time: 4.063 ms.
[ INFO ] Model inference time: 1.6947 ms.
[ INFO ] Classification Top 2 result :
[ INFO ] index: 386 score: 0.8754883
[ INFO ] index: 385 score: 0.08013916
[ INFO ] Classification Top 2 result :
[ INFO ] index: 293 score: 0.89160156
[ INFO ] index: 276 score: 0.05480957
[ INFO ] Classification Top 2 result :
[ INFO ] index: 14 score: 0.99853516
[ INFO ] index: 88 score: 0.0006980896
[ INFO ] Classification Top 2 result :
[ INFO ] index: 294 score: 0.96533203
[ INFO ] index: 269 score: 0.0124435425
[ INFO ] Classification Top 2 result :
[ INFO ] index: 127 score: 0.9008789
[ INFO ] index: 128 score: 0.07745361
[ INFO ] Inference result processing time: 3.5664 ms.
? 通過上面輸出可以看出,不同Bath模型推理時(shí)間在1.3685~1.6947ms,大大提升了模型的推理速度。
5. 總結(jié)
? 在本項(xiàng)目中,我們擴(kuò)展了TensorRT C# API 接口,使其支持動(dòng)態(tài)輸入模型。并結(jié)合分類模型部署流程向大家展示了TensorRT C# API 的使用方式,方便大家快速上手使用。
? 為了方便各位開發(fā)者使用,此處開發(fā)了配套的演示項(xiàng)目,主要是基于Yolov8開發(fā)的目標(biāo)檢測、目標(biāo)分割、人體關(guān)鍵點(diǎn)識(shí)別、圖像分類以及旋轉(zhuǎn)目標(biāo)識(shí)別,并且支持動(dòng)態(tài)輸入模型,用戶可以同時(shí)推理任意張圖像。
- Yolov8 Det 目標(biāo)檢測項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples/blob/master/model_samples/yolov8_custom_dynamic/Yolov8Det.cs
- Yolov8 Seg 目標(biāo)分割項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples/blob/master/model_samples/yolov8_custom_dynamic/Yolov8Seg.cs
- Yolov8 Pose 人體關(guān)鍵點(diǎn)識(shí)別項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples/blob/master/model_samples/yolov8_custom_dynamic/Yolov8Pose.cs
- Yolov8 Cls 圖像分類項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples/blob/master/model_samples/yolov8_custom_dynamic/Yolov8Cls.cs
- Yolov8 Obb 旋轉(zhuǎn)目標(biāo)識(shí)別項(xiàng)目源碼:
https://github.com/guojin-yan/TensorRT-CSharp-API-Samples/blob/master/model_samples/yolov8_custom_dynamic/Yolov8Obb.cs
? 同時(shí)對(duì)本項(xiàng)目開發(fā)的案例進(jìn)行了時(shí)間測試,以下時(shí)間只是程序運(yùn)行一次的時(shí)間,測試環(huán)境為:
-
CPU:i7-165G7
-
CUDA型號(hào):12.2
-
Cudnn:8.9.3
-
TensorRT:8.6.1.6文章來源:http://www.zghlxwxcb.cn/news/detail-847532.html
Model | Batch | 數(shù)據(jù)預(yù)處理 (ms) | 模型推理 (ms) | 結(jié)果后處理 (ms) |
---|---|---|---|---|
Yolov8s-Det | 1 | 16.6 | 4.6 | 13.1 |
4 | 38.0 | 12.4 | 32.4 | |
8 | 70.5 | 23.0 | 80.1 | |
Yolov8s-Obb | 1 | 28.7 | 8.9 | 17.7 |
4 | 81.7 | 25.9 | 67.4 | |
8 | 148.4 | 44.6 | 153.0 | |
Yolov8s-Seg | 1 | 15.4 | 5.4 | 67.4 |
4 | 37.3 | 15.5 | 220.6 | |
8 | 78.7 | 26.9 | 433.6 | |
Yolov8s-Pose | 1 | 15.1 | 5.2 | 8.7 |
4 | 39.2 | 13.2 | 14.3 | |
8 | 67.8 | 23.1 | 27.7 | |
Yolov8s-Cls | 1 | 9.9 | 1.3 | 1.9 |
4 | 14.7 | 1.5 | 2.3 | |
8 | 22.6 | 2.0 | 2.9 |
? 最后如果各位開發(fā)者在使用中有任何問題,歡迎大家與我聯(lián)系。文章來源地址http://www.zghlxwxcb.cn/news/detail-847532.html
到了這里,關(guān)于TensorRT C# API 項(xiàng)目更新 (1):支持動(dòng)態(tài)Bath輸入模型推理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!