深度学习caffe数据结构(五)—— blob数据结构blob.cpp文件详细解读

释放双眼,带上耳机,听听看~!

        在caffe中,Blob类实现的源码位于caffe根目录下的src/caffe/路径中的blob.cpp文件中,本文对这个文件进行详细解读。


1
2
3
4
5
6
7
8
1#include <climits>
2#include <vector>
3
4#include "caffe/blob.hpp"
5#include "caffe/common.hpp"
6#include "caffe/syncedmem.hpp"
7#include "caffe/util/math_functions.hpp"
8

这几行代码是blob.cpp文件包含的头文件。


1
2
1namespace caffe {
2

这一行定义caffe命名空间


1
2
3
4
5
6
7
8
9
10
11
1template <typename Dtype>
2void Blob<Dtype>::Reshape(const int num, const int channels, const int height,
3    const int width) {
4  vector<int> shape(4);
5  shape[0] = num;
6  shape[1] = channels;
7  shape[2] = height;
8  shape[3] = width;
9  Reshape(shape);
10}
11

这个函数的是Blob的变形函数,使用num、channels、height、width这四个维度信息对Blob进行变形。在函数中,将这四个维度信息保存到shape向量中,根据shape向量来改变Blob形状。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1template <typename Dtype>
2void Blob<Dtype>::Reshape(const vector<int>& shape) {
3  CHECK_LE(shape.size(), kMaxBlobAxes);
4  count_ = 1;
5  shape_.resize(shape.size());
6  if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {
7    shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));
8  }
9  int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());
10  for (int i = 0; i < shape.size(); ++i) {
11    CHECK_GE(shape[i], 0);
12    if (count_ != 0) {
13      CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";
14    }
15    count_ *= shape[i];
16    shape_[i] = shape[i];
17    shape_data[i] = shape[i];
18  }
19  if (count_ > capacity_) {
20    capacity_ = count_;
21    data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
22    diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
23  }
24}
25

        这个函数是Reshape函数的落实函数,通过shape向量来改变Blob的形状。在这个函数中,还对类的成员函数进行赋值,为数据分配内存空间。

        函数中先是判断shape的尺寸是否小于或等于最大的Blob尺寸,然后将成员变量shape_的尺寸赋值为shape的尺寸。

        接下来shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));为shape_data_分配内存空间。int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());的作用是定义一个指向shape_data_的指针shape_data。

        接下来在一个for循环中,对shape的每个维度进行遍历,先判断每个shape[i]是否大于等于0,然后判断shape[i]是否小于允许的最大值(即整数的最大值/count_),然后将每个shape[i]累乘到count_上,并将shape[i]赋值给shape_[i],再将shape[i]赋值给shape_data指向的内存空间数据。

        在函数的最后,判断Blob的元素数量count_是否大于Blob的容量capacity_。如果大于,将容量修改为与元素数相等,最后为data_和diff_分配内存空间。


1
2
3
4
5
6
7
8
9
10
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::Reshape(const BlobShape&amp; shape) {
3  CHECK_LE(shape.dim_size(), kMaxBlobAxes);
4  vector&lt;int&gt; shape_vec(shape.dim_size());
5  for (int i = 0; i &lt; shape.dim_size(); ++i) {
6    shape_vec[i] = shape.dim(i);
7  }
8  Reshape(shape_vec);
9}
10

上面这个函数也是一个Reshape函数,它的输入为BlobShape类型变量shape,在函数中,先判断shape的维度数是否小于等于最大的维度数。然后将shape每个维度的尺寸赋值给shape_vec向量,并依据shape_vec来调用相应的Reshape函数来改变blob的形状。


1
2
3
4
5
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::ReshapeLike(const Blob&lt;Dtype&gt;&amp; other) {
3  Reshape(other.shape());
4}
5

上面这个函数的作用是将本类的Blob尺寸改变为与other相同的尺寸,函数中调用Reshape函数实现。


1
2
3
4
5
6
7
8
1template &lt;typename Dtype&gt;
2Blob&lt;Dtype&gt;::Blob(const int num, const int channels, const int height,
3    const int width)
4  // capacity_ must be initialized before calling Reshape
5  : capacity_(0) {
6  Reshape(num, channels, height, width);
7}
8

