如何使用LeNet将opencv_dnn模块用于数字分类?

时间:2016-02-17 19:14:05

标签: c++ opencv caffe mnist

我尝试使用相同的代码和一些修改here进行数字识别,使用基于Caffe的mnist训练模型。 我修改过的源代码和网络如下。我还在最后包含了错误消息。代码中使用的图像是来自mnist的示例图像。 它无法通过的代码行是

net.forward();  

而且我很确定这是因为输入的维度。

如何为LeNet运行代码?

caffe_mnist.cpp

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <time.h>
#include <math.h>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
clock_t t;
/* 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;
}
std::vector<String> readClassNames(const char *filename = "./caffe_model/mnist/label.txt")
{
    std::vector<String> classNames;
    std::ifstream fp(filename);
    if (!fp.is_open())
    {
        std::cerr << "File with classes labels not found: " << filename << std::endl;
        exit(-1);
    }
    std::string name;
    while (!fp.eof())
    {
        std::getline(fp, name);
        if (name.length())
            classNames.push_back( name.substr(name.find(' ')+1) );
    }
    fp.close();
    return classNames;
}
int main(int argc, char **argv)
{
    String modelTxt = "./caffe_model/mnist/mnist.prototxt";
    String modelBin = "./caffe_model/mnist/mnist.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "./caffe_model/mnist/0.png";
    Ptr<dnn::Importer> importer;
    try                                     //Try to import Caffe GoogleNet model
    {
        importer = dnn::createCaffeImporter(modelTxt, modelBin);
    }
    catch (const cv::Exception &err)        //Importer can throw errors, we will catch them
    {
        std::cerr << err.msg << std::endl;
    }
    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);
    }
    dnn::Net net;
    importer->populateNet(net);
    std::cout << "done: importer->populateNet(net) " <<  std::endl;
    importer.release();                     //We don't need importer anymore
    Mat img = imread(imageFile);
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    resize(img, img, Size(28, 28));       //LeNet accepts 224x224 RGB-images
    std::cout << "done: resize(img, img, Size(28, 28));  " <<  std::endl;
    dnn::Blob inputBlob = dnn::Blob(img);   //Convert Mat to dnn::Blob image batch
    net.setBlob(".data", inputBlob);        //set the network input
    std::cout << "done: net.setBlob(.data, inputBlob);  " <<  std::endl;
    t = clock();
    net.forward();   
    std::cout << "done: net.forward();  " <<  std::endl;
    //compute output
    dnn::Blob prob = net.getBlob("prob");   //gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    t = clock() - t;
    std::cout << "Time Spent: " << ((float)t)/CLOCKS_PER_SEC << std::endl;
    return 0;
} //main

mnist.prototxt

name: "LeNet"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 28
input_dim: 28
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"
}

错误:

done: importer->populateNet(net)
done: resize(img, img, Size(28, 28));
done: net.setBlob(.data, inputBlob); 
 OpenCV Error: Assertion failed (blobs[0].num() == outCn && blobs[0].channels() == inpCn / group) in allocate, file /home/ubuntu/opencv_contrib/modules/dnn/src/layers/convolution_layer.cpp, line 87
    terminate called after throwing an instance of 'cv::Exception'
      what():  /home/ubuntu/opencv_contrib/modules/dnn/src/layers/convolution_layer.cpp:87: error: (-215) blobs[0].num() == outCn && blobs[0].channels() == inpCn / group in function allocate

    Aborted (core dumped)

2 个答案:

答案 0 :(得分:0)

也许您正在应用不正确的图像。正确的线: Mat img = imread(imageFile,CV_LOAD_IMAGE_GRAYSCALE);

答案 1 :(得分:0)

我遇到了同样的问题。将其更改为CV_LOAD_IMAGE_GRAYSCALE后,代码运行时没有错误。