FastDeploy:PaddleSeg C++部署方式(一)

目录

1.FastDeploy介绍

2. 通过FastDeploy C++ 部署PaddleSeg模型

1.FastDeploy介绍

⚡️FastDeploy是一款全场景易用灵活极致高效的AI推理部署工具, 支持云边端部署。提供超过 🔥160+ TextVision, Speech跨模态模型📦开箱即用的部署体验,并实现🔚端到端的推理性能优化,满足开发者多场景、多硬件、多平台的产业部署需求。

近期更新

        使用FastDeploy可以简单高效的在X86 CPU、NVIDIA GPU、飞腾CPU、ARM CPU、Intel GPU、昆仑、昇腾、瑞芯微、晶晨、算能等10+款硬件上对PaddleSeg语义分割模型进行快速部署,并且支持Paddle Inference、Paddle Lite、TensorRT、OpenVINO、ONNXRuntime、RKNPU2、SOPHGO等多种推理后端。

2. 通过FastDeploy C++ 部署PaddleSeg模型

支持PaddleSeg高于2.6版本的Segmentation模型,如果部署的为PP-MattingPP-HumanMatting以及ModNet请参考Matting模型部署。目前FastDeploy测试过成功部署的模型:

支持CpuInfer、GpuInfer、TrtInfer三种推理模式

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/vision.h"

#ifdef WIN32
const char sep = '\\';
#else
const char sep = '/';
#endif

void CpuInfer(const std::string& model_dir, const std::string& image_file) {
  auto model_file = model_dir + sep + "model.pdmodel";
  auto params_file = model_dir + sep + "model.pdiparams";
  auto config_file = model_dir + sep + "deploy.yaml";
  auto option = fastdeploy::RuntimeOption();
  option.UseCpu();
  auto model = fastdeploy::vision::segmentation::PaddleSegModel(
      model_file, params_file, config_file, option);

  if (!model.Initialized()) {
    std::cerr << "Failed to initialize." << std::endl;
    return;
  }

  auto im = cv::imread(image_file);

  fastdeploy::vision::SegmentationResult res;
  if (!model.Predict(im, &res)) {
    std::cerr << "Failed to predict." << std::endl;
    return;
  }

  std::cout << res.Str() << std::endl;
  auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);
  cv::imwrite("vis_result.jpg", vis_im);
  std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}

void GpuInfer(const std::string& model_dir, const std::string& image_file) {
  auto model_file = model_dir + sep + "model.pdmodel";
  auto params_file = model_dir + sep + "model.pdiparams";
  auto config_file = model_dir + sep + "deploy.yaml";

  auto option = fastdeploy::RuntimeOption();
  option.UseGpu();
  auto model = fastdeploy::vision::segmentation::PaddleSegModel(
      model_file, params_file, config_file, option);

  if (!model.Initialized()) {
    std::cerr << "Failed to initialize." << std::endl;
    return;
  }

  auto im = cv::imread(image_file);

  fastdeploy::vision::SegmentationResult res;
  if (!model.Predict(im, &res)) {
    std::cerr << "Failed to predict." << std::endl;
    return;
  }

  std::cout << res.Str() << std::endl;
  auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);
  cv::imwrite("vis_result.jpg", vis_im);
  std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}

void TrtInfer(const std::string& model_dir, const std::string& image_file) {
  auto model_file = model_dir + sep + "model.pdmodel";
  auto params_file = model_dir + sep + "model.pdiparams";
  auto config_file = model_dir + sep + "deploy.yaml";

  auto option = fastdeploy::RuntimeOption();
  option.UseGpu();
  option.UseTrtBackend();
  // If use original Tensorrt, not Paddle-TensorRT,
  // comment the following two lines
  option.EnablePaddleToTrt();
  option.EnablePaddleTrtCollectShape();
  option.SetTrtInputShape("x", {1, 3, 256, 256}, {1, 3, 1024, 1024},
                          {1, 3, 2048, 2048});

  auto model = fastdeploy::vision::segmentation::PaddleSegModel(
      model_file, params_file, config_file, option);

  if (!model.Initialized()) {
    std::cerr << "Failed to initialize." << std::endl;
    return;
  }

  auto im = cv::imread(image_file);

  fastdeploy::vision::SegmentationResult res;
  if (!model.Predict(im, &res)) {
    std::cerr << "Failed to predict." << std::endl;
    return;
  }

  std::cout << res.Str() << std::endl;
  auto vis_im = fastdeploy::vision::VisSegmentation(im, res, 0.5);
  
  cv::imwrite("vis_result.jpg", vis_im);
  std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}

int main(int argc, char* argv[]) {
    std::string model_dir = "model\\PP_LiteSeg_B_STDC2_cityscapes_without_argmax_infer";
   std::string image_file = "model\\cityscapes_demo.png";

 
  // CpuInfer(argv[1], argv[2]);

   GpuInfer(model_dir, image_file);

 //  TrtInfer(argv[1], argv[2]);
  
  return 0;
}

推理结果可视化:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PaddleSeg是一个基于飞桨深度学习框架的语义分割工具,它可以用于图像分割、实例分割、人体关键点检测等任务。针对你提到的在Windows系统上使用PaddleSeg,以下是一些相关的信息: 1. 系统要求:PaddleSeg可以在Windows系统上运行,但需要满足一定的系统要求。首先,你需要安装适用于Windows的Python环境,推荐使用Anaconda来管理环境。其次,你需要安装PaddlePaddle深度学习框架和PaddleSeg工具,可以通过pip命令或者源码安装。 2. 使用方式:在Windows系统上,你可以使用PaddleSeg提供的命令行工具或者Python API进行语义分割任务的训练和推理。命令行工具提供了一些预定义的配置文件和调用参数,可以直接使用,也可以通过修改参数来自定义配置。Python API则更加灵活,你可以根据自己的需求编写自定义的代码和逻辑。 3. 模型选择:PaddleSeg提供了一些经典和先进的模型供你选择,例如DeepLabv3+, U-Net等。你可以根据不同的应用场景和数据集特点选择合适的模型进行训练和推理。 4. 数据准备和标注:语义分割任务需要准备标注好的训练数据,包括图像和对应的分割标签。你可以使用PaddleSeg提供的数据处理脚本来处理数据,包括数据集划分、数据增强等操作。对于数据标注,你可以使用一些图像标注工具来手动标注或者使用一些半自动标注工具来加速标注过程。 总的来说,使用PaddleSeg在Windows上进行语义分割任务是可行的,你需要根据系统要求进行环境搭建,选择合适的模型和数据集,并根据具体需求使用命令行工具或者Python API进行训练和推理。希望以上信息能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI小怪兽

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值