这个函数是Blob的构造函数,输入为num、channels、height、width这四个维度信息,调用Reshape函数来实现。


1
2
3
4
5
6
7
1template &lt;typename Dtype&gt;
2Blob&lt;Dtype&gt;::Blob(const vector&lt;int&gt;&amp; shape)
3  // capacity_ must be initialized before calling Reshape
4  : capacity_(0) {
5  Reshape(shape);
6}
7

这个函数是Blob的另一个构造函数,它的输入为shape向量。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2const int* Blob&lt;Dtype&gt;::gpu_shape() const {
3  CHECK(shape_data_);
4  return (const int*)shape_data_-&gt;gpu_data();
5}
6

这个函数是读取gpu_shape地址的函数,函数返回指向shape_data_的指针。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2const Dtype* Blob&lt;Dtype&gt;::cpu_data() const {
3  CHECK(data_);
4  return (const Dtype*)data_-&gt;cpu_data();
5}
6

这个函数返回指向cpu_data的指针。


1
2
3
4
5
6
7
8
9
10
11
12
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::set_cpu_data(Dtype* data) {
3  CHECK(data);
4  // Make sure CPU and GPU sizes remain equal
5  size_t size = count_ * sizeof(Dtype);
6  if (data_-&gt;size() != size) {
7    data_.reset(new SyncedMemory(size));
8    diff_.reset(new SyncedMemory(size));
9  }
10  data_-&gt;set_cpu_data(data);
11}
12

这个函数用来设置cpu_data,用data指向的数据替代data_所指向的cpu数据。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2const Dtype* Blob&lt;Dtype&gt;::gpu_data() const {
3  CHECK(data_);
4  return (const Dtype*)data_-&gt;gpu_data();
5}
6

这个函数返回指向gpu_data的指针。


1
2
3
4
5
6
7
8
9
10
11
12
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::set_gpu_data(Dtype* data) {
3  CHECK(data);
4  // Make sure CPU and GPU sizes remain equal
5  size_t size = count_ * sizeof(Dtype);
6  if (data_-&gt;size() != size) {
7    data_.reset(new SyncedMemory(size));
8    diff_.reset(new SyncedMemory(size));
9  }
10  data_-&gt;set_gpu_data(data);
11}
12

这个函数用来设置gpu_data,用data指向的数据替代data_所指向的gpu数据。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2const Dtype* Blob&lt;Dtype&gt;::cpu_diff() const {
3  CHECK(diff_);
4  return (const Dtype*)diff_-&gt;cpu_data();
5}
6

这个函数返回指向cpu_diff的指针。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2const Dtype* Blob&lt;Dtype&gt;::gpu_diff() const {
3  CHECK(diff_);
4  return (const Dtype*)diff_-&gt;gpu_data();
5}
6

这个函数返回指向gpu_diff的指针。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2Dtype* Blob&lt;Dtype&gt;::mutable_cpu_data() {
3  CHECK(data_);
4  return static_cast&lt;Dtype*&gt;(data_-&gt;mutable_cpu_data());
5}
6

这个函数返回指向可以读写的cpu_data的指针。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2Dtype* Blob&lt;Dtype&gt;::mutable_gpu_data() {
3  CHECK(data_);
4  return static_cast&lt;Dtype*&gt;(data_-&gt;mutable_gpu_data());
5}
6

这个函数返回指向可以读写的gpu_data的指针。


1
2
3
4
5
6
7
1template &lt;typename Dtype&gt;
2Dtype* Blob&lt;Dtype&gt;::mutable_cpu_diff() {
3  CHECK(diff_);
4  return static_cast&lt;Dtype*&gt;(diff_-&gt;mutable_cpu_data());
5}
6
7

这个函数返回指向可以读写的cpu_diff的指针。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2Dtype* Blob&lt;Dtype&gt;::mutable_gpu_diff() {
3  CHECK(diff_);
4  return static_cast&lt;Dtype*&gt;(diff_-&gt;mutable_gpu_data());
5}
6

