# Revision history [back]

### DNN (SqueezeNet) OpenCV Error: Assertion failed in cv::Mat::reshape

Hi,

Am trying to deploy a trained Caffe model from DIGITS with OpenCV 3.1.0. I have attempted the sample code on both LeNet and AlexNet models without any problem.

However, for benchmarking purpose, I tried do run SqueezeNet V1.1 on the same sample code but having an error at the getMaxClass() function where the OpenCV reshape function is called.

The full error message:

OpenCV Error: Assertion failed (dims <= 2) in cv::Mat::reshape, file C:\opencv\sources\modules\core\src\matrix.cpp, line 982

For your reference, the full code I am running SqueezeNet on is as follows:

/**M///////////////////////////////////////////////////////////////////////////////////////
//
//
//  copy or use the software.
//
//
//                For Open Source Computer Vision Library
//
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.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) */
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 = "E:\\GitRepo\\tds-lane\\Release\\labels.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::dnn::initModule();  //Required if OpenCV is built as static libs

String modelTxt = "deploy.prototxt";
String modelBin = "snapshot_iter_37700.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "Y:\\20160227_191832.511704_2.jpg";

//! [Check that network was read successfully]
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);
}
//! [Check that network was read successfully]

//! [Prepare blob]
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}

//cvtColor(img,img,CV_BGR2GRAY);
resize(img, img, Size(227, 227));                   //GoogLeNet accepts only 224x224 RGB-images
dnn::Blob inputBlob = dnn::Blob::fromImages(img);   //Convert Mat to dnn::Blob batch of images
//! [Prepare blob]

//! [Set input blob]
net.setBlob(".data", inputBlob);        //set the network input
//! [Set input blob]

//! [Make forward pass]
net.forward();                          //compute output
//! [Make forward pass]

//! [Gather output]
dnn::Blob prob = net.getBlob("softmax");   //gather output of "prob" layer

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

//! [Print results]