{"action":"closed","issue":{"url":"https://api.github.com/repos/opencv/opencv/issues/11718","repository_url":"https://api.github.com/repos/opencv/opencv","labels_url":"https://api.github.com/repos/opencv/opencv/issues/11718/labels{/name}","comments_url":"https://api.github.com/repos/opencv/opencv/issues/11718/comments","events_url":"https://api.github.com/repos/opencv/opencv/issues/11718/events","html_url":"https://github.com/opencv/opencv/issues/11718","id":330515945,"node_id":"MDU6SXNzdWUzMzA1MTU5NDU=","number":11718,"title":"getMemoryShapes failed assertion throws cv::Exception. Where is the bug?","user":{"login":"bobuntu","id":355065,"node_id":"MDQ6VXNlcjM1NTA2NQ==","avatar_url":"https://avatars2.githubusercontent.com/u/355065?v=4","gravatar_id":"","url":"https://api.github.com/users/bobuntu","html_url":"https://github.com/bobuntu","followers_url":"https://api.github.com/users/bobuntu/followers","following_url":"https://api.github.com/users/bobuntu/following{/other_user}","gists_url":"https://api.github.com/users/bobuntu/gists{/gist_id}","starred_url":"https://api.github.com/users/bobuntu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bobuntu/subscriptions","organizations_url":"https://api.github.com/users/bobuntu/orgs","repos_url":"https://api.github.com/users/bobuntu/repos","events_url":"https://api.github.com/users/bobuntu/events{/privacy}","received_events_url":"https://api.github.com/users/bobuntu/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-06-08T04:30:52Z","updated_at":"2018-06-08T08:04:28Z","closed_at":"2018-06-08T08:04:28Z","author_association":"NONE","body":"This sample was reproduced from the [OpenCV official tutorial on DNN][1].\r\n\r\n```c++\r\n    #include <opencv2/dnn.hpp>\r\n    #include <opencv2/imgproc.hpp>\r\n    #include <opencv2/highgui.hpp>\r\n    #include <opencv2/core/utils/trace.hpp>\r\n    #include <opencv2/core/ocl.hpp>\r\n    \r\n    #include <fstream>\r\n    #include <iostream>\r\n    #include <cstdlib>\r\n    \r\n    using namespace std;\r\n    using namespace cv;\r\n    using namespace cv::dnn;\r\n    \r\n    static void getMaxClass(const Mat &probBlob, int *classId, double *classProb) {\r\n      Mat probMat = probBlob.reshape(1, 1);\r\n      Point classNumber;\r\n      minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);\r\n      *classId = classNumber.x;\r\n    }\r\n    static vector<String> readClassNames(const char *filename) {\r\n      vector<String> classNames;\r\n      ifstream fp(filename);\r\n      if (!fp.is_open()) {\r\n        cerr << \"Loading failed. FNF_ERR\" << filename << endl;\r\n        exit(-1);\r\n      }\r\n      string name;\r\n      while (!fp.eof()) {\r\n        getline(fp, name);\r\n        if (name.length()) {\r\n          classNames.push_back(name.substr(name.find(' ' + 1)));\r\n        }\r\n      }\r\n      fp.close();\r\n      return classNames;\r\n    }\r\n    \r\n    const char* params = \"{ help           | false | Tutorial OpenCV_DNN for loading googlenet model }\"\r\n                         \"{ proto          | bvlc_googlenet.prototxt | model configuration }\"\r\n                         \"{ model          | bvlc_googlenet.caffemodel | model weights }\"\r\n                         \"{ label          | synset_words.txt | names of ILSVRC2012 classes }\"\r\n                         \"{ image          | space_shuttle.jpg | path to image file }\"\r\n                         \"{ opencl         | false | enable OpenCL }\";\r\n    \r\n    int main(int argc, char **argv) {\r\n      CV_TRACE_FUNCTION();\r\n      CommandLineParser parser(argc, argv, params);\r\n      if (parser.get<bool>(\"help\")) {\r\n        parser.printMessage();\r\n        return 0;\r\n      }\r\n      String modelTxt = parser.get<string>(\"proto\");\r\n      String modelBin = parser.get<string>(\"model\");\r\n      String imageFile = parser.get<String>(\"image\");\r\n      String classNameFile = parser.get<String>(\"label\");\r\n      Net net;\r\n      try {\r\n        net = dnn::readNetFromCaffe(modelTxt, modelBin);\r\n      }\r\n      catch (const cv::Exception& e) {\r\n        cerr << \"Loading kernel and weights failed.\" << endl;\r\n        cerr << \"Exception thrown: \" << e.what() << endl;\r\n      }\r\n      if (net.empty()) {\r\n        std::cerr << \"Can't load network by using the following files: \" << std::endl;\r\n        std::cerr << \"prototxt:   \" << modelTxt << std::endl;\r\n        std::cerr << \"caffemodel: \" << modelBin << std::endl;\r\n        cerr << \"Loading model failed.\" << endl;\r\n        cerr << \"Get a copy from here: http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel\" << endl;\r\n        return -1;\r\n      }\r\n      if (parser.get<bool>(\"opencl\")) {\r\n        net.setPreferableTarget(DNN_TARGET_OPENCL);\r\n      }\r\n      Mat image = imread(imageFile, cv::IMREAD_GRAYSCALE);\r\n      if (image.empty()) {\r\n        cerr << \"Image read failed from: \" << imageFile << endl;\r\n        exit(-1);\r\n      }\r\n      Mat inputBlob = blobFromImage(image, 1.0f, Size(244, 244), Scalar(104, 117, 123), false);\r\n      net.setInput(inputBlob, \"data\");\r\n      Mat prob = net.forward(\"prob\");\r\n      TickMeter t;\r\n      for (int i = 0; i < 10; i++) {\r\n        CV__TRACE_REGION(\"forward\");\r\n        net.setInput(inputBlob, \"data\");\r\n        t.start();\r\n        prob = net.forward(\"prob\");\r\n        t.stop();\r\n      }\r\n      int classId;\r\n      double classProb;\r\n      getMaxClass(prob, &classId, &classProb);\r\n      vector<String> classNames = readClassNames(classNameFile.c_str());\r\n      cout << \"Best class: #\" << classId << \" '\" << classNames.at(classId) << \"'\" << endl;\r\n      cout << \"Probability: \" << classProb << endl;\r\n      cout << \"Time: \" << (double)t.getTimeMilli() / t.getCounter() << \" ms (avg. from \" << t.getCounter() << \" iterations)\" << endl;\r\n      return 0;\r\n    } // main --END\r\n\r\n```\r\nThe sample program returns a probability of success along with a label of the detected object.\r\n\r\n**Directory structure**\r\n```commandline\r\n    .\r\n    ├── build\r\n    │   ├── CMakeCache.txt\r\n    │   ├── CMakeFiles\r\n    │   ├── ...\r\n    │   └── binary\r\n    ├── CMakeLists.txt\r\n    └── src\r\n        └── main.cc\r\n```\r\n\r\n**Run Command**\r\n\r\n```commandline\r\n    ~/path/to/dir$ ./build/binary \\\r\n    --image=/path/to/img/space_shuttle.jpg \\\r\n    --label=/path/to/label/synset_words.txt \\\r\n    --model=/path/to/model/bvlc_googlenet.caffemodel \\\r\n    --opencl=true \\\r\n    --proto=/path/to/serialized/bvlc_googlenet.prototxt\r\n```\r\n\r\n**Output**\r\n\r\n\r\n>[ INFO:0] Initialize OpenCL runtime...\r\n\r\n>[ WARN:0] DNN: OpenCL target is not supported with current OpenCL device (tested with Intel GPUs only), switching to CPU.\r\n**terminate** called after throwing an instance of '**cv::Exception**'\r\n  what():  OpenCV(4.0.0-pre) /path/to/opencv/modules/dnn/src/layers/convolution_layer.cpp:235: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function '**getMemoryShapes**'\r\n\r\n>Aborted (core dumped)\r\n\r\n**Call Stack**\r\n\r\n```c++\r\n    cv::dnn::experimental_dnn_v4::Net::forward\r\n    cv::dnn::experimental_dnn_v4::Net::Impl::setUpNet\r\n    cv::dnn::experimental_dnn_v4::Net::Impl::allocateLayers\r\n    cv::dnn::experimental_dnn_v4::Net::Impl::getLayersShapes\r\n    cv::dnn::experimental_dnn_v4::Net::Impl::getLayerShapesRecursively\r\n    cv::dnn::ConvolutionLayerImpl::getMemoryShapes\r\n```\r\n\r\n**Environment**\r\n\r\n- Ubuntu 18.04 LTS (64-bit), 8 GB Memory\r\n- OpenCV 4.0.0-pre, GCC 8.1.0\r\n- OpenCL 2.1 (Build 10)\r\n- CPU Type: Intel(R) Core(TM) i7-6500U CPU 4 Core 2.5 GHz\r\n- GPU: NO\r\n\r\nI would really appreciate any pointers or direction to a solution.\r\n\r\n  [1]: https://docs.opencv.org/3.3.0/d5/de7/tutorial_dnn_googlenet.html"}}
{"actor":{"display_login":"bobuntu"}}