这个函数返回指向可以读写的gpu_diff的指针。


1
2
3
4
5
6
7
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::ShareData(const Blob&amp; other) {
3  CHECK_EQ(count_, other.count());
4  data_ = other.data();
5}
6
7

这个函数的作用是分享另一个Blob的数据data,在函数中如果other与本类Blob的元素数相等,则将other中的data复制到data_中。


1
2
3
4
5
6
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::ShareDiff(const Blob&amp; other) {
3  CHECK_EQ(count_, other.count());
4  diff_ = other.diff();
5}
6

这个函数的作用是分享另一个Blob的数据diff,在函数中,如果other与本类Blob的元素数相等,则将other中的diff复制到diff_中。


1
2
3
4
1template &lt;&gt; void Blob&lt;unsigned int&gt;::Update() { NOT_IMPLEMENTED; }
2template &lt;&gt; void Blob&lt;int&gt;::Update() { NOT_IMPLEMENTED; }
3
4

这两行声明了两个Blob的更新函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::Update() {
3  // We will perform update based on where the data is located.
4  switch (data_-&gt;head()) {
5  case SyncedMemory::HEAD_AT_CPU:
6    // perform computation on CPU
7    caffe_axpy&lt;Dtype&gt;(count_, Dtype(-1),
8        static_cast&lt;const Dtype*&gt;(diff_-&gt;cpu_data()),
9        static_cast&lt;Dtype*&gt;(data_-&gt;mutable_cpu_data()));
10    break;
11  case SyncedMemory::HEAD_AT_GPU:
12  case SyncedMemory::SYNCED:
13#ifndef CPU_ONLY
14    // perform computation on GPU
15    caffe_gpu_axpy&lt;Dtype&gt;(count_, Dtype(-1),
16        static_cast&lt;const Dtype*&gt;(diff_-&gt;gpu_data()),
17        static_cast&lt;Dtype*&gt;(data_-&gt;mutable_gpu_data()));
18#else
19    NO_GPU;
20#endif
21    break;
22  default:
23    LOG(FATAL) &lt;&lt; &quot;Syncedmem not initialized.&quot;;
24  }
25}
26

这个函数是更新函数Update的详细定义。首先通过一个switch语句判断data_位于哪里,如果在CPU中,则调用caffe_axpy函数计算法diff_和data_各元素的和。如果数据在GPU或者在CPU和GPU中同步,则调用caffe_gpu_axpy来计算diff_和data_各元素的和。需要注意的是,如果caffe编译时使能了CPU_ONLY选项,则不进行GPU相关的计算。其中caffe_axpy和caffe_gpu_axpy函数在math_functions.hpp文件中定义,这个文件位于caffe根目录下的include/caffe/util/路径中,有兴趣可以自己研究一下这个函数的实现过程。


1
2
3
4
5
6
7
8
9
10
1template &lt;&gt; unsigned int Blob&lt;unsigned int&gt;::asum_data() const {
2  NOT_IMPLEMENTED;
3  return 0;
4}
5
6template &lt;&gt; int Blob&lt;int&gt;::asum_data() const {
7  NOT_IMPLEMENTED;
8  return 0;
9}
10

这几行代码,声明了两个计算data元素绝对值和(L1范数)的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1template &lt;typename Dtype&gt;
2Dtype Blob&lt;Dtype&gt;::asum_data() const {
3  if (!data_) { return 0; }
4  switch (data_-&gt;head()) {
5  case SyncedMemory::HEAD_AT_CPU:
6    return caffe_cpu_asum(count_, cpu_data());
7  case SyncedMemory::HEAD_AT_GPU:
8  case SyncedMemory::SYNCED:
9#ifndef CPU_ONLY
10  {
11    Dtype asum;
12    caffe_gpu_asum(count_, gpu_data(), &amp;asum);
13    return asum;
14  }
15#else
16    NO_GPU;
17#endif
18  case SyncedMemory::UNINITIALIZED:
19    return 0;
20  default:
21    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; data_-&gt;head();
22  }
23  return 0;
24}
25
26

