FastDeploy 模型转换,案例测试

其他模型转paddle,PaddlePaddle/X2Paddle: Deep learning model converter for PaddlePaddle. (『飞桨』深度学习模型转换工具) (github.com)

paddle转onnx,PaddlePaddle/Paddle2ONNX: ONNX Model Exporter for PaddlePaddle (github.com)

  • 方式同上

【案例测试-win10】

1、OCR测试

模型下载 PaddleOCR/deploy/fastdeploy/cpu-gpu at dygraph · PaddlePaddle/PaddleOCR (github.com)

案例位置 C:\aa\FastDeploy-release-1.0.5\examples\vision\ocr\PP-OCR\cpu-gpu\cpp\infer.cc

2、YOLOv8测试

模型下载 PaddleYOLO/configs/yolov8 at release/2.5 · PaddlePaddle/PaddleYOLO (github.com)

2.1 案例位置 C:\aa\FastDeploy-release-1.0.5\examples\vision\detection\paddledetection\cpp\infer_yolov8.cc

2.2 案例位置 C:\aa\FastDeploy-release-1.0.5\examples\vision\detection\yolov8\cpp\infer.cc

对官方代码略微修改,方便落地:

#include "fastdeploy/vision.h"

const std::string& model_file = "yolov8s.onnx";
fastdeploy::vision::detection::YOLOv8 model = fastdeploy::vision::detection::YOLOv8(model_file);//官方没有默认构造,只能这样创建对象了

bool ModelInit(const std::string& model_file, const int i)
{
    auto option = fastdeploy::RuntimeOption();
    if (i==0) {
        option.UseCpu();    //默认
    }
    else if(i==1) {
        option.UseGpu();
    }
    else if (i == 2) {
        option.UseGpu();
        option.UseTrtBackend();
        option.SetTrtInputShape("images", { 1, 3, 640, 640 });
    }

    model = fastdeploy::vision::detection::YOLOv8(model_file, "", option);
    if (!model.Initialized()) {
        std::cerr << "Failed to initialize." << std::endl;
        return false;
    }
    return true;
}


//预测,输出带框图、框位置信息
bool ModelInfer(const cv::Mat& image_in, cv::Mat& image_out, fastdeploy::vision::DetectionResult& res)    //引用传参,不开辟内存,提高效率
{
    if (!model.Predict(image_in, &res)) {
        std::cerr << "Failed to predict." << std::endl;
        return false;
    }    

    image_out = fastdeploy::vision::VisDetection(image_in, res);    //输出带框图

    return true;
}

int main() 
{
    if (ModelInit(model_file,0))    //0 cpu(openvino),1 gpu(onnxruntime),2 gpu(tensorrt)
    {
        const std::string& image_file = "000000014439.jpg";
        auto im = cv::imread(image_file);
        cv::Mat image_result;
        bool isInfered = false;
        fastdeploy::vision::DetectionResult res;

        //预测并统计耗时
        double t = (double)cv::getTickCount();
        for (int i = 0; i < 50; i++)
        {            
            isInfered=ModelInfer(im, image_result, res);
        }
        t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
        std::cout<< "执行时间(秒): " << t << std::endl;

        //存图
        if (isInfered)
        {
            cv::imwrite("vis_result.jpg", image_result);
            std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
            std::cout << res.Str() << std::endl;    //输出框的位置、得分、label_id
        }
    }

    return 0;
}

 

posted @ 2023-03-30 14:50  夕西行  阅读(770)  评论(0编辑  收藏  举报