我怎么知道使用caffe framwork和c ++程序的层中是否存在偏差

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

本文关键字:是否 存在 程序 caffe framwork 我怎么知道      更新时间:2023-10-16

我正在尝试使用 c++ 读取 caffe 框架中的权重和偏差。这是我的代码

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

但是,如果对于某些模型,偏差不存在或定义,则通过分割错误错误来定义偏差。

那么哪个函数返回一个布尔值来指示偏差的预感以及如何在 c++ 中调用该函数呢?

current_layer->blobs()返回的blobs存储在一个std::vector中,可以使用其size属性:

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

有关更多详细信息,请参阅python接口的类似答案。

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;
     }
}

最终可以从中获取数据(权重和偏差参数)

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