Caffe2 - C++ API
A deep learning, cross platform ML framework
order_switch_ops.cc
1 #include "caffe2/operators/order_switch_ops.h"
2 
3 #include <string>
4 
5 namespace caffe2 {
6 
7 REGISTER_CPU_OPERATOR(NHWC2NCHW, NHWC2NCHWOp<float, CPUContext>);
8 REGISTER_CPU_OPERATOR(NCHW2NHWC, NCHW2NHWCOp<float, CPUContext>);
9 
10 OPERATOR_SCHEMA(NHWC2NCHW)
11  .NumInputs(1)
12  .NumOutputs(1)
13  .TensorInferenceFunction([](const OperatorDef& /*unused*/ /*def*/,
14  const std::vector<TensorShape>& in) {
15  CAFFE_ENFORCE_GE(
16  in[0].dims_size(), 3, "Input for NHWC2NCHW must be >= 3 dimensional");
17  std::vector<TensorShape> out(1);
18  out[0].add_dims(in[0].dims(0));
19  out[0].add_dims(in[0].dims(in[0].dims_size() - 1));
20  for (auto i = 1; i < in[0].dims_size() - 1; ++i) {
21  out[0].add_dims(in[0].dims(i));
22  }
23  return out;
24  })
25  .SetDoc(R"DOC(
26 The operator switches the order of data in a tensor from NHWC- sample index N,
27 height H, width H and channels C, to the NCHW order (this is for 2D images).
28 In general, this operator switches the order of data in a tensor from N H_1 ...
29 H_k C to N C H_1 ... H_k for k-dimensional features, and currently supports
30 k=1, 2, and 3.
31 )DOC")
32  .Input(0, "data", "The input data (Tensor) in the NHWC order.")
33  .Output(0, "output", "The output tensor (Tensor) in the NCHW order.");
34 
35 OPERATOR_SCHEMA(NCHW2NHWC)
36  .NumInputs(1)
37  .NumOutputs(1)
38  .TensorInferenceFunction([](const OperatorDef& /*unused*/ /*def*/,
39  const std::vector<TensorShape>& in) {
40  CAFFE_ENFORCE_GE(
41  in[0].dims_size(), 3, "Input for NCHW2NHWC must be >= 3 dimensional");
42  std::vector<TensorShape> out(1);
43  out[0].add_dims(in[0].dims(0));
44  for (auto i = 2; i < in[0].dims_size(); ++i) {
45  out[0].add_dims(in[0].dims(i));
46  }
47  out[0].add_dims(in[0].dims(1));
48  return out;
49  })
50  .SetDoc(R"DOC(
51 The operator switches the order of data in a tensor from NCHW- sample index N,
52 channels C, height H and width W, to the NHWC order (this is for 2D images).
53 In general, this operator switches the order of data in a tensor from N C H_1
54 ... H_k to N H_1 ... H_k C for k-dimensional features, and currently supports
55 k=1, 2, and 3.
56 )DOC")
57  .Input(0, "data", "The input data (Tensor) in the NCHW order.")
58  .Output(0, "output", "The output tensor (Tensor) in the NHWC order.");
59 
60 namespace {
61 
62 class GetNHWC2NCHWGradient : public GradientMakerBase {
63  using GradientMakerBase::GradientMakerBase;
64  std::vector<OperatorDef> GetGradientDefs() override {
65  return SingleGradientDef(
66  "NCHW2NHWC",
67  "",
68  std::vector<std::string>{GO(0)},
69  std::vector<std::string>{GI(0)});
70  }
71 };
72 
73 class GetNCHW2NHWCGradient : public GradientMakerBase {
74  using GradientMakerBase::GradientMakerBase;
75  std::vector<OperatorDef> GetGradientDefs() override {
76  return SingleGradientDef(
77  "NHWC2NCHW",
78  "",
79  std::vector<std::string>{GO(0)},
80  std::vector<std::string>{GI(0)});
81  }
82 };
83 
84 } // namespace
85 
86 REGISTER_GRADIENT(NHWC2NCHW, GetNHWC2NCHWGradient);
87 REGISTER_GRADIENT(NCHW2NHWC, GetNCHW2NHWCGradient);
88 
89 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13