深度学习部署:FastDeploy部署教程(CSharp版本)

本文详细介绍了FastDeploy的C#部署教程,涉及C++SDK配置、C#项目创建、目标检测和语义分割示例,展示了如何使用FastDeploy在C#环境中进行AI推理和模型部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FastDeploy部署教程(CSharp版本)

1. FastDeploy介绍

FastDeploy是一款全场景易用灵活极致高效的AI推理部署工具, 支持云边端部署。提供超过 🔥160+ TextVisionSpeech跨模态模型📦开箱即用的部署体验,并实现🔚端到端的推理性能优化。包括 物体检测字符识别(OCR)人脸人像扣图多目标跟踪系统NLPStable Diffusion文图生成TTS 等几十种任务场景,满足开发者多场景、多硬件、多平台的产业部署需求。

在这里插入图片描述

2. FastDeploy 部署(C++)

参考:

FastDeploy 安装教程

3. FastDeploy 部署(CSharp)

FastDeploy C# SDK的接口实现,为用户需要C# API的场景提供解决方案。

3.1 FastDeploy 源码编译

  1. CMAKE_CONFIGURATION_TYPES属性设置为Release

  2. 请不要勾选WITH_GPUENABLE_TRT_BACKEND

  3. 开启

    ENABLE_ORT_BACKEND=ON
    ENABLE_PADDLE_BACKEND=ON 
    ENABLE_OPENVINO_BACKEND=ON 
    ENABLE_VISION=ON 
    ENABLE_TEXT=ON 
    WITH_CAPI=ON 
    WITH_CSHARPAPI=ON 
    
  4. 指定CMAKE_INSTALL_PREFIX 安装路径

  5. 生成fastdeploy.sln解决方案文件选择Release版本,生成编译,点击"INSTALL"->右键点击"生成"将编译好的SDK安装到先前指定的目录步骤⑤。

    在这里插入图片描述

3.2 创建CSharp项目

  1. 在项目引用中添加–>fastdeploy_csharp;

  2. nuget 包添加OpenCvSharp、OpenCvSharp.Extensions等包

  3. 在debug\relese项目文件夹中添加Fastdeploy的动态链接库;

fastdeploy_init.bat install %cd% D:\workSpace\Paddle\deploy\FastDeplyCsharp\FastDeplyCsharp\bin\x64\Debug
  1. 结果预览

在这里插入图片描述

在这里插入图片描述

3.3 源代码