上面的函数是计算data元素绝对值和的详细定义。这个函数使用的方法与更新函数类似,也是使用一个switch语句判断data_位于哪里,如果在CPU中,则调用caffe_cpu_asum函数计算法data_各元素的绝对值和。如果数据在GPU或者在CPU和GPU中同步,则调用caffe_gpu_asum函数计算法data_各元素的绝对值和。如果caffe编译时使能了CPU_ONLY选项,则不进行GPU相关的计算。其中,caffe_cpu_asum和caffe_gpu_asum函数也是在math_functions.hpp文件中定义。


1
2
3
4
5
6
7
8
9
10
1template &lt;&gt; unsigned int Blob&lt;unsigned int&gt;::asum_diff() const {
2  NOT_IMPLEMENTED;
3  return 0;
4}
5
6template &lt;&gt; int Blob&lt;int&gt;::asum_diff() const {
7  NOT_IMPLEMENTED;
8  return 0;
9}
10

这几行代码,声明了两个计算diff元素绝对值和(L1范数)的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1template &lt;typename Dtype&gt;
2Dtype Blob&lt;Dtype&gt;::asum_diff() const {
3  if (!diff_) { return 0; }
4  switch (diff_-&gt;head()) {
5  case SyncedMemory::HEAD_AT_CPU:
6    return caffe_cpu_asum(count_, cpu_diff());
7  case SyncedMemory::HEAD_AT_GPU:
8  case SyncedMemory::SYNCED:
9#ifndef CPU_ONLY
10  {
11    Dtype asum;
12    caffe_gpu_asum(count_, gpu_diff(), &amp;asum);
13    return asum;
14  }
15#else
16    NO_GPU;
17#endif
18  case SyncedMemory::UNINITIALIZED:
19    return 0;
20  default:
21    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; diff_-&gt;head();
22  }
23  return 0;
24}
25

上面的函数是计算diff元素绝对值和的详细定义。与计算data元素绝对值和的函数实现是完全一样的,大家可以自行分析。


1
2
3
4
5
6
7
8
9
10
1template &lt;&gt; unsigned int Blob&lt;unsigned int&gt;::sumsq_data() const {
2  NOT_IMPLEMENTED;
3  return 0;
4}
5
6template &lt;&gt; int Blob&lt;int&gt;::sumsq_data() const {
7  NOT_IMPLEMENTED;
8  return 0;
9}
10

这几行代码,声明了两个计算data元素平方和(L2范数)的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1template &lt;typename Dtype&gt;
2Dtype Blob&lt;Dtype&gt;::sumsq_data() const {
3  Dtype sumsq;
4  const Dtype* data;
5  if (!data_) { return 0; }
6  switch (data_-&gt;head()) {
7  case SyncedMemory::HEAD_AT_CPU:
8    data = cpu_data();
9    sumsq = caffe_cpu_dot(count_, data, data);
10    break;
11  case SyncedMemory::HEAD_AT_GPU:
12  case SyncedMemory::SYNCED:
13#ifndef CPU_ONLY
14    data = gpu_data();
15    caffe_gpu_dot(count_, data, data, &amp;sumsq);
16#else
17    NO_GPU;
18#endif
19    break;
20  case SyncedMemory::UNINITIALIZED:
21    return 0;
22  default:
23    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; data_-&gt;head();
24  }
25  return sumsq;
26}
27

上面的函数是计算data元素平方和的详细定义。这个函数也是使用一个switch语句判断data_位于哪里,如果在CPU中,则调用caffe_cpu_dot函数计算法data_各元素的平方和。如果数据在GPU或者在CPU和GPU中同步,则调用caffe_gpu_dot函数计算法data_各元素的平方和。如果caffe编译时使能了CPU_ONLY选项,则不进行GPU相关的计算。其中,caffe_cpu_dot和caffe_gpu_dot函数也是在math_functions.hpp文件中定义。


1
2
3
4
5
6
7
8
9
10
1template &lt;&gt; unsigned int Blob&lt;unsigned int&gt;::sumsq_diff() const {
2  NOT_IMPLEMENTED;
3  return 0;
4}
5
6template &lt;&gt; int Blob&lt;int&gt;::sumsq_diff() const {
7  NOT_IMPLEMENTED;
8  return 0;
9}
10

