forresti / SqueezeNet

SqueezeNet: AlexNet-level accuracy with 50x fewer parameters
BSD 2-Clause "Simplified" License
2.17k stars 723 forks source link

Any one run SqueezeNet by opencv dnn module?? #26

Closed chibai closed 7 years ago

chibai commented 7 years ago

I tried to run SqueezeNet in opencv dnn module but i got the opencv error Assertion failed <dim <= 2> in cv::Mat::reshape..............................

Anyone succesfully run squeezeNet in opencv dnn module??!!

chibai commented 7 years ago

include <opencv2\opencv.hpp>

include <opencv2\dnn.hpp>

include

include

include

using namespace std; using namespace cv; using namespace cv::dnn;

int main() { //input string classNameFile = "synset_words.txt"; String imageFile = "1.jpg"; String modelTxt = "deploy.prototxt"; String modelBin = "squeezenet_v1.0.caffemodel"; // image preprocessing Mat img = imread(imageFile); if (img.empty()) { cerr << "fail to load the image" << endl; exit(-1); } resize(img, img, Size(227, 227)); //read class name syssets word vector className; string name; ifstream fp(classNameFile); if (!fp.is_open()) { cerr << "fail to load the class names"; } while (!fp.eof()) { getline(fp, name); if (name.length()) { className.push_back(name.substr(name.find(' ') + 1)); } } fp.close(); //load caffe model cv::dnn::initModule(); Net CaffeNet = dnn::readNetFromCaffe(modelTxt, modelBin); if (CaffeNet.empty()) { cerr << "fail to load model" << endl; exit(-1); } // forward pass dnn::Blob imgBlob = dnn::Blob::fromImages(img); CaffeNet.setBlob(".data", imgBlob); CaffeNet.forward(); dnn::Blob probBlob = CaffeNet.getBlob("prob"); Mat proMat = probBlob.matRefConst().reshape(1, 1); //

return 0;

}

this is the full code, and the problem comes from the last line Mat proMat = probBlob.matRefConst().reshape(1, 1);

chibai commented 7 years ago

I somehow solved this by probBlob.getPlane()function but still do not know why probBlob.matRefConst() didn't work

ZhangXinNan commented 5 years ago

@chibai I download pre-built opencv 3.4.3 on my windows 10. But init_module is not the member of cv::dnn .

chibai commented 5 years ago

@chibai I download pre-built opencv 3.4.3 on my windows 10. But init_module is not the member of cv::dnn .

API always changed. Please refer the newest version https://github.com/opencv/opencv/blob/master/samples/dnn/classification.cpp