add unfold op (new op)#17944
Conversation
test=develop
paddle/fluid/operators/unfold_op.cc
Outdated
| This Operator is used to extract sliding local blocks from a batched input tensor, also known | ||
| as im2col when operated on batched 2D image tensor. For each block under the convolution filter, | ||
| all element will be rearranged as a column. While the convolution filter silding over the input | ||
| feature map, a series of such columns will be formed. |
There was a problem hiding this comment.
give same examples or formula
There was a problem hiding this comment.
Yes, the formula is given in the python api
paddle/fluid/operators/unfold_op.cc
Outdated
| strides.size(), kernel_sizes.size(), | ||
| "The dims of strides shold be the same with that of kernel_sizes. " | ||
| "But recieved dims(strides: %u) != dims(kernel_sizes: %u).", | ||
| strides.size(), dilations.size()); |
There was a problem hiding this comment.
strides.size(), dilations.size() -> strides.size(), kernel_sizes.size()
| auto& dev_ctx = ctx.template device_context<DeviceContext>(); | ||
|
|
||
| math::SetConstant<DeviceContext, T> set_zero; | ||
| set_zero(dev_ctx, input_grad, static_cast<T>(0)); |
There was a problem hiding this comment.
下面for循环里input_grad会有点赋不到值么,如果没有的话这里应该可以不用初始化吧
There was a problem hiding this comment.
在Functor col2im里面,计算input_grad的时候会用的是累加,所以需要先把input_grad清零。
| std::unique_ptr<framework::OpDesc> op(new framework::OpDesc()); | ||
| op->SetType("unfold_grad"); | ||
| op->SetInput(framework::GradVarName("Y"), OutputGrad("Y")); | ||
| op->SetInput("X", Input("X")); |
There was a problem hiding this comment.
需要input(X)的dims,然后加上UnfoldGradOpNoNeedBufferVarsInference声明不需要input(X)的数据,这样在计算grad时只会保留input(X)的dims而不用保留数据。
| @@ -0,0 +1,102 @@ | |||
| # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | |||
| This is for test on unfold Op | ||
| """ | ||
|
|
||
| def init_data(self): |
| dkernel_h = self.dilations[0] * (self.kernel_sizes[0] - 1) + 1 | ||
| dkernel_w = self.dilations[1] * (self.kernel_sizes[1] - 1) + 1 | ||
| out_height = (self.input_height + self.paddings[0] + self.paddings[2] - | ||
| dkernel_h) / self.strides[0] + 1 |
e658cb6
python/paddle/fluid/layers/nn.py
Outdated
|
|
||
| def unfold(x, kernel_sizes, strides=1, paddings=0, dilations=1): | ||
| """ | ||
| **unfold** |
python/paddle/fluid/layers/nn.py
Outdated
| return output | ||
|
|
||
|
|
||
| def unfold(x, kernel_sizes, strides=1, paddings=0, dilations=1): |
paddle/fluid/operators/unfold_op.cc
Outdated
|
|
||
| int dkernel_h = dilations[0] * (kernel_sizes[0] - 1) + 1; | ||
| int dkernel_w = dilations[1] * (kernel_sizes[1] - 1) + 1; | ||
| int conv_out_height = |
There was a problem hiding this comment.
conv_out_height -> out_height remove prefix conv_
paddle/fluid/operators/unfold_op.h
Outdated
| 1; | ||
| int output_width = | ||
| (input_dims[3] + paddings[1] + paddings[3] - dkernel_w) / strides[1] + | ||
| 1; |
There was a problem hiding this comment.
这些代码重复了3次,conv里有抽取一些公用的函数吧
There was a problem hiding this comment.
另外写了一个inline fucntion, 没有用conv_op.h里面的计算conv output 尺寸的函数,那里面默认为padding左右是一样的,在unfold里面padding可以设置成上下左右四个值都不一样的。
test=develop
* add unfold op test=develop * fix divide bug in python3 when calculating output width and height test=develop * add name=None in python api, move redundant code into inline function * try to trigger ci for this code test=develop
This is for unfold op (new op), which is also called im2col for batched 2D image tensor. Details are in the following description: