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-854738.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-854738.html

到了這里,關(guān)于【TensorRT】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)!