Skip to content

OpenCV Error: Incorrect size of input array (Inconsitent shape for ConcatLayer) in getMemoryShapes, file opencv-3.4.0/modules/dnn/src/layers/concat_layer.cpp, line 91 terminate called after throwing an instance of 'cv::Exception' what(): opencv-3.4.0/modules/dnn/src/layers/concat_layer.cpp:91: error: (-201) Inconsitent shape for ConcatLayer in function getMemoryShapes #10739

@BKZero

Description

@BKZero

opencv 3.4.0
ubuntu 14.04
tensorflow 1.1
the example file
2.pb.zip
opencv_v1.py.zip
layerlist.txt

the error:
OpenCV Error: Incorrect size of input array (Inconsitent shape for ConcatLayer) in getMemoryShapes, file opencv-3.4.0/modules/dnn/src/layers/concat_layer.cpp, line 91
terminate called after throwing an instance of 'cv::Exception'
what(): opencv-3.4.0/modules/dnn/src/layers/concat_layer.cpp:91: error: (-201) Inconsitent shape for ConcatLayer in function getMemoryShapes

Aborted (core dumped)

i add log in concat_layer.cpp like:

            CV_Assert(curShape.size() == outputs[0].size()); std::cout<<"concat before for "<<outputs[0].size()<<std::endl;
            for (int curAxis = 0; curAxis < outputs[0].size(); curAxis++)
            {
                std::cout<<"concat "<<curAxis<<" "<<cAxis<<" "<<outputs[0][curAxis]<<" "<<curShape[curAxis]<<std::endl;
                if (curAxis != cAxis && outputs[0][curAxis] != curShape[curAxis])
                    CV_Error(Error::StsBadSize, "Inconsitent shape for ConcatLayer");
            }

and got log:
concat before for 4
concat 0 3 1 1
concat 1 3 15 15
concat 2 3 16 16
concat 3 3 18 18
concat before for 4
concat 0 3 1 1
concat 1 3 15 32

my code:
int main(int argc, char **argv)
{
cv::CommandLineParser parser(argc, argv, keys);

if (parser.has("help"))
{
    parser.printMessage();
    return 0;
}

String modelFile = "2.pb";
String imageFile = "foo-002.jpeg";
String inBlobName = "opencv_v1/InputPlaceholder";
String outBlobName = "opencv_v1/Output";

if (!parser.check())
{
    parser.printErrors();
    return 0;
}

String classNamesFile = "layerlist.txt"; //parser.get<String>("c_names");
String resultFile = "result.txt"; //parser.get<String>("result");

//! [Initialize network]
dnn::Net net = readNetFromTensorflow(modelFile);
//! [Initialize network]

if (net.empty())
{
    std::cerr << "Can't load network by using the mode file: " << std::endl;
    std::cerr << modelFile << std::endl;
    exit(-1);
}

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

cv::Mat img_gray;
std::cout<<"after read img"<<std::endl;
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);

Mat inputBlob = blobFromImage(img_gray, 1.0f, Size(39, 39), Scalar(), true, false);   //Convert Mat to batch of images
//! [Prepare blob]
inputBlob -= 117.0;

std::cout<<"blobFromImage"<<std::endl;
//! [Set input blob]
net.setInput(inputBlob, inBlobName);        //set the network input
//! [Set input blob]

std::cout<<"setInput"<<std::endl;

cv::TickMeter tm;
tm.start();

//! [Make forward pass]
Mat result = net.forward(outBlobName);                          //compute output
//! [Make forward pass]

std::cout<<"forward"<<std::endl;

tm.stop();

if (!resultFile.empty()) {
    CV_Assert(result.isContinuous());

    ofstream fout(resultFile.c_str(), ios::out | ios::binary);
    fout.write((char*)result.data, result.total() * sizeof(float));
    fout.close();
}

std::cout << "Output blob shape " << result.size[0] << " x " << result.size[1] << " x " << result.size[2] << " x " << result.size[3] << std::endl;
std::cout << "Inference time, ms: " << tm.getTimeMilli()  << std::endl;

if (!classNamesFile.empty()) {
    std::vector<String> classNames = readClassNames(classNamesFile.c_str());

    int classId;
    double classProb;
    getMaxClass(result, &classId, &classProb);//find the best class

    //! [Print results]
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
}
return 0;

} //main

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions