caffe中的Blob块

首先说明:Blob定义了一个类模板。

让我们看一下Blob的头文件里有什么哈:

定义了一个全局变量:

const int kMaxBlobAxes = 32;

看看它的构造函数:

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函数:

void Reshape(const int num, const int channels, const int height, const int width);
void Reshape(const vector<int>& shape);
void Reshape(const BlobShape& shape);

void ReshapeLike(const Blob& other);

内联函数shape_string:作用就是把shape的数据变为字符输出,里面用到了ostringstream流;

inline string shape_string() const {
    ostringstream stream;
    for (int i = 0; i < shape_.size(); ++i) {
      stream << shape_[i] << " ";
    }
    stream << "(" << count_ << ")";
    return stream.str();
  }

内联函数shape():

//作用:返回shape_的引用;
inline const vector<int>& shape() const { return shape_; }
//作用:输入第几维度,返回该维度的大小;
inline int shape(int index) const {
    return shape_[CanonicalAxisIndex(index)];
 
 
//内联函数:num_axes(),作用:返回是多少维度的块;
inline int num_axes() const { return shape_.size(); }
//内联函数:count():
inline int count() const { return count_; }   //返回一共多少数据啊,;
inline int count(int start_axis, int end_axis) const //返回输入的索引之间的大小;
inline int count(int start_axis) const  //太麻烦了,不写了,自己看代码吧;

canonicalAxisIndex的作用就是:把正的索引不变,负的改为正的,另外,还是检查一下它们的范围啦;

CanonicalAxisIndex(int axis_index) const

offset函数就是计算一下索引的偏移值,因为吧,多维数组在内存里也是按一维 存放的:

inline int offset(const int n, const int c = 0, const int h = 0, const int w = 0) const {
……
return ((n * channels() + c) * height() + h) * width() + w;
}
// 用向量的方式把坐标值传入:
inline int offset(const vector<int>& indices) const

函数copyfrom,作用把数据从Blob块中复制到内存中

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
inline Dtype diff_at(const int n, const int c, const int h, const int w) const
 inline Dtype data_at(const vector<int>& index) const
inline Dtype diff_at(const vector<int>& index) const

获得data_与diff_的函数:

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

下面是定义的变量:

shared_ptr<SyncedMemory> data_;
  shared_ptr<SyncedMemory> diff_;
  shared_ptr<SyncedMemory> shape_data_;
  vector<int> shape_;
  int count_;
  int capacity_;
时间: 2024-08-25 00:30:51

caffe中的Blob块的相关文章

caffe中权值初始化方法

首先说明:在caffe/include/caffe中的 filer.hpp文件中有它的源文件,如果想看,可以看看哦,反正我是不想看,代码细节吧,现在不想知道太多,有个宏观的idea就可以啦,如果想看代码的具体的话,可以看:http://blog.csdn.net/xizero00/article/details/50921692,写的还是很不错的(不过有的地方的备注不对,不知道改过来了没). 文件 filler.hpp提供了7种权值初始化的方法,分别为:常量初始化(constant).高斯分布初

Oracle数据库中的blob类型解析

Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据. 写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢? 这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor用select查询出来,这样通过两步操作

Caffe中对MNIST执行train操作执行流程解析

之前在 http://blog.csdn.net/fengbingchun/article/details/49849225 中简单介绍过使用Caffe train MNIST的文章,当时只是仿照caffe中的example实现了下,下面说一下执行流程,并精简代码到仅有10余行: 1.        先注册所有层,执行layer_factory.hpp中类LayerRegisterer的构造函数,类LayerRegistry的AddCreator和Registry静态函数:关于Caffe中Lay

如何在caffe中增加layer以及caffe中triple loss layer的实现

关于triplet loss的原理,目标函数和梯度推导在上一篇博客中已经讲过了,具体见:triplet loss原理以及梯度推导,这篇博文主要是讲caffe下实现triplet loss,编程菜鸟,如果有写的不优化的地方,欢迎指出. 1.如何在caffe中增加新的layer 新版的caffe中增加新的layer,变得轻松多了,概括说来,分四步: 1)在./src/caffe/proto/caffe.proto 中增加 对应layer的paramter message: 2)在./include/

如何在caffe中添加新的Layer

如何在caffe中添加新的Layer 本文分为两部分,先写一个入门的教程,然后再给出自己添加maxout与NIN的layer的方法 (一) 其实在Github上已经有答案了(https://github.com/BVLC/caffe/issues/684) Here's roughly the process I follow. Add a class declaration for your layer to the appropriate one of common_layers.hpp, 

如何在caffe中添加新类型的layer

如何在caffe中添加新类型的layer 参考:https://github.com/BVLC/caffe/issues/684 Add a class declaration for your layer to the appropriate one of common_layers.hpp,data_layers.hpp, loss_layers.hpp, neuron_layers.hpp, or vision_layers.hpp. Include an inline implement

Caffe中增加新的layer以及Caffe中triplet loss layer的实现

关于Tripletloss的原理,目标函数和梯度推导在上一篇博客中已经讲过了,具体见:Tripletloss原理以及梯度推导,这篇博文主要是讲caffe下实现Tripletloss,编程菜鸟,如果有写的不优化的地方,欢迎指出. 尊重原创,转载请注明:http://blog.csdn.net/tangwei2014 1.如何在caffe中增加新的layer 新版的caffe中增加新的layer,变得轻松多了,概括说来,分四步: 1)在./src/caffe/proto/caffe.proto 中增

CAFFE中训练与使用阶段网络设计的不同

神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正要使用训练好的数据时,我们需要的是网络给我们输入结果,对于分类问题,我们需要获得分类结果,如下右图最后一层我们得到 的是概率,我们不需要训练及测试阶段的LOSS,ACCURACY层了. 下图是能过$CAFFE_ROOT/python/draw_net.py绘制$CAFFE_ROOT/models/caf

caffe中关于(ReLU层,Dropout层,BatchNorm层,Scale层)输入输出层一致的问题

在卷积神经网络中.常见到的激活函数有Relu层 layer { name: "relu1" type: "ReLU" bottom: "pool1" top: "pool1" }其中可选参数为:negative_slope:默认为0. 对标准的ReLU函数进行变化,如果设置了这个值,那么数据为负数时,就不再设置为0,而是用原始数据乘以negative_slope relu层有个很大的特点:bottom(输入)和top(输出)一