camenduru commited on
Commit
7fc5a59
·
1 Parent(s): 91acc4c

thanks to openpose ❤

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +11 -0
  2. usr/local/include/caffe/blob.hpp +282 -0
  3. usr/local/include/caffe/caffe.hpp +21 -0
  4. usr/local/include/caffe/common.hpp +193 -0
  5. usr/local/include/caffe/data_transformer.hpp +154 -0
  6. usr/local/include/caffe/filler.hpp +301 -0
  7. usr/local/include/caffe/internal_thread.hpp +53 -0
  8. usr/local/include/caffe/layer.hpp +477 -0
  9. usr/local/include/caffe/layer_factory.hpp +141 -0
  10. usr/local/include/caffe/layers/absval_layer.hpp +68 -0
  11. usr/local/include/caffe/layers/accuracy_layer.hpp +99 -0
  12. usr/local/include/caffe/layers/argmax_layer.hpp +77 -0
  13. usr/local/include/caffe/layers/base_conv_layer.hpp +174 -0
  14. usr/local/include/caffe/layers/base_data_layer.hpp +82 -0
  15. usr/local/include/caffe/layers/batch_norm_layer.hpp +78 -0
  16. usr/local/include/caffe/layers/batch_reindex_layer.hpp +83 -0
  17. usr/local/include/caffe/layers/bias_layer.hpp +54 -0
  18. usr/local/include/caffe/layers/bnll_layer.hpp +70 -0
  19. usr/local/include/caffe/layers/clip_layer.hpp +75 -0
  20. usr/local/include/caffe/layers/concat_layer.hpp +87 -0
  21. usr/local/include/caffe/layers/contrastive_loss_layer.hpp +101 -0
  22. usr/local/include/caffe/layers/conv_layer.hpp +84 -0
  23. usr/local/include/caffe/layers/crop_layer.hpp +78 -0
  24. usr/local/include/caffe/layers/cudnn_conv_layer.hpp +72 -0
  25. usr/local/include/caffe/layers/cudnn_deconv_layer.hpp +68 -0
  26. usr/local/include/caffe/layers/cudnn_lcn_layer.hpp +49 -0
  27. usr/local/include/caffe/layers/cudnn_lrn_layer.hpp +44 -0
  28. usr/local/include/caffe/layers/cudnn_pooling_layer.hpp +49 -0
  29. usr/local/include/caffe/layers/cudnn_relu_layer.hpp +46 -0
  30. usr/local/include/caffe/layers/cudnn_sigmoid_layer.hpp +46 -0
  31. usr/local/include/caffe/layers/cudnn_softmax_layer.hpp +45 -0
  32. usr/local/include/caffe/layers/cudnn_tanh_layer.hpp +46 -0
  33. usr/local/include/caffe/layers/data_layer.hpp +40 -0
  34. usr/local/include/caffe/layers/deconv_layer.hpp +51 -0
  35. usr/local/include/caffe/layers/dropout_layer.hpp +80 -0
  36. usr/local/include/caffe/layers/dummy_data_layer.hpp +47 -0
  37. usr/local/include/caffe/layers/eltwise_layer.hpp +51 -0
  38. usr/local/include/caffe/layers/elu_layer.hpp +86 -0
  39. usr/local/include/caffe/layers/embed_layer.hpp +52 -0
  40. usr/local/include/caffe/layers/euclidean_loss_layer.hpp +107 -0
  41. usr/local/include/caffe/layers/exp_layer.hpp +80 -0
  42. usr/local/include/caffe/layers/filter_layer.hpp +77 -0
  43. usr/local/include/caffe/layers/flatten_layer.hpp +61 -0
  44. usr/local/include/caffe/layers/hdf5_data_layer.hpp +64 -0
  45. usr/local/include/caffe/layers/hdf5_output_layer.hpp +62 -0
  46. usr/local/include/caffe/layers/hinge_loss_layer.hpp +104 -0
  47. usr/local/include/caffe/layers/im2col_layer.hpp +65 -0
  48. usr/local/include/caffe/layers/image_data_layer.hpp +47 -0
  49. usr/local/include/caffe/layers/infogain_loss_layer.hpp +146 -0
  50. usr/local/include/caffe/layers/inner_product_layer.hpp +52 -0
