【问题标题】:OpenCV cv::dnn::Net::getBlob return dnn::Blob::matRefConst has rows == -1 and cols == -1OpenCV cv::dnn::Net::getBlob 返回 dnn::Blob::matRefConst 有 rows == -1 和 cols == -1
【发布时间】:2018-02-05 06:34:57
【问题描述】:

截图:getBlob返回错误结果:

这里是添加代码:

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"*/); 

和 prototxt 文件: # 名称:“nin_imagenet” # 下五行相比 caffe 的 prototxt 有所改变 # 我删除了 top: "data" 的层 input: "data" # 输入名称 input_dim: 1 # 批量
input_dim: 3 # 通道数 input_dim: 224 # 宽度 input_dim: 224 # 高度

# 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

【问题讨论】:

    标签: c++ opencv blob caffe proto


    【解决方案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
    

    【讨论】:

    • 感谢 Andrey。我尝试了 extractPlane 方法。真的很抱歉,我不知道如何选择参数 n 和参数 ch。我尝试了它们都为零。代码运行良好,但结果绝对错误。我想使用我经过试验的 caffe 模型获取“classID”。我的代码从“docs.opencv.org/trunk/d5/de7/tutorial_dnn_googlenet.html”示例更改。
    • 你打印输出张量大小了吗?也可能是您没有对输入数据进行规范化,或者以错误的方式打包它,这就是您得到错误结果的原因。这段代码来自我的工作项目,并且经过测试。
    • 另外,您可能需要交换 R 和 B 平面。检查 blobFromImage 手册。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2019-04-20
    相关资源
    最近更新 更多