这几行代码,声明了两个计算diff元素平方和(L2范数)的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1template &lt;typename Dtype&gt;
2Dtype Blob&lt;Dtype&gt;::sumsq_diff() const {
3  Dtype sumsq;
4  const Dtype* diff;
5  if (!diff_) { return 0; }
6  switch (diff_-&gt;head()) {
7  case SyncedMemory::HEAD_AT_CPU:
8    diff = cpu_diff();
9    sumsq = caffe_cpu_dot(count_, diff, diff);
10    break;
11  case SyncedMemory::HEAD_AT_GPU:
12  case SyncedMemory::SYNCED:
13#ifndef CPU_ONLY
14    diff = gpu_diff();
15    caffe_gpu_dot(count_, diff, diff, &amp;sumsq);
16    break;
17#else
18    NO_GPU;
19#endif
20  case SyncedMemory::UNINITIALIZED:
21    return 0;
22  default:
23    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; data_-&gt;head();
24  }
25  return sumsq;
26}
27

上面的函数是计算diff元素绝平方和的详细定义。与计算data元素平方和的函数实现是完全一样的,大家可以自行分析。


1
2
3
4
5
6
7
8
1template &lt;&gt; void Blob&lt;unsigned int&gt;::scale_data(unsigned int scale_factor) {
2  NOT_IMPLEMENTED;
3}
4
5template &lt;&gt; void Blob&lt;int&gt;::scale_data(int scale_factor) {
6  NOT_IMPLEMENTED;
7}
8

这几行代码,声明了两个对data元素进行比例缩放的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::scale_data(Dtype scale_factor) {
3  Dtype* data;
4  if (!data_) { return; }
5  switch (data_-&gt;head()) {
6  case SyncedMemory::HEAD_AT_CPU:
7    data = mutable_cpu_data();
8    caffe_scal(count_, scale_factor, data);
9    return;
10  case SyncedMemory::HEAD_AT_GPU:
11  case SyncedMemory::SYNCED:
12#ifndef CPU_ONLY
13    data = mutable_gpu_data();
14    caffe_gpu_scal(count_, scale_factor, data);
15    return;
16#else
17    NO_GPU;
18#endif
19  case SyncedMemory::UNINITIALIZED:
20    return;
21  default:
22    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; data_-&gt;head();
23  }
24}
25

上面的函数是对data元素进行比例缩放的详细定义。这个函数也是使用一个switch语句判断data_位于哪里,如果在CPU中,则调用caffe_scal函数计算法data_各元素的比例缩放,缩放系数为scale_factor。如果数据在GPU或者在CPU和GPU中同步,则调用caffe_gpu_scal函数计算法data_各元素的比例缩放。如果caffe编译时使能了CPU_ONLY选项,则不进行GPU相关的计算。其中,caffe_scal和caffe_gpu_scal函数也是在math_functions.hpp文件中定义。


1
2
3
4
5
6
7
8
1template &lt;&gt; void Blob&lt;unsigned int&gt;::scale_diff(unsigned int scale_factor) {
2  NOT_IMPLEMENTED;
3}
4
5template &lt;&gt; void Blob&lt;int&gt;::scale_diff(int scale_factor) {
6  NOT_IMPLEMENTED;
7}
8

这几行代码,声明了两个对diff元素进行比例缩放的函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::scale_diff(Dtype scale_factor) {
3  Dtype* diff;
4  if (!diff_) { return; }
5  switch (diff_-&gt;head()) {
6  case SyncedMemory::HEAD_AT_CPU:
7    diff = mutable_cpu_diff();
8    caffe_scal(count_, scale_factor, diff);
9    return;
10  case SyncedMemory::HEAD_AT_GPU:
11  case SyncedMemory::SYNCED:
12#ifndef CPU_ONLY
13    diff = mutable_gpu_diff();
14    caffe_gpu_scal(count_, scale_factor, diff);
15    return;
16#else
17    NO_GPU;
18#endif
19  case SyncedMemory::UNINITIALIZED:
20    return;
21  default:
22    LOG(FATAL) &lt;&lt; &quot;Unknown SyncedMemory head state: &quot; &lt;&lt; diff_-&gt;head();
23  }
24}
25

