3

I tried to use the same code with some modification here for digit recognitions using a trained model for mnist based on Caffe. My modified source code and network is as follows. I also included the error message at the end. The image is used in the code is a sample image from mnist. The line of code that it cannot go through is

net.forward();  

And I'm pretty sure it is because of the dimension of input.

Any ideas how to run the code for 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"
}

Error:

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)
4

2 回答 2

1

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

于 2016-03-25T06:38:56.800 回答
0

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

于 2016-05-17T22:57:02.263 回答