Ganesh M S
Ganesh M S

Reputation: 1063

How can I know if there is no Bias exists in a layer using caffe framwork with c++ program

I'm trying to read weight and bias in a caffe framework with c++. Here is my code

shared_ptr<Blob<float> >& weight = current_layer->blobs()[0];//for weights
shared_ptr<Blob<float> >& bias = current_layer->blobs()[1];//for bias

But if for some model the bias is not present or defined it throughs a segmentation fault error.

So which function returns a boolean value which indicates the presens of bias and how to call the function in c++?

Upvotes: 1

Views: 69

Answers (2)

ShreeVidhya
ShreeVidhya

Reputation: 123

const std::vector<string> lnames = net_->layer_names();

for (int layer_index = 0; layer_index < net_->layer_names().size(); ++layer_index)
{
     const shared_ptr<Layer<float> > CAlayer = net_->layer_by_name(lnames[layer_index]);
     std::cout << lnames[layer_index] << std::endl;

     if(CAlayer->blobs().size() > 1)
     {
             std::cout << "weight-shape" << CAlayer->blobs()[0]->shape_string() << std::endl;
             std::cout << "weight-count" << CAlayer->blobs()[0]->count() << std::endl;
             std::cout << "bias-shape" << CAlayer->blobs()[1]->shape_string() << std::endl;
             std::cout << "bias-count" << CAlayer->blobs()[1]->count() << std::endl;
     }
}

Eventally the data (weights & bias params) can be fetched from

 CAlayer->blobs()[0]->cpu_data()[...] 

Upvotes: 1

Shai
Shai

Reputation: 114966

The blobs returned from current_layer->blobs() are stored in a std::vector, you can use its size property:

if (current_layer->blobs().size() > 1) {
    shared_ptr<Blob<float> >& bias = current_layer->blobs()[1];//for bias
}

See this similar answer for python interface for more details.

Upvotes: 1

Related Questions