batuman batuman -4 years ago 209
C++ Question

How to extract layers' blob information in caffe?

I used caffe for my machine learning in

C++
.

After passing through the network as
net_->Forward();
, I like to extract individual layer's information.

What I did was

net_->Forward();

//Extract layer information
cout << "Num layers:" << "'" << net_->layer_names().size() << "'"<< endl;
for (int layer_index = 0; layer_index < net_->layer_names().size(); ++layer_index)
{
// get that layer blob and its dimension
const boost::shared_ptr<Blob<float> > blob = net_->blob_by_name(net_->blob_names()[layer_index]);
int batch_size = blob->num();
int dim_features = blob->count() / batch_size;
std::cout << "Layer name:" << "'" << net_->layer_names()[layer_index] << "'" << " Blob name:" << "'" <<net_->blob_names()[layer_index] << "'" << " batch size " << "'" << batch_size << "'" << " dim_features:" << "'" << dim_features << "'" << std::endl;
}


I can see all layer names and dimensions.

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'


But here I still need more in depth information.

Current dimension is 921600, that is composed of batch size x channels x height x width = 921600.

(1)So my first query is how to split the information?

(2)Say I have this information batch size x channels x height x width = 1 x 3 x 480 x 640. Then

how to extract each layer in blob like 1 x 1 x 480 x 640, so that I can print or I can plot.

That sort of thing I can do in Python like

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=",")


But for now, I like to do in
C++
.

Answer Source

For question 1:

If you look into blob.hpp file it has several method to get dimensions of the blob. You can call either shape() that returns const vector<int>& or deprecated methods like num(), channels(), height(), width() which returns int. So for your case it should be like:

int batch_size = blob->num();
int channels = blob->channels();
int height = blob->height();
int width = blob->width();
std::cout << "Layer name:" << "'" << net_->layer_names()[layer_index] << "'" << " Blob name:" << "'" <<net_->blob_names()[layer_index] << "'" << " batch size " << "'" << batch_size << "'" << " channels:" << "'" << channels << "'" << " height:" << "'" << height << "'" << " width:" << "'" << width << "'" <<  std::endl;

Edit:

For question 2: If the layer has 3 channels then you can do this to show as image:

// assign channels
std::vector<cv::Mat> ccs;
cv::Size ss(width, height);
float* data = blob->mutable_cpu_data();
for (int i = 0; i < channels; ++i) {
    cv::Mat channel(ss, CV_32FC1, data);
    ccs.push_back(channel);
    data += ss.area();
}
cv::Mat res,dst;
// merge them
cv::merge(ccs, res);
// optional add mean if needed
cv::normalize(res, dst, 0, 1, cv::NORM_MINMAX);
cv::namedWindow("Display window");
cv::imshow("Display window", dst);
cv::waitKey(0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download