//目标检测
public  void fastdeployDetInfer(string image_path)
{
    string model_dir = "D://workSpace//Paddle//models//ppyoloe_crn_l_300e_coco";

    string model_file = model_dir + "\\" + "model.pdmodel";
    string params_file = model_dir + "\\" + "model.pdiparams";
    string config_file = model_dir + "\\" + "infer_cfg.yml";
    RuntimeOption runtimeoption = new RuntimeOption();
    int device_option = 0;
    if (device_option == 0)
    {
        runtimeoption.UseCpu();
    }
    else
    {
        runtimeoption.UseGpu();
    }
    fastdeploy.vision.detection.PPYOLOE model = new fastdeploy.vision.detection.PPYOLOE(model_file, params_file, config_file, runtimeoption, ModelFormat.PADDLE);
    if (!model.Initialized())
    {
        Console.WriteLine("Failed to initialize.\n");
    }
    Mat image = Cv2.ImRead(image_path);
    fastdeploy.vision.DetectionResult res = model.Predict(image);
    //Console.WriteLine(res.ToString());
    Mat res_img = fastdeploy.vision.Visualize.VisDetection(image, res, 0.5f, 1, 0.5f);
    InferPictureBox.Image = res_img.ToBitmap();
}
// 语义分割
public void fastdeplySegInfer(string image_path)
{
    string model_dir = "D://workSpace//Paddle//models//PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer";

    string model_file = model_dir + "\\" + "model.pdmodel";
    string params_file = model_dir + "\\" + "model.pdiparams";
    string config_file = model_dir + "\\" + "deploy.yaml";
    RuntimeOption runtimeoption = new RuntimeOption();
    runtimeoption.UseCpu();


    fastdeploy.vision.segmentation.PaddleSegModel model = new fastdeploy.vision.segmentation.PaddleSegModel(model_file, params_file, config_file, runtimeoption, ModelFormat.PADDLE);

    if (!model.Initialized())
    {
        Console.WriteLine("Failed to initialize.\n");
    }
    Mat image = Cv2.ImRead(image_path);

    fastdeploy.vision.SegmentationResult res = model.Predict(image);


    Console.WriteLine(res.ToString());
    Mat res_img = fastdeploy.vision.Visualize.VisSegmentation(image, res, 0.5f);
    InferPictureBox.Image = res_img.ToBitmap();
}
### TensorRT C# 部署教程概述 TensorRT 是 NVIDIA 提供的一个高性能深度学习推理优化器和运行时库,能够显著加速神经网络模型的推理速度。对于希望在 .NET 平台中利用 TensorRT 进行高效推理的应用开发者来说,C# 接口的支持显得尤为重要。 以下是关于如何在 C# 中部署 TensorRT 的相关内容: #### 开发环境准备 在开始之前,需确保开发环境中已安装必要的依赖项。具体如下: - **操作系统**: Windows 11 (推荐)[^3]。 - **CUDA 版本**: CUDA 11.4。 - **cuDNN 版本**: cuDNN 8.2.4。 - **TensorRT 版本**: TensorRT 8.4.0.6。 - **OpenCV 和 OpenCvSharp**: OpenCV 4.5.5 及其对应的 C# 绑定包 OpenCvSharp4。 - **IDE**: Visual Studio 2022。 - **C# 框架**: .NET 6.0。 这些工具链的选择取决于目标平台的需求和支持情况。 #### TensorRT C# API 封装与使用 目前已有针对 TensorRT 的 C# API 封装工作完成,最新版本为 2.0[^1]。此版本重新设计了推理接口,简化了用户的调用逻辑,并通过实际案例演示了分类模型的部署过程。以下是一个简单的代码示例展示如何加载预训练模型并执行推理操作: ```csharp using System; using TensorRT; class Program { static void Main(string[] args) { // 初始化 Logger 对象用于调试日志记录 ILogger logger = new Logger(Severity.kINFO); // 创建 Builder 实例构建引擎 IBuilder builder = CreateInferBuilder(logger); IBuilderConfig config = builder.CreateBuilderConfig(); // 加载序列化后的计划文件 (.plan 文件) using (IRuntime runtime = CreateInferRuntime(logger)) { byte[] planFileContent = File.ReadAllBytes("model.plan"); ICudaEngine engine = runtime.DeserializeCudaEngine(planFileContent); // 执行推理... IntPtr contextPtr = engine.CreateExecutionContext().GetPointer(); Console.WriteLine($"Execution Context Created at Pointer: {contextPtr}"); } } public class Logger : ILogger { private Severity _reportableSeverity; public Logger(Severity reportableSeverity) { this._reportableSeverity = reportableSeverity; } public void Log(Severity severity, string msg) { if ((int)this._reportableSeverity <= (int)severity) Console.WriteLine(msg); } } } ``` 上述代码片段展示了如何初始化 TensorRT 引擎、读取 `.plan` 文件以及创建上下文对象来执行推理任务。 #### 库路径配置 为了使程序顺利找到所需的动态链接库(DLL),需要正确设置项目的库路径。通常情况下,这包括以下几个目录中的 DLL 文件: - `D:/Csharp_deploy/paddle` - `D:/DevelopmentSoft/CUDA-10.2/lib/x64` - `D:/Csharp_deploy/opencv` - `D:/Csharp_deploy/TensorRT-7.0.0.11`[^2] 注意:如果使用的 TensorRT 或其他组件版本较新,则应更新至对应的新版路径。 #### 常见问题处理 当尝试将 ONNX 格式的模型导入到 TensorRT 中时,可能会遇到输入节点名称不匹配等问题。例如,在某些场景下,直接指定标准字符串数组可能无法正常传递给底层函数。此时可参考类似 ONNX Runtime 的做法调整命名规则[^4]。 另外需要注意的是,虽然 FastDeploy 支持多种硬件架构上的 YOLO 系列算法部署[^5],但它主要面向 Python 和 C++ 用户群体提供便利功能。因此若想完全依靠纯 C# 完成端到端解决方案的话,仍建议优先考虑原生 TensorRT 方法。 ---
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热衷技术的尼古拉斯

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值