Caffe2 - C++ API
A deep learning, cross platform ML framework
pad_op.h
1 #ifndef CAFFE2_OPERATORS_PAD_OP_H_
2 #define CAFFE2_OPERATORS_PAD_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/core/operator.h"
7 #include "caffe2/operators/conv_pool_op_base.h"
8 #include "caffe2/utils/math.h"
9 
10 namespace caffe2 {
11 
12 // Padding mode similar to numpy.
13 enum class PadMode {
14  CONSTANT = 0, // pad constant values, with string "constant"
15  REFLECT = 1, // pads with reflect values, with string "reflect"
16  EDGE = 2, // pads with the edge values, with string "edge"
17 };
18 
19 CAFFE2_API PadMode StringToPadMode(const string&);
20 
21 template <typename T, class Context>
22 class PadImageOp final : public ConvPoolOpBase<Context> {
23  public:
24  USE_CONV_POOL_BASE_FUNCTIONS(Context);
25  template <class... Args>
26  explicit PadImageOp(Args&&... args)
27  : ConvPoolOpBase<Context>(std::forward<Args>(args)...),
28  mode_(StringToPadMode(
29  this->template GetSingleArgument<string>("mode", "constant"))),
30  value_(static_cast<T>(
31  this->template GetSingleArgument<float>("value", 0.0))) {
32  CAFFE_ENFORCE(
33  legacy_pad_ == LegacyPadding::NOTSET,
34  "Padding layer only supports explicit pad values.");
35  CAFFE_ENFORCE(
36  dilation_h() == 1 && dilation_w() == 1,
37  "Pooling op does not support dilation right now.");
38  CAFFE_ENFORCE(
39  stride_h() == 1 && stride_w() == 1,
40  "Pooling op does not support stride right now.");
41  // Pad op does not use kernel sizes, so we set it to 1 for computing the
42  // output size.
43  kernel_.assign(pads_.size() / 2, 1);
44  }
45  ~PadImageOp() {}
46 
47  bool RunOnDeviceWithOrderNCHW() override;
48  bool RunOnDeviceWithOrderNHWC() override;
49 
50  static std::vector<TensorShape> PadTensorInference(
51  const OperatorDef& def,
52  const vector<TensorShape>& in);
53 
54  private:
55  PadMode mode_;
56  T value_;
57 
58  // Input: X
59  // Output: Y
60 };
61 
62 template <typename T, class Context>
63 class PadImageGradientOp final : public ConvPoolOpBase<Context> {
64  public:
65  USE_CONV_POOL_BASE_FUNCTIONS(Context);
66  template <class... Args>
67  explicit PadImageGradientOp(Args&&... args)
68  : ConvPoolOpBase<Context>(std::forward<Args>(args)...),
69  mode_(StringToPadMode(
70  this->template GetSingleArgument<string>("mode", "constant"))) {
71  CAFFE_ENFORCE(
72  legacy_pad_ == LegacyPadding::NOTSET,
73  "Padding layer only supports explicit pad values.");
74  CAFFE_ENFORCE(
75  dilation_h() == 1 && dilation_w() == 1,
76  "Pooling op does not support dilation right now.");
77  // Pad op does not use kernel sizes, so we set it to 1 for computing the
78  // output size.
79  kernel_.assign(pads_.size() / 2, 1);
80  }
81  ~PadImageGradientOp() {}
82 
83  bool RunOnDeviceWithOrderNCHW() override;
84  bool RunOnDeviceWithOrderNHWC() override;
85 
86  private:
87  PadMode mode_;
88  // Input: dY
89  // Output: dX
90 };
91 
92 } // namespace caffe2
93 
94 #endif // CAFFE2_OPERATORS_PAD_OP_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13