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

FastDeploy部署教程(CSharp版本)

1. FastDeploy介绍

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

深度学习部署:FastDeploy部署教程(CSharp版本)_第1张图片

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安装到先前指定的目录步骤⑤。

    深度学习部署:FastDeploy部署教程(CSharp版本)_第2张图片

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. 结果预览

深度学习部署:FastDeploy部署教程(CSharp版本)_第3张图片

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();
}

你可能感兴趣的:(Paddle,深度学习)