上面的函数是计算diff元素比例缩放的详细定义。与计算data元素比例缩放的函数实现是完全一样的,大家可以自行分析。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1template &lt;typename Dtype&gt;
2bool Blob&lt;Dtype&gt;::ShapeEquals(const BlobProto&amp; other) {
3  if (other.has_num() || other.has_channels() ||
4      other.has_height() || other.has_width()) {
5    // Using deprecated 4D Blob dimensions --
6    // shape is (num, channels, height, width).
7    // Note: we do not use the normal Blob::num(), Blob::channels(), etc.
8    // methods as these index from the beginning of the blob shape, where legacy
9    // parameter blobs were indexed from the end of the blob shape (e.g., bias
10    // Blob shape (1 x 1 x 1 x N), IP layer weight Blob shape (1 x 1 x M x N)).
11    return shape_.size() &lt;= 4 &amp;&amp;
12           LegacyShape(-4) == other.num() &amp;&amp;
13           LegacyShape(-3) == other.channels() &amp;&amp;
14           LegacyShape(-2) == other.height() &amp;&amp;
15           LegacyShape(-1) == other.width();
16  }
17  vector&lt;int&gt; other_shape(other.shape().dim_size());
18  for (int i = 0; i &lt; other.shape().dim_size(); ++i) {
19    other_shape[i] = other.shape().dim(i);
20  }
21  return shape_ == other_shape;
22}
23

上面这个函数的作用是判断本类的Blob与函数形参other中定义的Blob形状是否相同。在函数中首先判断other中的维度信息是否是老版本的维度信息,即num、channels、height、width这四个维度。如果是,按照老版本的逻辑进行判断,如果维度信息则返回true。如果不是老版本的维度信息,则将other的维度信息赋值到other_shape向量中,然后判断other_shape与shape_是否相等,相等则返回true。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::CopyFrom(const Blob&amp; source, bool copy_diff, bool reshape) {
3  if (source.count() != count_ || source.shape() != shape_) {
4    if (reshape) {
5      ReshapeLike(source);
6    } else {
7      LOG(FATAL) &lt;&lt; &quot;Trying to copy blobs of different sizes.&quot;;
8    }
9  }
10  switch (Caffe::mode()) {
11  case Caffe::GPU:
12    if (copy_diff) {
13      caffe_copy(count_, source.gpu_diff(),
14          static_cast&lt;Dtype*&gt;(diff_-&gt;mutable_gpu_data()));
15    } else {
16      caffe_copy(count_, source.gpu_data(),
17          static_cast&lt;Dtype*&gt;(data_-&gt;mutable_gpu_data()));
18    }
19    break;
20  case Caffe::CPU:
21    if (copy_diff) {
22      caffe_copy(count_, source.cpu_diff(),
23          static_cast&lt;Dtype*&gt;(diff_-&gt;mutable_cpu_data()));
24    } else {
25      caffe_copy(count_, source.cpu_data(),
26          static_cast&lt;Dtype*&gt;(data_-&gt;mutable_cpu_data()));
27    }
28    break;
29  default:
30    LOG(FATAL) &lt;&lt; &quot;Unknown caffe mode.&quot;;
31  }
32}
33

上面的这个函数是复制blob数据的函数,由source复制到本类的Blob中,copy_diff表示赋值diff数据,reshape表示是否允许改变Blob的形状。在函数中,首先判断source的元素中与本类Blob的元素数是否相同,并判断source的形状与本类的形状是否相同,两者之一不同,则判读是否允许改变Blob的形状,如果允许,则按照source改变本类Blob的形状。

        接下来通过一个switch语句判断当前是什么模式,如果是GPU模式,则通过caffe_copy函数复制data或者diff,目标地址为data_->mutable_gpu_data()或diff_->mutable_gpu_data()。如果模式是CPU模式,则复制的目标地址为data_->mutable_cpu_data()或diff_->mutable_cpu_data()。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
