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"
12 template <typename Dtype>
16 : num_(0), channels_(0), height_(0), width_(0), count_(0), data_(),
18 explicit Blob(const int num, const int channels, const int height,
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;
32 // Copy from source. If copy_diff is false, we copy the data; if copy_diff
33 // is true, we copy the diff.
34 void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
35 bool reshape = false);
37 inline Dtype data_at(const int n, const int c, const int h,
39 return *(cpu_data() + offset(n, c, h, w));
42 inline Dtype diff_at(const int n, const int c, const int h,
44 return *(cpu_diff() + offset(n, c, h, w));
47 const Dtype* cpu_data() const;
48 const Dtype* gpu_data() const;
49 const Dtype* cpu_diff() const;
50 const Dtype* gpu_diff() const;
51 Dtype* mutable_cpu_data();
52 Dtype* mutable_gpu_data();
53 Dtype* mutable_cpu_diff();
54 Dtype* mutable_gpu_diff();
56 void FromProto(const BlobProto& proto);
57 void ToProto(BlobProto* proto, bool write_diff = false) const;
60 shared_ptr<SyncedMemory> data_;
61 shared_ptr<SyncedMemory> diff_;
68 DISABLE_COPY_AND_ASSIGN(Blob);
73 #endif // CAFFE_BLOB_HPP_