Caffe2 - C++ API
A deep learning, cross platform ML framework
space_batch_op.cc
1 #include "caffe2/operators/space_batch_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(SpaceToBatch, SpaceToBatchOp<CPUContext>);
6 OPERATOR_SCHEMA(SpaceToBatch).NumInputs(1).NumOutputs(1).SetDoc(R"DOC(
7 Zero-pads and then rearranges (permutes) blocks of spatial data into batch. More specifically, this op outputs a copy of the input tensor where values from the height and width dimensions are moved to the batch dimension. After the zero-padding is according to the `pad` argument, both height and width of the input must be divisible by the `block_size`. Only "NCHW" order is currently supported.
8 
9 Github Links:
10 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/space_batch_op.cc
11 
12 <details>
13 
14 <summary> <b>Example</b> </summary>
15 
16 **Code**
17 
18 ```
19 
20 workspace.ResetWorkspace()
21 
22 op = core.CreateOperator(
23  "SpaceToBatch",
24  ["X"],
25  ["Y"],
26  pad=2,
27  block_size=3
28 )
29 
30 workspace.FeedBlob("X", np.random.rand(1,3,5,5).astype(np.float32))
31 print("X.shape:", workspace.FetchBlob("X").shape)
32 workspace.RunOperatorOnce(op)
33 print("Y.shape:", workspace.FetchBlob("Y").shape)
34 
35 ```
36 
37 **Result**
38 
39 ```
40 
41 X.shape: (1, 3, 5, 5)
42 Y.shape: (9, 3, 3, 3)
43 
44 ```
45 
46 </details>
47 
48 )DOC")
49  .Arg("pad","(*int*): exclusive axis that divides the first and second dimension of matrix `A` (default=0)")
50  .Arg("block_size","(*int*): height/width of spatial blocks to be moved (default=2)")
51  .Arg("order","(*string*): order of dimensions of input and output blobs; only \"NCHW\" order is currently supported (default=\"NCHW\")")
52  .Input(0,"X","(*Tensor`<float>`*): input tensor (NCHW order)")
53  .Output(0,"Y","(*Tensor`<float>`*): output tensor (NCHW order)");
54 
55 REGISTER_CPU_OPERATOR(BatchToSpace, BatchToSpaceOp<CPUContext>);
56 OPERATOR_SCHEMA(BatchToSpace).NumInputs(1).NumOutputs(1).SetDoc(R"DOC(
57 Rearranges (permutes) data from batch into blocks of spatial data, followed by cropping. This is the reverse transformation of `SpaceToBatch`. More specifically, this op outputs a copy of the input tensor where values from the batch dimension are moved in spatial blocks to the height and width dimensions, followed by cropping along the height and width dimensions. Only "NCHW" order is currently supported.
58 
59 Github Links:
60 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/space_batch_op.cc
61 
62 <details>
63 
64 <summary> <b>Example</b> </summary>
65 
66 **Code**
67 
68 ```
69 
70 workspace.ResetWorkspace()
71 
72 op = core.CreateOperator(
73  "BatchToSpace",
74  ["X"],
75  ["Y"],
76  pad=3
77 )
78 
79 workspace.FeedBlob("X", np.random.rand(10,3,32,32).astype(np.float32))
80 print("X.shape:", workspace.FetchBlob("X").shape)
81 workspace.RunOperatorOnce(op)
82 print("Y.shape:", workspace.FetchBlob("Y").shape)
83 
84 ```
85 
86 **Result**
87 
88 ```
89 
90 X.shape: (10, 3, 32, 32)
91 Y.shape: (2, 3, 58, 58)
92 
93 ```
94 
95 </details>
96 
97 )DOC")
98  .Arg("pad","(*int*): exclusive axis that divides the first and second dimension of matrix `A` (default=0)")
99  .Arg("block_size","(*int*): height/width of spatial blocks to be moved (default=2)")
100  .Arg("order","(*string*): order of dimensions of input and output blobs; only \"NCHW\" order is currently supported (default=\"NCHW\")")
101  .Input(0,"X","(*Tensor`<float>`*): input tensor (NCHW order)")
102  .Output(0,"Y","(*Tensor`<float>`*): output tensor (NCHW order)");
103 
105  using GradientMakerBase::GradientMakerBase;
106  vector<OperatorDef> GetGradientDefs() override {
107  return SingleGradientDef(
108  "BatchToSpace", "", vector<string>{GO(0)}, vector<string>{GI(0)});
109  }
110 };
111 
113  using GradientMakerBase::GradientMakerBase;
114  vector<OperatorDef> GetGradientDefs() override {
115  return SingleGradientDef(
116  "SpaceToBatch", "", vector<string>{GO(0)}, vector<string>{GI(0)});
117  }
118 };
119 REGISTER_GRADIENT(SpaceToBatch, GetSpaceToBatchGradient);
120 REGISTER_GRADIENT(BatchToSpace, GetBatchToSpaceGradient);
121 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...