1template &lt;typename Dtype&gt;
2void Blob&lt;Dtype&gt;::FromProto(const BlobProto&amp; proto, bool reshape) {
3  if (reshape) {
4    vector&lt;int&gt; shape;
5    if (proto.has_num() || proto.has_channels() ||
6        proto.has_height() || proto.has_width()) {
7      // Using deprecated 4D Blob dimensions --
8      // shape is (num, channels, height, width).
9      shape.resize(4);
10      shape[0] = proto.num();
11      shape[1] = proto.channels();
12      shape[2] = proto.height();
13      shape[3] = proto.width();
14    } else {
15      shape.resize(proto.shape().dim_size());
16      for (int i = 0; i &lt; proto.shape().dim_size(); ++i) {
17        shape[i] = proto.shape().dim(i);
18      }
19    }
20    Reshape(shape);
21  } else {
22    CHECK(ShapeEquals(proto)) &lt;&lt; &quot;shape mismatch (reshape not set)&quot;;
23  }
24  // copy data
25  Dtype* data_vec = mutable_cpu_data();
26  if (proto.double_data_size() &gt; 0) {
27    CHECK_EQ(count_, proto.double_data_size());
28    for (int i = 0; i &lt; count_; ++i) {
29      data_vec[i] = proto.double_data(i);
30    }
31  } else {
32    CHECK_EQ(count_, proto.data_size());
33    for (int i = 0; i &lt; count_; ++i) {
34      data_vec[i] = proto.data(i);
35    }
36  }
37  if (proto.double_diff_size() &gt; 0) {
38    CHECK_EQ(count_, proto.double_diff_size());
39    Dtype* diff_vec = mutable_cpu_diff();
40    for (int i = 0; i &lt; count_; ++i) {
41      diff_vec[i] = proto.double_diff(i);
42    }
43  } else if (proto.diff_size() &gt; 0) {
44    CHECK_EQ(count_, proto.diff_size());
45    Dtype* diff_vec = mutable_cpu_diff();
46    for (int i = 0; i &lt; count_; ++i) {
47      diff_vec[i] = proto.diff(i);
48    }
49  }
50}
51

上面函数的作用是复制BlobProto数据结构中的数据到本类的BLob中。在函数中,第一步是确认本类BLob的形状,如果运行改变形状,则根据proto来改变Blob的形状,如果不允许改变形状,则检测本类Blob与proto形状是否相同,如果不同则直接退出。

        接下来就是数据复制的过程,分别复制data和diff,并区分数据类型为double还是 float类型,并分别采用相应的方法复制到本类的Blob的内存空间中。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1template &lt;&gt;
2void Blob&lt;float&gt;::ToProto(BlobProto* proto, bool write_diff) const {
3  proto-&gt;clear_shape();
4  for (int i = 0; i &lt; shape_.size(); ++i) {
5    proto-&gt;mutable_shape()-&gt;add_dim(shape_[i]);
6  }
7  proto-&gt;clear_data();
8  proto-&gt;clear_diff();
9  const float* data_vec = cpu_data();
10  for (int i = 0; i &lt; count_; ++i) {
11    proto-&gt;add_data(data_vec[i]);
12  }
13  if (write_diff) {
14    const float* diff_vec = cpu_diff();
15    for (int i = 0; i &lt; count_; ++i) {
16      proto-&gt;add_diff(diff_vec[i]);
17    }
18  }
19}
20

这个函数的作用是将本类的Blob数据写入到BlobProto数据结构proto中。在函数中,显示将形状信息写入到proto中,然后清除proto中的数据,并将data元素写入到proto中。最后根据函数形参write_diff是否为true来复制diff元素。


1
2
3
4
5
6
1INSTANTIATE_CLASS(Blob);
2template class Blob&lt;int&gt;;
3template class Blob&lt;unsigned int&gt;;
4
5}  // namespace caffe
6

这几行是blob.cpp的结尾,作用是实例化Blob模板类。至此我们完成了blob.cpp的介绍。

给TA打赏
共{{data.count}}人
人已打赏
安全运维

MongoDB最简单的入门教程之一 环境搭建

2021-12-11 11:36:11

安全运维

Ubuntu上NFS的安装配置

2021-12-19 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索