title: How to extract layers' blob information in caffe?
body: <p>I used <a href="http://caffe.berkeleyvision.org/" rel="nofollow noreferrer">caffe</a> for my machine learning in <strong><code>C++</code></strong>.</p>
  
  <p>After passing through the network as <code>net_-&gt;Forward();</code>, I like to extract individual layer's information.</p>
  
  <p>What I did was</p>
  
  <pre><code>          net_-&gt;Forward();
  
            //Extract layer information
            cout &lt;&lt; "Num layers:" &lt;&lt; "'" &lt;&lt; net_-&gt;layer_names().size() &lt;&lt; "'"&lt;&lt; endl;
            for (int layer_index = 0; layer_index &lt; net_-&gt;layer_names().size(); ++layer_index) 
            {
                  // get that layer blob and its dimension
                  const boost::shared_ptr&lt;Blob&lt;float&gt; &gt; blob = net_-&gt;blob_by_name(net_-&gt;blob_names()[layer_index]);
                  int batch_size = blob-&gt;num();                
                  int dim_features = blob-&gt;count() / batch_size;
                  std::cout &lt;&lt; "Layer name:" &lt;&lt; "'" &lt;&lt; net_-&gt;layer_names()[layer_index] &lt;&lt; "'" &lt;&lt; " Blob name:" &lt;&lt; "'" &lt;&lt;net_-&gt;blob_names()[layer_index] &lt;&lt; "'" &lt;&lt; " batch size " &lt;&lt; "'" &lt;&lt; batch_size &lt;&lt; "'" &lt;&lt; " dim_features:" &lt;&lt; "'" &lt;&lt; dim_features &lt;&lt; "'" &lt;&lt;  std::endl;
            }
  </code></pre>
  
  <p>I can see all layer names and dimensions.</p>
  
  <pre><code>Layer name'image' Blob name'image' batch_size'1' dim_features'921600'
  Layer name'conv1/7x7_s2' Blob name'conv1/7x7_s2' batch_size'1' dim_features'4915200'
  Layer name'conv1/relu_7x7' Blob name'pool1/3x3_s2' batch_size'1' dim_features'1228800'
  Layer name'pool1/3x3_s2' Blob name'pool1/norm1' batch_size'1' dim_features'1228800'
  Layer name'pool1/norm1' Blob name'conv2/3x3_reduce' batch_size'1' dim_features'1228800'
  Layer name'conv2/3x3_reduce' Blob name'conv2/3x3' batch_size'1' dim_features'3686400'
  Layer name'conv2/relu_3x3_reduce' Blob name'conv2/norm2' batch_size'1' dim_features'3686400'
  </code></pre>
  
  <p>But here I still need more in depth information.</p>
  
  <p>Current dimension is 921600, that is composed of batch size x channels x height x width = 921600.</p>
  
  <p>(1)So my first query is how to split the information?</p>
  
  <p>(2)Say I have this information batch size x channels x height x width = 1 x 3 x 480 x 640. Then</p>
  
  <p>how to extract each layer in blob like 1 x 1 x 480 x 640, so that I can print or I can plot.</p>
  
  <p>That sort of thing I can do in Python like</p>
  
  <pre><code>for layer_name, blob in net.blobs.iteritems():
          print layer_name + '\t' + str(blob.data.shape)
              mydata = net.blobs[layer_name].data[0,0,:,:]
              #cv2.imshow("mydata",mydata);
              #cv2.waitKey(1)
              layer_name = layer_name.replace('/', '_')
              np.savetxt("printdata/"+layer_name+".csv", mydata, delimiter=",")
  </code></pre>
  
  <p>But for now, I like to do in <strong><code>C++</code></strong>.</p>