Caffe2 - C++ API
A deep learning, cross platform ML framework
pad_op.h
1 
17 #ifndef CAFFE2_OPERATORS_PAD_OP_H_
18 #define CAFFE2_OPERATORS_PAD_OP_H_
19 
20 #include "caffe2/core/context.h"
21 #include "caffe2/core/logging.h"
22 #include "caffe2/core/operator.h"
23 #include "caffe2/operators/conv_pool_op_base.h"
24 #include "caffe2/utils/math.h"
25 
26 namespace caffe2 {
27 
28 // Padding mode similar to numpy.
29 enum class PadMode {
30  CONSTANT = 0, // pad constant values, with string "constant"
31  REFLECT = 1, // pads with reflect values, with string "reflect"
32  EDGE = 2, // pads with the edge values, with string "edge"
33 };
34 
35 PadMode StringToPadMode(const string&);
36 
37 template <typename T, class Context>
38 class PadImageOp final : public ConvPoolOpBase<Context> {
39  public:
40  USE_CONV_POOL_BASE_FUNCTIONS(Context);
41  PadImageOp(const OperatorDef& operator_def, Workspace* ws)
42  : ConvPoolOpBase<Context>(operator_def, ws),
43  mode_(StringToPadMode(
44  OperatorBase::GetSingleArgument<string>("mode", "constant"))),
45  value_(static_cast<T>(
46  OperatorBase::GetSingleArgument<float>("value", 0.0))) {
47  CAFFE_ENFORCE(
48  legacy_pad_ == LegacyPadding::NOTSET,
49  "Padding layer only supports explicit pad values.");
50  CAFFE_ENFORCE(
51  dilation_h() == 1 && dilation_w() == 1,
52  "Pooling op does not support dilation right now.");
53  CAFFE_ENFORCE(
54  stride_h() == 1 && stride_w() == 1,
55  "Pooling op does not support stride right now.");
56  // Pad op does not use kernel sizes, so we set it to 1 for computing the
57  // output size.
58  kernel_.assign(pads_.size() / 2, 1);
59  }
60  ~PadImageOp() {}
61 
62  bool RunOnDeviceWithOrderNCHW() override;
63  bool RunOnDeviceWithOrderNHWC() override;
64 
65  static std::vector<TensorShape> PadTensorInference(
66  const OperatorDef& def,
67  const vector<TensorShape>& in);
68 
69  private:
70  PadMode mode_;
71  T value_;
72 
73  // Input: X
74  // Output: Y
75 };
76 
77 template <typename T, class Context>
78 class PadImageGradientOp final : public ConvPoolOpBase<Context> {
79  public:
80  USE_CONV_POOL_BASE_FUNCTIONS(Context);
81  PadImageGradientOp(const OperatorDef& operator_def, Workspace* ws)
82  : ConvPoolOpBase<Context>(operator_def, ws),
83  mode_(StringToPadMode(
84  OperatorBase::GetSingleArgument<string>("mode", "constant"))) {
85  CAFFE_ENFORCE(
86  legacy_pad_ == LegacyPadding::NOTSET,
87  "Padding layer only supports explicit pad values.");
88  CAFFE_ENFORCE(
89  dilation_h() == 1 && dilation_w() == 1,
90  "Pooling op does not support dilation right now.");
91  // Pad op does not use kernel sizes, so we set it to 1 for computing the
92  // output size.
93  kernel_.assign(pads_.size() / 2, 1);
94  }
95  ~PadImageGradientOp() {}
96 
97  bool RunOnDeviceWithOrderNCHW() override;
98  bool RunOnDeviceWithOrderNHWC() override;
99 
100  private:
101  PadMode mode_;
102  // Input: dY
103  // Output: dX
104 };
105 
106 } // namespace caffe2
107 
108 #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:63
Copyright (c) 2016-present, Facebook, Inc.