如何使用 Opencv dnn 模块调用 Caffe 预训练模型?

Posted 學海無涯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用 Opencv dnn 模块调用 Caffe 预训练模型?相关的知识,希望对你有一定的参考价值。

 QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt";
    QString modelBin = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\snapshot_iter_10000.caffemodel";
    QString imageFile = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\9.png";

    //读取存储在caffe模型文件中的网络模型
    cv::dnn::Net net = cv::dnn::readNetFromCaffe(modelPrototxt.toStdString(),modelBin.toStdString());
    if (net.empty())
    {
        qDebug() << "readNetFromCaffe faild";
    }

    //读取图像文件
    cv::Mat img = cv::imread(imageFile.toStdString(),0);
    if(img.empty())
    {
        qDebug() << "imread faild";
    }

    cv::Mat inputBlob = cv::dnn::blobFromImage(img,0.00390625f, cv::Size(28, 28), cv::Scalar(), false);
    cv::Mat prob;
    cv::TickMeter t;
    for (int i = 0; i < 1; i++)
    {
        //设置网络的输入层名字(和训练网络模型文件里面的 name 对应)
        net.setInput(inputBlob, "data");
        t.start();
        //设置网络的输出层名字(和训练网络模型文件里面的 name 对应)
        prob = net.forward("prob");
        t.stop();
    }

    int classId;
    double classProb;
    cv::Mat probMat = prob.reshape(1, 1); //reshape the blob to 1x1000 matrix
    cv::Point classNumber;

    cv::minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
    classId = classNumber.x;

    qDebug() << t.getTimeMicro() << "index:" << classId << "%"<< classProb;

LeNet

name: "LeNet"  
input: "data"  
input_shape {  
  dim: 1   # batchsize  
  dim: 1   # number of channels  
  dim: 28  # width  
  dim: 28  # height  
}  
layer {  
  name: "conv1"  
  type: "Convolution"  
  bottom: "data"  
  top: "conv1"  
  param {  
    lr_mult: 1  
  }  
  param {  
    lr_mult: 2  
  }  
  convolution_param {  
    num_output: 20  
    kernel_size: 5  
    stride: 1  
    weight_filler {  
      type: "xavier"  
    }  
    bias_filler {  
      type: "constant"  
    }  
  }  
}  
layer {  
  name: "pool1"  
  type: "Pooling"  
  bottom: "conv1"  
  top: "pool1"  
  pooling_param {  
    pool: MAX  
    kernel_size: 2  
    stride: 2  
  }  
}  
layer {  
  name: "conv2"  
  type: "Convolution"  
  bottom: "pool1"  
  top: "conv2"  
  param {  
    lr_mult: 1  
  }  
  param {  
    lr_mult: 2  
  }  
  convolution_param {  
    num_output: 50  
    kernel_size: 5  
    stride: 1  
    weight_filler {  
      type: "xavier"  
    }  
    bias_filler {  
      type: "constant"  
    }  
  }  
}  
layer {  
  name: "pool2"  
  type: "Pooling"  
  bottom: "conv2"  
  top: "pool2"  
  pooling_param {  
    pool: MAX  
    kernel_size: 2  
    stride: 2  
  }  
}  
layer {  
  name: "ip1"  
  type: "InnerProduct"  
  bottom: "pool2"  
  top: "ip1"  
  param {  
    lr_mult: 1  
  }  
  param {  
    lr_mult: 2  
  }  
  inner_product_param {  
    num_output: 500  
    weight_filler {  
      type: "xavier"  
    }  
    bias_filler {  
      type: "constant"  
    }  
  }  
}  
layer {  
  name: "relu1"  
  type: "ReLU"  
  bottom: "ip1"  
  top: "ip1"  
}  
layer {  
  name: "ip2"  
  type: "InnerProduct"  
  bottom: "ip1"  
  top: "ip2"  
  param {  
    lr_mult: 1  
  }  
  param {  
    lr_mult: 2  
  }  
  inner_product_param {  
    num_output: 10  
    weight_filler {  
      type: "xavier"  
    }  
    bias_filler {  
      type: "constant"  
    }  
  }  
}  
layer {  
  name: "prob"  
  type: "Softmax"  
  bottom: "ip2"  
  top: "prob"  
}

以上是关于如何使用 Opencv dnn 模块调用 Caffe 预训练模型?的主要内容,如果未能解决你的问题,请参考以下文章

VS 2019配置QT和Opencv调用DNN模块进行图像分类

VS 2019配置QT和Opencv调用DNN模块进行图像分类

杂例 01=VS 2019配置QT和Opencv调用DNN模块进行图像分类

OpenCV的dnn模块调用TesorFlow训练的MoblieNet模型

OpenCV的dnn模块调用TesorFlow训练的MoblieNet模型

OpenCV4.5.4 DNN人脸识别模块使用介绍--如何快速搭建一个人脸识别系统