Ask Your Question

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

asked 2016-09-28 00:52:24 -0600

yBeans gravatar image


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

Kindly advise on possible solutions.

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

//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//                           License Agreement
//                For Open Source Computer Vision Library
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// 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.
#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;

    std::string name;
    while (!fp.eof())
        std::getline(fp ...
edit retag flag offensive close merge delete


can it be, that this simply does not have a "softmax" layer ?

berak gravatar imageberak ( 2016-09-28 01:46:36 -0600 )edit

Thanks for the reply. Sorry for forgetting that I have actually made some small changes to the final layers, as follows:

 layer {
      name: "loss"
      type: "SoftmaxWithLoss"
      bottom: "pool10"
      bottom: "label"
      top: "loss"
      exclude { stage: "deploy" }
    layer {
      name: "accuracy"
      type: "Accuracy"
      bottom: "pool10"
      bottom: "label"
      top: "accuracy"
      include { stage: "val" }
    layer {
      name: "softmax"
      type: "Softmax"
      bottom: "pool10"
      top: "softmax"
      include { stage: "deploy" }

which essentially replaces "accuracy_top5" with "softmax" and includes, exclude { stage: "deploy" }, include { stage: "val" } and include { stage: "deploy" } to the resp. layers. Managed to get a trained model.

yBeans gravatar imageyBeans ( 2016-09-28 02:40:37 -0600 )edit

Did you figure it out yet?

MrWorshipMe gravatar imageMrWorshipMe ( 2016-10-13 04:30:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-11-07 20:58:45 -0600

chibai gravatar image

I'm doing this, too. It's mainly because probBlob.matRefConst() didn't work. There's one inefficient way to solve the problem is by using the function probBlob.getplane(), you will get a Mat.

I suggest you to use imagewatch and debug to see what happened in each step.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-09-28 00:52:24 -0600

Seen: 1,716 times

Last updated: Nov 07 '16