caffe框架中Blob细则

12 篇文章 0 订阅
5 篇文章 0 订阅

Blob是一个类模板,封装了SyncedMemory类,作为基本计算单元服务Layer,Net,Solver等。使用同步时必须#include”caffe/syncedmem.hpp”。

//Blob中的shape_string()函数
    inline string shape_string() const{//使用const的函数,使得传入的this是const的,所以不能对数据成员进行修改。
        ostringstream stream;//定义一个输出字符串流对象
        for(int I = 0; i <  shape_size(); i++){
            stream << shape_[i] << " ";//将shape_的vector中每个元素都都给stream
        }
        stream << << "(" << count_ << ")";//将count_的值输出到stream
        return stream.str();//将stream转换成string并将该字符串返回
    }


//转换坐标轴函数:
    inline int CanonicalAxisIndex(int axis_index) const{…};
    该函数保证对于坐标索引 [-N,N) 转换到普通索引 [0,N)。如果输入的是负索引值,表示从后向前访问,-1表示最后一个元素。

偏移量计算函数:
    inline int offset(const int n,const int c = 0,const int h = 0;const int w = 0)const{
        //CHECK各个参数额范围合法性return ((n*channels() + c)*height() + h)*width()+w;
    }
    inline int offset(const vector<int>& indices)const{//上面函数的重载
        CHECK_LE(indices.size(),num_axes());//检查索引向量的大小不超过维度数目

        int offset = 0;
        for(int i = 0; i<num_axes(); ++i){
            offset *= shape(i);
            if(indices.size() > i){
                CHECK_GE(indices[i],0);
                CHECK_LT(indices.size(),shape(i));
                offset += indices[i];
            }
        }
    }

//通过inline const shared_ptr<SyncedMemory>& diff() const{…}函数来实现CPU/GPU的内存共享。

//反序列化函数
    void FromProto(const BlobProto &proto,bool reshape = true);
//序列化函数
void ToProto(BlobProto* proto,bool write_diff = false);//默认序列化时不带diff
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值