FastDeploy icon indicating copy to clipboard operation
FastDeploy copied to clipboard

yaml文件加载失败(Failed to load yaml file , maybe you should check this file.)

Open flinzhao opened this issue 11 months ago • 6 comments

环境

  • 【FastDeploy版本】: develop分支
  • 【编译命令】cmake .. -G "Visual Studio 17 2022" -A x64 -DENABLE_ORT_BACKEND=ON -DENABLE_TRT_BACKEND=ON -DENABLE_VISION=ON -DWITH_GPU=ON -DTRT_DIRECTORY="D:\Downloads\TensorRT-8.5.3.1.Windows10.x86_64.cuda-11.8.cudnn8.6\TensorRT-8.5.3.1" -DCUDA_DIRECTORY="D:\NVIDIA GPU Computing Toolkit\CUDA\v11.6" -DCMAKE_INSTALL_PREFIX="E:\cplus\compiled_fastdeploy" -DOPENCV_DIRECTORY="E:\cplus\opencv-win-x64-3.4.16\build" -DORT_DIRECTORY="E:\cplus\onnxruntime-win-x64-gpu-1.12.0"
  • 【系统平台】: Windows x64(Windows11)
  • 【硬件】: Nvidia GPU 3060, CUDA 11.6 CUDNN 8.6
  • 【编译语言】: C++
  • 【模型跑不通】 根据 https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/use_sdk_on_windows_build.md中的3.3 SDK使用方式三Visual Studio 2019 创建 CMake 工程使用 C++ SDK 来编译infer_ppyoloe.cpp源文件的,编译也成功了,得到了exe可执行文件: image 模型权值文件也下载了: image infer_ppyoloe.cpp源文件代码如下,我做了稍微的修改(主要是判断文件是否存在):
#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 + "infer_cfg.yml";
    auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file,
        config_file);
    if (!model.Initialized()) {
        std::cerr << "Failed to initialize." << std::endl;
        return;
    }

    auto im = cv::imread(image_file);
    auto im_bak = im.clone();

    fastdeploy::vision::DetectionResult 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::Visualize::VisDetection(im_bak, 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 + "infer_cfg.yml";

    auto option = fastdeploy::RuntimeOption();
    option.UseGpu();
    auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file,
        config_file, option);
    if (!model.Initialized()) {
        std::cerr << "Failed to initialize." << std::endl;
        return;
    }

    auto im = cv::imread(image_file);
    auto im_bak = im.clone();

    fastdeploy::vision::DetectionResult 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::Visualize::VisDetection(im_bak, 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 + "infer_cfg.yml";*/
    const std::string model_file = "E:/cplus/fastdeploy_test/infer_ppyoloe/out/build/x64-debug/Debug/ppyoloe_crn_l_300e_coco/model.pdmodel";
    const std::string params_file = "E:/cplus/fastdeploy_test/infer_ppyoloe/out/build/x64-debug/Debug/ppyoloe_crn_l_300e_coco/model.pdiparams";
    const std::string config_file = "E:/cplus/fastdeploy_test/infer_ppyoloe/out/build/x64-debug/Debug/ppyoloe_crn_l_300e_coco/infer_cfg.yml";

    std::ifstream fin(config_file);
    if (!fin) {
        std::cout << config_file + " no existed." << std::endl;
    }
    else {
        std::cout << config_file + " existed." << std::endl;
    }

    auto option = fastdeploy::RuntimeOption();
    option.UseGpu();
    option.UseTrtBackend();
    auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file,
        config_file, option);
    if (!model.Initialized()) {
        std::cerr << "Failed to initialize." << std::endl;
        return;
    }

    auto im = cv::imread(image_file);
    auto im_bak = im.clone();

    fastdeploy::vision::DetectionResult 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::Visualize::VisDetection(im_bak, res, 0.5);
    cv::imwrite("vis_result.jpg", vis_im);
    std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
}

static void check_cfg(const std::string& model_dir, const std::string& image_file) {
    auto config_file = model_dir + sep + "infer_cfg.yml";
    std::ifstream fin(config_file);
    if (!fin) {
        std::cout << config_file + " no existed." << std::endl;
    }
    else {
        std::cout << config_file + " existed." << std::endl;
    }

    std::ifstream fin2(image_file);
    if (!fin2) {
        std::cout << image_file + " no existed." << std::endl;
    }
    else {
        std::cout << image_file + " existed." << std::endl;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 4) {
        std::cout
            << "Usage: infer_demo path/to/model_dir path/to/image run_option, "
            "e.g ./infer_model ./ppyoloe_model_dir ./test.jpeg 0"
            << std::endl;
        std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
            "with gpu; 2: run with gpu and use tensorrt backend."
            << std::endl;
        return -1;
    }

    check_cfg(argv[1], argv[2]);

    if (std::atoi(argv[3]) == 0) {
        CpuInfer(argv[1], argv[2]);
    }
    else if (std::atoi(argv[3]) == 1) {
        GpuInfer(argv[1], argv[2]);
    }
    else if (std::atoi(argv[3]) == 2) {
        TrtInfer(argv[1], argv[2]);
    }
    return 0;
}

然后运行exe,报**Failed to load yaml file , maybe you should check this file.**错误,运行命令和结果如下: image

我明明判断了yaml文件是否存在,显示结果是存在的,但fastdeploySDK说不存在,我也看了fastdeploy的源代码 image image 触发异常的就是上的代码,但是我明明自己判断文件是存在的,但为什么fastdeploySDK判断文件却不存在。这到底是为啥? 请求大佬指教!

flinzhao avatar Mar 04 '24 13:03 flinzhao

如果文件存在,但仍然出错,那有可能是yaml文件内容不合法

jiangjiajun avatar Mar 05 '24 05:03 jiangjiajun

image 这就是yaml文件,文件是从 https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz中下载的 image 我没有改过这个yaml文件,内容原汁原味🫥

flinzhao avatar Mar 05 '24 06:03 flinzhao

换个短一点路径看看,你这个路径有点深,而且路径中有类似于x64-debug这样的短划线,都有可能出现乱码啥的。

ChaoII avatar Mar 05 '24 10:03 ChaoII

也可以试试将路径改成 "E:\cplus\fastdeploy_test\infer_ppyoloe\out\build\x64-debug\Debug\ppyoloe_crn_l_300e_coco\infer_cfg.yml"

jiangjiajun avatar Mar 06 '24 06:03 jiangjiajun

请问您解决了吗,我也遇到了这个问题,而且我训练了两个模型,用的是同一个框架,一个导出后做推理没问题,另一个却一直报错说failed to load yaml

smalie2222 avatar Apr 24 '24 02:04 smalie2222