简单实现cv::dnn推理深度学习模型

前边使用python api进行模型推理,这里使用opencv c++ api,模型选择还是前边提到的unet。

opencv 无法训练模型,但它支持载入其他深度学习框架训练好的模型,并使用该模型进行预测 inference;

OpenCV: Deep Neural Network module

1、图像预处理

将原始图像转换为可以直接输入网络的格式,在进行深度学习时,blobFromImage主要是用来对图片进行预处理。

dnn::blobFromImage(InputArray image, 
			  double scalefactor=1.0, 
		      const Size& size = Size(),
			  const Scalar& mean = Scalar(), 
			  bool swapRB = false, 
			  bool crop = false,
			  int ddepth = CV_32F)
  • image:输入图像(1、3或者4通道)
  • scalefactor:图像各通道数值的缩放比例
  • size:输出图像的空间尺寸,如size=(200,300)表示高h=300,宽w=200
  • mean:用于各通道减去的值,以降低光照的影响(e.g. image为bgr3通道的图像,mean=[104.0, 177.0, 123.0],表示b通道的值-104,g-177,r-123)
  • swapRB:交换RB通道,默认为False.(cv2.imread读取的是彩图是bgr通道)
  • crop:图像裁剪,默认为False.当值为True时,先按比例缩放,然后从中心裁剪成size尺寸
  • ddepth:输出的图像深度,可选CV_32F 或者 CV_8U

2、模型加载

解析onnx文件

dnn::Net net = cv::dnn::readNetFromONNX(net_onnx);

3、模型推理

使用cuda

net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

单输入单输出

net.setInput(blob); //设置模型的输入
output = net.forward(); //前向传播

代码实现:

#include <iostream>
#include <vector>
#include<opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>

using namespace cv;
using namespace std;

class unet
{
public:
    unet(string modelpath);
    void detect(Mat& frame);
private:
    dnn::Net net;
    const int inpWidth = 256;
    const int inpHeight = 256;
    void post_processing(Mat& out);
};
unet::unet(string modelpath)
{
    this->net = dnn::readNetFromONNX(modelpath);
    this->net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
    this->net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
}
void unet::detect(Mat& frame)
{
    Mat blob;
    dnn::blobFromImage(frame, blob, 1 / 255.0, Size(this->inpWidth, this->inpHeight), 
        Scalar(0, 0, 0), true, false);
    this->net.setInput(blob);
    Mat outs;
    this->net.forward(outs);
    post_processing(outs);
}
void unet::post_processing(Mat& outs)
{
    std::cout << "Dimension information of outs:" << std::endl;
    cout << outs.size[0] << "x" << outs.size[1] << "x" << outs.size[2] << endl;
    outs = outs.reshape(1, { 256 , 256 });
    cout << outs.size[0] << "x" << outs.size[1] << endl;
    std::vector<std::string> CLASSES = { "ignore", "crack", "spall", "rebar" };
    std::vector<cv::Vec3b> PALETTE = { cv::Vec3b(0, 0, 0), cv::Vec3b(255, 0, 0), cv::Vec3b(0, 255, 0), cv::Vec3b(0, 0, 255) }; // bgr
    cv::Mat color_seg(outs.rows, outs.cols, CV_8UC3, cv::Scalar(0, 0, 0));
    for (int row = 0; row < outs.rows; row++)
    {
        for (int col = 0; col < outs.cols; col++)
        {
            float label = outs.at<float>(row, col);
            assert(label >= 0 && label < PALETTE.size());
            cv::Vec3b color = PALETTE[label];
            color_seg.at<cv::Vec3b>(row, col) = color;
        }
    }
    imwrite("C:\\Users\\LYL\\Desktop\\a.jpg", color_seg);
}


int main()
{
    unet unet_infer("D:/learning/project/inference_demo/cv_infer/model_softmax_argmax2.onnx");
    string imgpath = "D:/learning/project/inference_demo/cv_infer/crack000680.jpg";
    Mat srcimg = imread(imgpath);
    unet_infer.detect(srcimg); 
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值