Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
ff6329bd
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
692
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ff6329bd
编写于
10月 29, 2018
作者:
D
dengkaipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix some inappropriate expressions in api doc for grid_sampler. test=develop
上级
593e1b18
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
436 addition
and
409 deletion
+436
-409
paddle/fluid/operators/grid_sampler_cudnn_op.cu.cc
paddle/fluid/operators/grid_sampler_cudnn_op.cu.cc
+88
-84
paddle/fluid/operators/grid_sampler_op.cc
paddle/fluid/operators/grid_sampler_op.cc
+97
-91
paddle/fluid/operators/grid_sampler_op.h
paddle/fluid/operators/grid_sampler_op.h
+171
-164
paddle/fluid/platform/cudnn_helper.h
paddle/fluid/platform/cudnn_helper.h
+5
-5
paddle/fluid/platform/dynload/cudnn.h
paddle/fluid/platform/dynload/cudnn.h
+45
-45
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+18
-11
python/paddle/fluid/tests/unittests/test_grid_sampler_op.py
python/paddle/fluid/tests/unittests/test_grid_sampler_op.py
+10
-6
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+2
-3
未找到文件。
paddle/fluid/operators/grid_sampler_cudnn_op.cu.cc
浏览文件 @
ff6329bd
...
@@ -60,10 +60,10 @@ class CUDNNGridSampleOpKernel : public framework::OpKernel<T> {
...
@@ -60,10 +60,10 @@ class CUDNNGridSampleOpKernel : public framework::OpKernel<T> {
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
output
->
dims
()));
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
output
->
dims
()));
CUDNN_ENFORCE
(
platform
::
dynload
::
cudnnSpatialTfSamplerForward
(
CUDNN_ENFORCE
(
platform
::
dynload
::
cudnnSpatialTfSamplerForward
(
handle
,
cudnn_st_desc
,
CudnnDataType
<
T
>::
kOne
(),
cudnn_input_desc
,
input_data
,
handle
,
cudnn_st_desc
,
CudnnDataType
<
T
>::
kOne
(),
cudnn_input_desc
,
grid_data
,
CudnnDataType
<
T
>::
kZero
(),
cudnn_output_desc
,
output_data
));
input_data
,
grid_data
,
CudnnDataType
<
T
>::
kZero
(),
cudnn_output_desc
,
output_data
));
}
}
};
};
template
<
typename
T
>
template
<
typename
T
>
...
@@ -94,25 +94,29 @@ class CUDNNGridSampleGradOpKernel : public framework::OpKernel<T> {
...
@@ -94,25 +94,29 @@ class CUDNNGridSampleGradOpKernel : public framework::OpKernel<T> {
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
grid_data
=
grid
->
data
<
T
>
();
const
T
*
grid_data
=
grid
->
data
<
T
>
();
const
T
*
output_grad_data
=
output_grad
->
data
<
T
>
();
const
T
*
output_grad_data
=
output_grad
->
data
<
T
>
();
T
*
input_grad_data
=
input_grad
->
mutable_data
<
T
>
(
output_grad_dims
,
ctx
.
GetPlace
());
T
*
input_grad_data
=
T
*
grid_grad_data
=
grid_grad
->
mutable_data
<
T
>
({
n
,
h
,
w
,
2
},
ctx
.
GetPlace
());
input_grad
->
mutable_data
<
T
>
(
output_grad_dims
,
ctx
.
GetPlace
());
T
*
grid_grad_data
=
grid_grad
->
mutable_data
<
T
>
({
n
,
h
,
w
,
2
},
ctx
.
GetPlace
());
ScopedTensorDescriptor
input_desc
;
ScopedTensorDescriptor
input_desc
;
ScopedTensorDescriptor
input_grad_desc
;
ScopedTensorDescriptor
input_grad_desc
;
ScopedTensorDescriptor
output_grad_desc
;
ScopedTensorDescriptor
output_grad_desc
;
cudnnTensorDescriptor_t
cudnn_input_desc
=
input_desc
.
descriptor
<
T
>
(
cudnnTensorDescriptor_t
cudnn_input_desc
=
input_desc
.
descriptor
<
T
>
(
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
input
->
dims
()));
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
input
->
dims
()));
cudnnTensorDescriptor_t
cudnn_input_grad_desc
=
input_grad_desc
.
descriptor
<
T
>
(
cudnnTensorDescriptor_t
cudnn_input_grad_desc
=
input_grad_desc
.
descriptor
<
T
>
(
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
input_grad
->
dims
()));
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
input_grad
->
dims
()));
cudnnTensorDescriptor_t
cudnn_output_grad_desc
=
output_grad_desc
.
descriptor
<
T
>
(
cudnnTensorDescriptor_t
cudnn_output_grad_desc
=
output_grad_desc
.
descriptor
<
T
>
(
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
output_grad
->
dims
()));
DataLayout
::
kNCHW
,
framework
::
vectorize2int
(
output_grad
->
dims
()));
CUDNN_ENFORCE
(
platform
::
dynload
::
cudnnSpatialTfSamplerBackward
(
CUDNN_ENFORCE
(
platform
::
dynload
::
cudnnSpatialTfSamplerBackward
(
handle
,
cudnn_st_dest
,
CudnnDataType
<
T
>::
kOne
()
,
handle
,
cudnn_st_dest
,
CudnnDataType
<
T
>::
kOne
(),
cudnn_input_desc
,
cudnn_input_desc
,
input_data
,
CudnnDataType
<
T
>::
kZero
()
,
input_data
,
CudnnDataType
<
T
>::
kZero
(),
cudnn_input_grad_desc
,
cudnn_input_grad_desc
,
input_grad_data
,
CudnnDataType
<
T
>::
kOne
()
,
input_grad_data
,
CudnnDataType
<
T
>::
kOne
(),
cudnn_output_grad_desc
,
cudnn_output_grad_desc
,
output_grad_data
,
grid_data
,
output_grad_data
,
grid_data
,
CudnnDataType
<
T
>::
kZero
()
,
CudnnDataType
<
T
>::
kZero
(),
grid_grad_data
));
grid_grad_data
));
}
}
};
};
...
...
paddle/fluid/operators/grid_sampler_op.cc
浏览文件 @
ff6329bd
...
@@ -36,12 +36,19 @@ class GridSampleOp : public framework::OperatorWithKernel {
...
@@ -36,12 +36,19 @@ class GridSampleOp : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
grid_dims
=
ctx
->
GetInputDim
(
"Grid"
);
auto
grid_dims
=
ctx
->
GetInputDim
(
"Grid"
);
PADDLE_ENFORCE
(
x_dims
.
size
()
==
4
,
"Input(X) of GridSampleOp should be 4-D Tensor."
);
PADDLE_ENFORCE
(
x_dims
.
size
()
==
4
,
PADDLE_ENFORCE
(
grid_dims
.
size
()
==
4
,
"Input(Grid) of GridSampleOp should be 4-D Tensor."
);
"Input(X) of GridSampleOp should be 4-D Tensor."
);
PADDLE_ENFORCE
(
grid_dims
.
size
()
==
4
,
"Input(Grid) of GridSampleOp should be 4-D Tensor."
);
PADDLE_ENFORCE
(
grid_dims
[
3
]
==
2
,
"Input(Grid) dims[3] should be 2."
);
PADDLE_ENFORCE
(
grid_dims
[
3
]
==
2
,
"Input(Grid) dims[3] should be 2."
);
PADDLE_ENFORCE_EQ
(
grid_dims
[
0
],
x_dims
[
0
],
"Input(X) and Input(Grid) dims[0] should be equal."
);
PADDLE_ENFORCE_EQ
(
grid_dims
[
0
],
x_dims
[
0
],
PADDLE_ENFORCE_EQ
(
grid_dims
[
1
],
x_dims
[
2
],
"Input(X) dims[2] and Input(Grid) dims[1] should be equal."
);
"Input(X) and Input(Grid) dims[0] should be equal."
);
PADDLE_ENFORCE_EQ
(
grid_dims
[
2
],
x_dims
[
3
],
"Input(X) dims[3] and Input(Grid) dims[2] should be equal."
);
PADDLE_ENFORCE_EQ
(
grid_dims
[
1
],
x_dims
[
2
],
"Input(X) dims[2] and Input(Grid) dims[1] should be equal."
);
PADDLE_ENFORCE_EQ
(
grid_dims
[
2
],
x_dims
[
3
],
"Input(X) dims[3] and Input(Grid) dims[2] should be equal."
);
ctx
->
SetOutputDim
(
"Output"
,
x_dims
);
ctx
->
SetOutputDim
(
"Output"
,
x_dims
);
ctx
->
ShareLoD
(
"X"
,
"Output"
);
ctx
->
ShareLoD
(
"X"
,
"Output"
);
...
@@ -57,16 +64,15 @@ class GridSampleOp : public framework::OperatorWithKernel {
...
@@ -57,16 +64,15 @@ class GridSampleOp : public framework::OperatorWithKernel {
}
}
#endif
#endif
return
framework
::
OpKernelType
(
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()
),
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()),
ctx
.
GetPlace
(
),
ctx
.
GetPlace
(),
framework
::
DataLayout
::
kAnyLayout
,
library_
);
framework
::
DataLayout
::
kAnyLayout
,
library_
);
}
}
};
};
class
GridSampleOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
GridSampleOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
AddInput
(
"X"
,
"X"
,
"(Tensor) The input data of GridSampleOp, "
"(Tensor) The input data of GridSampleOp, "
"This is a 4-D tensor with shape of [N, C, H, W]"
);
"This is a 4-D tensor with shape of [N, C, H, W]"
);
AddInput
(
AddInput
(
...
@@ -74,20 +80,20 @@ class GridSampleOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -74,20 +80,20 @@ class GridSampleOpMaker : public framework::OpProtoAndCheckerMaker {
"(Tensor) The input grid of GridSampleOp generated by AffineGridOp, "
"(Tensor) The input grid of GridSampleOp generated by AffineGridOp, "
"This is a 4-D tensor with shape of [N, H, W, 2] is the concatenation "
"This is a 4-D tensor with shape of [N, H, W, 2] is the concatenation "
"of x and y coordinates with shape [N, H, W] in last dimention"
);
"of x and y coordinates with shape [N, H, W] in last dimention"
);
AddOutput
(
AddOutput
(
"Output"
,
"(Tensor) Output tensor with shape [N, C, H, W]"
);
"Output"
,
"(Tensor) Output tensor with shape [N, C, H, W]"
);
AddAttr
<
bool
>
(
AddAttr
<
bool
>
(
"use_cudnn"
,
"use_cudnn"
,
"(bool, default true) Only used in cudnn kernel, need install cudnn"
)
"(bool, default true) Only used in cudnn kernel, need install cudnn"
)
.
SetDefault
(
true
);
.
SetDefault
(
true
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
It sample input X by grid gennerate by AffineGridOp. The grid of shape
This operation samples input X by using bilinear interpolation based on
[N, H, W, 2] is the concatenation of (x, y) coordinates with shape
flow field grid, which is usually gennerated by affine_grid. The grid of
[N, H, W] each, with x indexing the 4th-D(W) of input feature map and y to
shape [N, H, W, 2] is the concatenation of (grid_x, grid_y) coordinates
indexng the 3rd-D(H), finally results is the bilinear interpolation value
with shape [N, H, W] each, where grid_x is indexing the 4th dimension
of 4 nearest corner points.
(in width dimension) of input data x and grid_y is indexng the 3rd
dimention (in height dimension), finally results is the bilinear
interpolation value of 4 nearest corner points.
Step 1:
Step 1:
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
...
@@ -154,8 +160,8 @@ class GridSampleOpGrad : public framework::OperatorWithKernel {
...
@@ -154,8 +160,8 @@ class GridSampleOpGrad : public framework::OperatorWithKernel {
}
}
#endif
#endif
return
framework
::
OpKernelType
(
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()
),
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()),
ctx
.
GetPlace
(
),
ctx
.
GetPlace
(),
framework
::
DataLayout
::
kAnyLayout
,
library_
);
framework
::
DataLayout
::
kAnyLayout
,
library_
);
}
}
};
};
...
...
paddle/fluid/operators/grid_sampler_op.h
浏览文件 @
ff6329bd
...
@@ -19,7 +19,6 @@ limitations under the License. */
...
@@ -19,7 +19,6 @@ limitations under the License. */
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/hostdevice.h"
#include "paddle/fluid/platform/hostdevice.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -31,7 +30,6 @@ using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
...
@@ -31,7 +30,6 @@ using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
using
Array3
=
Eigen
::
DSizes
<
int64_t
,
3
>
;
using
Array3
=
Eigen
::
DSizes
<
int64_t
,
3
>
;
using
Array4
=
Eigen
::
DSizes
<
int64_t
,
4
>
;
using
Array4
=
Eigen
::
DSizes
<
int64_t
,
4
>
;
template
<
typename
T
>
template
<
typename
T
>
static
inline
bool
isInBound
(
T
x
,
T
y
,
T
x_max
,
T
y_max
)
{
static
inline
bool
isInBound
(
T
x
,
T
y
,
T
x_max
,
T
y_max
)
{
if
(
x
<
0
||
x
>
x_max
||
y
<
0
||
y
>
y_max
)
{
if
(
x
<
0
||
x
>
x_max
||
y
<
0
||
y
>
y_max
)
{
...
@@ -40,16 +38,17 @@ static inline bool isInBound(T x, T y, T x_max, T y_max) {
...
@@ -40,16 +38,17 @@ static inline bool isInBound(T x, T y, T x_max, T y_max) {
return
true
;
return
true
;
}
}
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
T
>
static
void
CalcGridLocations
(
const
DeviceContext
&
ctx
,
const
Tensor
&
grid
,
static
void
CalcGridLocations
(
const
platform
::
CPUDeviceContext
&
ctx
,
Tensor
*
x_w
,
Tensor
*
x_e
,
Tensor
*
y_n
,
Tensor
*
y_s
,
const
Tensor
&
grid
,
Tensor
*
x_w
,
Tensor
*
x_e
,
Tensor
*
d_w
,
Tensor
*
d_e
,
Tensor
*
d_n
,
Tensor
*
d_s
)
{
Tensor
*
y_n
,
Tensor
*
y_s
,
Tensor
*
d_w
,
Tensor
*
d_e
,
Tensor
*
d_n
,
Tensor
*
d_s
)
{
auto
&
place
=
*
ctx
.
eigen_device
();
auto
&
place
=
*
ctx
.
eigen_device
();
const
int
n
=
grid
.
dims
()[
0
];
const
int
n
=
grid
.
dims
()[
0
];
const
int
h
=
grid
.
dims
()[
1
];
const
int
h
=
grid
.
dims
()[
1
];
const
int
w
=
grid
.
dims
()[
2
];
const
int
w
=
grid
.
dims
()[
2
];
const
T
x_max
=
static_cast
<
T
>
(
w
-
1
);
const
T
x_max
=
static_cast
<
T
>
(
w
-
1
);
const
T
y_max
=
static_cast
<
T
>
(
h
-
1
);
const
T
y_max
=
static_cast
<
T
>
(
h
-
1
);
// split grid with shape (n, h, w, 2) into (x, y) by the 3rd Dim
// split grid with shape (n, h, w, 2) into (x, y) by the 3rd Dim
Tensor
grid_x
,
grid_y
;
Tensor
grid_x
,
grid_y
;
...
@@ -117,7 +116,9 @@ static void GetGridPointValue(const Tensor& input, Tensor* output,
...
@@ -117,7 +116,9 @@ static void GetGridPointValue(const Tensor& input, Tensor* output,
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
if
(
isInBound
(
x_t
(
i
,
k
,
l
),
y_t
(
i
,
k
,
l
),
(
T
)(
w
-
1
),
(
T
)(
h
-
1
)))
{
if
(
isInBound
(
x_t
(
i
,
k
,
l
),
y_t
(
i
,
k
,
l
),
(
T
)(
w
-
1
),
(
T
)(
h
-
1
)))
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
output_t
(
i
,
j
,
k
,
l
)
=
input_t
(
i
,
j
,
(
int
)
round
(
y_t
(
i
,
k
,
l
)),
(
int
)
round
(
x_t
(
i
,
k
,
l
)));
output_t
(
i
,
j
,
k
,
l
)
=
input_t
(
i
,
j
,
static_cast
<
int
>
(
round
(
y_t
(
i
,
k
,
l
))),
static_cast
<
int
>
(
round
(
x_t
(
i
,
k
,
l
))));
}
}
}
}
}
}
...
@@ -126,9 +127,10 @@ static void GetGridPointValue(const Tensor& input, Tensor* output,
...
@@ -126,9 +127,10 @@ static void GetGridPointValue(const Tensor& input, Tensor* output,
}
}
template
<
typename
T
>
template
<
typename
T
>
static
void
GatherOutputGradToInputGrad
(
const
Tensor
&
output_grad
,
Tensor
*
input_grad
,
static
void
GatherOutputGradToInputGrad
(
const
Tensor
&
output_grad
,
const
Tensor
&
x
,
const
Tensor
&
y
,
Tensor
*
input_grad
,
const
Tensor
&
x
,
const
Tensor
&
d1
,
const
Tensor
&
d2
)
{
const
Tensor
&
y
,
const
Tensor
&
d1
,
const
Tensor
&
d2
)
{
const
int
n
=
output_grad
.
dims
()[
0
];
const
int
n
=
output_grad
.
dims
()[
0
];
const
int
c
=
output_grad
.
dims
()[
1
];
const
int
c
=
output_grad
.
dims
()[
1
];
const
int
h
=
output_grad
.
dims
()[
2
];
const
int
h
=
output_grad
.
dims
()[
2
];
...
@@ -143,10 +145,11 @@ static void GatherOutputGradToInputGrad(const Tensor& output_grad, Tensor* input
...
@@ -143,10 +145,11 @@ static void GatherOutputGradToInputGrad(const Tensor& output_grad, Tensor* input
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
k
=
0
;
k
<
h
;
k
++
)
{
for
(
int
k
=
0
;
k
<
h
;
k
++
)
{
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
if
(
isInBound
(
x_t
(
i
,
k
,
l
),
y_t
(
i
,
k
,
l
),
(
T
)(
w
-
1
),
(
T
)(
h
-
1
)))
{
if
(
isInBound
(
x_t
(
i
,
k
,
l
),
y_t
(
i
,
k
,
l
),
(
T
)(
w
-
1
),
(
T
)(
h
-
1
)))
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
input_grad_t
(
i
,
j
,
(
int
)
y_t
(
i
,
k
,
l
),
(
int
)
x_t
(
i
,
k
,
l
))
+=
input_grad_t
(
i
,
j
,
static_cast
<
int
>
(
round
(
y_t
(
i
,
k
,
l
))),
output_grad_t
(
i
,
j
,
k
,
l
)
*
d1_t
(
i
,
k
,
l
)
*
d2_t
(
i
,
k
,
l
);
static_cast
<
int
>
(
round
(
x_t
(
i
,
k
,
l
))))
+=
output_grad_t
(
i
,
j
,
k
,
l
)
*
d1_t
(
i
,
k
,
l
)
*
d2_t
(
i
,
k
,
l
);
}
}
}
}
}
}
...
@@ -154,8 +157,6 @@ static void GatherOutputGradToInputGrad(const Tensor& output_grad, Tensor* input
...
@@ -154,8 +157,6 @@ static void GatherOutputGradToInputGrad(const Tensor& output_grad, Tensor* input
}
}
}
}
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
GridSampleOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
GridSampleOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
...
@@ -172,10 +173,9 @@ class GridSampleOpKernel : public framework::OpKernel<T> {
...
@@ -172,10 +173,9 @@ class GridSampleOpKernel : public framework::OpKernel<T> {
// calc locations and distances of 4 corner points
// calc locations and distances of 4 corner points
Tensor
x_w
,
x_e
,
y_n
,
y_s
;
Tensor
x_w
,
x_e
,
y_n
,
y_s
;
Tensor
d_w
,
d_e
,
d_n
,
d_s
;
Tensor
d_w
,
d_e
,
d_n
,
d_s
;
CalcGridLocations
<
DeviceContext
,
T
>
(
ctx
.
template
device_context
<
DeviceContext
>(),
CalcGridLocations
<
T
>
(
*
grid
,
ctx
.
template
device_context
<
platform
::
CPUDeviceContext
>(),
*
grid
,
&
x_w
,
&
x_w
,
&
x_e
,
&
y_n
,
&
y_s
,
&
x_e
,
&
y_n
,
&
y_s
,
&
d_w
,
&
d_e
,
&
d_n
,
&
d_s
);
&
d_w
,
&
d_e
,
&
d_n
,
&
d_s
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Output"
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Output"
);
output
->
mutable_data
<
T
>
({
n
,
c
,
h
,
w
},
ctx
.
GetPlace
());
output
->
mutable_data
<
T
>
({
n
,
c
,
h
,
w
},
ctx
.
GetPlace
());
...
@@ -198,22 +198,25 @@ class GridSampleOpKernel : public framework::OpKernel<T> {
...
@@ -198,22 +198,25 @@ class GridSampleOpKernel : public framework::OpKernel<T> {
auto
d_e_t
=
EigenTensor
<
T
,
3
>::
From
(
d_e
);
auto
d_e_t
=
EigenTensor
<
T
,
3
>::
From
(
d_e
);
auto
d_n_t
=
EigenTensor
<
T
,
3
>::
From
(
d_n
);
auto
d_n_t
=
EigenTensor
<
T
,
3
>::
From
(
d_n
);
auto
d_s_t
=
EigenTensor
<
T
,
3
>::
From
(
d_s
);
auto
d_s_t
=
EigenTensor
<
T
,
3
>::
From
(
d_s
);
auto
d_w_scaled_t
=
d_w_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
d_w_scaled_t
=
auto
d_e_scaled_t
=
d_e_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
d_w_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
d_n_scaled_t
=
d_n_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
d_e_scaled_t
=
auto
d_s_scaled_t
=
d_s_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
d_e_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
d_n_scaled_t
=
d_n_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
d_s_scaled_t
=
d_s_t
.
reshape
(
Array4
(
n
,
1
,
h
,
w
)).
broadcast
(
Array4
(
1
,
c
,
1
,
1
));
auto
v_wn_t
=
EigenTensor
<
T
,
4
>::
From
(
v_wn
);
auto
v_wn_t
=
EigenTensor
<
T
,
4
>::
From
(
v_wn
);
auto
v_en_t
=
EigenTensor
<
T
,
4
>::
From
(
v_en
);
auto
v_en_t
=
EigenTensor
<
T
,
4
>::
From
(
v_en
);
auto
v_ws_t
=
EigenTensor
<
T
,
4
>::
From
(
v_ws
);
auto
v_ws_t
=
EigenTensor
<
T
,
4
>::
From
(
v_ws
);
auto
v_es_t
=
EigenTensor
<
T
,
4
>::
From
(
v_es
);
auto
v_es_t
=
EigenTensor
<
T
,
4
>::
From
(
v_es
);
auto
output_t
=
EigenTensor
<
T
,
4
>::
From
(
*
output
);
auto
output_t
=
EigenTensor
<
T
,
4
>::
From
(
*
output
);
//
bilinear interpolaetion by 4 corner points
//
bilinear interpolaetion by 4 corner points
output_t
.
device
(
place
)
=
v_wn_t
*
d_e_scaled_t
*
d_s_scaled_t
output_t
.
device
(
place
)
=
v_wn_t
*
d_e_scaled_t
*
d_s_scaled_t
+
+
v_en_t
*
d_w_scaled_t
*
d_s_scaled_t
v_en_t
*
d_w_scaled_t
*
d_s_scaled_t
+
+
v_ws_t
*
d_e_scaled_t
*
d_n_scaled_t
v_ws_t
*
d_e_scaled_t
*
d_n_scaled_t
+
+
v_es_t
*
d_w_scaled_t
*
d_n_scaled_t
;
v_es_t
*
d_w_scaled_t
*
d_n_scaled_t
;
}
}
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
...
@@ -242,16 +245,19 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
...
@@ -242,16 +245,19 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
Tensor
x_w
,
x_e
,
y_n
,
y_s
;
Tensor
x_w
,
x_e
,
y_n
,
y_s
;
Tensor
d_w
,
d_e
,
d_n
,
d_s
;
Tensor
d_w
,
d_e
,
d_n
,
d_s
;
CalcGridLocations
<
DeviceContext
,
T
>
(
ctx
.
template
device_context
<
DeviceContext
>(),
CalcGridLocations
<
T
>
(
*
grid
,
ctx
.
template
device_context
<
platform
::
CPUDeviceContext
>(),
*
grid
,
&
x_w
,
&
x_w
,
&
x_e
,
&
y_n
,
&
y_s
,
&
x_e
,
&
y_n
,
&
y_s
,
&
d_w
,
&
d_e
,
&
d_n
,
&
d_s
);
&
d_w
,
&
d_e
,
&
d_n
,
&
d_s
);
// gather output grad value to input grad by corner point coords and weight
// gather output grad value to input grad by corner point coords and weight
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_w
,
y_n
,
d_e
,
d_s
);
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_w
,
y_n
,
d_e
,
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_w
,
y_s
,
d_e
,
d_n
);
d_s
);
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_e
,
y_n
,
d_w
,
d_s
);
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_w
,
y_s
,
d_e
,
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_e
,
y_s
,
d_w
,
d_n
);
d_n
);
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_e
,
y_n
,
d_w
,
d_s
);
GatherOutputGradToInputGrad
<
T
>
(
*
output_grad
,
input_grad
,
x_e
,
y_s
,
d_w
,
d_n
);
// calc 4 corner points value
// calc 4 corner points value
Tensor
v_wn
,
v_en
,
v_ws
,
v_es
;
Tensor
v_wn
,
v_en
,
v_ws
,
v_es
;
...
@@ -281,15 +287,17 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
...
@@ -281,15 +287,17 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
auto
grid_grad_x_t
=
EigenTensor
<
T
,
3
>::
From
(
grid_grad_x
).
setConstant
(
0.0
);
auto
grid_grad_x_t
=
EigenTensor
<
T
,
3
>::
From
(
grid_grad_x
).
setConstant
(
0.0
);
auto
grid_grad_y_t
=
EigenTensor
<
T
,
3
>::
From
(
grid_grad_y
).
setConstant
(
0.0
);
auto
grid_grad_y_t
=
EigenTensor
<
T
,
3
>::
From
(
grid_grad_y
).
setConstant
(
0.0
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
for
(
int
j
=
0
;
j
<
c
;
j
++
)
{
for
(
int
k
=
0
;
k
<
h
;
k
++
)
{
for
(
int
k
=
0
;
k
<
h
;
k
++
)
{
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
for
(
int
l
=
0
;
l
<
w
;
l
++
)
{
grid_grad_x_t
(
i
,
k
,
l
)
+=
((
v_en_t
(
i
,
j
,
k
,
l
)
-
v_wn_t
(
i
,
j
,
k
,
l
))
*
d_s_t
(
i
,
k
,
l
)
grid_grad_x_t
(
i
,
k
,
l
)
+=
+
(
v_es_t
(
i
,
j
,
k
,
l
)
-
v_ws_t
(
i
,
j
,
k
,
l
))
*
d_n_t
(
i
,
k
,
l
))
((
v_en_t
(
i
,
j
,
k
,
l
)
-
v_wn_t
(
i
,
j
,
k
,
l
))
*
d_s_t
(
i
,
k
,
l
)
+
*
output_grad_t
(
i
,
j
,
k
,
l
);
(
v_es_t
(
i
,
j
,
k
,
l
)
-
v_ws_t
(
i
,
j
,
k
,
l
))
*
d_n_t
(
i
,
k
,
l
))
*
grid_grad_y_t
(
i
,
k
,
l
)
+=
((
v_ws_t
(
i
,
j
,
k
,
l
)
-
v_wn_t
(
i
,
j
,
k
,
l
))
*
d_e_t
(
i
,
k
,
l
)
output_grad_t
(
i
,
j
,
k
,
l
);
+
(
v_es_t
(
i
,
j
,
k
,
l
)
-
v_en_t
(
i
,
j
,
k
,
l
))
*
d_w_t
(
i
,
k
,
l
))
grid_grad_y_t
(
i
,
k
,
l
)
+=
*
output_grad_t
(
i
,
j
,
k
,
l
);
((
v_ws_t
(
i
,
j
,
k
,
l
)
-
v_wn_t
(
i
,
j
,
k
,
l
))
*
d_e_t
(
i
,
k
,
l
)
+
(
v_es_t
(
i
,
j
,
k
,
l
)
-
v_en_t
(
i
,
j
,
k
,
l
))
*
d_w_t
(
i
,
k
,
l
))
*
output_grad_t
(
i
,
j
,
k
,
l
);
}
}
}
}
}
}
...
@@ -308,7 +316,6 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
...
@@ -308,7 +316,6 @@ class GridSampleGradOpKernel : public framework::OpKernel<T> {
grid_grad_data
[
2
*
i
+
1
]
=
grid_grad_y_data
[
i
];
grid_grad_data
[
2
*
i
+
1
]
=
grid_grad_y_data
[
i
];
}
}
}
}
};
};
}
// namespace operators
}
// namespace operators
...
...
paddle/fluid/platform/cudnn_helper.h
浏览文件 @
ff6329bd
paddle/fluid/platform/dynload/cudnn.h
浏览文件 @
ff6329bd
...
@@ -92,7 +92,7 @@ extern void EnforceCUDNNLoaded(const char* fn_name);
...
@@ -92,7 +92,7 @@ extern void EnforceCUDNNLoaded(const char* fn_name);
__macro(cudnnDeriveBNTensorDescriptor); \
__macro(cudnnDeriveBNTensorDescriptor); \
__macro(cudnnCreateSpatialTransformerDescriptor); \
__macro(cudnnCreateSpatialTransformerDescriptor); \
__macro(cudnnSetSpatialTransformerNdDescriptor); \
__macro(cudnnSetSpatialTransformerNdDescriptor); \
__macro(cudnnDestroySpatialTransformerDescriptor);\
__macro(cudnnDestroySpatialTransformerDescriptor);
\
__macro(cudnnSpatialTfGridGeneratorForward); \
__macro(cudnnSpatialTfGridGeneratorForward); \
__macro(cudnnSpatialTfGridGeneratorBackward); \
__macro(cudnnSpatialTfGridGeneratorBackward); \
__macro(cudnnSpatialTfSamplerForward); \
__macro(cudnnSpatialTfSamplerForward); \
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
ff6329bd
...
@@ -7586,11 +7586,13 @@ def hash(input, hash_size, num_hash=1, name=None):
...
@@ -7586,11 +7586,13 @@ def hash(input, hash_size, num_hash=1, name=None):
@
templatedoc
()
@
templatedoc
()
def
grid_sampler
(
x
,
grid
,
name
=
None
):
def
grid_sampler
(
x
,
grid
,
name
=
None
):
"""
"""
It sample input X by grid gennerate by AffineGridOp. The grid of shape
This operation samples input X by using bilinear interpolation based on
[N, H, W, 2] is the concatenation of (x, y) coordinates with shape
flow field grid, which is usually gennerated by affine_grid. The grid of
[N, H, W] each, with x indexing the 4th-D(W) of input feature map and y to
shape [N, H, W, 2] is the concatenation of (grid_x, grid_y) coordinates
indexng the 3rd-D(H), finally results is the bilinear interpolation value
with shape [N, H, W] each, where grid_x is indexing the 4th dimension
of 4 nearest corner points.
(in width dimension) of input data x and grid_y is indexng the 3rd
dimention (in height dimension), finally results is the bilinear
interpolation value of 4 nearest corner points.
Step 1:
Step 1:
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
Get (x, y) grid coordinates and scale to [0, H-1/W-1].
...
@@ -7636,7 +7638,16 @@ def grid_sampler(x, grid, name=None):
...
@@ -7636,7 +7638,16 @@ def grid_sampler(x, grid, name=None):
name (str, default None): The name of this layer.
name (str, default None): The name of this layer.
Returns:
Returns:
out(Variable): Output data indices by grid from x of shape [N, C, H, W].
out(Variable): Output of shape [N, C, H, W] data samples input X
using bilnear interpolation based on input grid.
Exmples:
.. code-block:: python
x = fluid.layers.data(name='x', shape=[3, 10, 32, 32], dtype='float32')
theta = fluid.layers.data(name='theta', shape=[3, 2, 3], dtype='float32')
grid = fluid.layers.affine_grid(input=theta, size=[3, 10, 32, 32]})
out = fluid.layers.grid_sampler(x=x, grid=grid)
"""
"""
helper
=
LayerHelper
(
"grid_sampler"
,
**
locals
())
helper
=
LayerHelper
(
"grid_sampler"
,
**
locals
())
...
@@ -7649,10 +7660,6 @@ def grid_sampler(x, grid, name=None):
...
@@ -7649,10 +7660,6 @@ def grid_sampler(x, grid, name=None):
out
=
helper
.
create_tmp_variable
(
x
.
dtype
)
out
=
helper
.
create_tmp_variable
(
x
.
dtype
)
ipts
=
{
'X'
:
x
,
'Grid'
:
grid
}
ipts
=
{
'X'
:
x
,
'Grid'
:
grid
}
helper
.
apppend_op
(
helper
.
apppend_op
(
type
=
'grid_sampler'
,
inputs
=
ipts
,
outputs
=
{
'Output'
,
out
})
type
=
'grid_sampler'
,
inputs
=
ipts
,
outputs
=
{
'Output'
,
out
})
return
out
return
out
python/paddle/fluid/tests/unittests/test_grid_sampler_op.py
浏览文件 @
ff6329bd
...
@@ -12,7 +12,6 @@
...
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
from
op_test
import
OpTest
from
op_test
import
OpTest
...
@@ -37,6 +36,7 @@ def AffineGrid(theta, size):
...
@@ -37,6 +36,7 @@ def AffineGrid(theta, size):
return
ret
.
reshape
([
n
,
h
,
w
,
2
]).
astype
(
"float32"
)
return
ret
.
reshape
([
n
,
h
,
w
,
2
]).
astype
(
"float32"
)
def
getGridPointValue
(
data
,
x
,
y
):
def
getGridPointValue
(
data
,
x
,
y
):
data_shape
=
data
.
shape
data_shape
=
data
.
shape
N
=
data_shape
[
0
]
N
=
data_shape
[
0
]
...
@@ -47,13 +47,15 @@ def getGridPointValue(data, x, y):
...
@@ -47,13 +47,15 @@ def getGridPointValue(data, x, y):
for
i
in
range
(
N
):
for
i
in
range
(
N
):
for
j
in
range
(
H
):
for
j
in
range
(
H
):
for
k
in
range
(
W
):
for
k
in
range
(
W
):
if
y
[
i
,
j
,
k
]
<
0
or
y
[
i
,
j
,
k
]
>
H
-
1
or
x
[
i
,
j
,
k
]
<
0
or
x
[
i
,
j
,
k
]
>
W
-
1
:
if
y
[
i
,
j
,
k
]
<
0
or
y
[
i
,
j
,
k
]
>
H
-
1
or
x
[
i
,
j
,
k
]
<
0
or
x
[
i
,
j
,
k
]
>
W
-
1
:
out
[
i
,
:,
j
,
k
]
=
0
out
[
i
,
:,
j
,
k
]
=
0
else
:
else
:
out
[
i
,
:,
j
,
k
]
=
data
[
i
,
:,
y
[
i
,
j
,
k
],
x
[
i
,
j
,
k
]]
out
[
i
,
:,
j
,
k
]
=
data
[
i
,
:,
y
[
i
,
j
,
k
],
x
[
i
,
j
,
k
]]
return
out
return
out
def
GridSampler
(
data
,
grid
):
def
GridSampler
(
data
,
grid
):
dims
=
data
.
shape
dims
=
data
.
shape
N
=
dims
[
0
]
N
=
dims
[
0
]
...
@@ -87,6 +89,7 @@ def GridSampler(data, grid):
...
@@ -87,6 +89,7 @@ def GridSampler(data, grid):
out
=
(
wa
*
va
+
wb
*
vb
+
wc
*
vc
+
wd
*
vd
).
astype
(
'float32'
)
out
=
(
wa
*
va
+
wb
*
vb
+
wc
*
vc
+
wd
*
vd
).
astype
(
'float32'
)
return
out
return
out
class
TestGridSamplerOp
(
OpTest
):
class
TestGridSamplerOp
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
initTestCase
()
self
.
initTestCase
()
...
@@ -115,5 +118,6 @@ class TestGridSamplerOp(OpTest):
...
@@ -115,5 +118,6 @@ class TestGridSamplerOp(OpTest):
self
.
grid_shape
=
(
2
,
7
,
3
,
2
)
self
.
grid_shape
=
(
2
,
7
,
3
,
2
)
self
.
theta_shape
=
(
2
,
2
,
3
)
self
.
theta_shape
=
(
2
,
2
,
3
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
ff6329bd
...
@@ -868,13 +868,12 @@ class TestBook(unittest.TestCase):
...
@@ -868,13 +868,12 @@ class TestBook(unittest.TestCase):
def
test_affine_grid_gen
(
self
):
def
test_affine_grid_gen
(
self
):
program
=
Program
()
program
=
Program
()
with
program_guard
(
program
):
with
program_guard
(
program
):
x
=
layers
.
data
(
name
=
'x'
,
shape
=
[
2
,
5
,
7
,
3
],
dtype
=
'float32'
)
x
=
layers
.
data
(
name
=
'x'
,
shape
=
[
2
,
5
,
7
,
3
],
dtype
=
'float32'
)
grid
=
layers
.
data
(
name
=
'grid'
,
shape
=
[
2
,
5
,
7
,
2
],
dtype
=
'float32'
)
grid
=
layers
.
data
(
name
=
'grid'
,
shape
=
[
2
,
5
,
7
,
2
],
dtype
=
'float32'
)
out
=
layers
.
grid_sampler
(
x
,
grid
)
out
=
layers
.
grid_sampler
(
x
,
grid
)
self
.
assertIsNotNone
(
out
)
self
.
assertIsNotNone
(
out
)
print
(
str
(
program
))
print
(
str
(
program
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录