Caffe研究之blob
转载请注明:http://blog.csdn.net/forest_world
Caffe:Blob、Layer、Net。
【Deep|Caffe研究之blob】Blob是一个四维的数组,用于存储数据,包括输入数据、输出数据、权值等;
Blob是Caffe中处理和传递实际数据的数据封装包,并且在CPU与GPU之间具有同步处理能力。从数学意义上说,blob是按C风格连续存储的N维数组。
template
class Blob {
public:
Blob()
: data_(), diff_(), count_(0), capacity_(0) {}/// @brief Deprecated;
use Blob(const vector& shape)
.
explicit Blob(const int num, const int channels, const int height,
const int width);
explicit Blob(const vector& shape);
/// @brief Deprecated;
use Reshape(const vector& shape)
.
void Reshape(const int num, const int channels, const int height,
const int width);
/**
* @brief Change the dimensions of the blob, allocating new memory if
*necessary.
*
* This function can be called both to create an initial allocation
* of memory, and to adjust the dimensions of a top blob during Layer::Reshape
* or Layer::Forward. When changing the size of blob, memory will only be
* reallocated if sufficient memory does not already exist, and excess memory
* will never be freed.
*
* Note that reshaping an input blob and immediately calling Net::Backward is
* an error;
either Net::Forward or Net::Reshape need to be called to
* propagate the new input shape to higher layers.
*/
void Reshape(const vector& shape);
void Reshape(const BlobShape& shape);
void ReshapeLike(const Blob& other);
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& shape() const { return shape_;
}
/**
* @brief Returns the dimension of the index-th axis (or the negative index-th
*axis from the end, if index is negative).
*
* @param index the axis index, which may be negative as it will be
*"canonicalized" using CanonicalAxisIndex.
*Dies on out of range index.
*/
inline int shape(int index) const {
return shape_[CanonicalAxisIndex(index)];
}
inline int num_axes() const { return shape_.size();
}
inline int count() const { return count_;
}/**
* @brief Compute the volume of a slice;
i.e., the product of dimensions
*among a range of axes.
*
* @param start_axis The first axis to include in the slice.
*
* @param end_axis The first axis to exclude from the slice.
*/
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;
}
/**
* @brief Compute the volume of a slice spanning from a particular first
*axis to the final axis.
*
* @param start_axis The first axis to include in the slice.
*/
inline int count(int start_axis) const {
return count(start_axis, num_axes());
}/**
* @brief Returns the 'canonical' version of a (usually) user-specified axis,
*allowing for negative indexing (e.g., -1 for the last axis).
*
* @param axis_index the axis index.
*If 0 <= index < num_axes(), return index.
*If -num_axes <= index <= -1, return (num_axes() - (-index)),
*e.g., the last axis index (num_axes() - 1) if index == -1,
*the second to last if index == -2, etc.
*Dies on out of range index.
*/
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;
}/// @brief Deprecated legacy shape accessor num: use shape(0) instead.
inline int num() const { return LegacyShape(0);
}
/// @brief Deprecated legacy shape accessor channels: use shape(1) instead.
inline int channels() const { return LegacyShape(1);
}
/// @brief Deprecated legacy shape accessor height: use shape(2) instead.
inline int height() const { return LegacyShape(2);
}
/// @brief Deprecated legacy shape accessor width: use shape(3) instead.
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& 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;
}
/**
* @brief Copy from a source Blob.
*
* @param source the Blob to copy from
* @param copy_diff if false, copy the data;
if true, copy the diff
* @param reshape if false, require this Blob to be pre-shaped to the shape
*of other (and die otherwise);
if true, Reshape this Blob to other's
*shape if necessary
*/
void CopyFrom(const Blob& 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& index) const {
return cpu_data()[offset(index)];
}inline Dtype diff_at(const vector& index) const {
return cpu_diff()[offset(index)];
}inline const shared_ptr& data() const {
CHECK(data_);
return data_;
}inline const shared_ptr& diff() const {
CHECK(diff_);
return diff_;
}const Dtype* cpu_data() const;
void set_cpu_data(Dtype* data);
const int* gpu_shape() const;
const Dtype* gpu_data() const;
const Dtype* cpu_diff() const;
const Dtype* gpu_diff() const;
Dtype* mutable_cpu_data();
Dtype* mutable_gpu_data();
Dtype* mutable_cpu_diff();
Dtype* mutable_gpu_diff();
void Update();
void FromProto(const BlobProto& proto, bool reshape = true);
void ToProto(BlobProto* proto, bool write_diff = false) const;
/// @brief Compute the sum of absolute values (L1 norm) of the data.
Dtype asum_data() const;
/// @brief Compute the sum of absolute values (L1 norm) of the diff.
Dtype asum_diff() const;
/// @brief Compute the sum of squares (L2 norm squared) of the data.
Dtype sumsq_data() const;
/// @brief Compute the sum of squares (L2 norm squared) of the diff.
Dtype sumsq_diff() const;
/// @brief Scale the blob data by a constant factor.
void scale_data(Dtype scale_factor);
/// @brief Scale the blob diff by a constant factor.
void scale_diff(Dtype scale_factor);
/**
* @brief Set the data_ shared_ptr to point to the SyncedMemory holding the
*data_ of Blob other -- useful in Layer%s which simply perform a copy
*in their Forward pass.
*
* This deallocates the SyncedMemory holding this Blob's data_, as
* shared_ptr calls its destructor when reset with the "=" operator.
*/
void ShareData(const Blob& other);
/**
* @brief Set the diff_ shared_ptr to point to the SyncedMemory holding the
*diff_ of Blob other -- useful in Layer%s which simply perform a copy
*in their Forward pass.
*
* This deallocates the SyncedMemory holding this Blob's diff_, as
* shared_ptr calls its destructor when reset with the "=" operator.
*/
void ShareDiff(const Blob& other);
bool ShapeEquals(const BlobProto& other);
protected:
shared_ptr data_;
shared_ptr diff_;
shared_ptr shape_data_;
vector shape_;
int count_;
int capacity_;
DISABLE_COPY_AND_ASSIGN(Blob);
};
// class Blob
Layer层则是神经网络中具体的各层结构,主要是计算的作用,在根据配置文件初始化结构后,前向计算结果,反向更新参数,都是它要做的,而它的输入和输出都是Blob数据;
Net层,多个Layer组合而成的有向无环图结构,具体的网络。
caffe实现了差不多40种不同的Layer层,里面有不同的激活函数。
Classifier::Classifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const string& label_file) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif/* Load the network. */
net_.reset(new Net(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";
Blob* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< "Input layer should have 1 or 3 channels.";
input_geometry_ = cv::Size(input_layer->width(), input_layer->height());
/* Load the binaryproto mean file. */
SetMean(mean_file);
/* Load labels. */
std::ifstream labels(label_file.c_str());
CHECK(labels) << "Unable to open labels file " << label_file;
string line;
while (std::getline(labels, line))
labels_.push_back(string(line));
Blob* output_layer = net_->output_blobs()[0];
CHECK_EQ(labels_.size(), output_layer->channels())
<< "Number of labels is different from the output layer dimension.";
}
函数说明:
Reshape()可以改变一个blob的大小:
template
void Blob::Reshape(const int num, const int channels, const int height,
const int width) {
vector shape(4);
shape[0] = num;
shape[1] = channels;
shape[2] = height;
shape[3] = width;
Reshape(shape);
}template
void Blob::Reshape(const vector& shape) {
CHECK_LE(shape.size(), kMaxBlobAxes);
count_ = 1;
shape_.resize(shape.size());
if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {
shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));
}
int* shape_data = https://www.it610.com/article/static_cast(shape_data_->mutable_cpu_data());
for (int i = 0;
i < shape.size();
++i) {
CHECK_GE(shape[i], 0);
if (count_ != 0) {
CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
}
count_ *= shape[i];
shape_[i] = shape[i];
shape_data[i] = shape[i];
}
if (count_ > capacity_) {
capacity_ = count_;
data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
}
}template
void Blob::Reshape(const BlobShape& shape) {
CHECK_LE(shape.dim_size(), kMaxBlobAxes);
vector shape_vec(shape.dim_size());
for (int i = 0;
i < shape.dim_size();
++i) {
shape_vec[i] = shape.dim(i);
}
Reshape(shape_vec);
}
ReshapeLike()为data和diff重新分配一块空间,大小和另一个blob的一样:
template
void Blob::ReshapeLike(const Blob& other) {
Reshape(other.shape());
}
Num_axes()返回的是blob的大小;
Count()计算得到count=num*channels*height*width。
Offset()可得到输入blob数据(n,c,h,w)的偏移量位置;
CopyFrom()从source拷贝数据,copy_diff来作为标志区分是拷贝data还是diff
template
void Blob::CopyFrom(const Blob& source, bool copy_diff, bool reshape) {
if (source.count() != count_ || source.shape() != shape_) {
if (reshape) {
ReshapeLike(source);
} else {
LOG(FATAL) << "Trying to copy blobs of different sizes.";
}
}
switch (Caffe::mode()) {
case Caffe::GPU:
if (copy_diff) {
caffe_copy(count_, source.gpu_diff(),
static_cast(diff_->mutable_gpu_data()));
} else {
caffe_copy(count_, source.gpu_data(),
static_cast(data_->mutable_gpu_data()));
}
break;
case Caffe::CPU:
if (copy_diff) {
caffe_copy(count_, source.cpu_diff(),
static_cast(diff_->mutable_cpu_data()));
} else {
caffe_copy(count_, source.cpu_data(),
static_cast(data_->mutable_cpu_data()));
}
break;
default:
LOG(FATAL) << "Unknown caffe mode.";
}
}
FromProto()从proto读数据进来,其实就是反序列化:
文章图片
文章图片
template
void Blob::FromProto(const BlobProto& proto, bool reshape) {
if (reshape) {
vector shape;
if (proto.has_num() || proto.has_channels() ||
proto.has_height() || proto.has_width()) {
// Using deprecated 4D Blob dimensions --
// shape is (num, channels, height, width).
shape.resize(4);
shape[0] = proto.num();
shape[1] = proto.channels();
shape[2] = proto.height();
shape[3] = proto.width();
} else {
shape.resize(proto.shape().dim_size());
for (int i = 0;
i < proto.shape().dim_size();
++i) {
shape[i] = proto.shape().dim(i);
}
}
Reshape(shape);
} else {
CHECK(ShapeEquals(proto)) << "shape mismatch (reshape not set)";
}
// copy data
Dtype* data_vec = mutable_cpu_data();
if (proto.double_data_size() > 0) {
CHECK_EQ(count_, proto.double_data_size());
for (int i = 0;
i < count_;
++i) {
data_vec[i] = proto.double_data(i);
}
} else {
CHECK_EQ(count_, proto.data_size());
for (int i = 0;
i < count_;
++i) {
data_vec[i] = proto.data(i);
}
}
if (proto.double_diff_size() > 0) {
CHECK_EQ(count_, proto.double_diff_size());
Dtype* diff_vec = mutable_cpu_diff();
for (int i = 0;
i < count_;
++i) {
diff_vec[i] = proto.double_diff(i);
}
} else if (proto.diff_size() > 0) {
CHECK_EQ(count_, proto.diff_size());
Dtype* diff_vec = mutable_cpu_diff();
for (int i = 0;
i < count_;
++i) {
diff_vec[i] = proto.diff(i);
}
}
}
ToProto()把blob数据保存到proto中:
template <>
void Blob::ToProto(BlobProto* proto, bool write_diff) const {
proto->clear_shape();
for (int i = 0;
i < shape_.size();
++i) {
proto->mutable_shape()->add_dim(shape_[i]);
}
proto->clear_double_data();
proto->clear_double_diff();
const double* data_vec = cpu_data();
for (int i = 0;
i < count_;
++i) {
proto->add_double_data(data_vec[i]);
}
if (write_diff) {
const double* diff_vec = cpu_diff();
for (int i = 0;
i < count_;
++i) {
proto->add_double_diff(diff_vec[i]);
}
}
}template <>
void Blob::ToProto(BlobProto* proto, bool write_diff) const {
proto->clear_shape();
for (int i = 0;
i < shape_.size();
++i) {
proto->mutable_shape()->add_dim(shape_[i]);
}
proto->clear_data();
proto->clear_diff();
const float* data_vec = cpu_data();
for (int i = 0;
i < count_;
++i) {
proto->add_data(data_vec[i]);
}
if (write_diff) {
const float* diff_vec = cpu_diff();
for (int i = 0;
i < count_;
++i) {
proto->add_diff(diff_vec[i]);
}
}
}
ShareDate()/ShareDiff()从other的blob复制data和diff的值;
推荐阅读
- Machine|scikit-learn-分类模型评价标准
- AI|bert实现端到端继续预训练
- deep|Andrew Ng, deeplearning. Course2 week2,Optimization
- 数学|matlab三维山峰/山脉/山地曲面数据图
- Deep|Caffe——整体结构剖析
- Deep|Caffe——环境安装和配置(CPU)
- Deep|Caffe——模型解析
- machine|LightGBM参数介绍
- Python|Python怎么控制浮点数保留几位小数
- statistic|隐马尔科夫模型(HMM)与线性动态系统(LDS)