Caffe2 - C++ API
A deep learning, cross platform ML framework
conv_transpose_op.cc
1 #include "caffe2/operators/conv_transpose_op.h"
2 #include "caffe2/operators/conv_transpose_op_impl.h"
3 
4 namespace caffe2 {
5 
6 REGISTER_CPU_OPERATOR(ConvTranspose, ConvTransposeOp<float, CPUContext>);
7 
8 OPERATOR_SCHEMA(ConvTranspose)
9  .NumInputs(2, 3)
10  .NumOutputs(1)
11  .SetDoc(R"DOC(
12 The ConvTranspose op takes an input data tensor $X$, an input weight tensor $filter$, and optionally an input bias tensor $bias$. It then computes the transposed convolution, sometimes referred to as deconvolution, and produces a single output tensor $Y$. The hyperparameters of the op such as kernel size, stride, and padding are specified as args. At each stride, the filter is deconvolved with a subset of $X$ and the $bias$ is added. This is done throughout the input data until the output computation is complete.
13 
14 The output shapes are computed as follows. The number of channels in the output feature map is the number of kernels specified in the filter blob. The spatial height and width are computed as:
15 
16 $$H_{out} = (H_{in}-1)*strides[0] - 2*pads[0] + kernels[0]$$
17 
18 
19 $$W_{out} = (W_{in}-1)*strides[1] - 2*pads[1] + kernels[1]$$
20 
21 Note on the implementation layout: conv_transpose_op_impl.h is the templated implementation of the conv_transpose_op.h file, which is why they are separate files. Also, in the implementation this operator inherits from the *ConvTransposeUnpoolOpBase* operator.
22 
23 Github Links:
24 - https://github.com/pytorch/pytorch/tree/master/caffe2/operators/conv_transpose_op.h
25 - https://github.com/pytorch/pytorch/tree/master/caffe2/operators/conv_transpose_op.cc
26 - https://github.com/pytorch/pytorch/tree/master/caffe2/operators/conv_transpose_unpool_op_base.h
27 
28 <details>
29 
30 <summary> <b>Example</b> </summary>
31 
32 **Code**
33 
34 ```
35 
36 workspace.ResetWorkspace()
37 
38 op = core.CreateOperator(
39  "ConvTranspose",
40  ["X", "filter", "bias"],
41  ["Y"],
42  kernels=[2,2],
43  pads=[4,4,4,4],
44  strides=[2,2]
45 )
46 
47 // Create X: (N,C,H,W)
48 data = np.random.randn(2,3,5,5).astype(np.float32)
49 print("Data shape: ",data.shape)
50 
51 // Create filter: (M,C,Kh,Kw)
52 filters = np.random.randn(3,1,2,2).astype(np.float32)
53 print("Filter shape: ",filters.shape)
54 
55 // Create b: M
56 bias = np.array([1.]).astype(np.float32)
57 print("Bias shape: ",bias.shape)
58 
59 // Put the inputs into the workspace
60 workspace.FeedBlob("X", data)
61 workspace.FeedBlob("filter", filters)
62 workspace.FeedBlob("bias", bias)
63 
64 // Run the operator
65 workspace.RunOperatorOnce(op)
66 print("Y:\n", workspace.FetchBlob("Y"))
67 
68 ```
69 
70 **Result**
71 
72 ```
73 
74 Data shape: (2, 3, 5, 5)
75 Filter shape: (3, 1, 2, 2)
76 Bias shape: (1,)
77 Y:
78  [[[[0.53606427 0.5775447 ]
79  [0.40148795 1.5188271 ]]]
80 
81 
82  [[[1.9903406 3.2794335 ]
83  [0.09960175 0.31917763]]]]
84 
85 ```
86 
87 </details>
88 
89  )DOC")
90  .Input(
91  0,
92  "X",
93  "Input data blob, of shape $(N, C_{in}, H_{in}, W_{in})$, to be operated on.")
94  .Input(
95  1,
96  "filter",
97  "The filter blob, of shape $(M, C_{out}, K_H, K_W)$, containing the filters to be used in the transposed convolution.")
98  .Input(
99  2,
100  "bias",
101  "The bias blob, of length $C_{out}$, containing the biases for the operation, one bias per output channel. If not passed, biases assumed to be zeros.")
102  .Output(
103  0,
104  "Y",
105  "Output data blob, of shape $(N, C_{out}, H_{out}, W_{out})$, that contains the result of the operation.")
106  .Arg(
107  "legacy_pad",
108  "*(type: int; optional)* Should the legacy padding be VALID or SAME. When used, pads should not be used.")
109  .Arg(
110  "kernels",
111  "*(type: [int]; default: [])* Desired kernel size. If left at default the kernel size will be inferred from the input $filter$ blob.")
112  .Arg(
113  "strides",
114  "*(type: [int]; default: [])* Controls the stride of the kernel as it traverses the input blob.")
115  .Arg(
116  "pads",
117  "*(type: [int]; default: [])* Controls the amount of padding applied to the input feature map before computation.")
118  .Arg("adjs", "*(type: [int]; default: [])*")
119  .Arg(
120  "order",
121  "*(type: string; default: \"NCHW\")* Specifies the order of the input data blob, where $N$ is batch size, $C$ is number of channels, $H$ is spatial height, and $W$ is spatial width. The only other valid option is \"NHWC\".")
122  .Arg("shared_buffer", "*(type: int; default: 0)*")
123  .Arg("no_bias", "*(type: bool; default: False)* ")
124  .InheritOnnxSchema();
125 
126 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13