opencv3.3调用 caffe 模型进行手写数字的分类

一、按照下面步骤修改caffe-master\examples\mnist文件夹中的  lenet_train_test.prototxt 文件

1、去掉数据输入层,即将top 为 “data” 的layers 去掉。 即将下面的内容删掉   

layer {  
  name: "mnist"  
  type: "Data"  
  top: "data"  
  top: "label"  
  include {  
    phase: TRAIN  
  }  
  transform_param {  
    scale: 0.00390625  
  }  
  data_param {  
    source: "examples/mnist/mnist_train_lmdb"  
    batch_size: 64  
    backend: LMDB  
  }  
}  
layer {  
  name: "mnist"  
  type: "Data"  
  top: "data"  
  top: "label"  
  include {  
    phase: TEST  
  }  
  transform_param {  
    scale: 0.00390625  
  }  
  data_param {  
    source: "examples/mnist/mnist_test_lmdb"  
    batch_size: 100  
    backend: LMDB  
  }  
}  
2、重新建立输入,即添加下面的内容
input: "data"  
input_shape {  
  dim: 1   # batchsize  
  dim: 1   # number of channels   
  dim: 28  # width  
  dim: 28  # height  

3、去掉输出层,即将bottom 为 “label” 的layers 去掉,即将下面的内容删掉

layer {  
  name: "accuracy"  
  type: "Accuracy"  
  bottom: "ip2"  
  bottom: "label"  
  top: "accuracy"  
  include {  
    phase: TEST  
  }  
}  
layer {  
  name: "loss"  
  type: "SoftmaxWithLoss"  
  bottom: "ip2"  
  bottom: "label"  
  top: "loss"  

4、重新建立输出,即添加下面的内容
layer {  
  name: "prob"  
  type: "Softmax"  
  bottom: "ip2"  
  top: "prob"  
}  


二、使用cv::dnn 里的API加载model, 输入图片,进行测试
#include <iostream>  
#include <opencv2/opencv.hpp>  
#include <opencv2/dnn.hpp>  
 
using namespace std;
using namespace cv;
using namespace cv::dnn;
 
/* Find best class for the blob (i. e. class with maximal probability) */
static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
{
    Mat probMat = probBlob.reshape(1, 1);
    Point classNumber;
    minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
    *classId = classNumber.x;
}
 
int main()
{
    string modelTxt = "lenet_train_test.prototxt";
    string modelBin = "lenet_iter_10000.caffemodel";
    //read image  
    Mat img = imread("4.bmp", 0);
 
    Mat inputBlob = dnn::blobFromImage(img, 0.00390625f, Size(28, 28), Scalar(), false); //Convert Mat to batch of images
    dnn::Net net;
    try{
        net = dnn::readNetFromCaffe(modelTxt, modelBin);
    }
    catch (cv::Exception &ee){
        cerr << "Exception: " << ee.what() << endl;
        if (net.empty()){
            cout << "Can't load the network by using the flowing files:" << endl;
            cout << "modelTxt: " << modelTxt << endl;
            cout << "modelBin: " << modelBin << endl; exit(-1);
        }
    }
    Mat pred;
    net.setInput(inputBlob, "data");//set the network input, "data" is the name of the input layer   
    pred = net.forward("prob");//compute output, "prob" is the name of the output layer   
    cout << pred << endl; int classId; double classProb; getMaxClass(pred, &classId, &classProb);
    cout << "Best Class: " << classId << endl;
    cout << "Probability: " << classProb * 100 << "%" << endl;
}


三、参考博文
http://blog.csdn.net/sushiqian/article/details/78555891
https://www.cnblogs.com/messier/p/7997951.html#_caption_1

https://docs.opencv.org/master/d5/de7/tutorial_dnn_googlenet.html


原文:https://blog.csdn.net/hust_bochu_xuchao/article/details/78931972 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值