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 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  PadImageOp(const OperatorDef& operator_def, Workspace* ws)
26  : ConvPoolOpBase<Context>(operator_def, ws),
27  mode_(StringToPadMode(
28  OperatorBase::GetSingleArgument<string>("mode", "constant"))),
29  value_(static_cast<T>(
30  OperatorBase::GetSingleArgument<float>("value", 0.0))) {
31  CAFFE_ENFORCE(
32  legacy_pad_ == LegacyPadding::NOTSET,
33  "Padding layer only supports explicit pad values.");
34  CAFFE_ENFORCE(
35  dilation_h() == 1 && dilation_w() == 1,
36  "Pooling op does not support dilation right now.");
37  CAFFE_ENFORCE(
38  stride_h() == 1 && stride_w() == 1,
39  "Pooling op does not support stride right now.");
40  // Pad op does not use kernel sizes, so we set it to 1 for computing the
41  // output size.
42  kernel_.assign(pads_.size() / 2, 1);
43  }
44  ~PadImageOp() {}
45 
46  bool RunOnDeviceWithOrderNCHW() override;
47  bool RunOnDeviceWithOrderNHWC() override;
48 
49  static std::vector<TensorShape> PadTensorInference(
50  const OperatorDef& def,
51  const vector<TensorShape>& in);
52 
53  private:
54  PadMode mode_;
55  T value_;
56 
57  // Input: X
58  // Output: Y
59 };
60 
61 template <typename T, class Context>
62 class PadImageGradientOp final : public ConvPoolOpBase<Context> {
63  public:
64  USE_CONV_POOL_BASE_FUNCTIONS(Context);
65  PadImageGradientOp(const OperatorDef& operator_def, Workspace* ws)
66  : ConvPoolOpBase<Context>(operator_def, ws),
67  mode_(StringToPadMode(
68  OperatorBase::GetSingleArgument<string>("mode", "constant"))) {
69  CAFFE_ENFORCE(
70  legacy_pad_ == LegacyPadding::NOTSET,
71  "Padding layer only supports explicit pad values.");
72  CAFFE_ENFORCE(
73  dilation_h() == 1 && dilation_w() == 1,
74  "Pooling op does not support dilation right now.");
75  // Pad op does not use kernel sizes, so we set it to 1 for computing the
76  // output size.
77  kernel_.assign(pads_.size() / 2, 1);
78  }
79  ~PadImageGradientOp() {}
80 
81  bool RunOnDeviceWithOrderNCHW() override;
82  bool RunOnDeviceWithOrderNHWC() override;
83 
84  private:
85  PadMode mode_;
86  // Input: dY
87  // Output: dX
88 };
89 
90 } // namespace caffe2
91 
92 #endif // CAFFE2_OPERATORS_PAD_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...