您的位置:首页 > 理论基础 > 数据结构算法

caffe基本数据结构---blob

2016-12-12 19:47 330 查看
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的头文件
*************************************************************************
#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_
#include <algorithm>
#include <string>
#include <vector>
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h" //里面声明了Blobproto、Blobshape等遵循caffe.proto协议的数据结构
#include "caffe/syncedmem.hpp"    //CPU/GPU共享内存类,用于数据同步,很多实际的动作都在这里面执行
*************************************************************************
const int kMaxBlobAxes = 32;     //blob的最大维数目
namespace caffe {
template <typename Dtype>
class Blob {
public:

//blob的构造函数,不允许隐式的数据类型转换
Blob(): data_(), diff_(), count_(0), capacity_(0) {}
explicit Blob(const int num, const int channels, const int height,
const int width);
explicit Blob(const vector<int>& shape);

//几个Reshape函数,对blob的维度进行更改
void Reshape(const int num, const int channels, const int height, const int width);       // 用户的reshape接口(常用)
void Reshape(const vector<int>& shape);       // 通过重载调用真正的reshape函数
void Reshape(const BlobShape& shape);         // 用户的reshape接口
void ReshapeLike(const Blob& other);          // 用户的reshape接口
//获取Blob的形状字符串,用于打印log,比如: 10 3 256 512 (3932160),总元素个数
inline string shape_string() const {
ostringstream stream;
for (int i = 0; i < shape_.size(); ++i) {
stream << shape_[i] << " ";           //打印每一个维度信息
}
stream << "(" << count_ << ")";         //打印总的元素的个数
return stream.str();
}
inline const vector<int>& shape() const { return shape_; }  // 成员函数,返回blob的形状信息(常用)

inline int shape(int index) const {                         // 返回blob特定维度的大小(常用)
return shape_[CanonicalAxisIndex(index)];
}
inline int num_axes() const { return shape_.size(); }       // 返回blob维度
inline int count() const { return count_; }                 // 返回元素的个数

inline int count(int start_axis, int end_axis) const {      // 返回特定维度区间的元素的个数
CHECK_LE(start_axis, end_axis);
CHECK_GE(start_axis, 0);
CHECK_GE(end_axis, 0);
CHECK_LE(start_axis, num_axes());
CHECK_LE(end_axis, num_axes());
int count = 1;
for (int i = start_axis; i < end_axis; ++i) {
count *= shape(i);
}
return count;
}

inline int CanonicalAxisIndex(int axis_index) const {       // 检查输入的维度的合法性
CHECK_GE(axis_index, -num_axes())
<< "axis " << axis_index << " out of range for " << num_axes()
<< "-D Blob with shape " << shape_string();
CHECK_LT(axis_index, num_axes())
<< "axis " << axis_index << " out of range for " << num_axes()
<< "-D Blob with shape " << shape_string();
if (axis_index < 0) {
return axis_index + num_axes();
}
return axis_index;
}

inline int num() const { return LegacyShape(0); }           // 返回样本的个数(常用)
inline int channels() const { return LegacyShape(1); }      // 返回通道的个数(常用)
inline int height() const { return LegacyShape(2); }        // 返回样本维度一,对于图像而言是高度(常用)
inline int width() const { return LegacyShape(3); }         // 返回样本维度二,对于图像而言是宽度(常用)

//返回特定维度的大小,包含对输入维度的合法性检查,被上面函数调用
inline int LegacyShape(int index) const {
CHECK_LE(num_axes(), 4)
<< "Cannot use legacy accessors on Blobs with > 4 axes.";
CHECK_LT(index, 4);
CHECK_GE(index, -4);
if (index >= num_axes() || index < -num_axes()) {
// Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
// indexing) -- this special case simulates the one-padding used to fill
// extraneous axes of legacy blobs.
return 1;
}
return shape(index);
}
// 计算当前的样本的偏移量,供后面序列化寻址使用
inline int offset(const int n, const int c = 0, const int h = 0,const int w = 0) const {
CHECK_GE(n, 0);
CHECK_LE(n, num());
CHECK_GE(channels(), 0);
CHECK_LE(c, channels());
CHECK_GE(height(), 0);
CHECK_LE(h, height());
CHECK_GE(width(), 0);
CHECK_LE(w, width());
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[i], shape(i));
offset += indices[i];
}
}
return offset;
}

