|
12 | 12 | See the License for the specific language governing permissions and |
13 | 13 | limitations under the License. */ |
14 | 14 |
|
15 | | -#include "paddle/operators/gemm_conv2d_op.h" |
| 15 | +#include "paddle/operators/conv2d_op.h" |
16 | 16 |
|
17 | 17 | namespace paddle { |
18 | 18 | namespace operators { |
19 | 19 |
|
20 | | -int outputSize(int input_size, int filter_size, int padding, int stride) { |
21 | | - int output_size = (input_size - filter_size + 2 * padding) / stride + 1; |
22 | | - return output_size; |
| 20 | +void Conv2DOp::InferShape(framework::InferShapeContext* ctx) const { |
| 21 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 22 | + "Input(Input) of Conv2DOp should not be null."); |
| 23 | + PADDLE_ENFORCE(ctx->HasInput("Filter"), |
| 24 | + "Input(Filter) of Conv2DOp should not be null."); |
| 25 | + PADDLE_ENFORCE(ctx->HasOutput("Output"), |
| 26 | + "Output(Output) of Conv2DOp should not be null."); |
| 27 | + |
| 28 | + auto in_dims = ctx->GetInputDim("Input"); |
| 29 | + auto filter_dims = ctx->GetInputDim("Filter"); |
| 30 | + std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides"); |
| 31 | + std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings"); |
| 32 | + int groups = ctx->Attrs().Get<int>("groups"); |
| 33 | + int input_channels = in_dims[1]; |
| 34 | + int output_channels = filter_dims[0]; |
| 35 | + |
| 36 | + PADDLE_ENFORCE_EQ(in_dims.size(), 4, "Conv2DOp input should be 4-D."); |
| 37 | + PADDLE_ENFORCE_EQ(filter_dims.size(), 4, "Conv2DOp filter should be 4-D."); |
| 38 | + PADDLE_ENFORCE_EQ(input_channels, filter_dims[1] * groups, |
| 39 | + "The number of input channels should be equal to filter " |
| 40 | + "channels * groups."); |
| 41 | + PADDLE_ENFORCE_EQ( |
| 42 | + output_channels % groups, 0, |
| 43 | + "The number of output channels should be divided by groups."); |
| 44 | + |
| 45 | + auto output_height = |
| 46 | + OutputSize(in_dims[2], filter_dims[2], paddings[0], strides[0]); |
| 47 | + auto output_width = |
| 48 | + OutputSize(in_dims[3], filter_dims[3], paddings[1], strides[1]); |
| 49 | + ctx->SetOutputDim("Output", |
| 50 | + {in_dims[0], filter_dims[0], output_height, output_width}); |
23 | 51 | } |
24 | 52 |
|
25 | | -class Conv2DOp : public framework::OperatorWithKernel { |
26 | | - public: |
27 | | - using framework::OperatorWithKernel::OperatorWithKernel; |
28 | | - |
29 | | - protected: |
30 | | - void InferShape(framework::InferShapeContext* ctx) const override { |
31 | | - PADDLE_ENFORCE(ctx->HasInput("Input"), |
32 | | - "Input(Input) of Conv2DOp should not be null."); |
33 | | - PADDLE_ENFORCE(ctx->HasInput("Filter"), |
34 | | - "Input(Filter) of Conv2DOp should not be null."); |
35 | | - PADDLE_ENFORCE(ctx->HasOutput("Output"), |
36 | | - "Output(Output) of Conv2DOp should not be null."); |
37 | | - |
38 | | - auto in_dims = ctx->GetInputDim("Input"); |
39 | | - auto filter_dims = ctx->GetInputDim("Filter"); |
40 | | - std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides"); |
41 | | - std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings"); |
42 | | - int groups = ctx->Attrs().Get<int>("groups"); |
43 | | - int input_channels = in_dims[1]; |
44 | | - int output_channels = filter_dims[0]; |
45 | | - |
46 | | - PADDLE_ENFORCE_EQ(in_dims.size(), 4, "Conv2DOp input should be 4-D."); |
47 | | - PADDLE_ENFORCE_EQ(filter_dims.size(), 4, "Conv2DOp filter should be 4-D."); |
48 | | - PADDLE_ENFORCE_EQ(input_channels, filter_dims[1] * groups, |
49 | | - "The number of input channels should be equal to filter " |
50 | | - "channels * groups."); |
51 | | - PADDLE_ENFORCE_EQ( |
52 | | - output_channels % groups, 0, |
53 | | - "The number of output channels should be divided by groups."); |
54 | | - |
55 | | - auto output_height = |
56 | | - outputSize(in_dims[2], filter_dims[2], paddings[0], strides[0]); |
57 | | - auto output_width = |
58 | | - outputSize(in_dims[3], filter_dims[3], paddings[1], strides[1]); |
59 | | - ctx->SetOutputDim( |
60 | | - "Output", {in_dims[0], filter_dims[0], output_height, output_width}); |
61 | | - } |
62 | | -}; |
63 | | - |
64 | | -class Conv2DOpMaker : public framework::OpProtoAndCheckerMaker { |
65 | | - public: |
66 | | - Conv2DOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker) |
67 | | - : OpProtoAndCheckerMaker(proto, op_checker) { |
68 | | - AddInput( |
69 | | - "Input", |
70 | | - "The input tensor of convolution operator. " |
71 | | - "The format of input tensor is NCHW. Where N is batch size, C is the " |
72 | | - "number of channels, H and W is the height and width of image."); |
73 | | - AddInput( |
74 | | - "Filter", |
75 | | - "The filter tensor of convolution operator." |
76 | | - "The format of the filter tensor is MCHW, where M is the number of " |
77 | | - "output image channels, C is the number of input image channels, " |
78 | | - "H and W is height and width of filter. " |
79 | | - "If the groups attribute is greater than 1, C equal the number of " |
80 | | - "input image channels divided by the groups."); |
81 | | - AddOutput("Output", |
82 | | - "The output tensor of convolution operator." |
83 | | - "The format of output tensor is also NCHW."); |
84 | | - AddAttr<std::vector<int>>("strides", "strides of convolution operator.") |
85 | | - .SetDefault({1, 1}); |
86 | | - AddAttr<std::vector<int>>("paddings", "paddings of convolution operator.") |
87 | | - .SetDefault({0, 0}); |
88 | | - AddAttr<int>( |
89 | | - "groups", |
90 | | - "group size of convolution operator. " |
91 | | - "Refer to grouped convolution in Alex Krizhevsky's paper: " |
92 | | - "when group=2, the first half of the filters are only connected to the " |
93 | | - "first half of the input channels, and the second half only connected " |
94 | | - "to the second half.") |
95 | | - .SetDefault(1); |
96 | | - AddComment(R"DOC( |
| 53 | +Conv2DOpMaker::Conv2DOpMaker(framework::OpProto* proto, |
| 54 | + framework::OpAttrChecker* op_checker) |
| 55 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 56 | + AddInput( |
| 57 | + "Input", |
| 58 | + "The input tensor of convolution operator. " |
| 59 | + "The format of input tensor is NCHW. Where N is batch size, C is the " |
| 60 | + "number of channels, H and W is the height and width of image."); |
| 61 | + AddInput("Filter", |
| 62 | + "The filter tensor of convolution operator." |
| 63 | + "The format of the filter tensor is MCHW, where M is the number of " |
| 64 | + "output image channels, C is the number of input image channels, " |
| 65 | + "H and W is height and width of filter. " |
| 66 | + "If the groups attribute is greater than 1, C equal the number of " |
| 67 | + "input image channels divided by the groups."); |
| 68 | + AddOutput("Output", |
| 69 | + "The output tensor of convolution operator." |
| 70 | + "The format of output tensor is also NCHW."); |
| 71 | + AddAttr<std::vector<int>>("strides", "strides of convolution operator.") |
| 72 | + .SetDefault({1, 1}); |
| 73 | + AddAttr<std::vector<int>>("paddings", "paddings of convolution operator.") |
| 74 | + .SetDefault({0, 0}); |
| 75 | + AddAttr<int>( |
| 76 | + "groups", |
| 77 | + "group size of convolution operator. " |
| 78 | + "Refer to grouped convolution in Alex Krizhevsky's paper: " |
| 79 | + "when group=2, the first half of the filters are only connected to the " |
| 80 | + "first half of the input channels, and the second half only connected " |
| 81 | + "to the second half.") |
| 82 | + .SetDefault(1); |
| 83 | + AddComment(R"DOC( |
97 | 84 | The convolution operation calculates the output based on the input, filter |
98 | 85 | and strides, paddings, groups parameters. The size of each dimension of the |
99 | 86 | parameters is checked in the infer-shape. |
100 | 87 | )DOC"); |
101 | | - } |
102 | | -}; |
103 | | - |
104 | | -class Conv2DOpGrad : public framework::OperatorWithKernel { |
105 | | - public: |
106 | | - using framework::OperatorWithKernel::OperatorWithKernel; |
| 88 | +} |
107 | 89 |
|
108 | | - protected: |
109 | | - void InferShape(framework::InferShapeContext* ctx) const override { |
110 | | - auto in_dims = ctx->GetInputDim("Input"); |
111 | | - auto filter_dims = ctx->GetInputDim("Filter"); |
112 | | - if (ctx->HasOutput(framework::GradVarName("Input"))) { |
113 | | - ctx->SetOutputDim(framework::GradVarName("Input"), in_dims); |
114 | | - } |
115 | | - if (ctx->HasOutput(framework::GradVarName("Filter"))) { |
116 | | - ctx->SetOutputDim(framework::GradVarName("Filter"), filter_dims); |
117 | | - } |
| 90 | +void Conv2DOpGrad::InferShape(framework::InferShapeContext* ctx) const { |
| 91 | + auto in_dims = ctx->GetInputDim("Input"); |
| 92 | + auto filter_dims = ctx->GetInputDim("Filter"); |
| 93 | + if (ctx->HasOutput(framework::GradVarName("Input"))) { |
| 94 | + ctx->SetOutputDim(framework::GradVarName("Input"), in_dims); |
118 | 95 | } |
119 | | -}; |
| 96 | + if (ctx->HasOutput(framework::GradVarName("Filter"))) { |
| 97 | + ctx->SetOutputDim(framework::GradVarName("Filter"), filter_dims); |
| 98 | + } |
| 99 | +} |
120 | 100 |
|
121 | 101 | } // namespace operators |
122 | 102 | } // namespace paddle |
|
0 commit comments