caffe练习实例(3)——使用训练好的模型

本实例是使用opencv编写代码,使用修改后的mnist的deploy文件并且调用训练好的模型,输入一张图片,输出分类结果。本工程的所有文件我都上传到了github上面,需要的可以下载。具体步骤如下:

改写deploy文件:

  • 把数据层和(Data Layer)和连接数据层的layers去掉(即top:data的层)如下:
    caffe练习实例(3)——使用训练好的模型_第1张图片
  • 去掉输出层和连接输出层的Layers(即bottom:label)
    caffe练习实例(3)——使用训练好的模型_第2张图片
  • 重新建立输入,添加如下代码:
input: "data"
input_shape {
  dim: 1 # batchsize,每次forward的时候输入的图片个数
  dim: 3 # number of colour channels - rgb
  dim: 28 # width
  dim: 28 # height
}
  • 重新建立输出,代码如下:
layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}


使用模型

  • opencv源码:
#include "opencv2/dnn.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace cv::dnn;

#include 
#include 
#include 
using namespace std;

/* Find best class for the blob (i. e. class with maximal probability) */ 
void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
{
    Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
    Point classNumber;
    minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
    *classId = classNumber.x;
}


int main(int argc,char* argv[]){

    String modelTxt = "mnist_deploy.prototxt";
    String modelBin = "lenet_iter_10000.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "5.jpg";

    //! [Create the importer of Caffe model] 导入一个caffe模型接口 
    Ptr importer; 
    importer = dnn::createCaffeImporter(modelTxt, modelBin);

    if (!importer){
        std::cerr << "Can't load network by using the following files: " << std::endl;
        std::cerr << "prototxt:   " << modelTxt << std::endl;
        std::cerr << "caffemodel: " << modelBin << std::endl;
        exit(-1);
    }

    //! [Initialize network] 通过接口创建和初始化网络
    Net net;
    importer->populateNet(net);  
    importer.release();

    //! [Prepare blob] 读取一张图片并转换到blob数据存储
    Mat img = imread(imageFile,0); //[] "0" for 1 channel, Mnist accepts 1 channel
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    resize(img, img, Size(28, 28));                   //[]Mnist accepts only 28x28 RGB-images

    dnn::Blob inputBlob = cv::dnn::Blob(img);   //Convert Mat to dnn::Blob batch of images

    //! [Set input blob] 将blob输入到网络
    net.setBlob(".data", inputBlob);        //set the network input

    //! [Make forward pass] 进行前向传播
    net.forward();                          //compute output

    //! [Gather output] 获取概率值
    dnn::Blob prob = net.getBlob("prob");   //[] gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class

    //! [Print results] 输出结果
    std::cout << "Best class: #" << classId << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;

    return 0;
}
  • 编译命令:g++ -o test_mnist test_mnist.cpp -lopencv_dnn -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lstdc++ -lopencv_core
  • 执行效果:

    caffe练习实例(3)——使用训练好的模型_第3张图片

题外话

这里给在自学caffe的同学一个建议,相比于自学,我觉得有个老师带领你去学更加有动力,效率会更高。所以我学caffe和opencv都是在练数成金上报课程学习的,建议有需要的可以去报一下,然后报名的时候输入我的优惠码,可以有一定的优惠哦!!!
我的优惠码:JE46
这是caffe课程链接: 深度学习框架Caffe学习与应用
这是opencv课程链接: 开源计算机视觉库OpenCV从入门到应用

你可能感兴趣的:(caffe)