// 从其他的blob来拷贝到当前的blob中,默认是不拷贝梯度的,如果形状不一致需要使能reshape,不然无法拷贝
void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
bool reshape = false);

// 返回特定位置的元素值
inline Dtype data_at(const int n, const int c, const int h,
const int w) const {
return cpu_data()[offset(n, c, h, w)];
}
// 返回特定位置的梯度值
inline Dtype diff_at(const int n, const int c, const int h,
const int w) const {
return cpu_diff()[offset(n, c, h, w)];     //这个是序列话的值
}
// 重载返回特定元素的值,作用与上面函数相同
inline Dtype data_at(const vector<int>& index) const {
return cpu_data()[offset(index)];
}
// 重载返回特定梯度的值,作用与上面函数相同
inline Dtype diff_at(const vector<int>& index) const {
return cpu_diff()[offset(index)];
}

// 返回当前的训练样本的数据(指针)(常用)
inline const shared_ptr<SyncedMemory>& data() const {
CHECK(data_);
return data_;
}
// 返回当前训练样本的梯度(指针)(常用)
inline const shared_ptr<SyncedMemory>& diff() const {
CHECK(diff_);
return diff_;
}

const Dtype* cpu_data() const;   // 只读获取cpu的data_的指针
void set_cpu_data(Dtype* data);  // 设置cpu的data_指针,修改指针仅
const int* gpu_shape() const;    // 只读获取gpu上数据的形状信息
const Dtype* gpu_data() const;   // 只读获取gpu上的data_的指针
const Dtype* cpu_diff() const;   // 只读获取cpu的diff_的指针
const Dtype* gpu_diff() const;   // 只读获取gpu的diff_的指针
Dtype* mutable_cpu_data();       // 读写访问cpu data
Dtype* mutable_gpu_data();       // 读写访问gpu data
Dtype* mutable_cpu_diff();       // 读写访问cpu diff
Dtype* mutable_gpu_diff();       // 读写访问cpu diff
void Update();                   // 数据更新,即减去当前计算出来的梯度
void FromProto(const BlobProto& proto, bool reshape = true);   // 将数据进行反序列化,从磁盘导入之前存储的blob
void ToProto(BlobProto* proto, bool write_diff = false) const; // 将数据进行序列化,便于存储

Dtype asum_data() const;         // 计算data的L1范数
Dtype asum_diff() const;         // 计算diff的L1范数
Dtype sumsq_data() const;        // 计算data的L2范数
Dtype sumsq_diff() const;        // 计算diff的L2范数

void scale_data(Dtype scale_factor);   // 按照一个标量进行伸缩data_
void scale_diff(Dtype scale_factor);   // 按照一个标量进行伸缩diff_

void ShareData(const Blob& other);     // 只是拷贝过来other的data
void ShareDiff(const Blob& other);     // 只是拷贝过来other的diff

bool ShapeEquals(const BlobProto& other);  // 判断两个blob的形状是否一致

protected:
shared_ptr<SyncedMemory> data_;            // 类的属性---数据
shared_ptr<SyncedMemory> diff_;            // 类的属性---梯度
shared_ptr<SyncedMemory> shape_data_;
vector<int> shape_;                        // 类的属性---形状信息
int count_;                                // 有效元素总的个数
int capacity_;                             // 存放bolb容器的容量信息,大于等于count_

DISABLE_COPY_AND_ASSIGN(Blob);
};  // class Blob

}  // namespace caffe

#endif  // CAFFE_BLOB_HPP_
备注一下最常用的:

blob.data()     // 返回数据

blob.diff()     // 返回梯度

blob.shape()    // 返回样本的形状

blob.num()      // 返回样本的个数(常用)

blob.channels() // 返回通道的个数(常用)

blob.height()   // 返回样本维度一,对于图像而言是高度(常用)

blob.width()    // 返回样本维度二,对于图像而言是宽度(常用)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: