OpenCV cv :: dnn :: Net :: getBlob返回dnn :: Blob :: matRefConst有行== -1和cols == -1

时间:2018-02-05 06:34:57

标签: c++ opencv blob caffe proto

屏幕截图:返回错误的getBlob结果:

image here

这是附加代码:

resize(img, img, Size(224, 224));

dnn::Blob inputBlob = dnn::Blob::fromImages(img); 
net.setBlob(".data", inputBlob); 
net.forward(); 
dnn::Blob prob = net.getBlob( "loss1"/*"prob"*/); 

和原型文件:     #name:“nin_imagenet”     与caffe的原型相比,#next五行改变了     #i删除 top:“data”的图层     输入:“data”#输入名称     input_dim:1#batchsize
    input_dim:3#通道数     input_dim:224#width     input_dim:224#height

# unchaged text
# ...

# another changed compared to caffe's prototxt
# i delete layers who has **bottom: "label"**  
layers {
  name: "loss1"
  type: SOFTMAX
  bottom: "fc81"
  top: "loss1"
}
# changed below

1 个答案:

答案 0 :(得分:1)

我认为是因为你处理4D blob而不是矩阵,大小存储在size数组中(见下面的例子)。 尝试使用此代码段提取平面:

//-------------------------------------------------------
// Extract plane with defined n and c from nchw blob
//-------------------------------------------------------
void mtcnn::extractPlane(Mat &src, int n, int ch, Mat &dst)
{
    const int rows = src.size[2];
    const int cols = src.size[3];
    dst = cv::Mat::zeros(rows, cols, CV_32FC1);

    for (int row = 0; row < rows; row++)
    {
        const float *ptrsrc = src.ptr<float>(n, ch, row);
        float *ptrdst = dst.ptr<float>(row);
        for (int col = 0; col < cols; col++)
        {
            ptrdst[col] = ptrsrc[col];
        }
    }
} 

希望您使用类似的东西来设置输入数据:

        inputBlob = blobFromImage(img, 0.0078125, Size(ws, hs), Scalar(127.5, 127.5, 127.5)); //Convert Mat to batch of images
        p_net.setInput(inputBlob, "data"); //set the network input