Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
fe45f211
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fe45f211
编写于
1月 17, 2018
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. Rename 'block_expand' to im2sequence
2. Refine code and doc
上级
09adb769
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
51 addition
and
35 deletion
+51
-35
paddle/operators/im2sequence_op.cc
paddle/operators/im2sequence_op.cc
+15
-15
paddle/operators/im2sequence_op.cu
paddle/operators/im2sequence_op.cu
+5
-5
paddle/operators/im2sequence_op.h
paddle/operators/im2sequence_op.h
+10
-6
python/paddle/v2/fluid/tests/test_im2sequence_op.py
python/paddle/v2/fluid/tests/test_im2sequence_op.py
+21
-9
未找到文件。
paddle/operators/
block_expand
_op.cc
→
paddle/operators/
im2sequence
_op.cc
浏览文件 @
fe45f211
...
@@ -12,21 +12,21 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...
@@ -12,21 +12,21 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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. */
#include "paddle/operators/
block_expand
_op.h"
#include "paddle/operators/
im2sequence
_op.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
class
BlockExpand
Op
:
public
framework
::
OperatorWithKernel
{
class
Im2Sequence
Op
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input
of BlockExpand
Op should not be null."
);
"Input
(X) of Im2Sequence
Op should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output
of BlockExpand
Op op should not be null."
);
"Output
(Out) of Im2Sequence
Op op should not be null."
);
auto
in_dim
=
ctx
->
GetInputDim
(
"X"
);
auto
in_dim
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
in_dim
.
size
(),
4
,
PADDLE_ENFORCE_EQ
(
in_dim
.
size
(),
4
,
...
@@ -55,9 +55,9 @@ class BlockExpandOp : public framework::OperatorWithKernel {
...
@@ -55,9 +55,9 @@ class BlockExpandOp : public framework::OperatorWithKernel {
}
}
};
};
class
BlockExpand
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
Im2Sequence
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
BlockExpand
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
Im2Sequence
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(Tensor)The input tensor has NCHW format."
"(Tensor)The input tensor has NCHW format."
...
@@ -65,7 +65,7 @@ class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -65,7 +65,7 @@ class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
"C: channels"
"C: channels"
"H: height"
"H: height"
"W: width"
);
"W: width"
);
AddOutput
(
"Out"
,
"(LodTensor)The output data of
block_expand
op,"
);
AddOutput
(
"Out"
,
"(LodTensor)The output data of
im2sequence
op,"
);
AddAttr
<
int
>
(
"block_height"
,
"(int)height of block."
);
AddAttr
<
int
>
(
"block_height"
,
"(int)height of block."
);
AddAttr
<
int
>
(
"block_width"
,
"(int)width of block."
);
AddAttr
<
int
>
(
"block_width"
,
"(int)width of block."
);
AddAttr
<
int
>
(
"stride_height"
,
"(int)height of stride."
);
AddAttr
<
int
>
(
"stride_height"
,
"(int)height of stride."
);
...
@@ -73,7 +73,7 @@ class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -73,7 +73,7 @@ class BlockExpandOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
int
>
(
"padding_height"
,
"(int)height of padding."
);
AddAttr
<
int
>
(
"padding_height"
,
"(int)height of padding."
);
AddAttr
<
int
>
(
"padding_width"
,
"(int)width of padding."
);
AddAttr
<
int
>
(
"padding_width"
,
"(int)width of padding."
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Expand
feature map to minibatch matrix.
Convert
feature map to minibatch matrix.
- matirx height is: output_height * output_width
- matirx height is: output_height * output_width
- matrix width is: block_height * block_width * channels
- matrix width is: block_height * block_width * channels
...
@@ -133,7 +133,7 @@ output.lod = [[0, 4, 8]]
...
@@ -133,7 +133,7 @@ output.lod = [[0, 4, 8]]
}
}
};
};
class
BlockExpand
GradOp
:
public
framework
::
OperatorWithKernel
{
class
Im2Sequence
GradOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
...
@@ -150,11 +150,11 @@ class BlockExpandGradOp : public framework::OperatorWithKernel {
...
@@ -150,11 +150,11 @@ class BlockExpandGradOp : public framework::OperatorWithKernel {
}
// namespace paddle
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
block_expand
,
ops
::
BlockExpandOp
,
ops
::
BlockExpand
OpMaker
,
REGISTER_OP
(
im2sequence
,
ops
::
Im2SequenceOp
,
ops
::
Im2Sequence
OpMaker
,
block_expand_grad
,
ops
::
BlockExpand
GradOp
);
im2sequence_grad
,
ops
::
Im2Sequence
GradOp
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
block_expand
,
im2sequence
,
ops
::
BlockExpand
Kernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
);
ops
::
Im2Sequence
Kernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
block_expand
_grad
,
im2sequence
_grad
,
ops
::
BlockExpand
GradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
);
ops
::
Im2Sequence
GradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
);
paddle/operators/
block_expand
_op.cu
→
paddle/operators/
im2sequence
_op.cu
浏览文件 @
fe45f211
...
@@ -13,13 +13,13 @@
...
@@ -13,13 +13,13 @@
limitations under the License. */
limitations under the License. */
#define EIGEN_USE_GPU
#define EIGEN_USE_GPU
#include "paddle/operators/
block_expand
_op.h"
#include "paddle/operators/
im2sequence
_op.h"
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
block_expand
,
im2sequence
,
ops
::
BlockExpand
Kernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
ops
::
Im2Sequence
Kernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
block_expand
_grad
,
im2sequence
_grad
,
ops
::
BlockExpand
GradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
ops
::
Im2Sequence
GradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
paddle/operators/
block_expand
_op.h
→
paddle/operators/
im2sequence
_op.h
浏览文件 @
fe45f211
...
@@ -14,11 +14,11 @@
...
@@ -14,11 +14,11 @@
#pragma once
#pragma once
#include "paddle/operators/math/math_function.h"
#include "paddle/framework/data_layout.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/im2col.h"
#include "paddle/operators/math/im2col.h"
#include "paddle/operators/math/math_function.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -32,13 +32,16 @@ inline int get_output_size(int img_size, int block_size, int stride,
...
@@ -32,13 +32,16 @@ inline int get_output_size(int img_size, int block_size, int stride,
}
}
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
BlockExpand
Kernel
:
public
framework
::
OpKernel
<
T
>
{
class
Im2Sequence
Kernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
Tensor
*
in
=
ctx
.
Input
<
Tensor
>
(
"X"
);
const
Tensor
*
in
=
ctx
.
Input
<
Tensor
>
(
"X"
);
LoDTensor
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
LoDTensor
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
// TODO(wanghaoshuang): Add layout checker after 'set_layout'
// being available for python API
// PADDLE_ENFORCE_EQ(in->layout(), framework::DataLayout::kNCHW,
// "Input(X) layout must be NCHW");
auto
in_dim
=
in
->
dims
();
auto
in_dim
=
in
->
dims
();
int
batch_size
=
in_dim
[
0
];
int
batch_size
=
in_dim
[
0
];
int
img_channels
=
in_dim
[
1
];
int
img_channels
=
in_dim
[
1
];
...
@@ -80,8 +83,9 @@ class BlockExpandKernel : public framework::OpKernel<T> {
...
@@ -80,8 +83,9 @@ class BlockExpandKernel : public framework::OpKernel<T> {
// set lod information
// set lod information
// TODO(wanghaoshuang): Move this to InferShape
// TODO(wanghaoshuang): Move this to InferShape
framework
::
LoD
lod
(
1
);
framework
::
LoD
lod
(
1
);
lod
[
0
].
reserve
(
batch_size
+
1
);
for
(
int
i
=
0
,
offset
=
0
;
i
<
batch_size
+
1
;
++
i
)
{
for
(
int
i
=
0
,
offset
=
0
;
i
<
batch_size
+
1
;
++
i
)
{
lod
[
0
]
.
push_back
(
offset
)
;
lod
[
0
]
[
i
]
=
offset
;
offset
+=
output_height
*
output_width
;
offset
+=
output_height
*
output_width
;
}
}
out
->
set_lod
(
lod
);
out
->
set_lod
(
lod
);
...
@@ -89,7 +93,7 @@ class BlockExpandKernel : public framework::OpKernel<T> {
...
@@ -89,7 +93,7 @@ class BlockExpandKernel : public framework::OpKernel<T> {
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
class
BlockExpand
GradKernel
:
public
framework
::
OpKernel
<
T
>
{
class
Im2Sequence
GradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
in
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
in
=
ctx
.
Input
<
Tensor
>
(
"X"
);
...
...
python/paddle/v2/fluid/tests/test_
block_expand
_op.py
→
python/paddle/v2/fluid/tests/test_
im2sequence
_op.py
浏览文件 @
fe45f211
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#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
def
get_output_shape
(
attrs
,
x
):
def
get_output_shape
(
attrs
,
in_shape
):
img_height
=
x
.
shape
[
2
]
img_height
=
in_
shape
[
2
]
img_width
=
x
.
shape
[
3
]
img_width
=
in_
shape
[
3
]
padding_height
=
attrs
[
'padding_height'
]
padding_height
=
attrs
[
'padding_height'
]
padding_width
=
attrs
[
'padding_width'
]
padding_width
=
attrs
[
'padding_width'
]
...
@@ -73,8 +86,8 @@ def im2col(attrs, im, col):
...
@@ -73,8 +86,8 @@ def im2col(attrs, im, col):
im_row_offset
][
im_col_offset
]
im_row_offset
][
im_col_offset
]
def
block_expand
(
inputs
,
attrs
):
def
Im2Sequence
(
inputs
,
attrs
):
output_height
,
output_width
=
get_output_shape
(
attrs
,
inputs
)
output_height
,
output_width
=
get_output_shape
(
attrs
,
inputs
.
shape
)
img_channels
=
inputs
.
shape
[
1
]
img_channels
=
inputs
.
shape
[
1
]
batch_size
=
inputs
.
shape
[
0
]
batch_size
=
inputs
.
shape
[
0
]
out
=
np
.
zeros
([
out
=
np
.
zeros
([
...
@@ -109,13 +122,12 @@ class TestBlockExpandOp(OpTest):
...
@@ -109,13 +122,12 @@ class TestBlockExpandOp(OpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
config
()
self
.
config
()
self
.
op_type
=
"block_expand"
self
.
op_type
=
"im2sequence"
#x = np.random.uniform(0.1, 1,
x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
x
=
np
.
random
.
randint
(
0
,
10
,
[
self
.
batch_size
,
self
.
img_channels
,
self
.
img_height
,
self
.
img_width
self
.
batch_size
,
self
.
img_channels
,
self
.
img_height
,
self
.
img_width
]).
astype
(
"float32"
)
]).
astype
(
"float32"
)
out
=
block_expand
(
x
,
self
.
attrs
)
out
=
Im2Sequence
(
x
,
self
.
attrs
)
self
.
inputs
=
{
'X'
:
x
}
self
.
inputs
=
{
'X'
:
x
}
self
.
outputs
=
{
'Out'
:
out
}
self
.
outputs
=
{
'Out'
:
out
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录