f0e19c277de09ae49591a4d774cba501376042e5
1 // Copyright 2013 Yangqing Jia
3 #ifndef CAFFE_BLOB_HPP_
4 #define CAFFE_BLOB_HPP_
6 #include "caffe/common.hpp"
7 #include "caffe/syncedmem.hpp"
8 #include "caffe/proto/caffe.pb.h"
10 namespace caffe {
12 template <typename Dtype>
13 class Blob {
14 public:
15 Blob()
16 : num_(0), channels_(0), height_(0), width_(0), count_(0), data_(),
17 diff_() {}
18 explicit Blob(const int num, const int channels, const int height,
19 const int width);
20 virtual ~Blob() {}
21 void Reshape(const int num, const int height,
22 const int width, const int channels);
23 inline int num() const { return num_; }
24 inline int channels() const { return channels_; }
25 inline int height() const { return height_; }
26 inline int width() const { return width_; }
27 inline int count() const {return count_; }
28 inline int offset(const int n, const int c = 0, const int h = 0,
29 const int w = 0) const {
30 return ((n * channels_ + c) * height_ + h) * width_ + w;
31 }
33 inline Dtype data_at(const int n, const int c, const int h,
34 const int w) const {
35 return *(cpu_data() + offset(n, c, h, w));
36 }
38 inline Dtype diff_at(const int n, const int c, const int h,
39 const int w) const {
40 return *(cpu_diff() + offset(n, c, h, w));
41 }
43 const Dtype* cpu_data() const;
44 const Dtype* gpu_data() const;
45 const Dtype* cpu_diff() const;
46 const Dtype* gpu_diff() const;
47 Dtype* mutable_cpu_data();
48 Dtype* mutable_gpu_data();
49 Dtype* mutable_cpu_diff();
50 Dtype* mutable_gpu_diff();
51 void Update();
52 void FromProto(const BlobProto& proto);
53 void ToProto(BlobProto* proto, bool write_diff = false) const;
55 protected:
56 shared_ptr<SyncedMemory> data_;
57 shared_ptr<SyncedMemory> diff_;
58 int num_;
59 int channels_;
60 int height_;
61 int width_;
62 int count_;
64 DISABLE_COPY_AND_ASSIGN(Blob);
65 }; // class Blob
67 } // namespace caffe
69 #endif // CAFFE_BLOB_HPP_