.gitattributes CHANGED
@@ -33,3 +33,14 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ usr/local/lib/libcaffe.so filter=lfs diff=lfs merge=lfs -text
37
+ usr/local/lib/libcaffe.so.1.0.0 filter=lfs diff=lfs merge=lfs -text
38
+ usr/local/lib/libcaffeproto.a filter=lfs diff=lfs merge=lfs -text
39
+ usr/local/lib/libopenpose.so filter=lfs diff=lfs merge=lfs -text
40
+ usr/local/lib/libopenpose.so.1.7.0 filter=lfs diff=lfs merge=lfs -text
41
+ usr/local/lib/libopenpose_core.so filter=lfs diff=lfs merge=lfs -text
42
+ usr/local/lib/libopenpose_face.so filter=lfs diff=lfs merge=lfs -text
43
+ usr/local/lib/libopenpose_gpu.so filter=lfs diff=lfs merge=lfs -text
44
+ usr/local/lib/libopenpose_hand.so filter=lfs diff=lfs merge=lfs -text
45
+ usr/local/lib/libopenpose_net.so filter=lfs diff=lfs merge=lfs -text
46
+ usr/local/lib/libopenpose_pose.so filter=lfs diff=lfs merge=lfs -text
usr/local/include/caffe/blob.hpp ADDED
@@ -0,0 +1,282 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BLOB_HPP_
2
+ #define CAFFE_BLOB_HPP_
3
+
4
+ #include <algorithm>
5
+ #include <string>
6
+ #include <vector>
7
+
8
+ #include "caffe/common.hpp"
9
+ #include "caffe/proto/caffe.pb.h"
10
+ #include "caffe/syncedmem.hpp"
11
+
12
+ const int kMaxBlobAxes = 32;
13
+
14
+ namespace caffe {
15
+
16
+ /**
17
+ * @brief A wrapper around SyncedMemory holders serving as the basic
18
+ * computational unit through which Layer%s, Net%s, and Solver%s
19
+ * interact.
20
+ *
21
+ * TODO(dox): more thorough description.
22
+ */
23
+ template <typename Dtype>
24
+ class Blob {
25
+ public:
26
+ Blob()
27
+ : data_(), diff_(), count_(0), capacity_(0) {}
28
+
29
+ /// @brief Deprecated; use <code>Blob(const vector<int>& shape)</code>.
30
+ explicit Blob(const int num, const int channels, const int height,
31
+ const int width);
32
+ explicit Blob(const vector<int>& shape);
33
+
34
+ /// @brief Deprecated; use <code>Reshape(const vector<int>& shape)</code>.
35
+ void Reshape(const int num, const int channels, const int height,
36
+ const int width);
37
+ /**
38
+ * @brief Change the dimensions of the blob, allocating new memory if
39
+ * necessary.
40
+ *
41
+ * This function can be called both to create an initial allocation
42
+ * of memory, and to adjust the dimensions of a top blob during Layer::Reshape
43
+ * or Layer::Forward. When changing the size of blob, memory will only be
44
+ * reallocated if sufficient memory does not already exist, and excess memory
45
+ * will never be freed.
46
+ *
47
+ * Note that reshaping an input blob and immediately calling Net::Backward is
48
+ * an error; either Net::Forward or Net::Reshape need to be called to
49
+ * propagate the new input shape to higher layers.
50
+ */
51
+ void Reshape(const vector<int>& shape);
52
+ void Reshape(const BlobShape& shape);
53
+ void ReshapeLike(const Blob& other);
54
+ inline string shape_string() const {
55
+ ostringstream stream;
56
+ for (unsigned int i = 0u; i < shape_.size(); ++i) {
57
+ stream << shape_[i] << " ";
58
+ }
59
+ stream << "(" << count_ << ")";
60
+ return stream.str();
61
+ }
62
+ inline const vector<int>& shape() const { return shape_; }
63
+ /**
64
+ * @brief Returns the dimension of the index-th axis (or the negative index-th
65
+ * axis from the end, if index is negative).
66
+ *
67
+ * @param index the axis index, which may be negative as it will be
68
+ * "canonicalized" using CanonicalAxisIndex.
69
+ * Dies on out of range index.
70
+ */
71
+ inline int shape(int index) const {
72
+ return shape_[CanonicalAxisIndex(index)];
73
+ }
74
+ inline int num_axes() const { return shape_.size(); }
75
+ inline int count() const { return count_; }
76
+
77
+ /**
78
+ * @brief Compute the volume of a slice; i.e., the product of dimensions
79
+ * among a range of axes.
80
+ *
81
+ * @param start_axis The first axis to include in the slice.
82
+ *
83
+ * @param end_axis The first axis to exclude from the slice.
84
+ */
85
+ inline int count(int start_axis, int end_axis) const {
86
+ CHECK_LE(start_axis, end_axis);
87
+ CHECK_GE(start_axis, 0);
88
+ CHECK_GE(end_axis, 0);
89
+ CHECK_LE(start_axis, num_axes());
90
+ CHECK_LE(end_axis, num_axes());
91
+ int count = 1;
92
+ for (int i = start_axis; i < end_axis; ++i) {
93
+ count *= shape(i);
94
+ }
95
+ return count;
96
+ }
97
+ /**
98
+ * @brief Compute the volume of a slice spanning from a particular first
99
+ * axis to the final axis.
100
+ *
101
+ * @param start_axis The first axis to include in the slice.
102
+ */
103
+ inline int count(int start_axis) const {
104
+ return count(start_axis, num_axes());
105
+ }
106
+
107
+ /**
108
+ * @brief Returns the 'canonical' version of a (usually) user-specified axis,
109
+ * allowing for negative indexing (e.g., -1 for the last axis).
110
+ *
111
+ * @param axis_index the axis index.
112
+ * If 0 <= index < num_axes(), return index.
113
+ * If -num_axes <= index <= -1, return (num_axes() - (-index)),
114
+ * e.g., the last axis index (num_axes() - 1) if index == -1,
115
+ * the second to last if index == -2, etc.
116
+ * Dies on out of range index.
117
+ */
118
+ inline int CanonicalAxisIndex(int axis_index) const {
119
+ CHECK_GE(axis_index, -num_axes())
120
+ << "axis " << axis_index << " out of range for " << num_axes()
121
+ << "-D Blob with shape " << shape_string();
122
+ CHECK_LT(axis_index, num_axes())
123
+ << "axis " << axis_index << " out of range for " << num_axes()
124
+ << "-D Blob with shape " << shape_string();
125
+ if (axis_index < 0) {
126
+ return axis_index + num_axes();
127
+ }
128
+ return axis_index;
129
+ }
130
+
131
+ /// @brief Deprecated legacy shape accessor num: use shape(0) instead.
132
+ inline int num() const { return LegacyShape(0); }
133
+ /// @brief Deprecated legacy shape accessor channels: use shape(1) instead.
134
+ inline int channels() const { return LegacyShape(1); }
135
+ /// @brief Deprecated legacy shape accessor height: use shape(2) instead.
136
+ inline int height() const { return LegacyShape(2); }
137
+ /// @brief Deprecated legacy shape accessor width: use shape(3) instead.
138
+ inline int width() const { return LegacyShape(3); }
139
+ inline int LegacyShape(int index) const {
140
+ CHECK_LE(num_axes(), 4)
141
+ << "Cannot use legacy accessors on Blobs with > 4 axes.";
142
+ CHECK_LT(index, 4);
143
+ CHECK_GE(index, -4);
144
+ if (index >= num_axes() || index < -num_axes()) {
145
+ // Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
146
+ // indexing) -- this special case simulates the one-padding used to fill
147
+ // extraneous axes of legacy blobs.
148
+ return 1;
149
+ }
150
+ return shape(index);
151
+ }
152
+
153
+ inline int offset(const int n, const int c = 0, const int h = 0,
154
+ const int w = 0) const {
155
+ CHECK_GE(n, 0);
156
+ CHECK_LE(n, num());
157
+ CHECK_GE(channels(), 0);
158
+ CHECK_LE(c, channels());
159
+ CHECK_GE(height(), 0);
160
+ CHECK_LE(h, height());
161
+ CHECK_GE(width(), 0);
162
+ CHECK_LE(w, width());
163
+ return ((n * channels() + c) * height() + h) * width() + w;
164
+ }
165
+
166
+ inline int offset(const vector<int>& indices) const {
167
+ CHECK_LE(indices.size(), num_axes());
168
+ int offset = 0;
169
+ for (int i = 0; i < num_axes(); ++i) {
170
+ offset *= shape(i);
171
+ if (indices.size() > i) {
172
+ CHECK_GE(indices[i], 0);
173
+ CHECK_LT(indices[i], shape(i));
174
+ offset += indices[i];
175
+ }
176
+ }
177
+ return offset;
178
+ }
179
+ /**
180
+ * @brief Copy from a source Blob.
181
+ *
182
+ * @param source the Blob to copy from
183
+ * @param copy_diff if false, copy the data; if true, copy the diff
184
+ * @param reshape if false, require this Blob to be pre-shaped to the shape
185
+ * of other (and die otherwise); if true, Reshape this Blob to other's
186
+ * shape if necessary
187
+ */
188
+ void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
189
+ bool reshape = false);
190
+
191
+ inline Dtype data_at(const int n, const int c, const int h,
192
+ const int w) const {
193
+ return cpu_data()[offset(n, c, h, w)];
194
+ }
195
+
196
+ inline Dtype diff_at(const int n, const int c, const int h,
197
+ const int w) const {
198
+ return cpu_diff()[offset(n, c, h, w)];
199
+ }
200
+
201
+ inline Dtype data_at(const vector<int>& index) const {
202
+ return cpu_data()[offset(index)];
203
+ }
204
+
205
+ inline Dtype diff_at(const vector<int>& index) const {
206
+ return cpu_diff()[offset(index)];
207
+ }
208
+
209
+ inline const shared_ptr<SyncedMemory>& data() const {
210
+ CHECK(data_);
211
+ return data_;
212
+ }
213
+
214
+ inline const shared_ptr<SyncedMemory>& diff() const {
215
+ CHECK(diff_);
216
+ return diff_;
217
+ }
218
+
219
+ const Dtype* cpu_data() const;
220
+ void set_cpu_data(Dtype* data);
221
+ const int* gpu_shape() const;
222
+ const Dtype* gpu_data() const;
223
+ void set_gpu_data(Dtype* data);
224
+ const Dtype* cpu_diff() const;
225
+ const Dtype* gpu_diff() const;
226
+ Dtype* mutable_cpu_data();
227
+ Dtype* mutable_gpu_data();
228
+ Dtype* mutable_cpu_diff();
229
+ Dtype* mutable_gpu_diff();
230
+ void Update();
231
+ void FromProto(const BlobProto& proto, bool reshape = true);
232
+ void ToProto(BlobProto* proto, bool write_diff = false) const;
233
+
234
+ /// @brief Compute the sum of absolute values (L1 norm) of the data.
235
+ Dtype asum_data() const;
236
+ /// @brief Compute the sum of absolute values (L1 norm) of the diff.
237
+ Dtype asum_diff() const;
238
+ /// @brief Compute the sum of squares (L2 norm squared) of the data.
239
+ Dtype sumsq_data() const;
240
+ /// @brief Compute the sum of squares (L2 norm squared) of the diff.
241
+ Dtype sumsq_diff() const;
242
+
243
+ /// @brief Scale the blob data by a constant factor.
244
+ void scale_data(Dtype scale_factor);
245
+ /// @brief Scale the blob diff by a constant factor.
246
+ void scale_diff(Dtype scale_factor);
247
+
248
+ /**
249
+ * @brief Set the data_ shared_ptr to point to the SyncedMemory holding the
250
+ * data_ of Blob other -- useful in Layer%s which simply perform a copy
251
+ * in their Forward pass.
252
+ *
253
+ * This deallocates the SyncedMemory holding this Blob's data_, as
254
+ * shared_ptr calls its destructor when reset with the "=" operator.
255
+ */
256
+ void ShareData(const Blob& other);
257
+ /**
258
+ * @brief Set the diff_ shared_ptr to point to the SyncedMemory holding the
259
+ * diff_ of Blob other -- useful in Layer%s which simply perform a copy
260
+ * in their Forward pass.
261
+ *
262
+ * This deallocates the SyncedMemory holding this Blob's diff_, as
263
+ * shared_ptr calls its destructor when reset with the "=" operator.
264
+ */
265
+ void ShareDiff(const Blob& other);
266
+
267
+ bool ShapeEquals(const BlobProto& other);
268
+
269
+ protected:
270
+ shared_ptr<SyncedMemory> data_;
271
+ shared_ptr<SyncedMemory> diff_;
272
+ shared_ptr<SyncedMemory> shape_data_;
273
+ vector<int> shape_;
274
+ int count_;
275
+ int capacity_;
276
+
277
+ DISABLE_COPY_AND_ASSIGN(Blob);
278
+ }; // class Blob
279
+
280
+ } // namespace caffe
281
+
282
+ #endif // CAFFE_BLOB_HPP_
usr/local/include/caffe/caffe.hpp ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // caffe.hpp is the header file that you need to include in your code. It wraps
2
+ // all the internal caffe header files into one for simpler inclusion.
3
+
4
+ #ifndef CAFFE_CAFFE_HPP_
5
+ #define CAFFE_CAFFE_HPP_
6
+
7
+ #include "caffe/blob.hpp"
8
+ #include "caffe/common.hpp"
9
+ #include "caffe/filler.hpp"
10
+ #include "caffe/layer.hpp"
11
+ #include "caffe/layer_factory.hpp"
12
+ #include "caffe/net.hpp"
13
+ #include "caffe/parallel.hpp"
14
+ #include "caffe/proto/caffe.pb.h"
15
+ #include "caffe/solver.hpp"
16
+ #include "caffe/solver_factory.hpp"
17
+ #include "caffe/util/benchmark.hpp"
18
+ #include "caffe/util/io.hpp"
19
+ #include "caffe/util/upgrade_proto.hpp"
20
+
21
+ #endif // CAFFE_CAFFE_HPP_
usr/local/include/caffe/common.hpp ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_COMMON_HPP_
2
+ #define CAFFE_COMMON_HPP_
3
+
4
+ #include <boost/shared_ptr.hpp>
5
+ #include <gflags/gflags.h>
6
+ #include <glog/logging.h>
7
+
8
+ #include <climits>
9
+ #include <cmath>
10
+ #include <fstream> // NOLINT(readability/streams)
11
+ #include <iostream> // NOLINT(readability/streams)
12
+ #include <map>
13
+ #include <set>
14
+ #include <sstream>
15
+ #include <string>
16
+ #include <utility> // pair
17
+ #include <vector>
18
+
19
+ #include "caffe/util/device_alternate.hpp"
20
+
21
+ // Convert macro to string
22
+ #define STRINGIFY(m) #m
23
+ #define AS_STRING(m) STRINGIFY(m)
24
+
25
+ // gflags 2.1 issue: namespace google was changed to gflags without warning.
26
+ // Luckily we will be able to use GFLAGS_GFLAGS_H_ to detect if it is version
27
+ // 2.1. If yes, we will add a temporary solution to redirect the namespace.
28
+ // TODO(Yangqing): Once gflags solves the problem in a more elegant way, let's
29
+ // remove the following hack.
30
+ #ifndef GFLAGS_GFLAGS_H_
31
+ namespace gflags = google;
32
+ #endif // GFLAGS_GFLAGS_H_
33
+
34
+ // Disable the copy and assignment operator for a class.
35
+ #define DISABLE_COPY_AND_ASSIGN(classname) \
36
+ private:\
37
+ classname(const classname&);\
38
+ classname& operator=(const classname&)
39
+
40
+ // Instantiate a class with float and double specifications.
41
+ #define INSTANTIATE_CLASS(classname) \
42
+ char gInstantiationGuard##classname; \
43
+ template class classname<float>; \
44
+ template class classname<double>
45
+
46
+ #define INSTANTIATE_LAYER_GPU_FORWARD(classname) \
47
+ template void classname<float>::Forward_gpu( \
48
+ const std::vector<Blob<float>*>& bottom, \
49
+ const std::vector<Blob<float>*>& top); \
50
+ template void classname<double>::Forward_gpu( \
51
+ const std::vector<Blob<double>*>& bottom, \
52
+ const std::vector<Blob<double>*>& top);
53
+
54
+ #define INSTANTIATE_LAYER_GPU_BACKWARD(classname) \
55
+ template void classname<float>::Backward_gpu( \
56
+ const std::vector<Blob<float>*>& top, \
57
+ const std::vector<bool>& propagate_down, \
58
+ const std::vector<Blob<float>*>& bottom); \
59
+ template void classname<double>::Backward_gpu( \
60
+ const std::vector<Blob<double>*>& top, \
61
+ const std::vector<bool>& propagate_down, \
62
+ const std::vector<Blob<double>*>& bottom)
63
+
64
+ #define INSTANTIATE_LAYER_GPU_FUNCS(classname) \
65
+ INSTANTIATE_LAYER_GPU_FORWARD(classname); \
66
+ INSTANTIATE_LAYER_GPU_BACKWARD(classname)
67
+
68
+ // A simple macro to mark codes that are not implemented, so that when the code
69
+ // is executed we will see a fatal log.
70
+ #define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet"
71
+
72
+ // See PR #1236
73
+ namespace cv { class Mat; }
74
+
75
+ namespace caffe {
76
+
77
+ // We will use the boost shared_ptr instead of the new C++11 one mainly
78
+ // because cuda does not work (at least now) well with C++11 features.
79
+ using boost::shared_ptr;
80
+
81
+ // Common functions and classes from std that caffe often uses.
82
+ using std::fstream;
83
+ using std::ios;
84
+ using std::isnan;
85
+ using std::isinf;
86
+ using std::iterator;
87
+ using std::make_pair;
88
+ using std::map;
89
+ using std::ostringstream;
90
+ using std::pair;
91
+ using std::set;
92
+ using std::string;
93
+ using std::stringstream;
94
+ using std::vector;
95
+
96
+ // A global initialization function that you should call in your main function.
97
+ // Currently it initializes google flags and google logging.
98
+ void GlobalInit(int* pargc, char*** pargv);
99
+
100
+ // A singleton class to hold common caffe stuff, such as the handler that
101
+ // caffe is going to use for cublas, curand, etc.
102
+ class Caffe {
103
+ public:
104
+ ~Caffe();
105
+
106
+ // Thread local context for Caffe. Moved to common.cpp instead of
107
+ // including boost/thread.hpp to avoid a boost/NVCC issues (#1009, #1010)
108
+ // on OSX. Also fails on Linux with CUDA 7.0.18.
109
+ static Caffe& Get();
110
+
111
+ enum Brew { CPU, GPU };
112
+
113
+ // This random number generator facade hides boost and CUDA rng
114
+ // implementation from one another (for cross-platform compatibility).
115
+ class RNG {
116
+ public:
117
+ RNG();
118
+ explicit RNG(unsigned int seed);
119
+ explicit RNG(const RNG&);
120
+ RNG& operator=(const RNG&);
121
+ void* generator();
122
+ private:
123
+ class Generator;
124
+ shared_ptr<Generator> generator_;
125
+ };
126
+
127
+ // Getters for boost rng, curand, and cublas handles
128
+ inline static RNG& rng_stream() {
129
+ if (!Get().random_generator_) {
130
+ Get().random_generator_.reset(new RNG());
131
+ }
132
+ return *(Get().random_generator_);
133
+ }
134
+ #ifndef CPU_ONLY
135
+ inline static cublasHandle_t cublas_handle() { return Get().cublas_handle_; }
136
+ inline static curandGenerator_t curand_generator() {
137
+ return Get().curand_generator_;
138
+ }
139
+ #endif
140
+
141
+ // Returns the mode: running on CPU or GPU.
142
+ inline static Brew mode() { return Get().mode_; }
143
+ // The setters for the variables
144
+ // Sets the mode. It is recommended that you don't change the mode halfway
145
+ // into the program since that may cause allocation of pinned memory being
146
+ // freed in a non-pinned way, which may cause problems - I haven't verified
147
+ // it personally but better to note it here in the header file.
148
+ inline static void set_mode(Brew mode) { Get().mode_ = mode; }
149
+ // Sets the random seed of both boost and curand
150
+ static void set_random_seed(const unsigned int seed);
151
+ // Sets the device. Since we have cublas and curand stuff, set device also
152
+ // requires us to reset those values.
153
+ static void SetDevice(const int device_id);
154
+ // Prints the current GPU status.
155
+ static void DeviceQuery();
156
+ // Check if specified device is available
157
+ static bool CheckDevice(const int device_id);
158
+ // Search from start_id to the highest possible device ordinal,
159
+ // return the ordinal of the first available device.
160
+ static int FindDevice(const int start_id = 0);
161
+ // Parallel training
162
+ inline static int solver_count() { return Get().solver_count_; }
163
+ inline static void set_solver_count(int val) { Get().solver_count_ = val; }
164
+ inline static int solver_rank() { return Get().solver_rank_; }
165
+ inline static void set_solver_rank(int val) { Get().solver_rank_ = val; }
166
+ inline static bool multiprocess() { return Get().multiprocess_; }
167
+ inline static void set_multiprocess(bool val) { Get().multiprocess_ = val; }
168
+ inline static bool root_solver() { return Get().solver_rank_ == 0; }
169
+
170
+ protected:
171
+ #ifndef CPU_ONLY
172
+ cublasHandle_t cublas_handle_;
173
+ curandGenerator_t curand_generator_;
174
+ #endif
175
+ shared_ptr<RNG> random_generator_;
176
+
177
+ Brew mode_;
178
+
179
+ // Parallel training
180
+ int solver_count_;
181
+ int solver_rank_;
182
+ bool multiprocess_;
183
+
184
+ private:
185
+ // The private constructor to avoid duplicate instantiation.
186
+ Caffe();
187
+
188
+ DISABLE_COPY_AND_ASSIGN(Caffe);
189
+ };
190
+
191
+ } // namespace caffe
192
+
193
+ #endif // CAFFE_COMMON_HPP_
usr/local/include/caffe/data_transformer.hpp ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_DATA_TRANSFORMER_HPP
2
+ #define CAFFE_DATA_TRANSFORMER_HPP
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/common.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Applies common transformations to the input data, such as
14
+ * scaling, mirroring, substracting the image mean...
15
+ */
16
+ template <typename Dtype>
17
+ class DataTransformer {
18
+ public:
19
+ explicit DataTransformer(const TransformationParameter& param, Phase phase);
20
+ virtual ~DataTransformer() {}
21
+
22
+ /**
23
+ * @brief Initialize the Random number generations if needed by the
24
+ * transformation.
25
+ */
26
+ void InitRand();
27
+
28
+ /**
29
+ * @brief Applies the transformation defined in the data layer's
30
+ * transform_param block to the data.
31
+ *
32
+ * @param datum
33
+ * Datum containing the data to be transformed.
34
+ * @param transformed_blob
35
+ * This is destination blob. It can be part of top blob's data if
36
+ * set_cpu_data() is used. See data_layer.cpp for an example.
37
+ */
38
+ void Transform(const Datum& datum, Blob<Dtype>* transformed_blob);
39
+
40
+ /**
41
+ * @brief Applies the transformation defined in the data layer's
42
+ * transform_param block to a vector of Datum.
43
+ *
44
+ * @param datum_vector
45
+ * A vector of Datum containing the data to be transformed.
46
+ * @param transformed_blob
47
+ * This is destination blob. It can be part of top blob's data if
48
+ * set_cpu_data() is used. See memory_layer.cpp for an example.
49
+ */
50
+ void Transform(const vector<Datum> & datum_vector,
51
+ Blob<Dtype>* transformed_blob);
52
+
53
+ #ifdef USE_OPENCV
54
+ /**
55
+ * @brief Applies the transformation defined in the data layer's
56
+ * transform_param block to a vector of Mat.
57
+ *
58
+ * @param mat_vector
59
+ * A vector of Mat containing the data to be transformed.
60
+ * @param transformed_blob
61
+ * This is destination blob. It can be part of top blob's data if
62
+ * set_cpu_data() is used. See memory_layer.cpp for an example.
63
+ */
64
+ void Transform(const vector<cv::Mat> & mat_vector,
65
+ Blob<Dtype>* transformed_blob);
66
+
67
+ /**
68
+ * @brief Applies the transformation defined in the data layer's
69
+ * transform_param block to a cv::Mat
70
+ *
71
+ * @param cv_img
72
+ * cv::Mat containing the data to be transformed.
73
+ * @param transformed_blob
74
+ * This is destination blob. It can be part of top blob's data if
75
+ * set_cpu_data() is used. See image_data_layer.cpp for an example.
76
+ */
77
+ void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob);
78
+ #endif // USE_OPENCV
79
+
80
+ /**
81
+ * @brief Applies the same transformation defined in the data layer's
82
+ * transform_param block to all the num images in a input_blob.
83
+ *
84
+ * @param input_blob
85
+ * A Blob containing the data to be transformed. It applies the same
86
+ * transformation to all the num images in the blob.
87
+ * @param transformed_blob
88
+ * This is destination blob, it will contain as many images as the
89
+ * input blob. It can be part of top blob's data.
90
+ */
91
+ void Transform(Blob<Dtype>* input_blob, Blob<Dtype>* transformed_blob);
92
+
93
+ /**
94
+ * @brief Infers the shape of transformed_blob will have when
95
+ * the transformation is applied to the data.
96
+ *
97
+ * @param datum
98
+ * Datum containing the data to be transformed.
99
+ */
100
+ vector<int> InferBlobShape(const Datum& datum);
101
+ /**
102
+ * @brief Infers the shape of transformed_blob will have when
103
+ * the transformation is applied to the data.
104
+ * It uses the first element to infer the shape of the blob.
105
+ *
106
+ * @param datum_vector
107
+ * A vector of Datum containing the data to be transformed.
108
+ */
109
+ vector<int> InferBlobShape(const vector<Datum> & datum_vector);
110
+ /**
111
+ * @brief Infers the shape of transformed_blob will have when
112
+ * the transformation is applied to the data.
113
+ * It uses the first element to infer the shape of the blob.
114
+ *
115
+ * @param mat_vector
116
+ * A vector of Mat containing the data to be transformed.
117
+ */
118
+ #ifdef USE_OPENCV
119
+ vector<int> InferBlobShape(const vector<cv::Mat> & mat_vector);
120
+ /**
121
+ * @brief Infers the shape of transformed_blob will have when
122
+ * the transformation is applied to the data.
123
+ *
124
+ * @param cv_img
125
+ * cv::Mat containing the data to be transformed.
126
+ */
127
+ vector<int> InferBlobShape(const cv::Mat& cv_img);
128
+ #endif // USE_OPENCV
129
+
130
+ protected:
131
+ /**
132
+ * @brief Generates a random integer from Uniform({0, 1, ..., n-1}).
133
+ *
134
+ * @param n
135
+ * The upperbound (exclusive) value of the random number.
136
+ * @return
137
+ * A uniformly random integer value from ({0, 1, ..., n-1}).
138
+ */
139
+ virtual int Rand(int n);
140
+
141
+ void Transform(const Datum& datum, Dtype* transformed_data);
142
+ // Tranformation parameters
143
+ TransformationParameter param_;
144
+
145
+
146
+ shared_ptr<Caffe::RNG> rng_;
147
+ Phase phase_;
148
+ Blob<Dtype> data_mean_;
149
+ vector<Dtype> mean_values_;
150
+ };
151
+
152
+ } // namespace caffe
153
+
154
+ #endif // CAFFE_DATA_TRANSFORMER_HPP_
usr/local/include/caffe/filler.hpp ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Fillers are random number generators that fills a blob using the specified
2
+ // algorithm. The expectation is that they are only going to be used during
3
+ // initialization time and will not involve any GPUs.
4
+
5
+ #ifndef CAFFE_FILLER_HPP
6
+ #define CAFFE_FILLER_HPP
7
+
8
+ #include <string>
9
+
10
+ #include "caffe/blob.hpp"
11
+ #include "caffe/proto/caffe.pb.h"
12
+ #include "caffe/syncedmem.hpp"
13
+ #include "caffe/util/math_functions.hpp"
14
+
15
+ namespace caffe {
16
+
17
+ /// @brief Fills a Blob with constant or randomly-generated data.
18
+ template <typename Dtype>
19
+ class Filler {
20
+ public:
21
+ explicit Filler(const FillerParameter& param) : filler_param_(param) {}
22
+ virtual ~Filler() {}
23
+ virtual void Fill(Blob<Dtype>* blob) = 0;
24
+ protected:
25
+ FillerParameter filler_param_;
26
+ }; // class Filler
27
+
28
+
29
+ /// @brief Fills a Blob with constant values @f$ x = 0 @f$.
30
+ template <typename Dtype>
31
+ class ConstantFiller : public Filler<Dtype> {
32
+ public:
33
+ explicit ConstantFiller(const FillerParameter& param)
34
+ : Filler<Dtype>(param) {}
35
+ virtual void Fill(Blob<Dtype>* blob) {
36
+ Dtype* data = blob->mutable_cpu_data();
37
+ const int count = blob->count();
38
+ const Dtype value = this->filler_param_.value();
39
+ CHECK(count);
40
+ for (int i = 0; i < count; ++i) {
41
+ data[i] = value;
42
+ }
43
+ CHECK_EQ(this->filler_param_.sparse(), -1)
44
+ << "Sparsity not supported by this Filler.";
45
+ }
46
+ };
47
+
48
+ /// @brief Fills a Blob with uniformly distributed values @f$ x\sim U(a, b) @f$.
49
+ template <typename Dtype>
50
+ class UniformFiller : public Filler<Dtype> {
51
+ public:
52
+ explicit UniformFiller(const FillerParameter& param)
53
+ : Filler<Dtype>(param) {}
54
+ virtual void Fill(Blob<Dtype>* blob) {
55
+ CHECK(blob->count());
56
+ caffe_rng_uniform<Dtype>(blob->count(), Dtype(this->filler_param_.min()),
57
+ Dtype(this->filler_param_.max()), blob->mutable_cpu_data());
58
+ CHECK_EQ(this->filler_param_.sparse(), -1)
59
+ << "Sparsity not supported by this Filler.";
60
+ }
61
+ };
62
+
63
+ /// @brief Fills a Blob with Gaussian-distributed values @f$ x = a @f$.
64
+ template <typename Dtype>
65
+ class GaussianFiller : public Filler<Dtype> {
66
+ public:
67
+ explicit GaussianFiller(const FillerParameter& param)
68
+ : Filler<Dtype>(param) {}
69
+ virtual void Fill(Blob<Dtype>* blob) {
70
+ Dtype* data = blob->mutable_cpu_data();
71
+ CHECK(blob->count());
72
+ caffe_rng_gaussian<Dtype>(blob->count(), Dtype(this->filler_param_.mean()),
73
+ Dtype(this->filler_param_.std()), blob->mutable_cpu_data());
74
+ int sparse = this->filler_param_.sparse();
75
+ CHECK_GE(sparse, -1);
76
+ if (sparse >= 0) {
77
+ // Sparse initialization is implemented for "weight" blobs; i.e. matrices.
78
+ // These have num == channels == 1; width is number of inputs; height is
79
+ // number of outputs. The 'sparse' variable specifies the mean number
80
+ // of non-zero input weights for a given output.
81
+ CHECK_GE(blob->num_axes(), 1);
82
+ const int num_outputs = blob->shape(0);
83
+ Dtype non_zero_probability = Dtype(sparse) / Dtype(num_outputs);
84
+ rand_vec_.reset(new SyncedMemory(blob->count() * sizeof(int)));
85
+ int* mask = reinterpret_cast<int*>(rand_vec_->mutable_cpu_data());
86
+ caffe_rng_bernoulli(blob->count(), non_zero_probability, mask);
87
+ for (int i = 0; i < blob->count(); ++i) {
88
+ data[i] *= mask[i];
89
+ }
90
+ }
91
+ }
92
+
93
+ protected:
94
+ shared_ptr<SyncedMemory> rand_vec_;
95
+ };
96
+
97
+ /** @brief Fills a Blob with values @f$ x \in [0, 1] @f$
98
+ * such that @f$ \forall i \sum_j x_{ij} = 1 @f$.
99
+ */
100
+ template <typename Dtype>
101
+ class PositiveUnitballFiller : public Filler<Dtype> {
102
+ public:
103
+ explicit PositiveUnitballFiller(const FillerParameter& param)
104
+ : Filler<Dtype>(param) {}
105
+ virtual void Fill(Blob<Dtype>* blob) {
106
+ Dtype* data = blob->mutable_cpu_data();
107
+ DCHECK(blob->count());
108
+ caffe_rng_uniform<Dtype>(blob->count(), 0, 1, blob->mutable_cpu_data());
109
+ // We expect the filler to not be called very frequently, so we will
110
+ // just use a simple implementation
111
+ int dim = blob->count() / blob->shape(0);
112
+ CHECK(dim);
113
+ for (int i = 0; i < blob->shape(0); ++i) {
114
+ Dtype sum = 0;
115
+ for (int j = 0; j < dim; ++j) {
116
+ sum += data[i * dim + j];
117
+ }
118
+ for (int j = 0; j < dim; ++j) {
119
+ data[i * dim + j] /= sum;
120
+ }
121
+ }
122
+ CHECK_EQ(this->filler_param_.sparse(), -1)
123
+ << "Sparsity not supported by this Filler.";
124
+ }
125
+ };
126
+
127
+ /**
128
+ * @brief Fills a Blob with values @f$ x \sim U(-a, +a) @f$ where @f$ a @f$ is
129
+ * set inversely proportional to number of incoming nodes, outgoing
130
+ * nodes, or their average.
131
+ *
132
+ * A Filler based on the paper [Bengio and Glorot 2010]: Understanding
133
+ * the difficulty of training deep feedforward neuralnetworks.
134
+ *
135
+ * It fills the incoming matrix by randomly sampling uniform data from [-scale,
136
+ * scale] where scale = sqrt(3 / n) where n is the fan_in, fan_out, or their
137
+ * average, depending on the variance_norm option. You should make sure the
138
+ * input blob has shape (num, a, b, c) where a * b * c = fan_in and num * b * c
139
+ * = fan_out. Note that this is currently not the case for inner product layers.
140
+ *
141
+ * TODO(dox): make notation in above comment consistent with rest & use LaTeX.
142
+ */
143
+ template <typename Dtype>
144
+ class XavierFiller : public Filler<Dtype> {
145
+ public:
146
+ explicit XavierFiller(const FillerParameter& param)
147
+ : Filler<Dtype>(param) {}
148
+ virtual void Fill(Blob<Dtype>* blob) {
149
+ CHECK(blob->count());
150
+ int fan_in = blob->count() / blob->shape(0);
151
+ // Compatibility with ND blobs
152
+ int fan_out = blob->num_axes() > 1 ?
153
+ blob->count() / blob->shape(1) :
154
+ blob->count();
155
+ Dtype n = fan_in; // default to fan_in
156
+ if (this->filler_param_.variance_norm() ==
157
+ FillerParameter_VarianceNorm_AVERAGE) {
158
+ n = (fan_in + fan_out) / Dtype(2);
159
+ } else if (this->filler_param_.variance_norm() ==
160
+ FillerParameter_VarianceNorm_FAN_OUT) {
161
+ n = fan_out;
162
+ }
163
+ Dtype scale = sqrt(Dtype(3) / n);
164
+ caffe_rng_uniform<Dtype>(blob->count(), -scale, scale,
165
+ blob->mutable_cpu_data());
166
+ CHECK_EQ(this->filler_param_.sparse(), -1)
167
+ << "Sparsity not supported by this Filler.";
168
+ }
169
+ };
170
+
171
+ /**
172
+ * @brief Fills a Blob with values @f$ x \sim N(0, \sigma^2) @f$ where
173
+ * @f$ \sigma^2 @f$ is set inversely proportional to number of incoming
174
+ * nodes, outgoing nodes, or their average.
175
+ *
176
+ * A Filler based on the paper [He, Zhang, Ren and Sun 2015]: Specifically
177
+ * accounts for ReLU nonlinearities.
178
+ *
179
+ * Aside: for another perspective on the scaling factor, see the derivation of
180
+ * [Saxe, McClelland, and Ganguli 2013 (v3)].
181
+ *
182
+ * It fills the incoming matrix by randomly sampling Gaussian data with std =
183
+ * sqrt(2 / n) where n is the fan_in, fan_out, or their average, depending on
184
+ * the variance_norm option. You should make sure the input blob has shape (num,
185
+ * a, b, c) where a * b * c = fan_in and num * b * c = fan_out. Note that this
186
+ * is currently not the case for inner product layers.
187
+ */
188
+ template <typename Dtype>
189
+ class MSRAFiller : public Filler<Dtype> {
190
+ public:
191
+ explicit MSRAFiller(const FillerParameter& param)
192
+ : Filler<Dtype>(param) {}
193
+ virtual void Fill(Blob<Dtype>* blob) {
194
+ CHECK(blob->count());
195
+ int fan_in = blob->count() / blob->shape(0);
196
+ // Compatibility with ND blobs
197
+ int fan_out = blob->num_axes() > 1 ?
198
+ blob->count() / blob->shape(1) :
199
+ blob->count();
200
+ Dtype n = fan_in; // default to fan_in
201
+ if (this->filler_param_.variance_norm() ==
202
+ FillerParameter_VarianceNorm_AVERAGE) {
203
+ n = (fan_in + fan_out) / Dtype(2);
204
+ } else if (this->filler_param_.variance_norm() ==
205
+ FillerParameter_VarianceNorm_FAN_OUT) {
206
+ n = fan_out;
207
+ }
208
+ Dtype std = sqrt(Dtype(2) / n);
209
+ caffe_rng_gaussian<Dtype>(blob->count(), Dtype(0), std,
210
+ blob->mutable_cpu_data());
211
+ CHECK_EQ(this->filler_param_.sparse(), -1)
212
+ << "Sparsity not supported by this Filler.";
213
+ }
214
+ };
215
+
216
+ /*!
217
+ @brief Fills a Blob with coefficients for bilinear interpolation.
218
+
219
+ A common use case is with the DeconvolutionLayer acting as upsampling.
220
+ You can upsample a feature map with shape of (B, C, H, W) by any integer factor
221
+ using the following proto.
222
+ \code
223
+ layer {
224
+ name: "upsample", type: "Deconvolution"
225
+ bottom: "{{bottom_name}}" top: "{{top_name}}"
226
+ convolution_param {
227
+ kernel_size: {{2 * factor - factor % 2}} stride: {{factor}}
228
+ num_output: {{C}} group: {{C}}
229
+ pad: {{ceil((factor - 1) / 2.)}}
230
+ weight_filler: { type: "bilinear" } bias_term: false
231
+ }
232
+ param { lr_mult: 0 decay_mult: 0 }
233
+ }
234
+ \endcode
235
+ Please use this by replacing `{{}}` with your values. By specifying
236
+ `num_output: {{C}} group: {{C}}`, it behaves as
237
+ channel-wise convolution. The filter shape of this deconvolution layer will be
238
+ (C, 1, K, K) where K is `kernel_size`, and this filler will set a (K, K)
239
+ interpolation kernel for every channel of the filter identically. The resulting
240
+ shape of the top feature map will be (B, C, factor * H, factor * W).
241
+ Note that the learning rate and the
242
+ weight decay are set to 0 in order to keep coefficient values of bilinear
243
+ interpolation unchanged during training. If you apply this to an image, this
244
+ operation is equivalent to the following call in Python with Scikit.Image.
245
+ \code{.py}
246
+ out = skimage.transform.rescale(img, factor, mode='constant', cval=0)
247
+ \endcode
248
+ */
249
+ template <typename Dtype>
250
+ class BilinearFiller : public Filler<Dtype> {
251
+ public:
252
+ explicit BilinearFiller(const FillerParameter& param)
253
+ : Filler<Dtype>(param) {}
254
+ virtual void Fill(Blob<Dtype>* blob) {
255
+ CHECK_EQ(blob->num_axes(), 4) << "Blob must be 4 dim.";
256
+ CHECK_EQ(blob->width(), blob->height()) << "Filter must be square";
257
+ Dtype* data = blob->mutable_cpu_data();
258
+ int f = ceil(blob->width() / 2.);
259
+ Dtype c = (blob->width() - 1) / (2. * f);
260
+ for (int i = 0; i < blob->count(); ++i) {
261
+ Dtype x = i % blob->width();
262
+ Dtype y = (i / blob->width()) % blob->height();
263
+ data[i] = (1 - fabs(x / f - c)) * (1 - fabs(y / f - c));
264
+ }
265
+ CHECK_EQ(this->filler_param_.sparse(), -1)
266
+ << "Sparsity not supported by this Filler.";
267
+ }
268
+ };
269
+
270
+ /**
271
+ * @brief Get a specific filler from the specification given in FillerParameter.
272
+ *
273
+ * Ideally this would be replaced by a factory pattern, but we will leave it
274
+ * this way for now.
275
+ */
276
+ template <typename Dtype>
277
+ Filler<Dtype>* GetFiller(const FillerParameter& param) {
278
+ const std::string& type = param.type();
279
+ if (type == "constant") {
280
+ return new ConstantFiller<Dtype>(param);
281
+ } else if (type == "gaussian") {
282
+ return new GaussianFiller<Dtype>(param);
283
+ } else if (type == "positive_unitball") {
284
+ return new PositiveUnitballFiller<Dtype>(param);
285
+ } else if (type == "uniform") {
286
+ return new UniformFiller<Dtype>(param);
287
+ } else if (type == "xavier") {
288
+ return new XavierFiller<Dtype>(param);
289
+ } else if (type == "msra") {
290
+ return new MSRAFiller<Dtype>(param);
291
+ } else if (type == "bilinear") {
292
+ return new BilinearFiller<Dtype>(param);
293
+ } else {
294
+ CHECK(false) << "Unknown filler name: " << param.type();
295
+ }
296
+ return (Filler<Dtype>*)(NULL);
297
+ }
298
+
299
+ } // namespace caffe
300
+
301
+ #endif // CAFFE_FILLER_HPP_
usr/local/include/caffe/internal_thread.hpp ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_INTERNAL_THREAD_HPP_
2
+ #define CAFFE_INTERNAL_THREAD_HPP_
3
+
4
+ #include "caffe/common.hpp"
5
+
6
+ /**
7
+ Forward declare boost::thread instead of including boost/thread.hpp
8
+ to avoid a boost/NVCC issues (#1009, #1010) on OSX.
9
+ */
10
+ namespace boost { class thread; }
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * Virtual class encapsulate boost::thread for use in base class
16
+ * The child class will acquire the ability to run a single thread,
17
+ * by reimplementing the virtual function InternalThreadEntry.
18
+ */
19
+ class InternalThread {
20
+ public:
21
+ InternalThread() : thread_() {}
22
+ virtual ~InternalThread();
23
+
24
+ /**
25
+ * Caffe's thread local state will be initialized using the current
26
+ * thread values, e.g. device id, solver index etc. The random seed
27
+ * is initialized using caffe_rng_rand.
28
+ */
29
+ void StartInternalThread();
30
+
31
+ /** Will not return until the internal thread has exited. */
32
+ void StopInternalThread();
33
+
34
+ bool is_started() const;
35
+
36
+ protected:
37
+ /* Implement this method in your subclass
38
+ with the code you want your thread to run. */
39
+ virtual void InternalThreadEntry() {}
40
+
41
+ /* Should be tested when running loops to exit when requested. */
42
+ bool must_stop();
43
+
44
+ private:
45
+ void entry(int device, Caffe::Brew mode, int rand_seed,
46
+ int solver_count, int solver_rank, bool multiprocess);
47
+
48
+ shared_ptr<boost::thread> thread_;
49
+ };
50
+
51
+ } // namespace caffe
52
+
53
+ #endif // CAFFE_INTERNAL_THREAD_HPP_
usr/local/include/caffe/layer.hpp ADDED
@@ -0,0 +1,477 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_LAYER_H_
2
+ #define CAFFE_LAYER_H_
3
+
4
+ #include <algorithm>
5
+ #include <string>
6
+ #include <vector>
7
+
8
+ #include "caffe/blob.hpp"
9
+ #include "caffe/common.hpp"
10
+ #include "caffe/layer_factory.hpp"
11
+ #include "caffe/proto/caffe.pb.h"
12
+ #include "caffe/util/math_functions.hpp"
13
+
14
+ /**
15
+ Forward declare boost::thread instead of including boost/thread.hpp
16
+ to avoid a boost/NVCC issues (#1009, #1010) on OSX.
17
+ */
18
+ namespace boost { class mutex; }
19
+
20
+ namespace caffe {
21
+
22
+ /**
23
+ * @brief An interface for the units of computation which can be composed into a
24
+ * Net.
25
+ *
26
+ * Layer%s must implement a Forward function, in which they take their input
27
+ * (bottom) Blob%s (if any) and compute their output Blob%s (if any).
28
+ * They may also implement a Backward function, in which they compute the error
29
+ * gradients with respect to their input Blob%s, given the error gradients with
30
+ * their output Blob%s.
31
+ */
32
+ template <typename Dtype>
33
+ class Layer {
34
+ public:
35
+ /**
36
+ * You should not implement your own constructor. Any set up code should go
37
+ * to SetUp(), where the dimensions of the bottom blobs are provided to the
38
+ * layer.
39
+ */
40
+ explicit Layer(const LayerParameter& param)
41
+ : layer_param_(param) {
42
+ // Set phase and copy blobs (if there are any).
43
+ phase_ = param.phase();
44
+ if (layer_param_.blobs_size() > 0) {
45
+ blobs_.resize(layer_param_.blobs_size());
46
+ for (int i = 0; i < layer_param_.blobs_size(); ++i) {
47
+ blobs_[i].reset(new Blob<Dtype>());
48
+ blobs_[i]->FromProto(layer_param_.blobs(i));
49
+ }
50
+ }
51
+ }
52
+ virtual ~Layer() {}
53
+
54
+ /**
55
+ * @brief Implements common layer setup functionality.
56
+ *
57
+ * @param bottom the preshaped input blobs
58
+ * @param top
59
+ * the allocated but unshaped output blobs, to be shaped by Reshape
60
+ *
61
+ * Checks that the number of bottom and top blobs is correct.
62
+ * Calls LayerSetUp to do special layer setup for individual layer types,
63
+ * followed by Reshape to set up sizes of top blobs and internal buffers.
64
+ * Sets up the loss weight multiplier blobs for any non-zero loss weights.
65
+ * This method may not be overridden.
66
+ */
67
+ void SetUp(const vector<Blob<Dtype>*>& bottom,
68
+ const vector<Blob<Dtype>*>& top) {
69
+ CheckBlobCounts(bottom, top);
70
+ LayerSetUp(bottom, top);
71
+ Reshape(bottom, top);
72
+ SetLossWeights(top);
73
+ }
74
+
75
+ /**
76
+ * @brief Does layer-specific setup: your layer should implement this function
77
+ * as well as Reshape.
78
+ *
79
+ * @param bottom
80
+ * the preshaped input blobs, whose data fields store the input data for
81
+ * this layer
82
+ * @param top
83
+ * the allocated but unshaped output blobs
84
+ *
85
+ * This method should do one-time layer specific setup. This includes reading
86
+ * and processing relevent parameters from the <code>layer_param_</code>.
87
+ * Setting up the shapes of top blobs and internal buffers should be done in
88
+ * <code>Reshape</code>, which will be called before the forward pass to
89
+ * adjust the top blob sizes.
90
+ */
91
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
92
+ const vector<Blob<Dtype>*>& top) {}
93
+
94
+ /**
95
+ * @brief Adjust the shapes of top blobs and internal buffers to accommodate
96
+ * the shapes of the bottom blobs.
97
+ *
98
+ * @param bottom the input blobs, with the requested input shapes
99
+ * @param top the top blobs, which should be reshaped as needed
100
+ *
101
+ * This method should reshape top blobs as needed according to the shapes
102
+ * of the bottom (input) blobs, as well as reshaping any internal buffers
103
+ * and making any other necessary adjustments so that the layer can
104
+ * accommodate the bottom blobs.
105
+ */
106
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
107
+ const vector<Blob<Dtype>*>& top) = 0;
108
+
109
+ /**
110
+ * @brief Given the bottom blobs, compute the top blobs and the loss.
111
+ *
112
+ * @param bottom
113
+ * the input blobs, whose data fields store the input data for this layer
114
+ * @param top
115
+ * the preshaped output blobs, whose data fields will store this layers'
116
+ * outputs
117
+ * \return The total loss from the layer.
118
+ *
119
+ * The Forward wrapper calls the relevant device wrapper function
120
+ * (Forward_cpu or Forward_gpu) to compute the top blob values given the
121
+ * bottom blobs. If the layer has any non-zero loss_weights, the wrapper
122
+ * then computes and returns the loss.
123
+ *
124
+ * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
125
+ */
126
+ inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
127
+ const vector<Blob<Dtype>*>& top);
128
+
129
+ /**
130
+ * @brief Given the top blob error gradients, compute the bottom blob error
131
+ * gradients.
132
+ *
133
+ * @param top
134
+ * the output blobs, whose diff fields store the gradient of the error
135
+ * with respect to themselves
136
+ * @param propagate_down
137
+ * a vector with equal length to bottom, with each index indicating
138
+ * whether to propagate the error gradients down to the bottom blob at
139
+ * the corresponding index
140
+ * @param bottom
141
+ * the input blobs, whose diff fields will store the gradient of the error
142
+ * with respect to themselves after Backward is run
143
+ *
144
+ * The Backward wrapper calls the relevant device wrapper function
145
+ * (Backward_cpu or Backward_gpu) to compute the bottom blob diffs given the
146
+ * top blob diffs.
147
+ *
148
+ * Your layer should implement Backward_cpu and (optionally) Backward_gpu.
149
+ */
150
+ inline void Backward(const vector<Blob<Dtype>*>& top,
151
+ const vector<bool>& propagate_down,
152
+ const vector<Blob<Dtype>*>& bottom);
153
+
154
+ /**
155
+ * @brief Returns the vector of learnable parameter blobs.
156
+ */
157
+ vector<shared_ptr<Blob<Dtype> > >& blobs() {
158
+ return blobs_;
159
+ }
160
+
161
+ /**
162
+ * @brief Returns the layer parameter.
163
+ */
164
+ const LayerParameter& layer_param() const { return layer_param_; }
165
+
166
+ /**
167
+ * @brief Writes the layer parameter to a protocol buffer
168
+ */
169
+ virtual void ToProto(LayerParameter* param, bool write_diff = false);
170
+
171
+ /**
172
+ * @brief Returns the scalar loss associated with a top blob at a given index.
173
+ */
174
+ inline Dtype loss(const int top_index) const {
175
+ return (loss_.size() > top_index) ? loss_[top_index] : Dtype(0);
176
+ }
177
+
178
+ /**
179
+ * @brief Sets the loss associated with a top blob at a given index.
180
+ */
181
+ inline void set_loss(const int top_index, const Dtype value) {
182
+ if (loss_.size() <= top_index) {
183
+ loss_.resize(top_index + 1, Dtype(0));
184
+ }
185
+ loss_[top_index] = value;
186
+ }
187
+
188
+ /**
189
+ * @brief Returns the layer type.
190
+ */
191
+ virtual inline const char* type() const { return ""; }
192
+
193
+ /**
194
+ * @brief Returns the exact number of bottom blobs required by the layer,
195
+ * or -1 if no exact number is required.
196
+ *
197
+ * This method should be overridden to return a non-negative value if your
198
+ * layer expects some exact number of bottom blobs.
199
+ */
200
+ virtual inline int ExactNumBottomBlobs() const { return -1; }
201
+ /**
202
+ * @brief Returns the minimum number of bottom blobs required by the layer,
203
+ * or -1 if no minimum number is required.
204
+ *
205
+ * This method should be overridden to return a non-negative value if your
206
+ * layer expects some minimum number of bottom blobs.
207
+ */
208
+ virtual inline int MinBottomBlobs() const { return -1; }
209
+ /**
210
+ * @brief Returns the maximum number of bottom blobs required by the layer,
211
+ * or -1 if no maximum number is required.
212
+ *
213
+ * This method should be overridden to return a non-negative value if your
214
+ * layer expects some maximum number of bottom blobs.
215
+ */
216
+ virtual inline int MaxBottomBlobs() const { return -1; }
217
+ /**
218
+ * @brief Returns the exact number of top blobs required by the layer,
219
+ * or -1 if no exact number is required.
220
+ *
221
+ * This method should be overridden to return a non-negative value if your
222
+ * layer expects some exact number of top blobs.
223
+ */
224
+ virtual inline int ExactNumTopBlobs() const { return -1; }
225
+ /**
226
+ * @brief Returns the minimum number of top blobs required by the layer,
227
+ * or -1 if no minimum number is required.
228
+ *
229
+ * This method should be overridden to return a non-negative value if your
230
+ * layer expects some minimum number of top blobs.
231
+ */
232
+ virtual inline int MinTopBlobs() const { return -1; }
233
+ /**
234
+ * @brief Returns the maximum number of top blobs required by the layer,
235
+ * or -1 if no maximum number is required.
236
+ *
237
+ * This method should be overridden to return a non-negative value if your
238
+ * layer expects some maximum number of top blobs.
239
+ */
240
+ virtual inline int MaxTopBlobs() const { return -1; }
241
+ /**
242
+ * @brief Returns true if the layer requires an equal number of bottom and
243
+ * top blobs.
244
+ *
245
+ * This method should be overridden to return true if your layer expects an
246
+ * equal number of bottom and top blobs.
247
+ */
248
+ virtual inline bool EqualNumBottomTopBlobs() const { return false; }
249
+
250
+ /**
251
+ * @brief Return whether "anonymous" top blobs are created automatically
252
+ * by the layer.
253
+ *
254
+ * If this method returns true, Net::Init will create enough "anonymous" top
255
+ * blobs to fulfill the requirement specified by ExactNumTopBlobs() or
256
+ * MinTopBlobs().
257
+ */
258
+ virtual inline bool AutoTopBlobs() const { return false; }
259
+
260
+ /**
261
+ * @brief Return whether to allow force_backward for a given bottom blob
262
+ * index.
263
+ *
264
+ * If AllowForceBackward(i) == false, we will ignore the force_backward
265
+ * setting and backpropagate to blob i only if it needs gradient information
266
+ * (as is done when force_backward == false).
267
+ */
268
+ virtual inline bool AllowForceBackward(const int bottom_index) const {
269
+ return true;
270
+ }
271
+
272
+ /**
273
+ * @brief Specifies whether the layer should compute gradients w.r.t. a
274
+ * parameter at a particular index given by param_id.
275
+ *
276
+ * You can safely ignore false values and always compute gradients
277
+ * for all parameters, but possibly with wasteful computation.
278
+ */
279
+ inline bool param_propagate_down(const int param_id) {
280
+ return (param_propagate_down_.size() > param_id) ?
281
+ param_propagate_down_[param_id] : false;
282
+ }
283
+ /**
284
+ * @brief Sets whether the layer should compute gradients w.r.t. a
285
+ * parameter at a particular index given by param_id.
286
+ */
287
+ inline void set_param_propagate_down(const int param_id, const bool value) {
288
+ if (param_propagate_down_.size() <= param_id) {
289
+ param_propagate_down_.resize(param_id + 1, true);
290
+ }
291
+ param_propagate_down_[param_id] = value;
292
+ }
293
+
294
+
295
+ protected:
296
+ /** The protobuf that stores the layer parameters */
297
+ LayerParameter layer_param_;
298
+ /** The phase: TRAIN or TEST */
299
+ Phase phase_;
300
+ /** The vector that stores the learnable parameters as a set of blobs. */
301
+ vector<shared_ptr<Blob<Dtype> > > blobs_;
302
+ /** Vector indicating whether to compute the diff of each param blob. */
303
+ vector<bool> param_propagate_down_;
304
+
305
+ /** The vector that indicates whether each top blob has a non-zero weight in
306
+ * the objective function. */
307
+ vector<Dtype> loss_;
308
+
309
+ /** @brief Using the CPU device, compute the layer output. */
310
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
311
+ const vector<Blob<Dtype>*>& top) = 0;
312
+ /**
313
+ * @brief Using the GPU device, compute the layer output.
314
+ * Fall back to Forward_cpu() if unavailable.
315
+ */
316
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
317
+ const vector<Blob<Dtype>*>& top) {
318
+ // LOG(WARNING) << "Using CPU code as backup.";
319
+ return Forward_cpu(bottom, top);
320
+ }
321
+
322
+ /**
323
+ * @brief Using the CPU device, compute the gradients for any parameters and
324
+ * for the bottom blobs if propagate_down is true.
325
+ */
326
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
327
+ const vector<bool>& propagate_down,
328
+ const vector<Blob<Dtype>*>& bottom) = 0;
329
+ /**
330
+ * @brief Using the GPU device, compute the gradients for any parameters and
331
+ * for the bottom blobs if propagate_down is true.
332
+ * Fall back to Backward_cpu() if unavailable.
333
+ */
334
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
335
+ const vector<bool>& propagate_down,
336
+ const vector<Blob<Dtype>*>& bottom) {
337
+ // LOG(WARNING) << "Using CPU code as backup.";
338
+ Backward_cpu(top, propagate_down, bottom);
339
+ }
340
+
341
+ /**
342
+ * Called by the parent Layer's SetUp to check that the number of bottom
343
+ * and top Blobs provided as input match the expected numbers specified by
344
+ * the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.
345
+ */
346
+ virtual void CheckBlobCounts(const vector<Blob<Dtype>*>& bottom,
347
+ const vector<Blob<Dtype>*>& top) {
348
+ if (ExactNumBottomBlobs() >= 0) {
349
+ CHECK_EQ(ExactNumBottomBlobs(), bottom.size())
350
+ << type() << " Layer takes " << ExactNumBottomBlobs()
351
+ << " bottom blob(s) as input.";
352
+ }
353
+ if (MinBottomBlobs() >= 0) {
354
+ CHECK_LE(MinBottomBlobs(), bottom.size())
355
+ << type() << " Layer takes at least " << MinBottomBlobs()
356
+ << " bottom blob(s) as input.";
357
+ }
358
+ if (MaxBottomBlobs() >= 0) {
359
+ CHECK_GE(MaxBottomBlobs(), bottom.size())
360
+ << type() << " Layer takes at most " << MaxBottomBlobs()
361
+ << " bottom blob(s) as input.";
362
+ }
363
+ if (ExactNumTopBlobs() >= 0) {
364
+ CHECK_EQ(ExactNumTopBlobs(), top.size())
365
+ << type() << " Layer produces " << ExactNumTopBlobs()
366
+ << " top blob(s) as output.";
367
+ }
368
+ if (MinTopBlobs() >= 0) {
369
+ CHECK_LE(MinTopBlobs(), top.size())
370
+ << type() << " Layer produces at least " << MinTopBlobs()
371
+ << " top blob(s) as output.";
372
+ }
373
+ if (MaxTopBlobs() >= 0) {
374
+ CHECK_GE(MaxTopBlobs(), top.size())
375
+ << type() << " Layer produces at most " << MaxTopBlobs()
376
+ << " top blob(s) as output.";
377
+ }
378
+ if (EqualNumBottomTopBlobs()) {
379
+ CHECK_EQ(bottom.size(), top.size())
380
+ << type() << " Layer produces one top blob as output for each "
381
+ << "bottom blob input.";
382
+ }
383
+ }
384
+
385
+ /**
386
+ * Called by SetUp to initialize the weights associated with any top blobs in
387
+ * the loss function. Store non-zero loss weights in the diff blob.
388
+ */
389
+ inline void SetLossWeights(const vector<Blob<Dtype>*>& top) {
390
+ const int num_loss_weights = layer_param_.loss_weight_size();
391
+ if (num_loss_weights) {
392
+ CHECK_EQ(top.size(), num_loss_weights) << "loss_weight must be "
393
+ "unspecified or specified once per top blob.";
394
+ for (int top_id = 0; top_id < top.size(); ++top_id) {
395
+ const Dtype loss_weight = layer_param_.loss_weight(top_id);
396
+ if (loss_weight == Dtype(0)) { continue; }
397
+ this->set_loss(top_id, loss_weight);
398
+ const int count = top[top_id]->count();
399
+ Dtype* loss_multiplier = top[top_id]->mutable_cpu_diff();
400
+ caffe_set(count, loss_weight, loss_multiplier);
401
+ }
402
+ }
403
+ }
404
+
405
+ private:
406
+ DISABLE_COPY_AND_ASSIGN(Layer);
407
+ }; // class Layer
408
+
409
+ // Forward and backward wrappers. You should implement the cpu and
410
+ // gpu specific implementations instead, and should not change these
411
+ // functions.
412
+ template <typename Dtype>
413
+ inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
414
+ const vector<Blob<Dtype>*>& top) {
415
+ Dtype loss = 0;
416
+ Reshape(bottom, top);
417
+ switch (Caffe::mode()) {
418
+ case Caffe::CPU:
419
+ Forward_cpu(bottom, top);
420
+ for (int top_id = 0; top_id < top.size(); ++top_id) {
421
+ if (!this->loss(top_id)) { continue; }
422
+ const int count = top[top_id]->count();
423
+ const Dtype* data = top[top_id]->cpu_data();
424
+ const Dtype* loss_weights = top[top_id]->cpu_diff();
425
+ loss += caffe_cpu_dot(count, data, loss_weights);
426
+ }
427
+ break;
428
+ case Caffe::GPU:
429
+ Forward_gpu(bottom, top);
430
+ #ifndef CPU_ONLY
431
+ for (int top_id = 0; top_id < top.size(); ++top_id) {
432
+ if (!this->loss(top_id)) { continue; }
433
+ const int count = top[top_id]->count();
434
+ const Dtype* data = top[top_id]->gpu_data();
435
+ const Dtype* loss_weights = top[top_id]->gpu_diff();
436
+ Dtype blob_loss = 0;
437
+ caffe_gpu_dot(count, data, loss_weights, &blob_loss);
438
+ loss += blob_loss;
439
+ }
440
+ #endif
441
+ break;
442
+ default:
443
+ LOG(FATAL) << "Unknown caffe mode.";
444
+ }
445
+ return loss;
446
+ }
447
+
448
+ template <typename Dtype>
449
+ inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
450
+ const vector<bool>& propagate_down,
451
+ const vector<Blob<Dtype>*>& bottom) {
452
+ switch (Caffe::mode()) {
453
+ case Caffe::CPU:
454
+ Backward_cpu(top, propagate_down, bottom);
455
+ break;
456
+ case Caffe::GPU:
457
+ Backward_gpu(top, propagate_down, bottom);
458
+ break;
459
+ default:
460
+ LOG(FATAL) << "Unknown caffe mode.";
461
+ }
462
+ }
463
+
464
+ // Serialize LayerParameter to protocol buffer
465
+ template <typename Dtype>
466
+ void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
467
+ param->Clear();
468
+ param->CopyFrom(layer_param_);
469
+ param->clear_blobs();
470
+ for (int i = 0; i < blobs_.size(); ++i) {
471
+ blobs_[i]->ToProto(param->add_blobs(), write_diff);
472
+ }
473
+ }
474
+
475
+ } // namespace caffe
476
+
477
+ #endif // CAFFE_LAYER_H_
usr/local/include/caffe/layer_factory.hpp ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * @brief A layer factory that allows one to register layers.
3
+ * During runtime, registered layers can be called by passing a LayerParameter
4
+ * protobuffer to the CreateLayer function:
5
+ *
6
+ * LayerRegistry<Dtype>::CreateLayer(param);
7
+ *
8
+ * There are two ways to register a layer. Assuming that we have a layer like:
9
+ *
10
+ * template <typename Dtype>
11
+ * class MyAwesomeLayer : public Layer<Dtype> {
12
+ * // your implementations
13
+ * };
14
+ *
15
+ * and its type is its C++ class name, but without the "Layer" at the end
16
+ * ("MyAwesomeLayer" -> "MyAwesome").
17
+ *
18
+ * If the layer is going to be created simply by its constructor, in your c++
19
+ * file, add the following line:
20
+ *
21
+ * REGISTER_LAYER_CLASS(MyAwesome);
22
+ *
23
+ * Or, if the layer is going to be created by another creator function, in the
24
+ * format of:
25
+ *
26
+ * template <typename Dtype>
27
+ * Layer<Dtype*> GetMyAwesomeLayer(const LayerParameter& param) {
28
+ * // your implementation
29
+ * }
30
+ *
31
+ * (for example, when your layer has multiple backends, see GetConvolutionLayer
32
+ * for a use case), then you can register the creator function instead, like
33
+ *
34
+ * REGISTER_LAYER_CREATOR(MyAwesome, GetMyAwesomeLayer)
35
+ *
36
+ * Note that each layer type should only be registered once.
37
+ */
38
+
39
+ #ifndef CAFFE_LAYER_FACTORY_H_
40
+ #define CAFFE_LAYER_FACTORY_H_
41
+
42
+ #include <map>
43
+ #include <string>
44
+ #include <vector>
45
+
46
+ #include "caffe/common.hpp"
47
+ #include "caffe/layer.hpp"
48
+ #include "caffe/proto/caffe.pb.h"
49
+
50
+ namespace caffe {
51
+
52
+ template <typename Dtype>
53
+ class Layer;
54
+
55
+ template <typename Dtype>
56
+ class LayerRegistry {
57
+ public:
58
+ typedef shared_ptr<Layer<Dtype> > (*Creator)(const LayerParameter&);
59
+ typedef std::map<string, Creator> CreatorRegistry;
60
+
61
+ static CreatorRegistry& Registry() {
62
+ static CreatorRegistry* g_registry_ = new CreatorRegistry();
63
+ return *g_registry_;
64
+ }
65
+
66
+ // Adds a creator.
67
+ static void AddCreator(const string& type, Creator creator) {
68
+ CreatorRegistry& registry = Registry();
69
+ CHECK_EQ(registry.count(type), 0)
70
+ << "Layer type " << type << " already registered.";
71
+ registry[type] = creator;
72
+ }
73
+
74
+ // Get a layer using a LayerParameter.
75
+ static shared_ptr<Layer<Dtype> > CreateLayer(const LayerParameter& param) {
76
+ if (Caffe::root_solver()) {
77
+ LOG(INFO) << "Creating layer " << param.name();
78
+ }
79
+ const string& type = param.type();
80
+ CreatorRegistry& registry = Registry();
81
+ CHECK_EQ(registry.count(type), 1) << "Unknown layer type: " << type
82
+ << " (known types: " << LayerTypeListString() << ")";
83
+ return registry[type](param);
84
+ }
85
+
86
+ static vector<string> LayerTypeList() {
87
+ CreatorRegistry& registry = Registry();
88
+ vector<string> layer_types;
89
+ for (typename CreatorRegistry::iterator iter = registry.begin();
90
+ iter != registry.end(); ++iter) {
91
+ layer_types.push_back(iter->first);
92
+ }
93
+ return layer_types;
94
+ }
95
+
96
+ private:
97
+ // Layer registry should never be instantiated - everything is done with its
98
+ // static variables.
99
+ LayerRegistry() {}
100
+
101
+ static string LayerTypeListString() {
102
+ vector<string> layer_types = LayerTypeList();
103
+ string layer_types_str;
104
+ for (vector<string>::iterator iter = layer_types.begin();
105
+ iter != layer_types.end(); ++iter) {
106
+ if (iter != layer_types.begin()) {
107
+ layer_types_str += ", ";
108
+ }
109
+ layer_types_str += *iter;
110
+ }
111
+ return layer_types_str;
112
+ }
113
+ };
114
+
115
+
116
+ template <typename Dtype>
117
+ class LayerRegisterer {
118
+ public:
119
+ LayerRegisterer(const string& type,
120
+ shared_ptr<Layer<Dtype> > (*creator)(const LayerParameter&)) {
121
+ // LOG(INFO) << "Registering layer type: " << type;
122
+ LayerRegistry<Dtype>::AddCreator(type, creator);
123
+ }
124
+ };
125
+
126
+
127
+ #define REGISTER_LAYER_CREATOR(type, creator) \
128
+ static LayerRegisterer<float> g_creator_f_##type(#type, creator<float>); \
129
+ static LayerRegisterer<double> g_creator_d_##type(#type, creator<double>) \
130
+
131
+ #define REGISTER_LAYER_CLASS(type) \
132
+ template <typename Dtype> \
133
+ shared_ptr<Layer<Dtype> > Creator_##type##Layer(const LayerParameter& param) \
134
+ { \
135
+ return shared_ptr<Layer<Dtype> >(new type##Layer<Dtype>(param)); \
136
+ } \
137
+ REGISTER_LAYER_CREATOR(type, Creator_##type##Layer)
138
+
139
+ } // namespace caffe
140
+
141
+ #endif // CAFFE_LAYER_FACTORY_H_
usr/local/include/caffe/layers/absval_layer.hpp ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_ABSVAL_LAYER_HPP_
2
+ #define CAFFE_ABSVAL_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes @f$ y = |x| @f$
16
+ *
17
+ * @param bottom input Blob vector (length 1)
18
+ * -# @f$ (N \times C \times H \times W) @f$
19
+ * the inputs @f$ x @f$
20
+ * @param top output Blob vector (length 1)
21
+ * -# @f$ (N \times C \times H \times W) @f$
22
+ * the computed outputs @f$ y = |x| @f$
23
+ */
24
+ template <typename Dtype>
25
+ class AbsValLayer : public NeuronLayer<Dtype> {
26
+ public:
27
+ explicit AbsValLayer(const LayerParameter& param)
28
+ : NeuronLayer<Dtype>(param) {}
29
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
30
+ const vector<Blob<Dtype>*>& top);
31
+
32
+ virtual inline const char* type() const { return "AbsVal"; }
33
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
34
+ virtual inline int ExactNumTopBlobs() const { return 1; }
35
+
36
+ protected:
37
+ /// @copydoc AbsValLayer
38
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
39
+ const vector<Blob<Dtype>*>& top);
40
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
41
+ const vector<Blob<Dtype>*>& top);
42
+
43
+ /**
44
+ * @brief Computes the error gradient w.r.t. the absolute value inputs.
45
+ *
46
+ * @param top output Blob vector (length 1), providing the error gradient with
47
+ * respect to the outputs
48
+ * -# @f$ (N \times C \times H \times W) @f$
49
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
50
+ * with respect to computed outputs @f$ y @f$
51
+ * @param propagate_down see Layer::Backward.
52
+ * @param bottom input Blob vector (length 2)
53
+ * -# @f$ (N \times C \times H \times W) @f$
54
+ * the inputs @f$ x @f$; Backward fills their diff with
55
+ * gradients @f$
56
+ * \frac{\partial E}{\partial x} =
57
+ * \mathrm{sign}(x) \frac{\partial E}{\partial y}
58
+ * @f$ if propagate_down[0]
59
+ */
60
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
61
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
62
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
63
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
64
+ };
65
+
66
+ } // namespace caffe
67
+
68
+ #endif // CAFFE_ABSVAL_LAYER_HPP_
usr/local/include/caffe/layers/accuracy_layer.hpp ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_ACCURACY_LAYER_HPP_
2
+ #define CAFFE_ACCURACY_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/loss_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes the classification accuracy for a one-of-many
16
+ * classification task.
17
+ */
18
+ template <typename Dtype>
19
+ class AccuracyLayer : public Layer<Dtype> {
20
+ public:
21
+ /**
22
+ * @param param provides AccuracyParameter accuracy_param,
23
+ * with AccuracyLayer options:
24
+ * - top_k (\b optional, default 1).
25
+ * Sets the maximum rank @f$ k @f$ at which a prediction is considered
26
+ * correct. For example, if @f$ k = 5 @f$, a prediction is counted
27
+ * correct if the correct label is among the top 5 predicted labels.
28
+ */
29
+ explicit AccuracyLayer(const LayerParameter& param)
30
+ : Layer<Dtype>(param) {}
31
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
34
+ const vector<Blob<Dtype>*>& top);
35
+
36
+ virtual inline const char* type() const { return "Accuracy"; }
37
+ virtual inline int ExactNumBottomBlobs() const { return 2; }
38
+
39
+ // If there are two top blobs, then the second blob will contain
40
+ // accuracies per class.
41
+ virtual inline int MinTopBlobs() const { return 1; }
42
+ virtual inline int MaxTopBlobs() const { return 2; }
43
+
44
+ protected:
45
+ /**
46
+ * @param bottom input Blob vector (length 2)
47
+ * -# @f$ (N \times C \times H \times W) @f$
48
+ * the predictions @f$ x @f$, a Blob with values in
49
+ * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of
50
+ * the @f$ K = CHW @f$ classes. Each @f$ x_n @f$ is mapped to a predicted
51
+ * label @f$ \hat{l}_n @f$ given by its maximal index:
52
+ * @f$ \hat{l}_n = \arg\max\limits_k x_{nk} @f$
53
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
54
+ * the labels @f$ l @f$, an integer-valued Blob with values
55
+ * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$
56
+ * indicating the correct class label among the @f$ K @f$ classes
57
+ * @param top output Blob vector (length 1)
58
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
59
+ * the computed accuracy: @f$
60
+ * \frac{1}{N} \sum\limits_{n=1}^N \delta\{ \hat{l}_n = l_n \}
61
+ * @f$, where @f$
62
+ * \delta\{\mathrm{condition}\} = \left\{
63
+ * \begin{array}{lr}
64
+ * 1 & \mbox{if condition} \\
65
+ * 0 & \mbox{otherwise}
66
+ * \end{array} \right.
67
+ * @f$
68
+ */
69
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
70
+ const vector<Blob<Dtype>*>& top);
71
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
72
+ const vector<Blob<Dtype>*>& top);
73
+
74
+
75
+ /// @brief Not implemented -- AccuracyLayer cannot be used as a loss.
76
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
77
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
78
+ for (int i = 0; i < propagate_down.size(); ++i) {
79
+ if (propagate_down[i]) { NOT_IMPLEMENTED; }
80
+ }
81
+ }
82
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
83
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
84
+
85
+ int label_axis_, outer_num_, inner_num_;
86
+
87
+ int top_k_;
88
+
89
+ /// Whether to ignore instances with a certain label.
90
+ bool has_ignore_label_;
91
+ /// The label indicating that an instance should be ignored.
92
+ int ignore_label_;
93
+ /// Keeps counts of the number of samples per class.
94
+ Blob<Dtype> nums_buffer_;
95
+ };
96
+
97
+ } // namespace caffe
98
+
99
+ #endif // CAFFE_ACCURACY_LAYER_HPP_
usr/local/include/caffe/layers/argmax_layer.hpp ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_ARGMAX_LAYER_HPP_
2
+ #define CAFFE_ARGMAX_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Compute the index of the @f$ K @f$ max values for each datum across
14
+ * all dimensions @f$ (C \times H \times W) @f$.
15
+ *
16
+ * Intended for use after a classification layer to produce a prediction.
17
+ * If parameter out_max_val is set to true, output is a vector of pairs
18
+ * (max_ind, max_val) for each image. The axis parameter specifies an axis
19
+ * along which to maximise.
20
+ *
21
+ * NOTE: does not implement Backwards operation.
22
+ */
23
+ template <typename Dtype>
24
+ class ArgMaxLayer : public Layer<Dtype> {
25
+ public:
26
+ /**
27
+ * @param param provides ArgMaxParameter argmax_param,
28
+ * with ArgMaxLayer options:
29
+ * - top_k (\b optional uint, default 1).
30
+ * the number @f$ K @f$ of maximal items to output.
31
+ * - out_max_val (\b optional bool, default false).
32
+ * if set, output a vector of pairs (max_ind, max_val) unless axis is set then
33
+ * output max_val along the specified axis.
34
+ * - axis (\b optional int).
35
+ * if set, maximise along the specified axis else maximise the flattened
36
+ * trailing dimensions for each index of the first / num dimension.
37
+ */
38
+ explicit ArgMaxLayer(const LayerParameter& param)
39
+ : Layer<Dtype>(param) {}
40
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
41
+ const vector<Blob<Dtype>*>& top);
42
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
43
+ const vector<Blob<Dtype>*>& top);
44
+
45
+ virtual inline const char* type() const { return "ArgMax"; }
46
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
47
+ virtual inline int ExactNumTopBlobs() const { return 1; }
48
+
49
+ protected:
50
+ /**
51
+ * @param bottom input Blob vector (length 1)
52
+ * -# @f$ (N \times C \times H \times W) @f$
53
+ * the inputs @f$ x @f$
54
+ * @param top output Blob vector (length 1)
55
+ * -# @f$ (N \times 1 \times K) @f$ or, if out_max_val
56
+ * @f$ (N \times 2 \times K) @f$ unless axis set than e.g.
57
+ * @f$ (N \times K \times H \times W) @f$ if axis == 1
58
+ * the computed outputs @f$
59
+ * y_n = \arg\max\limits_i x_{ni}
60
+ * @f$ (for @f$ K = 1 @f$).
61
+ */
62
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
63
+ const vector<Blob<Dtype>*>& top);
64
+ /// @brief Not implemented (non-differentiable function)
65
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
66
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
67
+ NOT_IMPLEMENTED;
68
+ }
69
+ bool out_max_val_;
70
+ size_t top_k_;
71
+ bool has_axis_;
72
+ int axis_;
73
+ };
74
+
75
+ } // namespace caffe
76
+
77
+ #endif // CAFFE_ARGMAX_LAYER_HPP_
usr/local/include/caffe/layers/base_conv_layer.hpp ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BASE_CONVOLUTION_LAYER_HPP_
2
+ #define CAFFE_BASE_CONVOLUTION_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+ #include "caffe/util/im2col.hpp"
10
+
11
+ namespace caffe {
12
+
13
+ /**
14
+ * @brief Abstract base class that factors out the BLAS code common to
15
+ * ConvolutionLayer and DeconvolutionLayer.
16
+ */
17
+ template <typename Dtype>
18
+ class BaseConvolutionLayer : public Layer<Dtype> {
19
+ public:
20
+ explicit BaseConvolutionLayer(const LayerParameter& param)
21
+ : Layer<Dtype>(param) {}
22
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
23
+ const vector<Blob<Dtype>*>& top);
24
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+
27
+ virtual inline int MinBottomBlobs() const { return 1; }
28
+ virtual inline int MinTopBlobs() const { return 1; }
29
+ virtual inline bool EqualNumBottomTopBlobs() const { return true; }
30
+
31
+ protected:
32
+ // Helper functions that abstract away the column buffer and gemm arguments.
33
+ // The last argument in forward_cpu_gemm is so that we can skip the im2col if
34
+ // we just called weight_cpu_gemm with the same input.
35
+ void forward_cpu_gemm(const Dtype* input, const Dtype* weights,
36
+ Dtype* output, bool skip_im2col = false);
37
+ void forward_cpu_bias(Dtype* output, const Dtype* bias);
38
+ void backward_cpu_gemm(const Dtype* input, const Dtype* weights,
39
+ Dtype* output);
40
+ void weight_cpu_gemm(const Dtype* input, const Dtype* output, Dtype*
41
+ weights);
42
+ void backward_cpu_bias(Dtype* bias, const Dtype* input);
43
+
44
+ #ifndef CPU_ONLY
45
+ void forward_gpu_gemm(const Dtype* col_input, const Dtype* weights,
46
+ Dtype* output, bool skip_im2col = false);
47
+ void forward_gpu_bias(Dtype* output, const Dtype* bias);
48
+ void backward_gpu_gemm(const Dtype* input, const Dtype* weights,
49
+ Dtype* col_output);
50
+ void weight_gpu_gemm(const Dtype* col_input, const Dtype* output, Dtype*
51
+ weights);
52
+ void backward_gpu_bias(Dtype* bias, const Dtype* input);
53
+ #endif
54
+
55
+ /// @brief The spatial dimensions of the input.
56
+ inline int input_shape(int i) {
57
+ return (*bottom_shape_)[channel_axis_ + i];
58
+ }
59
+ // reverse_dimensions should return true iff we are implementing deconv, so
60
+ // that conv helpers know which dimensions are which.
61
+ virtual bool reverse_dimensions() = 0;
62
+ // Compute height_out_ and width_out_ from other parameters.
63
+ virtual void compute_output_shape() = 0;
64
+
65
+ /// @brief The spatial dimensions of a filter kernel.
66
+ Blob<int> kernel_shape_;
67
+ /// @brief The spatial dimensions of the stride.
68
+ Blob<int> stride_;
69
+ /// @brief The spatial dimensions of the padding.
70
+ Blob<int> pad_;
71
+ /// @brief The spatial dimensions of the dilation.
72
+ Blob<int> dilation_;
73
+ /// @brief The spatial dimensions of the convolution input.
74
+ Blob<int> conv_input_shape_;
75
+ /// @brief The spatial dimensions of the col_buffer.
76
+ vector<int> col_buffer_shape_;
77
+ /// @brief The spatial dimensions of the output.
78
+ vector<int> output_shape_;
79
+ const vector<int>* bottom_shape_;
80
+
81
+ int num_spatial_axes_;
82
+ int bottom_dim_;
83
+ int top_dim_;
84
+
85
+ int channel_axis_;
86
+ int num_;
87
+ int channels_;
88
+ int group_;
89
+ int out_spatial_dim_;
90
+ int weight_offset_;
91
+ int num_output_;
92
+ bool bias_term_;
93
+ bool is_1x1_;
94
+ bool force_nd_im2col_;
95
+
96
+ private:
97
+ // wrap im2col/col2im so we don't have to remember the (long) argument lists
98
+ inline void conv_im2col_cpu(const Dtype* data, Dtype* col_buff) {
99
+ if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
100
+ im2col_cpu(data, conv_in_channels_,
101
+ conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
102
+ kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
103
+ pad_.cpu_data()[0], pad_.cpu_data()[1],
104
+ stride_.cpu_data()[0], stride_.cpu_data()[1],
105
+ dilation_.cpu_data()[0], dilation_.cpu_data()[1], col_buff);
106
+ } else {
107
+ im2col_nd_cpu(data, num_spatial_axes_, conv_input_shape_.cpu_data(),
108
+ col_buffer_shape_.data(), kernel_shape_.cpu_data(),
109
+ pad_.cpu_data(), stride_.cpu_data(), dilation_.cpu_data(), col_buff);
110
+ }
111
+ }
112
+ inline void conv_col2im_cpu(const Dtype* col_buff, Dtype* data) {
113
+ if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
114
+ col2im_cpu(col_buff, conv_in_channels_,
115
+ conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
116
+ kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
117
+ pad_.cpu_data()[0], pad_.cpu_data()[1],
118
+ stride_.cpu_data()[0], stride_.cpu_data()[1],
119
+ dilation_.cpu_data()[0], dilation_.cpu_data()[1], data);
120
+ } else {
121
+ col2im_nd_cpu(col_buff, num_spatial_axes_, conv_input_shape_.cpu_data(),
122
+ col_buffer_shape_.data(), kernel_shape_.cpu_data(),
123
+ pad_.cpu_data(), stride_.cpu_data(), dilation_.cpu_data(), data);
124
+ }
125
+ }
126
+ #ifndef CPU_ONLY
127
+ inline void conv_im2col_gpu(const Dtype* data, Dtype* col_buff) {
128
+ if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
129
+ im2col_gpu(data, conv_in_channels_,
130
+ conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
131
+ kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
132
+ pad_.cpu_data()[0], pad_.cpu_data()[1],
133
+ stride_.cpu_data()[0], stride_.cpu_data()[1],
134
+ dilation_.cpu_data()[0], dilation_.cpu_data()[1], col_buff);
135
+ } else {
136
+ im2col_nd_gpu(data, num_spatial_axes_, num_kernels_im2col_,
137
+ conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(),
138
+ kernel_shape_.gpu_data(), pad_.gpu_data(),
139
+ stride_.gpu_data(), dilation_.gpu_data(), col_buff);
140
+ }
141
+ }
142
+ inline void conv_col2im_gpu(const Dtype* col_buff, Dtype* data) {
143
+ if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
144
+ col2im_gpu(col_buff, conv_in_channels_,
145
+ conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
146
+ kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
147
+ pad_.cpu_data()[0], pad_.cpu_data()[1],
148
+ stride_.cpu_data()[0], stride_.cpu_data()[1],
149
+ dilation_.cpu_data()[0], dilation_.cpu_data()[1], data);
150
+ } else {
151
+ col2im_nd_gpu(col_buff, num_spatial_axes_, num_kernels_col2im_,
152
+ conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(),
153
+ kernel_shape_.gpu_data(), pad_.gpu_data(), stride_.gpu_data(),
154
+ dilation_.gpu_data(), data);
155
+ }
156
+ }
157
+ #endif
158
+
159
+ int num_kernels_im2col_;
160
+ int num_kernels_col2im_;
161
+ int conv_out_channels_;
162
+ int conv_in_channels_;
163
+ int conv_out_spatial_dim_;
164
+ int kernel_dim_;
165
+ int col_offset_;
166
+ int output_offset_;
167
+
168
+ Blob<Dtype> col_buffer_;
169
+ Blob<Dtype> bias_multiplier_;
170
+ };
171
+
172
+ } // namespace caffe
173
+
174
+ #endif // CAFFE_BASE_CONVOLUTION_LAYER_HPP_
usr/local/include/caffe/layers/base_data_layer.hpp ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
15
+ /**
16
+ * @brief Provides base for data layers that feed blobs to the Net.
17
+ *
18
+ * TODO(dox): thorough documentation for Forward and proto params.
19
+ */
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>
53
+ class BasePrefetchingDataLayer :
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_
usr/local/include/caffe/layers/batch_norm_layer.hpp ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BATCHNORM_LAYER_HPP_
2
+ #define CAFFE_BATCHNORM_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Normalizes the input to have 0-mean and/or unit (1) variance across
14
+ * the batch.
15
+ *
16
+ * This layer computes Batch Normalization as described in [1]. For each channel
17
+ * in the data (i.e. axis 1), it subtracts the mean and divides by the variance,
18
+ * where both statistics are computed across both spatial dimensions and across
19
+ * the different examples in the batch.
20
+ *
21
+ * By default, during training time, the network is computing global
22
+ * mean/variance statistics via a running average, which is then used at test
23
+ * time to allow deterministic outputs for each input. You can manually toggle
24
+ * whether the network is accumulating or using the statistics via the
25
+ * use_global_stats option. For reference, these statistics are kept in the
26
+ * layer's three blobs: (0) mean, (1) variance, and (2) moving average factor.
27
+ *
28
+ * Note that the original paper also included a per-channel learned bias and
29
+ * scaling factor. To implement this in Caffe, define a `ScaleLayer` configured
30
+ * with `bias_term: true` after each `BatchNormLayer` to handle both the bias
31
+ * and scaling factor.
32
+ *
33
+ * [1] S. Ioffe and C. Szegedy, "Batch Normalization: Accelerating Deep Network
34
+ * Training by Reducing Internal Covariate Shift." arXiv preprint
35
+ * arXiv:1502.03167 (2015).
36
+ *
37
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
38
+ */
39
+ template <typename Dtype>
40
+ class BatchNormLayer : public Layer<Dtype> {
41
+ public:
42
+ explicit BatchNormLayer(const LayerParameter& param)
43
+ : Layer<Dtype>(param) {}
44
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
45
+ const vector<Blob<Dtype>*>& top);
46
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
47
+ const vector<Blob<Dtype>*>& top);
48
+
49
+ virtual inline const char* type() const { return "BatchNorm"; }
50
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
51
+ virtual inline int ExactNumTopBlobs() const { return 1; }
52
+
53
+ protected:
54
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
55
+ const vector<Blob<Dtype>*>& top);
56
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
57
+ const vector<Blob<Dtype>*>& top);
58
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
59
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
60
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
61
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
62
+
63
+ Blob<Dtype> mean_, variance_, temp_, x_norm_;
64
+ bool use_global_stats_;
65
+ Dtype moving_average_fraction_;
66
+ int channels_;
67
+ Dtype eps_;
68
+
69
+ // extra temporarary variables is used to carry out sums/broadcasting
70
+ // using BLAS
71
+ Blob<Dtype> batch_sum_multiplier_;
72
+ Blob<Dtype> num_by_chans_;
73
+ Blob<Dtype> spatial_sum_multiplier_;
74
+ };
75
+
76
+ } // namespace caffe
77
+
78
+ #endif // CAFFE_BATCHNORM_LAYER_HPP_
usr/local/include/caffe/layers/batch_reindex_layer.hpp ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BATCHREINDEX_LAYER_HPP_
2
+ #define CAFFE_BATCHREINDEX_LAYER_HPP_
3
+
4
+ #include <utility>
5
+ #include <vector>
6
+
7
+ #include "caffe/blob.hpp"
8
+ #include "caffe/layer.hpp"
9
+ #include "caffe/proto/caffe.pb.h"
10
+
11
+ namespace caffe {
12
+
13
+ /**
14
+ * @brief Index into the input blob along its first axis.
15
+ *
16
+ * This layer can be used to select, reorder, and even replicate examples in a
17
+ * batch. The second blob is cast to int and treated as an index into the
18
+ * first axis of the first blob.
19
+ */
20
+ template <typename Dtype>
21
+ class BatchReindexLayer : public Layer<Dtype> {
22
+ public:
23
+ explicit BatchReindexLayer(const LayerParameter& param)
24
+ : Layer<Dtype>(param) {}
25
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
26
+ const vector<Blob<Dtype>*>& top);
27
+
28
+ virtual inline const char* type() const { return "BatchReindex"; }
29
+ virtual inline int ExactNumBottomBlobs() const { return 2; }
30
+ virtual inline int ExactNumTopBlobs() const { return 1; }
31
+
32
+ protected:
33
+ /**
34
+ * @param bottom input Blob vector (length 2+)
35
+ * -# @f$ (N \times ...) @f$
36
+ * the inputs @f$ x_1 @f$
37
+ * -# @f$ (M) @f$
38
+ * the inputs @f$ x_2 @f$
39
+ * @param top output Blob vector (length 1)
40
+ * -# @f$ (M \times ...) @f$:
41
+ * the reindexed array @f$
42
+ * y = x_1[x_2]
43
+ * @f$
44
+ */
45
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
46
+ const vector<Blob<Dtype>*>& top);
47
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
48
+ const vector<Blob<Dtype>*>& top);
49
+
50
+ /**
51
+ * @brief Computes the error gradient w.r.t. the reordered input.
52
+ *
53
+ * @param top output Blob vector (length 1), providing the error gradient
54
+ * with respect to the outputs
55
+ * -# @f$ (M \times ...) @f$:
56
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
57
+ * with respect to concatenated outputs @f$ y @f$
58
+ * @param propagate_down see Layer::Backward.
59
+ * @param bottom input Blob vector (length 2):
60
+ * - @f$ \frac{\partial E}{\partial y} @f$ is de-indexed (summing where
61
+ * required) back to the input x_1
62
+ * - This layer cannot backprop to x_2, i.e. propagate_down[1] must be
63
+ * false.
64
+ */
65
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
66
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
67
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
68
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
69
+
70
+ private:
71
+ struct pair_sort_first {
72
+ bool operator()(const std::pair<int, int> &left,
73
+ const std::pair<int, int> &right) {
74
+ return left.first < right.first;
75
+ }
76
+ };
77
+ void check_batch_reindex(int initial_num, int final_num,
78
+ const Dtype* ridx_data);
79
+ };
80
+
81
+ } // namespace caffe
82
+
83
+ #endif // CAFFE_BATCHREINDEX_LAYER_HPP_
usr/local/include/caffe/layers/bias_layer.hpp ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BIAS_LAYER_HPP_
2
+ #define CAFFE_BIAS_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Computes a sum of two input Blobs, with the shape of the latter Blob
14
+ * "broadcast" to match the shape of the former. Equivalent to tiling
15
+ * the latter Blob, then computing the elementwise sum.
16
+ *
17
+ * The second input may be omitted, in which case it's learned as a parameter
18
+ * of the layer. Note: in case bias and scaling are desired, both operations can
19
+ * be handled by `ScaleLayer` configured with `bias_term: true`.
20
+ */
21
+ template <typename Dtype>
22
+ class BiasLayer : public Layer<Dtype> {
23
+ public:
24
+ explicit BiasLayer(const LayerParameter& param)
25
+ : Layer<Dtype>(param) {}
26
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
29
+ const vector<Blob<Dtype>*>& top);
30
+
31
+ virtual inline const char* type() const { return "Bias"; }
32
+ virtual inline int MinBottomBlobs() const { return 1; }
33
+ virtual inline int MaxBottomBlobs() const { return 2; }
34
+ virtual inline int ExactNumTopBlobs() const { return 1; }
35
+
36
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
39
+ const vector<Blob<Dtype>*>& top);
40
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
41
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
42
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
43
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
44
+
45
+ private:
46
+ Blob<Dtype> bias_multiplier_;
47
+ int outer_dim_, bias_dim_, inner_dim_, dim_;
48
+ };
49
+
50
+
51
+
52
+ } // namespace caffe
53
+
54
+ #endif // CAFFE_BIAS_LAYER_HPP_
usr/local/include/caffe/layers/bnll_layer.hpp ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_BNLL_LAYER_HPP_
2
+ #define CAFFE_BNLL_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes @f$ y = x + \log(1 + \exp(-x)) @f$ if @f$ x > 0 @f$;
16
+ * @f$ y = \log(1 + \exp(x)) @f$ otherwise.
17
+ *
18
+ * @param bottom input Blob vector (length 1)
19
+ * -# @f$ (N \times C \times H \times W) @f$
20
+ * the inputs @f$ x @f$
21
+ * @param top output Blob vector (length 1)
22
+ * -# @f$ (N \times C \times H \times W) @f$
23
+ * the computed outputs @f$
24
+ * y = \left\{
25
+ * \begin{array}{ll}
26
+ * x + \log(1 + \exp(-x)) & \mbox{if } x > 0 \\
27
+ * \log(1 + \exp(x)) & \mbox{otherwise}
28
+ * \end{array} \right.
29
+ * @f$
30
+ */
31
+ template <typename Dtype>
32
+ class BNLLLayer : public NeuronLayer<Dtype> {
33
+ public:
34
+ explicit BNLLLayer(const LayerParameter& param)
35
+ : NeuronLayer<Dtype>(param) {}
36
+
37
+ virtual inline const char* type() const { return "BNLL"; }
38
+
39
+ protected:
40
+ /// @copydoc BNLLLayer
41
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
42
+ const vector<Blob<Dtype>*>& top);
43
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
44
+ const vector<Blob<Dtype>*>& top);
45
+
46
+ /**
47
+ * @brief Computes the error gradient w.r.t. the BNLL inputs.
48
+ *
49
+ * @param top output Blob vector (length 1), providing the error gradient with
50
+ * respect to the outputs
51
+ * -# @f$ (N \times C \times H \times W) @f$
52
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
53
+ * with respect to computed outputs @f$ y @f$
54
+ * @param propagate_down see Layer::Backward.
55
+ * @param bottom input Blob vector (length 2)
56
+ * -# @f$ (N \times C \times H \times W) @f$
57
+ * the inputs @f$ x @f$; Backward fills their diff with
58
+ * gradients @f$
59
+ * \frac{\partial E}{\partial x}
60
+ * @f$ if propagate_down[0]
61
+ */
62
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
63
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
64
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
65
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
66
+ };
67
+
68
+ } // namespace caffe
69
+
70
+ #endif // CAFFE_BNLL_LAYER_HPP_
usr/local/include/caffe/layers/clip_layer.hpp ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CLIP_LAYER_HPP_
2
+ #define CAFFE_CLIP_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Clip: @f$ y = \max(min, \min(max, x)) @f$.
16
+ */
17
+ template <typename Dtype>
18
+ class ClipLayer : public NeuronLayer<Dtype> {
19
+ public:
20
+ /**
21
+ * @param param provides ClipParameter clip_param,
22
+ * with ClipLayer options:
23
+ * - min
24
+ * - max
25
+ */
26
+ explicit ClipLayer(const LayerParameter& param)
27
+ : NeuronLayer<Dtype>(param) {}
28
+
29
+ virtual inline const char* type() const { return "Clip"; }
30
+
31
+ protected:
32
+ /**
33
+ * @param bottom input Blob vector (length 1)
34
+ * -# @f$ (N \times C \times H \times W) @f$
35
+ * the inputs @f$ x @f$
36
+ * @param top output Blob vector (length 1)
37
+ * -# @f$ (N \times C \times H \times W) @f$
38
+ * the computed outputs @f$
39
+ * y = \max(min, \min(max, x))
40
+ * @f$
41
+ */
42
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
43
+ const vector<Blob<Dtype>*>& top);
44
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
45
+ const vector<Blob<Dtype>*>& top);
46
+
47
+ /**
48
+ * @brief Computes the error gradient w.r.t. the clipped inputs.
49
+ *
50
+ * @param top output Blob vector (length 1), providing the error gradient with
51
+ * respect to the outputs
52
+ * -# @f$ (N \times C \times H \times W) @f$
53
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
54
+ * with respect to computed outputs @f$ y @f$
55
+ * @param propagate_down see Layer::Backward.
56
+ * @param bottom input Blob vector (length 1)
57
+ * -# @f$ (N \times C \times H \times W) @f$
58
+ * the inputs @f$ x @f$; Backward fills their diff with
59
+ * gradients @f$
60
+ * \frac{\partial E}{\partial x} = \left\{
61
+ * \begin{array}{lr}
62
+ * 0 & \mathrm{if} \; x < min \vee x > max \\
63
+ * \frac{\partial E}{\partial y} & \mathrm{if} \; x \ge min \wedge x \le max
64
+ * \end{array} \right.
65
+ * @f$
66
+ */
67
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
68
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
69
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
70
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
71
+ };
72
+
73
+ } // namespace caffe
74
+
75
+ #endif // CAFFE_CLIP_LAYER_HPP_
usr/local/include/caffe/layers/concat_layer.hpp ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CONCAT_LAYER_HPP_
2
+ #define CAFFE_CONCAT_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Takes at least two Blob%s and concatenates them along either the num
14
+ * or channel dimension, outputting the result.
15
+ */
16
+ template <typename Dtype>
17
+ class ConcatLayer : public Layer<Dtype> {
18
+ public:
19
+ explicit ConcatLayer(const LayerParameter& param)
20
+ : Layer<Dtype>(param) {}
21
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
22
+ const vector<Blob<Dtype>*>& top);
23
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
24
+ const vector<Blob<Dtype>*>& top);
25
+
26
+ virtual inline const char* type() const { return "Concat"; }
27
+ virtual inline int MinBottomBlobs() const { return 1; }
28
+ virtual inline int ExactNumTopBlobs() const { return 1; }
29
+
30
+ protected:
31
+ /**
32
+ * @param bottom input Blob vector (length 2+)
33
+ * -# @f$ (N \times C \times H \times W) @f$
34
+ * the inputs @f$ x_1 @f$
35
+ * -# @f$ (N \times C \times H \times W) @f$
36
+ * the inputs @f$ x_2 @f$
37
+ * -# ...
38
+ * - K @f$ (N \times C \times H \times W) @f$
39
+ * the inputs @f$ x_K @f$
40
+ * @param top output Blob vector (length 1)
41
+ * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or
42
+ * @f$ (N \times KC \times H \times W) @f$ if axis == 1:
43
+ * the concatenated output @f$
44
+ * y = [\begin{array}{cccc} x_1 & x_2 & ... & x_K \end{array}]
45
+ * @f$
46
+ */
47
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
48
+ const vector<Blob<Dtype>*>& top);
49
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
50
+ const vector<Blob<Dtype>*>& top);
51
+
52
+ /**
53
+ * @brief Computes the error gradient w.r.t. the concatenate inputs.
54
+ *
55
+ * @param top output Blob vector (length 1), providing the error gradient with
56
+ * respect to the outputs
57
+ * -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or
58
+ * @f$ (N \times KC \times H \times W) @f$ if axis == 1:
59
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
60
+ * with respect to concatenated outputs @f$ y @f$
61
+ * @param propagate_down see Layer::Backward.
62
+ * @param bottom input Blob vector (length K), into which the top gradient
63
+ * @f$ \frac{\partial E}{\partial y} @f$ is deconcatenated back to the
64
+ * inputs @f$
65
+ * \left[ \begin{array}{cccc}
66
+ * \frac{\partial E}{\partial x_1} &
67
+ * \frac{\partial E}{\partial x_2} &
68
+ * ... &
69
+ * \frac{\partial E}{\partial x_K}
70
+ * \end{array} \right] =
71
+ * \frac{\partial E}{\partial y}
72
+ * @f$
73
+ */
74
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
75
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
76
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
77
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
78
+
79
+ int count_;
80
+ int num_concats_;
81
+ int concat_input_size_;
82
+ int concat_axis_;
83
+ };
84
+
85
+ } // namespace caffe
86
+
87
+ #endif // CAFFE_CONCAT_LAYER_HPP_
usr/local/include/caffe/layers/contrastive_loss_layer.hpp ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_
2
+ #define CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/loss_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes the contrastive loss @f$
16
+ * E = \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 +
17
+ * \left(1-y\right) \max \left(margin-d, 0\right)^2
18
+ * @f$ where @f$
19
+ * d = \left| \left| a_n - b_n \right| \right|_2 @f$. This can be
20
+ * used to train siamese networks.
21
+ *
22
+ * @param bottom input Blob vector (length 3)
23
+ * -# @f$ (N \times C \times 1 \times 1) @f$
24
+ * the features @f$ a \in [-\infty, +\infty]@f$
25
+ * -# @f$ (N \times C \times 1 \times 1) @f$
26
+ * the features @f$ b \in [-\infty, +\infty]@f$
27
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
28
+ * the binary similarity @f$ s \in [0, 1]@f$
29
+ * @param top output Blob vector (length 1)
30
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
31
+ * the computed contrastive loss: @f$ E =
32
+ * \frac{1}{2N} \sum\limits_{n=1}^N \left(y\right) d^2 +
33
+ * \left(1-y\right) \max \left(margin-d, 0\right)^2
34
+ * @f$ where @f$
35
+ * d = \left| \left| a_n - b_n \right| \right|_2 @f$.
36
+ * This can be used to train siamese networks.
37
+ */
38
+ template <typename Dtype>
39
+ class ContrastiveLossLayer : public LossLayer<Dtype> {
40
+ public:
41
+ explicit ContrastiveLossLayer(const LayerParameter& param)
42
+ : LossLayer<Dtype>(param), diff_() {}
43
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
44
+ const vector<Blob<Dtype>*>& top);
45
+
46
+ virtual inline int ExactNumBottomBlobs() const { return 3; }
47
+ virtual inline const char* type() const { return "ContrastiveLoss"; }
48
+ /**
49
+ * Unlike most loss layers, in the ContrastiveLossLayer we can backpropagate
50
+ * to the first two inputs.
51
+ */
52
+ virtual inline bool AllowForceBackward(const int bottom_index) const {
53
+ return bottom_index != 2;
54
+ }
55
+
56
+ protected:
57
+ /// @copydoc ContrastiveLossLayer
58
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
59
+ const vector<Blob<Dtype>*>& top);
60
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
61
+ const vector<Blob<Dtype>*>& top);
62
+
63
+ /**
64
+ * @brief Computes the Contrastive error gradient w.r.t. the inputs.
65
+ *
66
+ * Computes the gradients with respect to the two input vectors (bottom[0] and
67
+ * bottom[1]), but not the similarity label (bottom[2]).
68
+ *
69
+ * @param top output Blob vector (length 1), providing the error gradient with
70
+ * respect to the outputs
71
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
72
+ * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$,
73
+ * as @f$ \lambda @f$ is the coefficient of this layer's output
74
+ * @f$\ell_i@f$ in the overall Net loss
75
+ * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence
76
+ * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$.
77
+ * (*Assuming that this top Blob is not used as a bottom (input) by any
78
+ * other layer of the Net.)
79
+ * @param propagate_down see Layer::Backward.
80
+ * @param bottom input Blob vector (length 2)
81
+ * -# @f$ (N \times C \times 1 \times 1) @f$
82
+ * the features @f$a@f$; Backward fills their diff with
83
+ * gradients if propagate_down[0]
84
+ * -# @f$ (N \times C \times 1 \times 1) @f$
85
+ * the features @f$b@f$; Backward fills their diff with gradients if
86
+ * propagate_down[1]
87
+ */
88
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
89
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
90
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
91
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
92
+
93
+ Blob<Dtype> diff_; // cached for backward pass
94
+ Blob<Dtype> dist_sq_; // cached for backward pass
95
+ Blob<Dtype> diff_sq_; // tmp storage for gpu forward pass
96
+ Blob<Dtype> summer_vec_; // tmp storage for gpu forward pass
97
+ };
98
+
99
+ } // namespace caffe
100
+
101
+ #endif // CAFFE_CONTRASTIVE_LOSS_LAYER_HPP_
usr/local/include/caffe/layers/conv_layer.hpp ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CONV_LAYER_HPP_
2
+ #define CAFFE_CONV_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/base_conv_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Convolves the input image with a bank of learned filters,
16
+ * and (optionally) adds biases.
17
+ *
18
+ * Caffe convolves by reduction to matrix multiplication. This achieves
19
+ * high-throughput and generality of input and filter dimensions but comes at
20
+ * the cost of memory for matrices. This makes use of efficiency in BLAS.
21
+ *
22
+ * The input is "im2col" transformed to a channel K' x H x W data matrix
23
+ * for multiplication with the N x K' x H x W filter matrix to yield a
24
+ * N' x H x W output matrix that is then "col2im" restored. K' is the
25
+ * input channel * kernel height * kernel width dimension of the unrolled
26
+ * inputs so that the im2col matrix has a column for each input region to
27
+ * be filtered. col2im restores the output spatial structure by rolling up
28
+ * the output channel N' columns of the output matrix.
29
+ */
30
+ template <typename Dtype>
31
+ class ConvolutionLayer : public BaseConvolutionLayer<Dtype> {
32
+ public:
33
+ /**
34
+ * @param param provides ConvolutionParameter convolution_param,
35
+ * with ConvolutionLayer options:
36
+ * - num_output. The number of filters.
37
+ * - kernel_size / kernel_h / kernel_w. The filter dimensions, given by
38
+ * kernel_size for square filters or kernel_h and kernel_w for rectangular
39
+ * filters.
40
+ * - stride / stride_h / stride_w (\b optional, default 1). The filter
41
+ * stride, given by stride_size for equal dimensions or stride_h and stride_w
42
+ * for different strides. By default the convolution is dense with stride 1.
43
+ * - pad / pad_h / pad_w (\b optional, default 0). The zero-padding for
44
+ * convolution, given by pad for equal dimensions or pad_h and pad_w for
45
+ * different padding. Input padding is computed implicitly instead of
46
+ * actually padding.
47
+ * - dilation (\b optional, default 1). The filter
48
+ * dilation, given by dilation_size for equal dimensions for different
49
+ * dilation. By default the convolution has dilation 1.
50
+ * - group (\b optional, default 1). The number of filter groups. Group
51
+ * convolution is a method for reducing parameterization by selectively
52
+ * connecting input and output channels. The input and output channel dimensions must be divisible
53
+ * by the number of groups. For group @f$ \geq 1 @f$, the
54
+ * convolutional filters' input and output channels are separated s.t. each
55
+ * group takes 1 / group of the input channels and makes 1 / group of the
56
+ * output channels. Concretely 4 input channels, 8 output channels, and
57
+ * 2 groups separate input channels 1-2 and output channels 1-4 into the
58
+ * first group and input channels 3-4 and output channels 5-8 into the second
59
+ * group.
60
+ * - bias_term (\b optional, default true). Whether to have a bias.
61
+ * - engine: convolution has CAFFE (matrix multiplication) and CUDNN (library
62
+ * kernels + stream parallelism) engines.
63
+ */
64
+ explicit ConvolutionLayer(const LayerParameter& param)
65
+ : BaseConvolutionLayer<Dtype>(param) {}
66
+
67
+ virtual inline const char* type() const { return "Convolution"; }
68
+
69
+ protected:
70
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
71
+ const vector<Blob<Dtype>*>& top);
72
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
73
+ const vector<Blob<Dtype>*>& top);
74
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
75
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
76
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
77
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
78
+ virtual inline bool reverse_dimensions() { return false; }
79
+ virtual void compute_output_shape();
80
+ };
81
+
82
+ } // namespace caffe
83
+
84
+ #endif // CAFFE_CONV_LAYER_HPP_
usr/local/include/caffe/layers/crop_layer.hpp ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CROP_LAYER_HPP_
2
+ #define CAFFE_CROP_LAYER_HPP_
3
+
4
+ #include <utility>
5
+ #include <vector>
6
+
7
+ #include "caffe/blob.hpp"
8
+ #include "caffe/layer.hpp"
9
+ #include "caffe/proto/caffe.pb.h"
10
+
11
+ namespace caffe {
12
+
13
+ /**
14
+ * @brief Takes a Blob and crop it, to the shape specified by the second input
15
+ * Blob, across all dimensions after the specified axis.
16
+ *
17
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
18
+ */
19
+
20
+ template <typename Dtype>
21
+ class CropLayer : public Layer<Dtype> {
22
+ public:
23
+ explicit CropLayer(const LayerParameter& param)
24
+ : Layer<Dtype>(param) {}
25
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
26
+ const vector<Blob<Dtype>*>& top);
27
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
28
+ const vector<Blob<Dtype>*>& top);
29
+
30
+ virtual inline const char* type() const { return "Crop"; }
31
+ virtual inline int ExactNumBottomBlobs() const { return 2; }
32
+ virtual inline int ExactNumTopBlobs() const { return 1; }
33
+
34
+ protected:
35
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
36
+ const vector<Blob<Dtype>*>& top);
37
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
38
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
39
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
40
+ const vector<Blob<Dtype>*>& top);
41
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
42
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
43
+
44
+ Blob<int> offsets;
45
+ Blob<int> src_strides_;
46
+ Blob<int> dest_strides_;
47
+
48
+ private:
49
+ // Recursive copy function.
50
+ void crop_copy(const vector<Blob<Dtype>*>& bottom,
51
+ const vector<Blob<Dtype>*>& top,
52
+ const int* offsets,
53
+ vector<int> indices,
54
+ int cur_dim,
55
+ const Dtype* src_data,
56
+ Dtype* dest_data,
57
+ bool is_forward);
58
+
59
+ // Recursive copy function: this is similar to crop_copy() but loops over all
60
+ // but the last two dimensions to allow for ND cropping while still relying on
61
+ // a CUDA kernel for the innermost two dimensions for performance reasons. An
62
+ // alterantive implementation could rely on the kernel more by passing
63
+ // offsets, but this is problematic because of its variable length.
64
+ // Since in the standard (N,C,W,H) case N,C are usually not cropped a speedup
65
+ // could be achieved by not looping the application of the copy_kernel around
66
+ // these dimensions.
67
+ void crop_copy_gpu(const vector<Blob<Dtype>*>& bottom,
68
+ const vector<Blob<Dtype>*>& top,
69
+ const vector<int>& offsets,
70
+ vector<int> indices,
71
+ int cur_dim,
72
+ const Dtype* src_data,
73
+ Dtype* dest_data,
74
+ bool is_forward);
75
+ };
76
+ } // namespace caffe
77
+
78
+ #endif // CAFFE_CROP_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_conv_layer.hpp ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_CONV_LAYER_HPP_
2
+ #define CAFFE_CUDNN_CONV_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/conv_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ #ifdef USE_CUDNN
15
+ /*
16
+ * @brief cuDNN implementation of ConvolutionLayer.
17
+ * Fallback to ConvolutionLayer for CPU mode.
18
+ *
19
+ * cuDNN accelerates convolution through forward kernels for filtering and bias
20
+ * plus backward kernels for the gradient w.r.t. the filters, biases, and
21
+ * inputs. Caffe + cuDNN further speeds up the computation through forward
22
+ * parallelism across groups and backward parallelism across gradients.
23
+ *
24
+ * The CUDNN engine does not have memory overhead for matrix buffers. For many
25
+ * input and filter regimes the CUDNN engine is faster than the CAFFE engine,
26
+ * but for fully-convolutional models and large inputs the CAFFE engine can be
27
+ * faster as long as it fits in memory.
28
+ */
29
+ template <typename Dtype>
30
+ class CuDNNConvolutionLayer : public ConvolutionLayer<Dtype> {
31
+ public:
32
+ explicit CuDNNConvolutionLayer(const LayerParameter& param)
33
+ : ConvolutionLayer<Dtype>(param), handles_setup_(false) {}
34
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
35
+ const vector<Blob<Dtype>*>& top);
36
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual ~CuDNNConvolutionLayer();
39
+
40
+ protected:
41
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
42
+ const vector<Blob<Dtype>*>& top);
43
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
44
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
45
+
46
+ bool handles_setup_;
47
+ cudnnHandle_t* handle_;
48
+ cudaStream_t* stream_;
49
+
50
+ // algorithms for forward and backwards convolutions
51
+ cudnnConvolutionFwdAlgo_t *fwd_algo_;
52
+ cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_;
53
+ cudnnConvolutionBwdDataAlgo_t *bwd_data_algo_;
54
+
55
+ vector<cudnnTensorDescriptor_t> bottom_descs_, top_descs_;
56
+ cudnnTensorDescriptor_t bias_desc_;
57
+ cudnnFilterDescriptor_t filter_desc_;
58
+ vector<cudnnConvolutionDescriptor_t> conv_descs_;
59
+ int bottom_offset_, top_offset_, bias_offset_;
60
+
61
+ size_t *workspace_fwd_sizes_;
62
+ size_t *workspace_bwd_data_sizes_;
63
+ size_t *workspace_bwd_filter_sizes_;
64
+ size_t workspaceSizeInBytes; // size of underlying storage
65
+ void *workspaceData; // underlying storage
66
+ void **workspace; // aliases into workspaceData
67
+ };
68
+ #endif
69
+
70
+ } // namespace caffe
71
+
72
+ #endif // CAFFE_CUDNN_CONV_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_deconv_layer.hpp ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_DECONV_LAYER_HPP_
2
+ #define CAFFE_CUDNN_DECONV_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/deconv_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ #ifdef USE_CUDNN
15
+ /*
16
+ * @brief cuDNN implementation of DeConvolutionLayer.
17
+ * Fallback to DeConvolutionLayer for CPU mode.
18
+ *
19
+ * cuDNN accelerates deconvolution through forward kernels for filtering and
20
+ * bias plus backward kernels for the gradient w.r.t. the filters, biases, and
21
+ * inputs. Caffe + cuDNN further speeds up the computation through forward
22
+ * parallelism across groups and backward parallelism across gradients.
23
+ */
24
+ template <typename Dtype>
25
+ class CuDNNDeconvolutionLayer : public DeconvolutionLayer<Dtype> {
26
+ public:
27
+ explicit CuDNNDeconvolutionLayer(const LayerParameter& param)
28
+ : DeconvolutionLayer<Dtype>(param), handles_setup_(false) {}
29
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
30
+ const vector<Blob<Dtype>*>& top);
31
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual ~CuDNNDeconvolutionLayer();
34
+
35
+ protected:
36
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
39
+ const vector<bool>& propagate_down,
40
+ const vector<Blob<Dtype>*>& bottom);
41
+
42
+ bool handles_setup_;
43
+ cudnnHandle_t* handle_;
44
+ cudaStream_t* stream_;
45
+
46
+ // algorithms for forward and backwards convolutions
47
+ cudnnConvolutionFwdAlgo_t *fwd_algo_;
48
+ cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_;
49
+ cudnnConvolutionBwdDataAlgo_t *bwd_data_algo_;
50
+
51
+ vector<cudnnTensorDescriptor_t> bottom_descs_, top_descs_;
52
+ cudnnTensorDescriptor_t bias_desc_;
53
+ cudnnFilterDescriptor_t filter_desc_;
54
+ vector<cudnnConvolutionDescriptor_t> conv_descs_;
55
+ int bottom_offset_, top_offset_, bias_offset_;
56
+
57
+ size_t *workspace_fwd_sizes_;
58
+ size_t *workspace_bwd_data_sizes_;
59
+ size_t *workspace_bwd_filter_sizes_;
60
+ size_t workspaceSizeInBytes; // size of underlying storage
61
+ void *workspaceData; // underlying storage
62
+ void **workspace; // aliases into workspaceData
63
+ };
64
+ #endif
65
+
66
+ } // namespace caffe
67
+
68
+ #endif // CAFFE_CUDNN_DECONV_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_lcn_layer.hpp ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_LCN_LAYER_HPP_
2
+ #define CAFFE_CUDNN_LCN_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/lrn_layer.hpp"
11
+ #include "caffe/layers/power_layer.hpp"
12
+
13
+ namespace caffe {
14
+
15
+ #ifdef USE_CUDNN
16
+ template <typename Dtype>
17
+ class CuDNNLCNLayer : public LRNLayer<Dtype> {
18
+ public:
19
+ explicit CuDNNLCNLayer(const LayerParameter& param)
20
+ : LRNLayer<Dtype>(param), handles_setup_(false), tempDataSize(0),
21
+ tempData1(NULL), tempData2(NULL) {}
22
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
23
+ const vector<Blob<Dtype>*>& top);
24
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual ~CuDNNLCNLayer();
27
+
28
+ protected:
29
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
30
+ const vector<Blob<Dtype>*>& top);
31
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
32
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
33
+
34
+ bool handles_setup_;
35
+ cudnnHandle_t handle_;
36
+ cudnnLRNDescriptor_t norm_desc_;
37
+ cudnnTensorDescriptor_t bottom_desc_, top_desc_;
38
+
39
+ int size_, pre_pad_;
40
+ Dtype alpha_, beta_, k_;
41
+
42
+ size_t tempDataSize;
43
+ void *tempData1, *tempData2;
44
+ };
45
+ #endif
46
+
47
+ } // namespace caffe
48
+
49
+ #endif // CAFFE_CUDNN_LCN_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_lrn_layer.hpp ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_LRN_LAYER_HPP_
2
+ #define CAFFE_CUDNN_LRN_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/lrn_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ #ifdef USE_CUDNN
15
+ template <typename Dtype>
16
+ class CuDNNLRNLayer : public LRNLayer<Dtype> {
17
+ public:
18
+ explicit CuDNNLRNLayer(const LayerParameter& param)
19
+ : LRNLayer<Dtype>(param), handles_setup_(false) {}
20
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
21
+ const vector<Blob<Dtype>*>& top);
22
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
23
+ const vector<Blob<Dtype>*>& top);
24
+ virtual ~CuDNNLRNLayer();
25
+
26
+ protected:
27
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
28
+ const vector<Blob<Dtype>*>& top);
29
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
30
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
31
+
32
+ bool handles_setup_;
33
+ cudnnHandle_t handle_;
34
+ cudnnLRNDescriptor_t norm_desc_;
35
+ cudnnTensorDescriptor_t bottom_desc_, top_desc_;
36
+
37
+ int size_;
38
+ Dtype alpha_, beta_, k_;
39
+ };
40
+ #endif
41
+
42
+ } // namespace caffe
43
+
44
+ #endif // CAFFE_CUDNN_LRN_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_pooling_layer.hpp ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_POOLING_LAYER_HPP_
2
+ #define CAFFE_CUDNN_POOLING_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/pooling_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ #ifdef USE_CUDNN
15
+ /*
16
+ * @brief cuDNN implementation of PoolingLayer.
17
+ * Fallback to PoolingLayer for CPU mode.
18
+ */
19
+ template <typename Dtype>
20
+ class CuDNNPoolingLayer : public PoolingLayer<Dtype> {
21
+ public:
22
+ explicit CuDNNPoolingLayer(const LayerParameter& param)
23
+ : PoolingLayer<Dtype>(param), handles_setup_(false) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual ~CuDNNPoolingLayer();
29
+ // Currently, cuDNN does not support the extra top blob.
30
+ virtual inline int MinTopBlobs() const { return -1; }
31
+ virtual inline int ExactNumTopBlobs() const { return 1; }
32
+
33
+ protected:
34
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
35
+ const vector<Blob<Dtype>*>& top);
36
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
37
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
38
+
39
+ bool handles_setup_;
40
+ cudnnHandle_t handle_;
41
+ cudnnTensorDescriptor_t bottom_desc_, top_desc_;
42
+ cudnnPoolingDescriptor_t pooling_desc_;
43
+ cudnnPoolingMode_t mode_;
44
+ };
45
+ #endif
46
+
47
+ } // namespace caffe
48
+
49
+ #endif // CAFFE_CUDNN_POOLING_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_relu_layer.hpp ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_RELU_LAYER_HPP_
2
+ #define CAFFE_CUDNN_RELU_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+ #include "caffe/layers/relu_layer.hpp"
12
+
13
+ namespace caffe {
14
+
15
+ #ifdef USE_CUDNN
16
+ /**
17
+ * @brief CuDNN acceleration of ReLULayer.
18
+ */
19
+ template <typename Dtype>
20
+ class CuDNNReLULayer : public ReLULayer<Dtype> {
21
+ public:
22
+ explicit CuDNNReLULayer(const LayerParameter& param)
23
+ : ReLULayer<Dtype>(param), handles_setup_(false) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual ~CuDNNReLULayer();
29
+
30
+ protected:
31
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
34
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
35
+
36
+ bool handles_setup_;
37
+ cudnnHandle_t handle_;
38
+ cudnnTensorDescriptor_t bottom_desc_;
39
+ cudnnTensorDescriptor_t top_desc_;
40
+ cudnnActivationDescriptor_t activ_desc_;
41
+ };
42
+ #endif
43
+
44
+ } // namespace caffe
45
+
46
+ #endif // CAFFE_CUDNN_RELU_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_sigmoid_layer.hpp ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_SIGMOID_LAYER_HPP_
2
+ #define CAFFE_CUDNN_SIGMOID_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+ #include "caffe/layers/sigmoid_layer.hpp"
12
+
13
+ namespace caffe {
14
+
15
+ #ifdef USE_CUDNN
16
+ /**
17
+ * @brief CuDNN acceleration of SigmoidLayer.
18
+ */
19
+ template <typename Dtype>
20
+ class CuDNNSigmoidLayer : public SigmoidLayer<Dtype> {
21
+ public:
22
+ explicit CuDNNSigmoidLayer(const LayerParameter& param)
23
+ : SigmoidLayer<Dtype>(param), handles_setup_(false) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual ~CuDNNSigmoidLayer();
29
+
30
+ protected:
31
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
34
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
35
+
36
+ bool handles_setup_;
37
+ cudnnHandle_t handle_;
38
+ cudnnTensorDescriptor_t bottom_desc_;
39
+ cudnnTensorDescriptor_t top_desc_;
40
+ cudnnActivationDescriptor_t activ_desc_;
41
+ };
42
+ #endif
43
+
44
+ } // namespace caffe
45
+
46
+ #endif // CAFFE_CUDNN_SIGMOID_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_softmax_layer.hpp ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_SOFTMAX_LAYER_HPP_
2
+ #define CAFFE_CUDNN_SOFTMAX_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/softmax_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ #ifdef USE_CUDNN
15
+ /**
16
+ * @brief cuDNN implementation of SoftmaxLayer.
17
+ * Fallback to SoftmaxLayer for CPU mode.
18
+ */
19
+ template <typename Dtype>
20
+ class CuDNNSoftmaxLayer : public SoftmaxLayer<Dtype> {
21
+ public:
22
+ explicit CuDNNSoftmaxLayer(const LayerParameter& param)
23
+ : SoftmaxLayer<Dtype>(param), handles_setup_(false) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual ~CuDNNSoftmaxLayer();
29
+
30
+ protected:
31
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
34
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
35
+
36
+ bool handles_setup_;
37
+ cudnnHandle_t handle_;
38
+ cudnnTensorDescriptor_t bottom_desc_;
39
+ cudnnTensorDescriptor_t top_desc_;
40
+ };
41
+ #endif
42
+
43
+ } // namespace caffe
44
+
45
+ #endif // CAFFE_CUDNN_SOFTMAX_LAYER_HPP_
usr/local/include/caffe/layers/cudnn_tanh_layer.hpp ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_CUDNN_TANH_LAYER_HPP_
2
+ #define CAFFE_CUDNN_TANH_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+ #include "caffe/layers/tanh_layer.hpp"
12
+
13
+ namespace caffe {
14
+
15
+ #ifdef USE_CUDNN
16
+ /**
17
+ * @brief CuDNN acceleration of TanHLayer.
18
+ */
19
+ template <typename Dtype>
20
+ class CuDNNTanHLayer : public TanHLayer<Dtype> {
21
+ public:
22
+ explicit CuDNNTanHLayer(const LayerParameter& param)
23
+ : TanHLayer<Dtype>(param), handles_setup_(false) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+ virtual ~CuDNNTanHLayer();
29
+
30
+ protected:
31
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top);
33
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
34
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
35
+
36
+ bool handles_setup_;
37
+ cudnnHandle_t handle_;
38
+ cudnnTensorDescriptor_t bottom_desc_;
39
+ cudnnTensorDescriptor_t top_desc_;
40
+ cudnnActivationDescriptor_t activ_desc_;
41
+ };
42
+ #endif
43
+
44
+ } // namespace caffe
45
+
46
+ #endif // CAFFE_CUDNN_TANH_LAYER_HPP_
usr/local/include/caffe/layers/data_layer.hpp ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_DATA_LAYER_HPP_
2
+ #define CAFFE_DATA_LAYER_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/layers/base_data_layer.hpp"
11
+ #include "caffe/proto/caffe.pb.h"
12
+ #include "caffe/util/db.hpp"
13
+
14
+ namespace caffe {
15
+
16
+ template <typename Dtype>
17
+ class DataLayer : public BasePrefetchingDataLayer<Dtype> {
18
+ public:
19
+ explicit DataLayer(const LayerParameter& param);
20
+ virtual ~DataLayer();
21
+ virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
22
+ const vector<Blob<Dtype>*>& top);
23
+ virtual inline const char* type() const { return "Data"; }
24
+ virtual inline int ExactNumBottomBlobs() const { return 0; }
25
+ virtual inline int MinTopBlobs() const { return 1; }
26
+ virtual inline int MaxTopBlobs() const { return 2; }
27
+
28
+ protected:
29
+ void Next();
30
+ bool Skip();
31
+ virtual void load_batch(Batch<Dtype>* batch);
32
+
33
+ shared_ptr<db::DB> db_;
34
+ shared_ptr<db::Cursor> cursor_;
35
+ uint64_t offset_;
36
+ };
37
+
38
+ } // namespace caffe
39
+
40
+ #endif // CAFFE_DATA_LAYER_HPP_
usr/local/include/caffe/layers/deconv_layer.hpp ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_DECONV_LAYER_HPP_
2
+ #define CAFFE_DECONV_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/base_conv_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Convolve the input with a bank of learned filters, and (optionally)
16
+ * add biases, treating filters and convolution parameters in the
17
+ * opposite sense as ConvolutionLayer.
18
+ *
19
+ * ConvolutionLayer computes each output value by dotting an input window with
20
+ * a filter; DeconvolutionLayer multiplies each input value by a filter
21
+ * elementwise, and sums over the resulting output windows. In other words,
22
+ * DeconvolutionLayer is ConvolutionLayer with the forward and backward passes
23
+ * reversed. DeconvolutionLayer reuses ConvolutionParameter for its
24
+ * parameters, but they take the opposite sense as in ConvolutionLayer (so
25
+ * padding is removed from the output rather than added to the input, and
26
+ * stride results in upsampling rather than downsampling).
27
+ */
28
+ template <typename Dtype>
29
+ class DeconvolutionLayer : public BaseConvolutionLayer<Dtype> {
30
+ public:
31
+ explicit DeconvolutionLayer(const LayerParameter& param)
32
+ : BaseConvolutionLayer<Dtype>(param) {}
33
+
34
+ virtual inline const char* type() const { return "Deconvolution"; }
35
+
36
+ protected:
37
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
38
+ const vector<Blob<Dtype>*>& top);
39
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
40
+ const vector<Blob<Dtype>*>& top);
41
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
42
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
43
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
44
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
45
+ virtual inline bool reverse_dimensions() { return true; }
46
+ virtual void compute_output_shape();
47
+ };
48
+
49
+ } // namespace caffe
50
+
51
+ #endif // CAFFE_DECONV_LAYER_HPP_
usr/local/include/caffe/layers/dropout_layer.hpp ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_DROPOUT_LAYER_HPP_
2
+ #define CAFFE_DROPOUT_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief During training only, sets a random portion of @f$x@f$ to 0, adjusting
16
+ * the rest of the vector magnitude accordingly.
17
+ *
18
+ * @param bottom input Blob vector (length 1)
19
+ * -# @f$ (N \times C \times H \times W) @f$
20
+ * the inputs @f$ x @f$
21
+ * @param top output Blob vector (length 1)
22
+ * -# @f$ (N \times C \times H \times W) @f$
23
+ * the computed outputs @f$ y = |x| @f$
24
+ */
25
+ template <typename Dtype>
26
+ class DropoutLayer : public NeuronLayer<Dtype> {
27
+ public:
28
+ /**
29
+ * @param param provides DropoutParameter dropout_param,
30
+ * with DropoutLayer options:
31
+ * - dropout_ratio (\b optional, default 0.5).
32
+ * Sets the probability @f$ p @f$ that any given unit is dropped.
33
+ */
34
+ explicit DropoutLayer(const LayerParameter& param)
35
+ : NeuronLayer<Dtype>(param) {}
36
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
39
+ const vector<Blob<Dtype>*>& top);
40
+
41
+ virtual inline const char* type() const { return "Dropout"; }
42
+
43
+ protected:
44
+ /**
45
+ * @param bottom input Blob vector (length 1)
46
+ * -# @f$ (N \times C \times H \times W) @f$
47
+ * the inputs @f$ x @f$
48
+ * @param top output Blob vector (length 1)
49
+ * -# @f$ (N \times C \times H \times W) @f$
50
+ * the computed outputs. At training time, we have @f$
51
+ * y_{\mbox{train}} = \left\{
52
+ * \begin{array}{ll}
53
+ * \frac{x}{1 - p} & \mbox{if } u > p \\
54
+ * 0 & \mbox{otherwise}
55
+ * \end{array} \right.
56
+ * @f$, where @f$ u \sim U(0, 1)@f$ is generated independently for each
57
+ * input at each iteration. At test time, we simply have
58
+ * @f$ y_{\mbox{test}} = \mathbb{E}[y_{\mbox{train}}] = x @f$.
59
+ */
60
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
61
+ const vector<Blob<Dtype>*>& top);
62
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
63
+ const vector<Blob<Dtype>*>& top);
64
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
65
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
66
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
67
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
68
+
69
+ /// when divided by UINT_MAX, the randomly generated values @f$u\sim U(0,1)@f$
70
+ Blob<unsigned int> rand_vec_;
71
+ /// the probability @f$ p @f$ of dropping any input
72
+ Dtype threshold_;
73
+ /// the scale for undropped inputs at train time @f$ 1 / (1 - p) @f$
74
+ Dtype scale_;
75
+ unsigned int uint_thres_;
76
+ };
77
+
78
+ } // namespace caffe
79
+
80
+ #endif // CAFFE_DROPOUT_LAYER_HPP_
usr/local/include/caffe/layers/dummy_data_layer.hpp ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_DUMMY_DATA_LAYER_HPP_
2
+ #define CAFFE_DUMMY_DATA_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/filler.hpp"
8
+ #include "caffe/layer.hpp"
9
+ #include "caffe/proto/caffe.pb.h"
10
+
11
+ namespace caffe {
12
+
13
+ /**
14
+ * @brief Provides data to the Net generated by a Filler.
15
+ *
16
+ * TODO(dox): thorough documentation for Forward and proto params.
17
+ */
18
+ template <typename Dtype>
19
+ class DummyDataLayer : public Layer<Dtype> {
20
+ public:
21
+ explicit DummyDataLayer(const LayerParameter& param)
22
+ : Layer<Dtype>(param) {}
23
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
24
+ const vector<Blob<Dtype>*>& top);
25
+ // Data layers have no bottoms, so reshaping is trivial.
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top) {}
28
+
29
+ virtual inline const char* type() const { return "DummyData"; }
30
+ virtual inline int ExactNumBottomBlobs() const { return 0; }
31
+ virtual inline int MinTopBlobs() const { return 1; }
32
+
33
+ protected:
34
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
35
+ const vector<Blob<Dtype>*>& top);
36
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
37
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
38
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
39
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
40
+
41
+ vector<shared_ptr<Filler<Dtype> > > fillers_;
42
+ vector<bool> refill_;
43
+ };
44
+
45
+ } // namespace caffe
46
+
47
+ #endif // CAFFE_DUMMY_DATA_LAYER_HPP_
usr/local/include/caffe/layers/eltwise_layer.hpp ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_ELTWISE_LAYER_HPP_
2
+ #define CAFFE_ELTWISE_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Compute elementwise operations, such as product and sum,
14
+ * along multiple input Blobs.
15
+ *
16
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
17
+ */
18
+ template <typename Dtype>
19
+ class EltwiseLayer : public Layer<Dtype> {
20
+ public:
21
+ explicit EltwiseLayer(const LayerParameter& param)
22
+ : Layer<Dtype>(param) {}
23
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
24
+ const vector<Blob<Dtype>*>& top);
25
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
26
+ const vector<Blob<Dtype>*>& top);
27
+
28
+ virtual inline const char* type() const { return "Eltwise"; }
29
+ virtual inline int MinBottomBlobs() const { return 2; }
30
+ virtual inline int ExactNumTopBlobs() const { return 1; }
31
+
32
+ protected:
33
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
34
+ const vector<Blob<Dtype>*>& top);
35
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
36
+ const vector<Blob<Dtype>*>& top);
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
+ EltwiseParameter_EltwiseOp op_;
43
+ vector<Dtype> coeffs_;
44
+ Blob<int> max_idx_;
45
+
46
+ bool stable_prod_grad_;
47
+ };
48
+
49
+ } // namespace caffe
50
+
51
+ #endif // CAFFE_ELTWISE_LAYER_HPP_
usr/local/include/caffe/layers/elu_layer.hpp ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_ELU_LAYER_HPP_
2
+ #define CAFFE_ELU_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Exponential Linear Unit non-linearity @f$
16
+ * y = \left\{
17
+ * \begin{array}{lr}
18
+ * x & \mathrm{if} \; x > 0 \\
19
+ * \alpha (\exp(x)-1) & \mathrm{if} \; x \le 0
20
+ * \end{array} \right.
21
+ * @f$.
22
+ */
23
+ template <typename Dtype>
24
+ class ELULayer : public NeuronLayer<Dtype> {
25
+ public:
26
+ /**
27
+ * @param param provides ELUParameter elu_param,
28
+ * with ELULayer options:
29
+ * - alpha (\b optional, default 1).
30
+ * the value @f$ \alpha @f$ by which controls saturation for negative inputs.
31
+ */
32
+ explicit ELULayer(const LayerParameter& param)
33
+ : NeuronLayer<Dtype>(param) {}
34
+
35
+ virtual inline const char* type() const { return "ELU"; }
36
+
37
+ protected:
38
+ /**
39
+ * @param bottom input Blob vector (length 1)
40
+ * -# @f$ (N \times C \times H \times W) @f$
41
+ * the inputs @f$ x @f$
42
+ * @param top output Blob vector (length 1)
43
+ * -# @f$ (N \times C \times H \times W) @f$
44
+ * the computed outputs @f$
45
+ * y = \left\{
46
+ * \begin{array}{lr}
47
+ * x & \mathrm{if} \; x > 0 \\
48
+ * \alpha (\exp(x)-1) & \mathrm{if} \; x \le 0
49
+ * \end{array} \right.
50
+ * @f$.
51
+ */
52
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
53
+ const vector<Blob<Dtype>*>& top);
54
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
55
+ const vector<Blob<Dtype>*>& top);
56
+
57
+ /**
58
+ * @brief Computes the error gradient w.r.t. the ELU inputs.
59
+ *
60
+ * @param top output Blob vector (length 1), providing the error gradient with
61
+ * respect to the outputs
62
+ * -# @f$ (N \times C \times H \times W) @f$
63
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
64
+ * with respect to computed outputs @f$ y @f$
65
+ * @param propagate_down see Layer::Backward.
66
+ * @param bottom input Blob vector (length 1)
67
+ * -# @f$ (N \times C \times H \times W) @f$
68
+ * the inputs @f$ x @f$; Backward fills their diff with
69
+ * gradients @f$
70
+ * \frac{\partial E}{\partial x} = \left\{
71
+ * \begin{array}{lr}
72
+ * 1 & \mathrm{if} \; x > 0 \\
73
+ * y + \alpha & \mathrm{if} \; x \le 0
74
+ * \end{array} \right.
75
+ * @f$ if propagate_down[0].
76
+ */
77
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
78
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
79
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
80
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
81
+ };
82
+
83
+
84
+ } // namespace caffe
85
+
86
+ #endif // CAFFE_ELU_LAYER_HPP_
usr/local/include/caffe/layers/embed_layer.hpp ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_EMBED_LAYER_HPP_
2
+ #define CAFFE_EMBED_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief A layer for learning "embeddings" of one-hot vector input.
14
+ * Equivalent to an InnerProductLayer with one-hot vectors as input, but
15
+ * for efficiency the input is the "hot" index of each column itself.
16
+ *
17
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
18
+ */
19
+ template <typename Dtype>
20
+ class EmbedLayer : public Layer<Dtype> {
21
+ public:
22
+ explicit EmbedLayer(const LayerParameter& param)
23
+ : Layer<Dtype>(param) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+
29
+ virtual inline const char* type() const { return "Embed"; }
30
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
31
+ virtual inline int ExactNumTopBlobs() const { return 1; }
32
+
33
+ protected:
34
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
35
+ const vector<Blob<Dtype>*>& top);
36
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
39
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
40
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
41
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
42
+
43
+ int M_;
44
+ int K_;
45
+ int N_;
46
+ bool bias_term_;
47
+ Blob<Dtype> bias_multiplier_;
48
+ };
49
+
50
+ } // namespace caffe
51
+
52
+ #endif // CAFFE_EMBED_LAYER_HPP_
usr/local/include/caffe/layers/euclidean_loss_layer.hpp ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_
2
+ #define CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/loss_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes the Euclidean (L2) loss @f$
16
+ * E = \frac{1}{2N} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n
17
+ * \right| \right|_2^2 @f$ for real-valued regression tasks.
18
+ *
19
+ * @param bottom input Blob vector (length 2)
20
+ * -# @f$ (N \times C \times H \times W) @f$
21
+ * the predictions @f$ \hat{y} \in [-\infty, +\infty]@f$
22
+ * -# @f$ (N \times C \times H \times W) @f$
23
+ * the targets @f$ y \in [-\infty, +\infty]@f$
24
+ * @param top output Blob vector (length 1)
25
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
26
+ * the computed Euclidean loss: @f$ E =
27
+ * \frac{1}{2n} \sum\limits_{n=1}^N \left| \left| \hat{y}_n - y_n
28
+ * \right| \right|_2^2 @f$
29
+ *
30
+ * This can be used for least-squares regression tasks. An InnerProductLayer
31
+ * input to a EuclideanLossLayer exactly formulates a linear least squares
32
+ * regression problem. With non-zero weight decay the problem becomes one of
33
+ * ridge regression -- see src/caffe/test/test_gradient_based_solver.cpp for a concrete
34
+ * example wherein we check that the gradients computed for a Net with exactly
35
+ * this structure match hand-computed gradient formulas for ridge regression.
36
+ *
37
+ * (Note: Caffe, and SGD in general, is certainly \b not the best way to solve
38
+ * linear least squares problems! We use it only as an instructive example.)
39
+ */
40
+ template <typename Dtype>
41
+ class EuclideanLossLayer : public LossLayer<Dtype> {
42
+ public:
43
+ explicit EuclideanLossLayer(const LayerParameter& param)
44
+ : LossLayer<Dtype>(param), diff_() {}
45
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
46
+ const vector<Blob<Dtype>*>& top);
47
+
48
+ virtual inline const char* type() const { return "EuclideanLoss"; }
49
+ /**
50
+ * Unlike most loss layers, in the EuclideanLossLayer we can backpropagate
51
+ * to both inputs -- override to return true and always allow force_backward.
52
+ */
53
+ virtual inline bool AllowForceBackward(const int bottom_index) const {
54
+ return true;
55
+ }
56
+
57
+ protected:
58
+ /// @copydoc EuclideanLossLayer
59
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
60
+ const vector<Blob<Dtype>*>& top);
61
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
62
+ const vector<Blob<Dtype>*>& top);
63
+
64
+ /**
65
+ * @brief Computes the Euclidean error gradient w.r.t. the inputs.
66
+ *
67
+ * Unlike other children of LossLayer, EuclideanLossLayer \b can compute
68
+ * gradients with respect to the label inputs bottom[1] (but still only will
69
+ * if propagate_down[1] is set, due to being produced by learnable parameters
70
+ * or if force_backward is set). In fact, this layer is "commutative" -- the
71
+ * result is the same regardless of the order of the two bottoms.
72
+ *
73
+ * @param top output Blob vector (length 1), providing the error gradient with
74
+ * respect to the outputs
75
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
76
+ * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$,
77
+ * as @f$ \lambda @f$ is the coefficient of this layer's output
78
+ * @f$\ell_i@f$ in the overall Net loss
79
+ * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence
80
+ * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$.
81
+ * (*Assuming that this top Blob is not used as a bottom (input) by any
82
+ * other layer of the Net.)
83
+ * @param propagate_down see Layer::Backward.
84
+ * @param bottom input Blob vector (length 2)
85
+ * -# @f$ (N \times C \times H \times W) @f$
86
+ * the predictions @f$\hat{y}@f$; Backward fills their diff with
87
+ * gradients @f$
88
+ * \frac{\partial E}{\partial \hat{y}} =
89
+ * \frac{1}{n} \sum\limits_{n=1}^N (\hat{y}_n - y_n)
90
+ * @f$ if propagate_down[0]
91
+ * -# @f$ (N \times C \times H \times W) @f$
92
+ * the targets @f$y@f$; Backward fills their diff with gradients
93
+ * @f$ \frac{\partial E}{\partial y} =
94
+ * \frac{1}{n} \sum\limits_{n=1}^N (y_n - \hat{y}_n)
95
+ * @f$ if propagate_down[1]
96
+ */
97
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
98
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
99
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
100
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
101
+
102
+ Blob<Dtype> diff_;
103
+ };
104
+
105
+ } // namespace caffe
106
+
107
+ #endif // CAFFE_EUCLIDEAN_LOSS_LAYER_HPP_
usr/local/include/caffe/layers/exp_layer.hpp ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_EXP_LAYER_HPP_
2
+ #define CAFFE_EXP_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/neuron_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes @f$ y = \gamma ^ {\alpha x + \beta} @f$,
16
+ * as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$,
17
+ * and base @f$ \gamma @f$.
18
+ */
19
+ template <typename Dtype>
20
+ class ExpLayer : public NeuronLayer<Dtype> {
21
+ public:
22
+ /**
23
+ * @param param provides ExpParameter exp_param,
24
+ * with ExpLayer options:
25
+ * - scale (\b optional, default 1) the scale @f$ \alpha @f$
26
+ * - shift (\b optional, default 0) the shift @f$ \beta @f$
27
+ * - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$)
28
+ * the base @f$ \gamma @f$
29
+ */
30
+ explicit ExpLayer(const LayerParameter& param)
31
+ : NeuronLayer<Dtype>(param) {}
32
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
33
+ const vector<Blob<Dtype>*>& top);
34
+
35
+ virtual inline const char* type() const { return "Exp"; }
36
+
37
+ protected:
38
+ /**
39
+ * @param bottom input Blob vector (length 1)
40
+ * -# @f$ (N \times C \times H \times W) @f$
41
+ * the inputs @f$ x @f$
42
+ * @param top output Blob vector (length 1)
43
+ * -# @f$ (N \times C \times H \times W) @f$
44
+ * the computed outputs @f$
45
+ * y = \gamma ^ {\alpha x + \beta}
46
+ * @f$
47
+ */
48
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
49
+ const vector<Blob<Dtype>*>& top);
50
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
51
+ const vector<Blob<Dtype>*>& top);
52
+
53
+ /**
54
+ * @brief Computes the error gradient w.r.t. the exp inputs.
55
+ *
56
+ * @param top output Blob vector (length 1), providing the error gradient with
57
+ * respect to the outputs
58
+ * -# @f$ (N \times C \times H \times W) @f$
59
+ * containing error gradients @f$ \frac{\partial E}{\partial y} @f$
60
+ * with respect to computed outputs @f$ y @f$
61
+ * @param propagate_down see Layer::Backward.
62
+ * @param bottom input Blob vector (length 1)
63
+ * -# @f$ (N \times C \times H \times W) @f$
64
+ * the inputs @f$ x @f$; Backward fills their diff with
65
+ * gradients @f$
66
+ * \frac{\partial E}{\partial x} =
67
+ * \frac{\partial E}{\partial y} y \alpha \log_e(gamma)
68
+ * @f$ if propagate_down[0]
69
+ */
70
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
71
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
72
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
73
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
74
+
75
+ Dtype inner_scale_, outer_scale_;
76
+ };
77
+
78
+ } // namespace caffe
79
+
80
+ #endif // CAFFE_EXP_LAYER_HPP_
usr/local/include/caffe/layers/filter_layer.hpp ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_FILTER_LAYER_HPP_
2
+ #define CAFFE_FILTER_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Takes two+ Blobs, interprets last Blob as a selector and
14
+ * filter remaining Blobs accordingly with selector data (0 means that
15
+ * the corresponding item has to be filtered, non-zero means that corresponding
16
+ * item needs to stay).
17
+ */
18
+ template <typename Dtype>
19
+ class FilterLayer : public Layer<Dtype> {
20
+ public:
21
+ explicit FilterLayer(const LayerParameter& param)
22
+ : Layer<Dtype>(param) {}
23
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
24
+ const vector<Blob<Dtype>*>& top);
25
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
26
+ const vector<Blob<Dtype>*>& top);
27
+
28
+ virtual inline const char* type() const { return "Filter"; }
29
+ virtual inline int MinBottomBlobs() const { return 2; }
30
+ virtual inline int MinTopBlobs() const { return 1; }
31
+
32
+ protected:
33
+ /**
34
+ * @param bottom input Blob vector (length 2+)
35
+ * -# @f$ (N \times C \times H \times W) @f$
36
+ * the inputs to be filtered @f$ x_1 @f$
37
+ * -# ...
38
+ * -# @f$ (N \times C \times H \times W) @f$
39
+ * the inputs to be filtered @f$ x_K @f$
40
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
41
+ * the selector blob
42
+ * @param top output Blob vector (length 1+)
43
+ * -# @f$ (S \times C \times H \times W) @f$ ()
44
+ * the filtered output @f$ x_1 @f$
45
+ * where S is the number of items
46
+ * that haven't been filtered
47
+ * @f$ (S \times C \times H \times W) @f$
48
+ * the filtered output @f$ x_K @f$
49
+ * where S is the number of items
50
+ * that haven't been filtered
51
+ */
52
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
53
+ const vector<Blob<Dtype>*>& top);
54
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
55
+ const vector<Blob<Dtype>*>& top);
56
+
57
+ /**
58
+ * @brief Computes the error gradient w.r.t. the forwarded inputs.
59
+ *
60
+ * @param top output Blob vector (length 1+), providing the error gradient with
61
+ * respect to the outputs
62
+ * @param propagate_down see Layer::Backward.
63
+ * @param bottom input Blob vector (length 2+), into which the top error
64
+ * gradient is copied
65
+ */
66
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
67
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
68
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
69
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
70
+
71
+ bool first_reshape_;
72
+ vector<int> indices_to_forward_;
73
+ };
74
+
75
+ } // namespace caffe
76
+
77
+ #endif // CAFFE_FILTER_LAYER_HPP_
usr/local/include/caffe/layers/flatten_layer.hpp ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_FLATTEN_LAYER_HPP_
2
+ #define CAFFE_FLATTEN_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Reshapes the input Blob into flat vectors.
14
+ *
15
+ * Note: because this layer does not change the input values -- merely the
16
+ * dimensions -- it can simply copy the input. The copy happens "virtually"
17
+ * (thus taking effectively 0 real time) by setting, in Forward, the data
18
+ * pointer of the top Blob to that of the bottom Blob (see Blob::ShareData),
19
+ * and in Backward, the diff pointer of the bottom Blob to that of the top Blob
20
+ * (see Blob::ShareDiff).
21
+ */
22
+ template <typename Dtype>
23
+ class FlattenLayer : public Layer<Dtype> {
24
+ public:
25
+ explicit FlattenLayer(const LayerParameter& param)
26
+ : Layer<Dtype>(param) {}
27
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
28
+ const vector<Blob<Dtype>*>& top);
29
+
30
+ virtual inline const char* type() const { return "Flatten"; }
31
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
32
+ virtual inline int ExactNumTopBlobs() const { return 1; }
33
+
34
+ protected:
35
+ /**
36
+ * @param bottom input Blob vector (length 2+)
37
+ * -# @f$ (N \times C \times H \times W) @f$
38
+ * the inputs
39
+ * @param top output Blob vector (length 1)
40
+ * -# @f$ (N \times CHW \times 1 \times 1) @f$
41
+ * the outputs -- i.e., the (virtually) copied, flattened inputs
42
+ */
43
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
44
+ const vector<Blob<Dtype>*>& top);
45
+
46
+ /**
47
+ * @brief Computes the error gradient w.r.t. the concatenate inputs.
48
+ *
49
+ * @param top output Blob vector (length 1), providing the error gradient with
50
+ * respect to the outputs
51
+ * @param propagate_down see Layer::Backward.
52
+ * @param bottom input Blob vector (length K), into which the top error
53
+ * gradient is (virtually) copied
54
+ */
55
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
56
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
57
+ };
58
+
59
+ } // namespace caffe
60
+
61
+ #endif // CAFFE_FLATTEN_LAYER_HPP_
usr/local/include/caffe/layers/hdf5_data_layer.hpp ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_HDF5_DATA_LAYER_HPP_
2
+ #define CAFFE_HDF5_DATA_LAYER_HPP_
3
+
4
+ #include "hdf5.h"
5
+
6
+ #include <string>
7
+ #include <vector>
8
+
9
+ #include "caffe/blob.hpp"
10
+ #include "caffe/layer.hpp"
11
+ #include "caffe/proto/caffe.pb.h"
12
+
13
+ #include "caffe/layers/base_data_layer.hpp"
14
+
15
+ namespace caffe {
16
+
17
+ /**
18
+ * @brief Provides data to the Net from HDF5 files.
19
+ *
20
+ * TODO(dox): thorough documentation for Forward and proto params.
21
+ */
22
+ template <typename Dtype>
23
+ class HDF5DataLayer : public Layer<Dtype> {
24
+ public:
25
+ explicit HDF5DataLayer(const LayerParameter& param)
26
+ : Layer<Dtype>(param), offset_() {}
27
+ virtual ~HDF5DataLayer();
28
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
29
+ const vector<Blob<Dtype>*>& top);
30
+ // Data layers have no bottoms, so reshaping is trivial.
31
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
32
+ const vector<Blob<Dtype>*>& top) {}
33
+
34
+ virtual inline const char* type() const { return "HDF5Data"; }
35
+ virtual inline int ExactNumBottomBlobs() const { return 0; }
36
+ virtual inline int MinTopBlobs() const { return 1; }
37
+
38
+ protected:
39
+ void Next();
40
+ bool Skip();
41
+
42
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
43
+ const vector<Blob<Dtype>*>& top);
44
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
45
+ const vector<Blob<Dtype>*>& top);
46
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
47
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
48
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
49
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
50
+ virtual void LoadHDF5FileData(const char* filename);
51
+
52
+ std::vector<std::string> hdf_filenames_;
53
+ unsigned int num_files_;
54
+ unsigned int current_file_;
55
+ hsize_t current_row_;
56
+ std::vector<shared_ptr<Blob<Dtype> > > hdf_blobs_;
57
+ std::vector<unsigned int> data_permutation_;
58
+ std::vector<unsigned int> file_permutation_;
59
+ uint64_t offset_;
60
+ };
61
+
62
+ } // namespace caffe
63
+
64
+ #endif // CAFFE_HDF5_DATA_LAYER_HPP_
usr/local/include/caffe/layers/hdf5_output_layer.hpp ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_HDF5_OUTPUT_LAYER_HPP_
2
+ #define CAFFE_HDF5_OUTPUT_LAYER_HPP_
3
+
4
+ #include "hdf5.h"
5
+
6
+ #include <string>
7
+ #include <vector>
8
+
9
+ #include "caffe/blob.hpp"
10
+ #include "caffe/layer.hpp"
11
+ #include "caffe/proto/caffe.pb.h"
12
+
13
+ namespace caffe {
14
+
15
+ #define HDF5_DATA_DATASET_NAME "data"
16
+ #define HDF5_DATA_LABEL_NAME "label"
17
+
18
+ /**
19
+ * @brief Write blobs to disk as HDF5 files.
20
+ *
21
+ * TODO(dox): thorough documentation for Forward and proto params.
22
+ */
23
+ template <typename Dtype>
24
+ class HDF5OutputLayer : public Layer<Dtype> {
25
+ public:
26
+ explicit HDF5OutputLayer(const LayerParameter& param)
27
+ : Layer<Dtype>(param), file_opened_(false) {}
28
+ virtual ~HDF5OutputLayer();
29
+ virtual void LayerSetUp(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 inline const char* type() const { return "HDF5Output"; }
36
+ // TODO: no limit on the number of blobs
37
+ virtual inline int ExactNumBottomBlobs() const { return 2; }
38
+ virtual inline int ExactNumTopBlobs() const { return 0; }
39
+
40
+ inline std::string file_name() const { return file_name_; }
41
+
42
+ protected:
43
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
44
+ const vector<Blob<Dtype>*>& top);
45
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
46
+ const vector<Blob<Dtype>*>& top);
47
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
48
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
49
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
50
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
51
+ virtual void SaveBlobs();
52
+
53
+ bool file_opened_;
54
+ std::string file_name_;
55
+ hid_t file_id_;
56
+ Blob<Dtype> data_blob_;
57
+ Blob<Dtype> label_blob_;
58
+ };
59
+
60
+ } // namespace caffe
61
+
62
+ #endif // CAFFE_HDF5_OUTPUT_LAYER_HPP_
usr/local/include/caffe/layers/hinge_loss_layer.hpp ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_HINGE_LOSS_LAYER_HPP_
2
+ #define CAFFE_HINGE_LOSS_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/loss_layer.hpp"
11
+
12
+ namespace caffe {
13
+
14
+ /**
15
+ * @brief Computes the hinge loss for a one-of-many classification task.
16
+ *
17
+ * @param bottom input Blob vector (length 2)
18
+ * -# @f$ (N \times C \times H \times W) @f$
19
+ * the predictions @f$ t @f$, a Blob with values in
20
+ * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of
21
+ * the @f$ K = CHW @f$ classes. In an SVM, @f$ t @f$ is the result of
22
+ * taking the inner product @f$ X^T W @f$ of the D-dimensional features
23
+ * @f$ X \in \mathcal{R}^{D \times N} @f$ and the learned hyperplane
24
+ * parameters @f$ W \in \mathcal{R}^{D \times K} @f$, so a Net with just
25
+ * an InnerProductLayer (with num_output = D) providing predictions to a
26
+ * HingeLossLayer and no other learnable parameters or losses is
27
+ * equivalent to an SVM.
28
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
29
+ * the labels @f$ l @f$, an integer-valued Blob with values
30
+ * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$
31
+ * indicating the correct class label among the @f$ K @f$ classes
32
+ * @param top output Blob vector (length 1)
33
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
34
+ * the computed hinge loss: @f$ E =
35
+ * \frac{1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^K
36
+ * [\max(0, 1 - \delta\{l_n = k\} t_{nk})] ^ p
37
+ * @f$, for the @f$ L^p @f$ norm
38
+ * (defaults to @f$ p = 1 @f$, the L1 norm; L2 norm, as in L2-SVM,
39
+ * is also available), and @f$
40
+ * \delta\{\mathrm{condition}\} = \left\{
41
+ * \begin{array}{lr}
42
+ * 1 & \mbox{if condition} \\
43
+ * -1 & \mbox{otherwise}
44
+ * \end{array} \right.
45
+ * @f$
46
+ *
47
+ * In an SVM, @f$ t \in \mathcal{R}^{N \times K} @f$ is the result of taking
48
+ * the inner product @f$ X^T W @f$ of the features
49
+ * @f$ X \in \mathcal{R}^{D \times N} @f$
50
+ * and the learned hyperplane parameters
51
+ * @f$ W \in \mathcal{R}^{D \times K} @f$. So, a Net with just an
52
+ * InnerProductLayer (with num_output = @f$k@f$) providing predictions to a
53
+ * HingeLossLayer is equivalent to an SVM (assuming it has no other learned
54
+ * outside the InnerProductLayer and no other losses outside the
55
+ * HingeLossLayer).
56
+ */
57
+ template <typename Dtype>
58
+ class HingeLossLayer : public LossLayer<Dtype> {
59
+ public:
60
+ explicit HingeLossLayer(const LayerParameter& param)
61
+ : LossLayer<Dtype>(param) {}
62
+
63
+ virtual inline const char* type() const { return "HingeLoss"; }
64
+
65
+ protected:
66
+ /// @copydoc HingeLossLayer
67
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
68
+ const vector<Blob<Dtype>*>& top);
69
+
70
+ /**
71
+ * @brief Computes the hinge loss error gradient w.r.t. the predictions.
72
+ *
73
+ * Gradients cannot be computed with respect to the label inputs (bottom[1]),
74
+ * so this method ignores bottom[1] and requires !propagate_down[1], crashing
75
+ * if propagate_down[1] is set.
76
+ *
77
+ * @param top output Blob vector (length 1), providing the error gradient with
78
+ * respect to the outputs
79
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
80
+ * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$,
81
+ * as @f$ \lambda @f$ is the coefficient of this layer's output
82
+ * @f$\ell_i@f$ in the overall Net loss
83
+ * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence
84
+ * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$.
85
+ * (*Assuming that this top Blob is not used as a bottom (input) by any
86
+ * other layer of the Net.)
87
+ * @param propagate_down see Layer::Backward.
88
+ * propagate_down[1] must be false as we can't compute gradients with
89
+ * respect to the labels.
90
+ * @param bottom input Blob vector (length 2)
91
+ * -# @f$ (N \times C \times H \times W) @f$
92
+ * the predictions @f$t@f$; Backward computes diff
93
+ * @f$ \frac{\partial E}{\partial t} @f$
94
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
95
+ * the labels -- ignored as we can't compute their error gradients
96
+ */
97
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
98
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
99
+ };
100
+
101
+
102
+ } // namespace caffe
103
+
104
+ #endif // CAFFE_HINGE_LOSS_LAYER_HPP_
usr/local/include/caffe/layers/im2col_layer.hpp ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_IM2COL_LAYER_HPP_
2
+ #define CAFFE_IM2COL_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief A helper for image operations that rearranges image regions into
14
+ * column vectors. Used by ConvolutionLayer to perform convolution
15
+ * by matrix multiplication.
16
+ *
17
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
18
+ */
19
+ template <typename Dtype>
20
+ class Im2colLayer : public Layer<Dtype> {
21
+ public:
22
+ explicit Im2colLayer(const LayerParameter& param)
23
+ : Layer<Dtype>(param) {}
24
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
25
+ const vector<Blob<Dtype>*>& top);
26
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
27
+ const vector<Blob<Dtype>*>& top);
28
+
29
+ virtual inline const char* type() const { return "Im2col"; }
30
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
31
+ virtual inline int ExactNumTopBlobs() const { return 1; }
32
+
33
+ protected:
34
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
35
+ const vector<Blob<Dtype>*>& top);
36
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
37
+ const vector<Blob<Dtype>*>& top);
38
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
39
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
40
+ virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
41
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
42
+
43
+ /// @brief The spatial dimensions of a filter kernel.
44
+ Blob<int> kernel_shape_;
45
+ /// @brief The spatial dimensions of the stride.
46
+ Blob<int> stride_;
47
+ /// @brief The spatial dimensions of the padding.
48
+ Blob<int> pad_;
49
+ /// @brief The spatial dimensions of the dilation.
50
+ Blob<int> dilation_;
51
+
52
+ int num_spatial_axes_;
53
+ int bottom_dim_;
54
+ int top_dim_;
55
+
56
+ int channel_axis_;
57
+ int num_;
58
+ int channels_;
59
+
60
+ bool force_nd_im2col_;
61
+ };
62
+
63
+ } // namespace caffe
64
+
65
+ #endif // CAFFE_IM2COL_LAYER_HPP_
usr/local/include/caffe/layers/image_data_layer.hpp ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_IMAGE_DATA_LAYER_HPP_
2
+ #define CAFFE_IMAGE_DATA_LAYER_HPP_
3
+
4
+ #include <string>
5
+ #include <utility>
6
+ #include <vector>
7
+
8
+ #include "caffe/blob.hpp"
9
+ #include "caffe/data_transformer.hpp"
10
+ #include "caffe/internal_thread.hpp"
11
+ #include "caffe/layer.hpp"
12
+ #include "caffe/layers/base_data_layer.hpp"
13
+ #include "caffe/proto/caffe.pb.h"
14
+
15
+ namespace caffe {
16
+
17
+ /**
18
+ * @brief Provides data to the Net from image files.
19
+ *
20
+ * TODO(dox): thorough documentation for Forward and proto params.
21
+ */
22
+ template <typename Dtype>
23
+ class ImageDataLayer : public BasePrefetchingDataLayer<Dtype> {
24
+ public:
25
+ explicit ImageDataLayer(const LayerParameter& param)
26
+ : BasePrefetchingDataLayer<Dtype>(param) {}
27
+ virtual ~ImageDataLayer();
28
+ virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
29
+ const vector<Blob<Dtype>*>& top);
30
+
31
+ virtual inline const char* type() const { return "ImageData"; }
32
+ virtual inline int ExactNumBottomBlobs() const { return 0; }
33
+ virtual inline int ExactNumTopBlobs() const { return 2; }
34
+
35
+ protected:
36
+ shared_ptr<Caffe::RNG> prefetch_rng_;
37
+ virtual void ShuffleImages();
38
+ virtual void load_batch(Batch<Dtype>* batch);
39
+
40
+ vector<std::pair<std::string, int> > lines_;
41
+ int lines_id_;
42
+ };
43
+
44
+
45
+ } // namespace caffe
46
+
47
+ #endif // CAFFE_IMAGE_DATA_LAYER_HPP_
usr/local/include/caffe/layers/infogain_loss_layer.hpp ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_INFOGAIN_LOSS_LAYER_HPP_
2
+ #define CAFFE_INFOGAIN_LOSS_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ #include "caffe/layers/loss_layer.hpp"
11
+ #include "caffe/layers/softmax_layer.hpp"
12
+
13
+ namespace caffe {
14
+
15
+ /**
16
+ * @brief A generalization of SoftmaxWithLossLayer that takes an
17
+ * "information gain" (infogain) matrix specifying the "value" of all label
18
+ * pairs.
19
+ *
20
+ * Equivalent to the SoftmaxWithLossLayer if the infogain matrix is the
21
+ * identity.
22
+ *
23
+ * @param bottom input Blob vector (length 2-3)
24
+ * -# @f$ (N \times C \times H \times W) @f$
25
+ * the predictions @f$ x @f$, a Blob with values in
26
+ * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of
27
+ * the @f$ K = CHW @f$ classes. This layer maps these scores to a
28
+ * probability distribution over classes using the softmax function
29
+ * @f$ \hat{p}_{nk} = \exp(x_{nk}) /
30
+ * \left[\sum_{k'} \exp(x_{nk'})\right] @f$ (see SoftmaxLayer).
31
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
32
+ * the labels @f$ l @f$, an integer-valued Blob with values
33
+ * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$
34
+ * indicating the correct class label among the @f$ K @f$ classes
35
+ * -# @f$ (1 \times 1 \times K \times K) @f$
36
+ * (\b optional) the infogain matrix @f$ H @f$. This must be provided as
37
+ * the third bottom blob input if not provided as the infogain_mat in the
38
+ * InfogainLossParameter. If @f$ H = I @f$, this layer is equivalent to the
39
+ * SoftmaxWithLossLayer.
40
+ * @param top output Blob vector (length 1)
41
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
42
+ * the computed infogain multinomial logistic loss: @f$ E =
43
+ * \frac{-1}{N} \sum\limits_{n=1}^N H_{l_n} \log(\hat{p}_n) =
44
+ * \frac{-1}{N} \sum\limits_{n=1}^N \sum\limits_{k=1}^{K} H_{l_n,k}
45
+ * \log(\hat{p}_{n,k})
46
+ * @f$, where @f$ H_{l_n} @f$ denotes row @f$l_n@f$ of @f$H@f$.
47
+ */
48
+ template <typename Dtype>
49
+ class InfogainLossLayer : public LossLayer<Dtype> {
50
+ public:
51
+ explicit InfogainLossLayer(const LayerParameter& param)
52
+ : LossLayer<Dtype>(param), infogain_() {}
53
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
54
+ const vector<Blob<Dtype>*>& top);
55
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
56
+ const vector<Blob<Dtype>*>& top);
57
+
58
+ // InfogainLossLayer takes 2-3 bottom Blobs; if there are 3 the third should
59
+ // be the infogain matrix. (Otherwise the infogain matrix is loaded from a
60
+ // file specified by LayerParameter.)
61
+ virtual inline int ExactNumBottomBlobs() const { return -1; }
62
+ virtual inline int MinBottomBlobs() const { return 2; }
63
+ virtual inline int MaxBottomBlobs() const { return 3; }
64
+
65
+ // InfogainLossLayer computes softmax prob internally.
66
+ // optional second "top" outputs the softmax prob
67
+ virtual inline int ExactNumTopBlobs() const { return -1; }
68
+ virtual inline int MinTopBlobs() const { return 1; }
69
+ virtual inline int MaxTopBlobs() const { return 2; }
70
+
71
+ virtual inline const char* type() const { return "InfogainLoss"; }
72
+
73
+ protected:
74
+ /// @copydoc InfogainLossLayer
75
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
76
+ const vector<Blob<Dtype>*>& top);
77
+
78
+ /**
79
+ * @brief Computes the infogain loss error gradient w.r.t. the predictions.
80
+ *
81
+ * Gradients cannot be computed with respect to the label inputs (bottom[1]),
82
+ * so this method ignores bottom[1] and requires !propagate_down[1], crashing
83
+ * if propagate_down[1] is set. (The same applies to the infogain matrix, if
84
+ * provided as bottom[2] rather than in the layer_param.)
85
+ *
86
+ * @param top output Blob vector (length 1), providing the error gradient
87
+ * with respect to the outputs
88
+ * -# @f$ (1 \times 1 \times 1 \times 1) @f$
89
+ * This Blob's diff will simply contain the loss_weight* @f$ \lambda @f$,
90
+ * as @f$ \lambda @f$ is the coefficient of this layer's output
91
+ * @f$\ell_i@f$ in the overall Net loss
92
+ * @f$ E = \lambda_i \ell_i + \mbox{other loss terms}@f$; hence
93
+ * @f$ \frac{\partial E}{\partial \ell_i} = \lambda_i @f$.
94
+ * (*Assuming that this top Blob is not used as a bottom (input) by any
95
+ * other layer of the Net.)
96
+ * @param propagate_down see Layer::Backward.
97
+ * propagate_down[1] must be false as we can't compute gradients with
98
+ * respect to the labels (similarly for propagate_down[2] and the
99
+ * infogain matrix, if provided as bottom[2])
100
+ * @param bottom input Blob vector (length 2-3)
101
+ * -# @f$ (N \times C \times H \times W) @f$
102
+ * the predictions @f$ x @f$; Backward computes diff
103
+ * @f$ \frac{\partial E}{\partial x} @f$
104
+ * -# @f$ (N \times 1 \times 1 \times 1) @f$
105
+ * the labels -- ignored as we can't compute their error gradients
106
+ * -# @f$ (1 \times 1 \times K \times K) @f$
107
+ * (\b optional) the information gain matrix -- ignored as its error
108
+ * gradient computation is not implemented.
109
+ */
110
+ virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
111
+ const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
112
+
113
+ /// Read the normalization mode parameter and compute the normalizer based
114
+ /// on the blob size. If normalization_mode is VALID, the count of valid
115
+ /// outputs will be read from valid_count, unless it is -1 in which case
116
+ /// all outputs are assumed to be valid.
117
+ virtual Dtype get_normalizer(
118
+ LossParameter_NormalizationMode normalization_mode, int valid_count);
119
+ /// fill sum_rows_H_ according to matrix H
120
+ virtual void sum_rows_of_H(const Blob<Dtype>* H);
121
+
122
+ /// The internal SoftmaxLayer used to map predictions to a distribution.
123
+ shared_ptr<Layer<Dtype> > softmax_layer_;
124
+ /// prob stores the output probability predictions from the SoftmaxLayer.
125
+ Blob<Dtype> prob_;
126
+ /// bottom vector holder used in call to the underlying SoftmaxLayer::Forward
127
+ vector<Blob<Dtype>*> softmax_bottom_vec_;
128
+ /// top vector holder used in call to the underlying SoftmaxLayer::Forward
129
+ vector<Blob<Dtype>*> softmax_top_vec_;
130
+
131
+ Blob<Dtype> infogain_;
132
+ Blob<Dtype> sum_rows_H_; // cache the row sums of H.
133
+
134
+ /// Whether to ignore instances with a certain label.
135
+ bool has_ignore_label_;
136
+ /// The label indicating that an instance should be ignored.
137
+ int ignore_label_;
138
+ /// How to normalize the output loss.
139
+ LossParameter_NormalizationMode normalization_;
140
+
141
+ int infogain_axis_, outer_num_, inner_num_, num_labels_;
142
+ };
143
+
144
+ } // namespace caffe
145
+
146
+ #endif // CAFFE_INFOGAIN_LOSS_LAYER_HPP_
usr/local/include/caffe/layers/inner_product_layer.hpp ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef CAFFE_INNER_PRODUCT_LAYER_HPP_
2
+ #define CAFFE_INNER_PRODUCT_LAYER_HPP_
3
+
4
+ #include <vector>
5
+
6
+ #include "caffe/blob.hpp"
7
+ #include "caffe/layer.hpp"
8
+ #include "caffe/proto/caffe.pb.h"
9
+
10
+ namespace caffe {
11
+
12
+ /**
13
+ * @brief Also known as a "fully-connected" layer, computes an inner product
14
+ * with a set of learned weights, and (optionally) adds biases.
15
+ *
16
+ * TODO(dox): thorough documentation for Forward, Backward, and proto params.
17
+ */
18
+ template <typename Dtype>
19
+ class InnerProductLayer : public Layer<Dtype> {
20
+ public:
21
+ explicit InnerProductLayer(const LayerParameter& param)
22
+ : Layer<Dtype>(param) {}
23
+ virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
24
+ const vector<Blob<Dtype>*>& top);
25
+ virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
26
+ const vector<Blob<Dtype>*>& top);
27
+
28
+ virtual inline const char* type() const { return "InnerProduct"; }
29
+ virtual inline int ExactNumBottomBlobs() const { return 1; }
30
+ virtual inline int ExactNumTopBlobs() const { return 1; }
31
+
32
+ protected:
33
+ virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
34
+ const vector<Blob<Dtype>*>& top);
35
+ virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
36
+ const vector<Blob<Dtype>*>& top);
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
+ int M_;
43
+ int K_;
44
+ int N_;
45
+ bool bias_term_;
46
+ Blob<Dtype> bias_multiplier_;
47
+ bool transpose_; ///< if true, assume transposed weights
48
+ };
49
+
50
+ } // namespace caffe
51
+
52
+ #endif // CAFFE_INNER_PRODUCT_LAYER_HPP_