• MMDeploy部署实战系列【第六章】:将编译好的MMdeploy导入到自己的项目中 (C++)


    MMDeploy部署实战系列【第六章】:将编译好的MMdeploy导入到自己的项目中 (C++)

    ⭐️ ⭐️ ⭐️ 这个系列是一个随笔,是我走过的一些路,有些地方可能不太完善。如果有那个地方没看懂,评论区问就可以,我给补充。

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    目录:

    0️⃣ mmdeploy源码安装 (转换faster rcnn r50/yolox为tensorrt,并进行推理)_gy77

    内容:一文包含了在Linux系统下安装mmdeploy模型转换环境,模型转换为TensorRT,在Linux,Windows下模型推理,推理结果展示。

    MMDeploy部署实战系列

    1️⃣ MMDeploy部署实战系列【第一章】:Docker,Nvidia-docker安装_gy77

    内容:docker/nvidia-docker安装,docker/nvidia-docker国内源,docker/nvidia-docker常用命令。

    2️⃣ MMDeploy部署实战系列【第二章】:mmdeploy安装及环境搭建_gy77

    内容:mmdeploy环境安装三种方法:源码安装,官方docker安装,自定义Dockerfile安装。

    3️⃣ MMDeploy部署实战系列【第三章】:MMdeploy pytorch模型转换onnx,tensorrt_gy77

    内容:如何查找pytorch模型对应的部署配置文件,模型转换示例:mmcls:resnext50,mmdet:yolox-s,faster rcnn50。

    4️⃣ MMDeploy部署实战系列【第四章】:onnx,tensorrt模型推理_gy77

    内容:在linux,windows环境下推理,Windows下推理环境安装,推理速度对比,显存对比,可视化展示。

    5️⃣ MMDeploy部署实战系列【第五章】:Windows下Release x64编译mmdeploy(C++),对TensorRT模型进行推理_gy77

    内容:Windows下环境安装编译环境,编译c++ mmdeploy,编译c++ mmdeploy demo,运行实例。

    6️⃣ MMDeploy部署实战系列【第六章】:将编译好的MMdeploy导入到自己的项目中 (C++)_gy77

    内容:Windows下环境导入我们编译好的mmdeploy 静态/动态库。

    下面是正文:


    官方文档: 操作概述 — mmdeploy 0.6.0 文档

    项目环境配置:

    1️⃣ 创建一个控制台应用,并起名inference_sdk(有自己项目的就在自己项目中直接修改即可,不用这一步)

    2️⃣ 右下角属性管理器,右击Release |x64,因为我们编译mmdeploy时用的Release x64,这个要对应,新建一个项目属性表,起名mmlab。

    image-20220801122146588

    3️⃣ 双击打开mmlab属性表,

    🔸 VC++ 目录 --> 包含目录 --> 添加

    F:\gy77\mmdeploy\build\install\include
    F:\env\opencv455\opencv\build\include\opencv2
    F:\env\opencv455\opencv\build\include\opencv
    F:\env\opencv455\opencv\build\include
    
    • 1
    • 2
    • 3
    • 4

    🔸 链接器 --> 输入 --> 附加依赖项。根据自己的目录修改,第一个是opencv目录。(当然也可以通过在VC++里添加库目录,然后在链接器输入里加lib文件)

    F:\env\opencv455\opencv\build\x64\vc15\lib\opencv_world455.lib
    F:\gy77\mmdeploy\build\install\example\build\Release\object_detection_loader.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_opencv_utils.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_classifier.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_detector.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_segmentor.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_text_detector.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_text_recognizer.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_restorer.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_pose_detector.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_rotated_detector.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_pipeline.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_model.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_executor.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_common.lib
    F:\gy77\mmdeploy\build\install\lib\mmdeploy_core.lib
    F:\gy77\mmdeploy\build\install\lib\spdlog.lib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    🔸 链接器 --> 命令行 --> 其他选项:

    %(AdditionalOptions) /machine:x64 /WHOLEARCHIVE:object_detection_loader
    
    • 1

    编写代码

    在解决方案资源管理器,源文件中,修改inference_sdk.cpp,内容如下:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "mmdeploy/detector.h"
    
    using namespace std;
    
    string coco_classes[80] = {"person", "bicycle", "car",
                               "motorcycle", "airplane", "bus",
                               "train", "truck", "boat",
                               "traffic light", "fire hydrant", "stop sign",
                               "parking meter", "bench", "bird",
                               "cat", "dog", "horse",
                               "sheep", "cow", "elephant",
                               "bear", "zebra", "giraffe",
                               "backpack", "umbrella", "handbag",
                               "tie", "suitcase", "frisbee",
                               "skis", "snowboard", "sports ball",
                               "kite", "baseball bat", "baseball glove",
                               "skateboard", "surfboard", "tennis racket",
                               "bottle", "wine glass", "cup",
                               "fork", "knife", "spoon",
                               "bowl", "banana", "apple",
                               "sandwich", "orange", "broccoli",
                               "carrot", "hot dog", "pizza",
                               "donut", "cake", "chair",
                               "couch", "potted plant", "bed",
                               "dining table", "toilet", "tv",
                               "laptop", "mouse", "remote",
                               "keyboard", "cell phone", "microwave",
                               "oven", "toaster", "sink",
                               "refrigerator", "book", "clock",
                               "vase", "scissors", "teddy bear",
                               "hair drier", "toothbrush"};
    
    
    int main() {
        // init
        const char *device_name = "cuda";
        const char *model_path = "F:\\gy77\\mmdeploy\\mmdeploy_models\\yolox_s";
        const char *img_path = "F:\\imgs\\demo.jpg";
    
        mmdeploy_detector_t detector{};
        mmdeploy_detection_t *bboxes{};
        int *res_count{};
        int status{};
        int max_res_count = 10;
        status = mmdeploy_detector_create_by_path(model_path, device_name, 0, &detector);
        if (status != MMDEPLOY_SUCCESS) {
            fprintf(stderr, "failed to create detector, code: %d\n", (int) status);
            return 1;
        }
    
        // load image
        clock_t t0 = clock();
        cv::Mat img = cv::imread(img_path);
    
        mmdeploy_mat_t mat{
                img.data, img.rows, img.cols, 3, MMDEPLOY_PIXEL_FORMAT_BGR, MMDEPLOY_DATA_TYPE_UINT8};
    
        // inference
        clock_t t1 = clock();
    
        status = mmdeploy_detector_apply(detector, &mat, 1, &bboxes, &res_count);
    
        clock_t t2 = clock();
    
        for (int i = 0; i < max_res_count; ++i) {
            const auto &box = bboxes[i].bbox;
            const auto &mask = bboxes[i].mask;
    
            // skip detections with invalid bbox size (bbox height or width < 1)
            if ((box.right - box.left) < 1 || (box.bottom - box.top) < 1) {
                continue;
            }
    
            // skip detections less than specified score threshold
            if (bboxes[i].score < 0.3) {
                continue;
            }
    
            cv::rectangle(img, cv::Point{(int) box.left, (int) box.top},
                          cv::Point{(int) box.right, (int) box.bottom}, cv::Scalar{0, 255, 0}, 2);
            cv::putText(img, coco_classes[bboxes[i].label_id] + ":" + std::to_string(bboxes[i].score),
                        cv::Point{(int) box.left, (int) box.top}, cv::FONT_HERSHEY_SIMPLEX, 0.5,
                        cv::Scalar{0, 0, 255}, 2);
    
        }
    
        //show image
        cv::imshow("img", img);
    
        clock_t t3 = clock();
    
        cout << "load image cost time: " << (t1 - t0) / (double) CLOCKS_PER_SEC << "s" << endl;
        cout << "mmdeploy_detector_apply cost time" << (double) (t2 - t1) / CLOCKS_PER_SEC << "s" << endl;
        cout << "cv::imshow cost time" << (double) (t3 - t2) / CLOCKS_PER_SEC << "s" << endl;
        cv::waitKey(0);
        cv::destroyAllWindows();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    结果展示:

    log:

    loading mmdeploy_execution ...
    loading mmdeploy_cpu_device ...
    loading mmdeploy_cuda_device ...
    loading mmdeploy_graph ...
    loading mmdeploy_directory_model ...
    [2022-08-01 12:33:53.232] [mmdeploy] [info] [model.cpp:95] Register 'DirectoryModel'
    loading mmdeploy_transform ...
    loading mmdeploy_cpu_transform_impl ...
    loading mmdeploy_cuda_transform_impl ...
    loading mmdeploy_transform_module ...
    loading mmdeploy_trt_net ...
    loading mmdeploy_net_module ...
    loading mmdeploy_mmcls ...
    loading mmdeploy_mmdet ...
    loading mmdeploy_mmseg ...
    loading mmdeploy_mmocr ...
    loading mmdeploy_mmedit ...
    loading mmdeploy_mmpose ...
    loading mmdeploy_mmrotate ...
    [2022-08-01 12:33:53.293] [mmdeploy] [info] [model.cpp:38] DirectoryModel successfully load model F:\gy77\mmdeploy\mmdeploy_models\yolox_s
    [2022-08-01 12:33:53.928] [mmdeploy] [warning] [trt_net.cpp:24] TRTNet: Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors.
    [2022-08-01 12:33:54.503] [mmdeploy] [warning] [trt_net.cpp:24] TRTNet: TensorRT was linked against cuBLAS/cuBLAS LT 11.6.3 but loaded cuBLAS/cuBLAS LT 11.2.1
    [2022-08-01 12:33:55.109] [mmdeploy] [warning] [trt_net.cpp:24] TRTNet: TensorRT was linked against cuBLAS/cuBLAS LT 11.6.3 but loaded cuBLAS/cuBLAS LT 11.2.1
    load image cost time: 0.018s
    mmdeploy_detector_apply cost time0.019s
    cv::imshow cost time0.029s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    图片推理结果:

    demoimage-20220801123657293
  • 相关阅读:
    C++ Qt开发:MdiArea多窗体组件
    【乘风伯乐奖】寻找百位乘风者伯乐,邀请新博主入驻即可获奖
    【JVM笔记】G1回收器垃圾回收过程
    【AI视野·今日Robot 机器人论文速览 第五十五期】Mon, 16 Oct 2023
    常见的排序方法
    autoware之轮式里程计计算
    leetcode 216. 组合总和 III
    怎么把PDF转换为PPT格式?分享三种简单的转换方法
    PyQt5安装详细教程
    根据pid查看jar包(windows)
  • 原文地址:https://blog.csdn.net/qq_39435411/article/details/126100059