caffe基本数据结构---blob

Caffe使用blob存储、交换、操纵这些信息。blob是整个框架的标准的数组结构和统一存储接口。
Blob是Caffe处理和传输的真实数据的包装类,同时它还隐含提供了在CPU和GPU之间同步数据的能力。在数学上,一个blob就是一个4维的数组,它是按照c语言风格存储的,即行优先。由于我们经常对blob的值和梯度感兴趣,所以blob存储了2块data和diff.前者是正常的传输数据,后者是网络计算的梯度。
在实际中如果使用了GPU,你从磁盘上吧数据读取到CPU模式的内存中的blob里,然后调用GPU的kernel来进行计算,然后把blob数据传给下一层,这一切过程都隐藏了底层的实现细节,并且获得很高的性能。只要所有层都有GPU实现,所有的中间数据和梯度都会保存在GPU中。
caffe中最基础的数据结构是blob,它是一个四维的数组。维度从高到低分别是:(num_,channels_,height_,width_)对于图像数据来说就是:图片个数,彩色通道个数,宽,高,比如说有10张图片,分别是512*256大小,彩色三通道,则为:(10,3,256,512)


blob是一个模板类,现在来看一下blob的头文件
[cpp]  view plain  copy
  1. *************************************************************************  
  2. #ifndef CAFFE_BLOB_HPP_  
  3. #define CAFFE_BLOB_HPP_  
  4. #include <algorithm>  
  5. #include <string>  
  6. #include <vector>  
  7. #include "caffe/common.hpp"  
  8. #include "caffe/proto/caffe.pb.h" //里面声明了Blobproto、Blobshape等遵循caffe.proto协议的数据结构  
  9. #include "caffe/syncedmem.hpp"    //CPU/GPU共享内存类,用于数据同步,很多实际的动作都在这里面执行  
  10. *************************************************************************  
  11. const int kMaxBlobAxes = 32;     //blob的最大维数目  
  12. namespace caffe {  
  13. template <typename Dtype>  
  14. class Blob {  
  15.  public:  
  16.    
  17. //blob的构造函数,不允许隐式的数据类型转换  
  18.   Blob(): data_(), diff_(), count_(0), capacity_(0) {}  
  19.   explicit Blob(const int num, const int channels, const int height,  
  20.       const int width);  
  21.   explicit Blob(const vector<int>& shape);  
  22.     
  23.   //几个Reshape函数,对blob的维度进行更改  
  24.   void Reshape(const int num, const int channels, const int height, const int width);       // 用户的reshape接口(常用)  
  25.   void Reshape(const vector<int>& shape);       // 通过重载调用真正的reshape函数  
  26.   void Reshape(const BlobShape& shape);         // 用户的reshape接口  
  27.   void ReshapeLike(const Blob& other);          // 用户的reshape接口  
  28.   //获取Blob的形状字符串,用于打印log,比如: 10 3 256 512 (3932160),总元素个数    
  29.   inline string shape_string() const {  
  30.     ostringstream stream;  
  31.     for (int i = 0; i < shape_.size(); ++i) {  
  32.       stream << shape_[i] << " ";           //打印每一个维度信息  
  33.     }  
  34.     stream << "(" << count_ << ")";         //打印总的元素的个数  
  35.     return stream.str();  
  36.   }  
  37.   inline const vector<int>& shape() const { return shape_; }  // 成员函数,返回blob的形状信息(常用)  
  38.   
  39.   
  40.   inline int shape(int index) const {                         // 返回blob特定维度的大小(常用)  
  41.     return shape_[CanonicalAxisIndex(index)];   
  42.   }  
  43.   inline int num_axes() const { return shape_.size(); }       // 返回blob维度  
  44.   inline int count() const { return count_; }                 // 返回元素的个数  
  45.   
  46.   
  47.   inline int count(int start_axis, int end_axis) const {      // 返回特定维度区间的元素的个数  
  48.     CHECK_LE(start_axis, end_axis);  
  49.     CHECK_GE(start_axis, 0);  
  50.     CHECK_GE(end_axis, 0);  
  51.     CHECK_LE(start_axis, num_axes());  
  52.     CHECK_LE(end_axis, num_axes());  
  53.     int count = 1;  
  54.     for (int i = start_axis; i < end_axis; ++i) {  
  55.       count *= shape(i);  
  56.     }  
  57.     return count;  
  58.   }  
  59.     
  60.   inline int CanonicalAxisIndex(int axis_index) const {       // 检查输入的维度的合法性  
  61.     CHECK_GE(axis_index, -num_axes())  
  62.         << "axis " << axis_index << " out of range for " << num_axes()  
  63.         << "-D Blob with shape " << shape_string();  
  64.     CHECK_LT(axis_index, num_axes())  
  65.         << "axis " << axis_index << " out of range for " << num_axes()  
  66.         << "-D Blob with shape " << shape_string();  
  67.     if (axis_index < 0) {  
  68.       return axis_index + num_axes();  
  69.     }  
  70.     return axis_index;  
  71.   }  
  72.   
  73.   
  74.   inline int num() const { return LegacyShape(0); }           // 返回样本的个数(常用)  
  75.   inline int channels() const { return LegacyShape(1); }      // 返回通道的个数(常用)  
  76.   inline int height() const { return LegacyShape(2); }        // 返回样本维度一,对于图像而言是高度(常用)  
  77.   inline int width() const { return LegacyShape(3); }         // 返回样本维度二,对于图像而言是宽度(常用)  
  78.     
  79.   //返回特定维度的大小,包含对输入维度的合法性检查,被上面函数调用  
  80.   inline int LegacyShape(int index) const {       
  81.     CHECK_LE(num_axes(), 4)  
  82.         << "Cannot use legacy accessors on Blobs with > 4 axes.";  
  83.     CHECK_LT(index, 4);  
  84.     CHECK_GE(index, -4);  
  85.     if (index >= num_axes() || index < -num_axes()) {  
  86.       // Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse  
  87.       // indexing) -- this special case simulates the one-padding used to fill  
  88.       // extraneous axes of legacy blobs.  
  89.       return 1;  
  90.     }  
  91.     return shape(index);  
  92.   }  
  93.   // 计算当前的样本的偏移量,供后面序列化寻址使用  
  94.   inline int offset(const int n, const int c = 0, const int h = 0,const int w = 0) const {  
  95.     CHECK_GE(n, 0);  
  96.     CHECK_LE(n, num());  
  97.     CHECK_GE(channels(), 0);  
  98.     CHECK_LE(c, channels());  
  99.     CHECK_GE(height(), 0);  
  100.     CHECK_LE(h, height());  
  101.     CHECK_GE(width(), 0);  
  102.     CHECK_LE(w, width());  
  103.     return ((n * channels() + c) * height() + h) * width() + w;  
  104.   }  
  105.   
  106.   
  107.   inline int offset(const vector<int>& indices) const {  
  108.     CHECK_LE(indices.size(), num_axes());  
  109.     int offset = 0;  
  110.     for (int i = 0; i < num_axes(); ++i) {  
  111.       offset *= shape(i);  
  112.       if (indices.size() > i) {  
  113.         CHECK_GE(indices[i], 0);  
  114.         CHECK_LT(indices[i], shape(i));  
  115.         offset += indices[i];  
  116.       }  
  117.     }  
  118.     return offset;  
  119.   }  
  120.     
  121.   // 从其他的blob来拷贝到当前的blob中,默认是不拷贝梯度的,如果形状不一致需要使能reshape,不然无法拷贝  
  122.   void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,  
  123.       bool reshape = false);  
  124.         
  125.   // 返回特定位置的元素值  
  126.   inline Dtype data_at(const int n, const int c, const int h,  
  127.       const int w) const {  
  128.     return cpu_data()[offset(n, c, h, w)];  
  129.   }  
  130.   // 返回特定位置的梯度值  
  131.   inline Dtype diff_at(const int n, const int c, const int h,  
  132.       const int w) const {  
  133.     return cpu_diff()[offset(n, c, h, w)];     //这个是序列话的值  
  134.   }  
  135.   // 重载返回特定元素的值,作用与上面函数相同  
  136.   inline Dtype data_at(const vector<int>& index) const {  
  137.     return cpu_data()[offset(index)];  
  138.   }  
  139.   // 重载返回特定梯度的值,作用与上面函数相同  
  140.   inline Dtype diff_at(const vector<int>& index) const {  
  141.     return cpu_diff()[offset(index)];  
  142.   }  
  143.     
  144.   // 返回当前的训练样本的数据(指针)(常用)  
  145.   inline const shared_ptr<SyncedMemory>& data() const {  
  146.     CHECK(data_);  
  147.     return data_;  
  148.   }  
  149.   // 返回当前训练样本的梯度(指针)(常用)  
  150.   inline const shared_ptr<SyncedMemory>& diff() const {  
  151.     CHECK(diff_);  
  152.     return diff_;  
  153.   }  
  154.   
  155.   
  156.   const Dtype* cpu_data() const;   // 只读获取cpu的data_的指针  
  157.   void set_cpu_data(Dtype* data);  // 设置cpu的data_指针,修改指针仅  
  158.   const int* gpu_shape() const;    // 只读获取gpu上数据的形状信息  
  159.   const Dtype* gpu_data() const;   // 只读获取gpu上的data_的指针  
  160.   const Dtype* cpu_diff() const;   // 只读获取cpu的diff_的指针  
  161.   const Dtype* gpu_diff() const;   // 只读获取gpu的diff_的指针  
  162.   Dtype* mutable_cpu_data();       // 读写访问cpu data  
  163.   Dtype* mutable_gpu_data();       // 读写访问gpu data  
  164.   Dtype* mutable_cpu_diff();       // 读写访问cpu diff  
  165.   Dtype* mutable_gpu_diff();       // 读写访问cpu diff  
  166.   void Update();                   // 数据更新,即减去当前计算出来的梯度  
  167.   void FromProto(const BlobProto& proto, bool reshape = true);   // 将数据进行反序列化,从磁盘导入之前存储的blob  
  168.   void ToProto(BlobProto* proto, bool write_diff = falseconst// 将数据进行序列化,便于存储  
  169.   
  170.   
  171.   Dtype asum_data() const;         // 计算data的L1范数  
  172.   Dtype asum_diff() const;         // 计算diff的L1范数  
  173.   Dtype sumsq_data() const;        // 计算data的L2范数  
  174.   Dtype sumsq_diff() const;        // 计算diff的L2范数  
  175.   
  176.   
  177.   void scale_data(Dtype scale_factor);   // 按照一个标量进行伸缩data_  
  178.   void scale_diff(Dtype scale_factor);   // 按照一个标量进行伸缩diff_  
  179.   
  180.   
  181.   void ShareData(const Blob& other);     // 只是拷贝过来other的data  
  182.   void ShareDiff(const Blob& other);     // 只是拷贝过来other的diff  
  183.   
  184.   
  185.   bool ShapeEquals(const BlobProto& other);  // 判断两个blob的形状是否一致  
  186.   
  187.   
  188.  protected:  
  189.   shared_ptr<SyncedMemory> data_;            // 类的属性---数据  
  190.   shared_ptr<SyncedMemory> diff_;            // 类的属性---梯度  
  191.   shared_ptr<SyncedMemory> shape_data_;         
  192.   vector<int> shape_;                        // 类的属性---形状信息  
  193.   int count_;                                // 有效元素总的个数  
  194.   int capacity_;                             // 存放bolb容器的容量信息,大于等于count_  
  195.   
  196.   
  197.   DISABLE_COPY_AND_ASSIGN(Blob);  
  198. };  // class Blob  
  199.   
  200.   
  201. }  // namespace caffe  
  202.   
  203.   
  204. #endif  // CAFFE_BLOB_HPP_  
备注一下最常用的:
blob.data()     // 返回数据
blob.diff()     // 返回梯度
blob.shape()    // 返回样本的形状
blob.num()      // 返回样本的个数(常用)
blob.channels() // 返回通道的个数(常用)
blob.height()   // 返回样本维度一,对于图像而言是高度(常用)
blob.width()    // 返回样本维度二,对于图像而言是宽度(常用)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值