Caffe
base_data_layer.hpp
1 #ifndef CAFFE_DATA_LAYERS_HPP_
2 #define CAFFE_DATA_LAYERS_HPP_
3 
4 #include <vector>
5 
6 #include "caffe/blob.hpp"
7 #include "caffe/data_transformer.hpp"
8 #include "caffe/internal_thread.hpp"
9 #include "caffe/layer.hpp"
10 #include "caffe/proto/caffe.pb.h"
11 #include "caffe/util/blocking_queue.hpp"
12 
13 namespace caffe {
14 
20 template <typename Dtype>
21 class BaseDataLayer : public Layer<Dtype> {
22  public:
23  explicit BaseDataLayer(const LayerParameter& param);
24  // LayerSetUp: implements common data layer setup functionality, and calls
25  // DataLayerSetUp to do special data layer setup for individual layer types.
26  // This method may not be overridden except by the BasePrefetchingDataLayer.
27  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
28  const vector<Blob<Dtype>*>& top);
29  // Data layers should be shared by multiple solvers in parallel
30  virtual inline bool ShareInParallel() const { return true; }
31  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
32  const vector<Blob<Dtype>*>& top) {}
33  // Data layers have no bottoms, so reshaping is trivial.
34  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
35  const vector<Blob<Dtype>*>& top) {}
36 
37  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
38  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
39  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
40  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
41 
42  protected:
43  TransformationParameter transform_param_;
44  shared_ptr<DataTransformer<Dtype> > data_transformer_;
45  bool output_labels_;
46 };
47 
48 template <typename Dtype>
49 class Batch {
50  public:
51  Blob<Dtype> data_, label_;
52 };
53 
54 template <typename Dtype>
56  public BaseDataLayer<Dtype>, public InternalThread {
57  public:
58  explicit BasePrefetchingDataLayer(const LayerParameter& param);
59  // LayerSetUp: implements common data layer setup functionality, and calls
60  // DataLayerSetUp to do special data layer setup for individual layer types.
61  // This method may not be overridden.
62  void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
63  const vector<Blob<Dtype>*>& top);
64 
65  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
66  const vector<Blob<Dtype>*>& top);
67  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
68  const vector<Blob<Dtype>*>& top);
69 
70  // Prefetches batches (asynchronously if to GPU memory)
71  static const int PREFETCH_COUNT = 3;
72 
73  protected:
74  virtual void InternalThreadEntry();
75  virtual void load_batch(Batch<Dtype>* batch) = 0;
76 
77  Batch<Dtype> prefetch_[PREFETCH_COUNT];
78  BlockingQueue<Batch<Dtype>*> prefetch_free_;
79  BlockingQueue<Batch<Dtype>*> prefetch_full_;
80 
81  Blob<Dtype> transformed_data_;
82 };
83 
84 } // namespace caffe
85 
86 #endif // CAFFE_DATA_LAYERS_HPP_
Definition: base_data_layer.hpp:49
Definition: base_data_layer.hpp:55
virtual void Forward_gpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Using the GPU device, compute the layer output. Fall back to Forward_cpu() if unavailable.
Definition: layer.hpp:341
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
virtual void Reshape(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs...
Definition: base_data_layer.hpp:34
virtual void Backward_cpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Using the CPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
Definition: base_data_layer.hpp:37
virtual bool ShareInParallel() const
Whether a layer should be shared by multiple nets during data parallelism. By default, all layers except for data layers should not be shared. data layers should be shared to ensure each worker solver access data sequentially during data parallelism.
Definition: base_data_layer.hpp:30
Provides base for data layers that feed blobs to the Net.
Definition: base_data_layer.hpp:21
virtual void Forward_cpu(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)=0
Using the CPU device, compute the layer output.
Definition: internal_thread.hpp:19
virtual void Backward_gpu(const vector< Blob< Dtype > *> &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > *> &bottom)
Using the GPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
Definition: base_data_layer.hpp:39
Definition: blocking_queue.hpp:10
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24
virtual void LayerSetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: base_data_layer.cpp:21