opencv3.3 dnn模块的结果与caffe预测不匹配

时间:2017-08-21 09:44:16

标签: opencv caffe

我使用opencv dnn分类,但结果与caffe预测不匹配。令我感到困惑的是,有些图像可能会得到类似于caffe的结果,而少数图像则没有。当我将BGR更改为RGB时,大多数结果都是错误的。

类似的结果: similar result:

不同的结果: different result:

blobFromImage(norm_img,1.0,cv :: Size(64,64));当使用默认参数时将BGR更改为RGB,但结果会出错。所以我像这样使用blobFromImage(norm_img,1.0,cv :: Size (64,64),cv :: Scalar(),false); 。大部分结果会与caffe预测相匹配,为什么少数图像没有?

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/trace.hpp>
using namespace cv;
using namespace cv::dnn;

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

/* 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); //reshape the blob to 1x1000 matrix
    Point classNumber;

    minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
    *classId = classNumber.x;
}

static std::vector<String> readClassNames(const char *filename = "./config/type.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)
{
    CV_TRACE_FUNCTION();

    String modelTxt = "./config/HCCR3755_res20_deploy.prototxt";
    String modelBin = "./config/HCCR3755-res20_iter_790000.caffemodel";
    String imageFile = "./config/b9.jpg";
    Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
    if (net.empty())
    {
        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);
    }
    Mat img = imread(imageFile);
    FileStorage fs("./config/mean.xml", FileStorage::READ);
    Mat _mean;
    fs["vocabulary"] >> _mean;
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    cv::Mat img_resize;
    resize(img, img_resize, Size(64, 64));

    cv::Mat img_float;
    img_resize.convertTo(img_float, CV_32FC3);
    cv::Mat norm_img;
    cv::subtract(img_float, _mean, norm_img);

    Mat inputBlob = blobFromImage(norm_img, 1.0, cv::Size(64, 64), cv::Scalar(),false);   //Convert Mat to batch of images

    Mat prob;

    cv::TickMeter t;
    for (int i = 0; i < 1; i++)
    {
        CV_TRACE_REGION("forward");
        //! [Set input blob]
        net.setInput(inputBlob, "data");        //set the network input
        //! [Set input blob]
        t.start();
        //! [Make forward pass]
        prob = net.forward("prob"); 
        //std::cout << prob << std::endl;//compute output
        //! [Make forward pass]
        t.stop();
    }

    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    //! [Gather output]

    //! [Print results]
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    //! [Print results]
    std::cout << "Time: " << (double)t.getTimeMilli() / t.getCounter() << " ms (average from " << t.getCounter() << " iterations)" << std::endl;
    getchar();
    return 0;
} //main

0 个答案:

没有答案