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  virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
30  const vector<Blob<Dtype>*>& top) {}
31  // Data layers have no bottoms, so reshaping is trivial.
32  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
33  const vector<Blob<Dtype>*>& top) {}
34 
35  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
36  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
37  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
38  const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
39 
40  protected:
41  TransformationParameter transform_param_;
42  shared_ptr<DataTransformer<Dtype> > data_transformer_;
43  bool output_labels_;
44 };
45 
46 template <typename Dtype>
47 class Batch {
48  public:
49  Blob<Dtype> data_, label_;
50 };
51 
52 template <typename Dtype>
54  public BaseDataLayer<Dtype>, public InternalThread {
55  public:
56  explicit BasePrefetchingDataLayer(const LayerParameter& param);
57  // LayerSetUp: implements common data layer setup functionality, and calls
58  // DataLayerSetUp to do special data layer setup for individual layer types.
59  // This method may not be overridden.
60  void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
61  const vector<Blob<Dtype>*>& top);
62 
63  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
64  const vector<Blob<Dtype>*>& top);
65  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
66  const vector<Blob<Dtype>*>& top);
67 
68  protected:
69  virtual void InternalThreadEntry();
70  virtual void load_batch(Batch<Dtype>* batch) = 0;
71 
72  vector<shared_ptr<Batch<Dtype> > > prefetch_;
73  BlockingQueue<Batch<Dtype>*> prefetch_free_;
74  BlockingQueue<Batch<Dtype>*> prefetch_full_;
75  Batch<Dtype>* prefetch_current_;
76 
77  Blob<Dtype> transformed_data_;
78 };
79 
80 } // namespace caffe
81 
82 #endif // CAFFE_DATA_LAYERS_HPP_
Definition: base_data_layer.hpp:47
Definition: base_data_layer.hpp:53
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:316
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:32
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:35
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:37
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