opencv - The result of opencv3.3 dnn module not match the caffe prediction -
i used opencv dnn classification, result not match caffe prediction. confused me images similar result caffe,a small number of images not.when changed bgr rgb, of results ware wrong.
similar result: 
different result: 
blobfromimage(norm_img, 1.0, cv::size(64, 64));when used default parameters changed bgr rgb ,but result wrong .so used blobfromimage(norm_img, 1.0, cv::size(64, 64), cv::scalar(),false); .most of result matched caffe prediction,why small number of images not?
#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 blob (i. e. class maximal probability) */ static void getmaxclass(const mat &probblob, int *classid, double *classprob) { mat probmat = probblob.reshape(1, 1); //reshape blob 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 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 using 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 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 batch of images mat prob; cv::tickmeter t; (int = 0; < 1; i++) { cv_trace_region("forward"); //! [set input blob] net.setinput(inputblob, "data"); //set 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 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 " << t.getcounter() << " iterations)" << std::endl; getchar(); return 0; } //main
Comments
Post a Comment