• 【计算机视觉】使用opencv中dnn模块及openvino2022进行模型推理(C++接口)


    使用openvino:(推理使用由onnx生成的xml及bin文件)

    0.查看支持的推理设备:

    ov::Core ie;
    vector<string> availableDevices = ie.get_available_devices();
    for (int i = 0; i < availableDevices.size(); i++) {
        qDebug()<<"supported device name : "<<availableDevices[i].c_str();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.构造模型:

    ov::Core ie;
    auto network = ie.read_model(xml,bin);
    if(gpu)
    {
    auto compiled_model = ie.compile_model(network, "GPU");
    infer_request = compiled_model.create_infer_request();
    }
    else
    {
    auto compiled_model = ie.compile_model(network, "CPU");
    infer_request = compiled_model.create_infer_request();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.进行预处理工作,cv::mat转ov::tensor

    计算机内存float*是一个一维数组线性存储,故相互赋值需将mat及tensor的索引统一为线性索引:从头逐一连续赋值;
    一般而言有:

    void cvImageToTensor(const cv::Mat & image, float *tensor, nvinfer1::Dims dimensions)
    {
      const size_t channels = dimensions.d[1];
      const size_t height = dimensions.d[2];
      const size_t width = dimensions.d[3];
      // TODO: validate dimensions match
      const size_t stridesCv[3] = { width * channels, channels, 1 };
      const size_t strides[3] = { height * width, width, 1 };
    
      for (int i = 0; i < height; i++) 
      {
        for (int j = 0; j < width; j++) 
        {
          for (int k = 0; k < channels; k++) 
          {
            const size_t offsetCv = i * stridesCv[0] + j * stridesCv[1] + k * stridesCv[2];
            const size_t offset = k * strides[0] + i * strides[1] + j * strides[2];
            tensor[offset] = (float) image.data[offsetCv];
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    或使用opencv自己的取值函数at<>:

    cv::Mat img = this->resize_image(frame, &newh, &neww, &padh, &padw);
        ov::Tensor input_tensor1 = infer_request.get_input_tensor(0);
        auto data1 = input_tensor1.data<float>();
        cv::cvtColor(img,img,cv::COLOR_BGR2RGB);
         for (int h = 0; h < 640; h++)
         {
             for (int w = 0; w < 640; w++)
             {
                 for (int c = 0; c < 3; c++)
                 {
                     //tensor:chw排列,这里待转tensor为(1,3,640,640)
                     int out_index = c * 640 * 640 + h * 640 + w;
                     //mat是hwc排列,原始mat为(640,640,3)
                     data1[out_index] = (float(img.at<cv::Vec3b>(h, w)[c])-127.5)/128.0;
                 }
             }
         }
        infer_request.infer();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.推理后结果提取:

     float* pdata_score = infer_request.get_output_tensor(n).data<float>();  
    
    • 1

    使用opencv自带的dnn模块进行推理

    0.模型构造

    这里使用的是onnx

    cv::dnn::Net net;
     this->net = cv::dnn::readNet(config.modelfile);
    
    • 1
    • 2

    1.数据准备:原始mat变形为cv::dnn::Net的输入mat

    cv::Mat img = this->resize_image(frame, &newh, &neww, &padh, &padw);
        cv::Mat blob;
        //先减127.5再调换RB通道,再除以128.0,得出的blob为(1,3,640,640)的形状
        cv::dnn::blobFromImage(img, blob, 1 / 128.0, cv::Size(this->inpWidth, this->inpHeight), cv::Scalar(127.5, 127.5, 127.5), true, false);
        
        this->net.setInput(blob);
        std::vector<cv::Mat> outs;
        this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.推理后结果提取:

    float* pdata_score = (float*)outs[n * 3].data;
    
    • 1
  • 相关阅读:
    通过match看rust
    【c++】跟webrtc学std array 4: H264PacketBuffer 包缓存
    典型神经网络及应用论文,神经网络架构论文题目
    计算几何+2sat:1020T3
    pnpm快速创建 Vue.js 项目(npm类似)
    Java线程池的知识
    qt 画多边形,可以拖拽
    k8s-核心实战
    Flutter 最有用的 5 个优秀的依赖包
    力扣-226.翻转二叉树
  • 原文地址:https://blog.csdn.net/hh1357102/article/details/128097761