在caffe中获取Blob数据

时间:2018-01-12 05:32:43

标签: c++ deep-learning caffe

我试图将caffe中的图层绘制成如下图。

    int ncols = (int)(sqrt (blob.channels()));
    int nrows;
    if(blob.channels()%ncols!=0){
        nrows = ncols+1;
    }

    int Rows = nrows*blob.height();
    int Cols = ncols*blob.width();
    cv::Mat image = cv::Mat::zeros(Rows+nrows, Cols+ncols, CV_32FC1);
    ///////Plotting output of individual layer
    if(blob.height()>1 && blob.width()>1){
            cv::Size ss(blob.width(), blob.height());
            Dtype* data = blob.mutable_cpu_data();
            int r=0; int c=0;
            for(int k=0; k < blob.channels(); k++)
            {
              cv::Mat channel(ss, CV_32FC1, data);

              channel.copyTo(image(cv::Rect(c*blob.width()+1, r*blob.height()+1,  blob.width(), blob.height())));
              c++;
              if(c>0 &&c%ncols==0){
                  r++;
                  c=0;
              }

              channel.release();
              data += ss.area();
            }
    }

为此我有错误

CXX src/caffe/net.cpp
src/caffe/net.cpp: In instantiation of ‘void caffe::Net<Dtype>::ForwardDebugInfo(int) [with Dtype = float]’:
src/caffe/net.cpp:1040:1:   required from here
src/caffe/net.cpp:632:49: error: passing ‘const caffe::Blob<float>’ as ‘this’ argument discards qualifiers [-fpermissive]
             Dtype* data = blob.mutable_cpu_data();
                                                 ^
In file included from ./include/caffe/layer.hpp:8:0,
                 from src/caffe/net.cpp:11:
./include/caffe/blob.hpp:225:10: note:   in call to ‘Dtype* caffe::Blob<Dtype>::mutable_cpu_data() [with Dtype = float]’
   Dtype* mutable_cpu_data();
          ^
src/caffe/net.cpp: In instantiation of ‘void caffe::Net<Dtype>::ForwardDebugInfo(int) [with Dtype = double]’:
src/caffe/net.cpp:1040:1:   required from here
src/caffe/net.cpp:632:49: error: passing ‘const caffe::Blob<double>’ as ‘this’ argument discards qualifiers [-fpermissive]
             Dtype* data = blob.mutable_cpu_data();
                                                 ^
In file included from ./include/caffe/layer.hpp:8:0,
                 from src/caffe/net.cpp:11:
./include/caffe/blob.hpp:225:10: note:   in call to ‘Dtype* caffe::Blob<Dtype>::mutable_cpu_data() [with Dtype = double]’
   Dtype* mutable_cpu_data();
          ^
Makefile:575: recipe for target '.build_debug/src/caffe/net.o' failed
make: *** [.build_debug/src/caffe/net.o] Error 1

该错误意味着什么? 早期版本的咖啡,很好。我之前做过。 现在可能是什么错误?

1 个答案:

答案 0 :(得分:2)

该错误转换为&#34;您将const对象作为this参数传递给非const方法mutable_cpu_data&#34;

const Dtype* cpu_data() const;
Dtype* mutable_cpu_data();

&#34;将对象作为this参数传递&#34;建议使用运营商。或 - &gt;访问对象的方法和使用operator()。

如果你这样做,你可能会改变const对象,所以它是一个错误,除非使用了许可模式。