Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
19749d52
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看板
提交
19749d52
编写于
2月 08, 2018
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine prior_box
上级
dd6b59da
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
87 addition
and
39 deletion
+87
-39
paddle/operators/prior_box_op.cc
paddle/operators/prior_box_op.cc
+10
-10
paddle/operators/prior_box_op.h
paddle/operators/prior_box_op.h
+4
-4
python/paddle/v2/fluid/layers/nn.py
python/paddle/v2/fluid/layers/nn.py
+71
-23
python/paddle/v2/fluid/tests/test_prior_box_op.py
python/paddle/v2/fluid/tests/test_prior_box_op.py
+2
-2
未找到文件。
paddle/operators/prior_box_op.cc
浏览文件 @
19749d52
...
...
@@ -38,8 +38,8 @@ class PriorBoxOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_LT
(
input_dims
[
3
],
image_dims
[
3
],
"The width of input must smaller than image."
);
auto
min_sizes
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
in
t
>>
(
"min_sizes"
);
auto
max_sizes
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
in
t
>>
(
"max_sizes"
);
auto
min_sizes
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
floa
t
>>
(
"min_sizes"
);
auto
max_sizes
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
floa
t
>>
(
"max_sizes"
);
auto
variances
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
float
>>
(
"variances"
);
auto
aspect_ratios
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
float
>>
(
"aspect_ratios"
);
bool
flip
=
ctx
->
Attrs
().
Get
<
bool
>
(
"flip"
);
...
...
@@ -47,7 +47,7 @@ class PriorBoxOp : public framework::OperatorWithKernel {
std
::
vector
<
float
>
aspect_ratios_vec
;
ExpandAspectRatios
(
aspect_ratios
,
flip
,
aspect_ratios_vec
);
in
t
num_priors
=
aspect_ratios_vec
.
size
()
*
min_sizes
.
size
();
size_
t
num_priors
=
aspect_ratios_vec
.
size
()
*
min_sizes
.
size
();
if
(
max_sizes
.
size
()
>
0
)
{
PADDLE_ENFORCE_EQ
(
max_sizes
.
size
(),
min_sizes
.
size
(),
"The number of min_size and max_size must be equal."
);
...
...
@@ -90,20 +90,20 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
"H is the height of input, W is the width of input, num_priors "
"is the box count of each position."
);
AddAttr
<
std
::
vector
<
in
t
>>
(
"min_sizes"
,
"(vector<in
t>) List of min sizes "
"of generated prior boxes."
)
.
AddCustomChecker
([](
const
std
::
vector
<
in
t
>&
min_sizes
)
{
AddAttr
<
std
::
vector
<
floa
t
>>
(
"min_sizes"
,
"(vector<floa
t>) List of min sizes "
"of generated prior boxes."
)
.
AddCustomChecker
([](
const
std
::
vector
<
floa
t
>&
min_sizes
)
{
PADDLE_ENFORCE_GT
(
min_sizes
.
size
(),
0
,
"Size of min_sizes must be at least 1."
);
for
(
size_t
i
=
0
;
i
<
min_sizes
.
size
();
++
i
)
{
PADDLE_ENFORCE_GT
(
min_sizes
[
i
],
0
,
PADDLE_ENFORCE_GT
(
min_sizes
[
i
],
0
.0
,
"min_sizes[%d] must be positive."
,
i
);
}
});
AddAttr
<
std
::
vector
<
in
t
>>
(
AddAttr
<
std
::
vector
<
floa
t
>>
(
"max_sizes"
,
"(vector<
in
t>) List of max sizes of generated prior boxes."
);
"(vector<
floa
t>) List of max sizes of generated prior boxes."
);
AddAttr
<
std
::
vector
<
float
>>
(
"aspect_ratios"
,
"(vector<float>) List of aspect ratios of generated prior boxes."
);
...
...
paddle/operators/prior_box_op.h
浏览文件 @
19749d52
...
...
@@ -60,8 +60,8 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
auto
*
boxes
=
ctx
.
Output
<
paddle
::
framework
::
Tensor
>
(
"Boxes"
);
auto
*
vars
=
ctx
.
Output
<
paddle
::
framework
::
Tensor
>
(
"Variances"
);
auto
min_sizes
=
ctx
.
Attr
<
std
::
vector
<
in
t
>>
(
"min_sizes"
);
auto
max_sizes
=
ctx
.
Attr
<
std
::
vector
<
in
t
>>
(
"max_sizes"
);
auto
min_sizes
=
ctx
.
Attr
<
std
::
vector
<
floa
t
>>
(
"min_sizes"
);
auto
max_sizes
=
ctx
.
Attr
<
std
::
vector
<
floa
t
>>
(
"max_sizes"
);
auto
input_aspect_ratio
=
ctx
.
Attr
<
std
::
vector
<
float
>>
(
"aspect_ratios"
);
auto
variances
=
ctx
.
Attr
<
std
::
vector
<
float
>>
(
"variances"
);
auto
flip
=
ctx
.
Attr
<
bool
>
(
"flip"
);
...
...
@@ -108,7 +108,7 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
T
box_width
,
box_height
;
int
idx
=
0
;
for
(
size_t
s
=
0
;
s
<
min_sizes
.
size
();
++
s
)
{
int
min_size
=
min_sizes
[
s
];
auto
min_size
=
min_sizes
[
s
];
// first prior: aspect_ratio = 1, size = min_size
box_width
=
box_height
=
min_size
;
// xmin
...
...
@@ -124,7 +124,7 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
idx
++
;
if
(
max_sizes
.
size
()
>
0
)
{
int
max_size
=
max_sizes
[
s
];
auto
max_size
=
max_sizes
[
s
];
// second prior: aspect_ratio = 1,
// size = sqrt(min_size * max_size)
box_width
=
box_height
=
sqrt
(
min_size
*
max_size
);
...
...
python/paddle/v2/fluid/layers/nn.py
浏览文件 @
19749d52
...
...
@@ -14,13 +14,16 @@
"""
All layers just related to the neural network.
"""
import
math
from
..layer_helper
import
LayerHelper
from
..initializer
import
Normal
,
Constant
from
..framework
import
Variable
from
..param_attr
import
ParamAttr
from
layer_function_generator
import
autodoc
from
tensor
import
concat
import
math
import
numpy
as
np
from
operator
import
mul
__all__
=
[
'fc'
,
...
...
@@ -64,7 +67,10 @@ __all__ = [
'nce'
,
'beam_search'
,
'row_conv'
,
'reshape'
,
'reshape_with_axis'
,
'multiplex'
,
'prior_box'
'prior_boxes'
,
]
...
...
@@ -2996,6 +3002,40 @@ def multiplex(inputs, index):
return
out
def
reshape_with_axis
(
input
,
axis
):
"""
**ReshapeWithAxis Layer**
"""
assert
len
(
input
.
shape
)
>
axis
and
axis
>=
0
,
' '
input_shape
=
input
.
shape
new_dim
=
[
-
1
,
reduce
(
mul
,
input_shape
[
axis
:
len
(
input_shape
)],
1
)]
helper
=
LayerHelper
(
'reshape'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'reshape'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
[
out
]},
attrs
=
{
'shape'
:
new_dim
})
return
out
def
reshape
(
input
,
new_dim
):
"""
**Reshape Layer**
"""
helper
=
LayerHelper
(
'reshape'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'reshape'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
[
out
]},
attrs
=
{
'shape'
:
new_dim
})
return
out
def
prior_box
(
input
,
image
,
min_sizes
,
...
...
@@ -3041,13 +3081,13 @@ def prior_boxes(input_layers,
image
,
min_ratio
,
max_ratio
,
steps
,
aspect_ratios
,
min_dim
,
steps
=
None
,
step_w
=
None
,
step_h
=
None
,
offset
=
0.5
,
variance
=
[
0.1
],
variance
=
[
0.1
,
0.1
,
0.1
,
0.1
],
flip
=
True
,
clip
=
True
,
name
=
None
):
...
...
@@ -3059,8 +3099,8 @@ def prior_boxes(input_layers,
image = data,
min_ratio = 0.2,
max_ratio = 0.9,
steps = [8
, 16, 32, 64, 100, 300
],
aspect_ratios = [[2
], [2, 3], [2, 3], [2, 3], [2], [2
]],
steps = [8
., 16., 32., 64., 100., 300.
],
aspect_ratios = [[2
.], [2., 3.], [2., 3.], [2., 3.], [2.], [2.
]],
min_dim = 300,
offset = 0.5,
variance = [0.1],
...
...
@@ -3068,19 +3108,16 @@ def prior_boxes(input_layers,
clip=True)
"""
assert
isinstance
(
input_layers
,
list
),
'input_layer should be a list.'
assert
not
step_h
and
not
steps
,
''
assert
not
step_w
and
not
steps
,
''
num_layer
=
len
(
input_layers
)
assert
num_layer
>
2
# TODO(zcd): currently, num_layer must be bigger than two.
min_sizes
=
[]
max_sizes
=
[]
if
num_layer
>
2
:
step
=
int
(
math
.
floor
((
max_ratio
-
min_ratio
)
/
(
num_layer
-
2
)))
step
=
int
(
math
.
floor
((
(
max_ratio
-
min_ratio
)
)
/
(
num_layer
-
2
)))
for
ratio
in
xrange
(
min_ratio
,
max_ratio
+
1
,
step
):
min_sizes
.
append
(
min_dim
*
ratio
)
max_sizes
.
append
(
min_dim
*
(
ratio
+
step
))
min_sizes
.
append
(
min_dim
*
ratio
/
100.
)
max_sizes
.
append
(
min_dim
*
(
ratio
+
step
)
/
100.
)
min_sizes
=
[
min_dim
*
.
10
]
+
min_sizes
max_sizes
=
[
min_dim
*
.
20
]
+
max_sizes
...
...
@@ -3091,7 +3128,7 @@ def prior_boxes(input_layers,
assert
isinstance
(
step_w
,
list
)
and
len
(
step_w
)
==
num_layer
,
\
'step_w should be list and input_layers and step_w should have same length'
if
steps
:
assert
isinstance
(
steps
,
list
)
and
len
(
step
_w
)
==
num_layer
,
\
assert
isinstance
(
steps
,
list
)
and
len
(
step
s
)
==
num_layer
,
\
'steps should be list and input_layers and step_w should have same length'
step_w
=
steps
step_h
=
steps
...
...
@@ -3100,25 +3137,25 @@ def prior_boxes(input_layers,
'aspect_ratios should be list and input_layers and aspect_ratios should '
\
'have same length'
helper
=
LayerHelper
(
"prior_box"
,
**
locals
())
dtype
=
helper
.
input_dtype
()
box_results
=
[]
var_results
=
[]
for
i
,
input
in
enumerate
(
input_layers
):
min_size
=
min_sizes
[
i
]
max_size
=
max_sizes
[
i
]
if
isinstance
(
min_size
,
list
):
aspect_ratio
=
[]
if
not
isinstance
(
min_size
,
list
):
min_size
=
[
min_size
]
if
isinstance
(
max_size
,
list
):
if
not
isinstance
(
max_size
,
list
):
max_size
=
[
max_size
]
if
aspect_ratios
:
aspect_ratio
=
aspect_ratios
[
i
]
if
isinstance
(
aspect_ratio
,
list
):
if
not
isinstance
(
aspect_ratio
,
list
):
aspect_ratio
=
[
aspect_ratio
]
box
,
var
=
prior_box
(
input
,
image
,
min_size
,
max_size
,
aspect_ratios
,
variance
,
flip
,
clip
,
step_w
[
i
],
step_h
[
i
],
offset
)
box
,
var
=
prior_box
(
input
,
image
,
min_size
,
max_size
,
aspect_ratio
,
variance
,
flip
,
clip
,
step_w
[
i
]
if
step_w
else
[],
step_h
[
i
]
if
step_w
else
[],
offset
)
box_results
.
append
(
box
)
var_results
.
append
(
var
)
...
...
@@ -3127,18 +3164,29 @@ def prior_boxes(input_layers,
box
=
box_results
[
0
]
var
=
var_results
[
0
]
else
:
axis
=
1
axis
=
3
reshaped_boxes
=
[]
reshaped_vars
=
[]
for
i
in
range
(
len
(
box_results
)):
reshaped_boxes
+=
[
reshape_with_axis
(
box_results
[
i
],
axis
=
axis
)]
reshaped_vars
+=
[
reshape_with_axis
(
var_results
[
i
],
axis
=
axis
)]
helper
=
LayerHelper
(
"concat"
,
**
locals
())
dtype
=
helper
.
input_dtype
()
box
=
helper
.
create_tmp_variable
(
dtype
)
var
=
helper
.
create_tmp_variable
(
dtype
)
axis
=
0
helper
.
append_op
(
type
=
"concat"
,
inputs
=
{
"X"
:
box_result
s
},
inputs
=
{
"X"
:
reshaped_boxe
s
},
outputs
=
{
"Out"
:
box
},
attrs
=
{
'axis'
:
axis
})
var
=
helper
.
create_tmp_variable
(
dtype
)
helper
.
append_op
(
type
=
"concat"
,
inputs
=
{
"X"
:
var_result
s
},
inputs
=
{
"X"
:
reshaped_var
s
},
outputs
=
{
"Out"
:
var
},
attrs
=
{
'axis'
:
axis
})
...
...
python/paddle/v2/fluid/tests/test_prior_box_op.py
浏览文件 @
19749d52
...
...
@@ -65,9 +65,9 @@ class TestPriorBoxOp(OpTest):
self
.
batch_size
=
10
self
.
min_sizes
=
[
2
,
4
]
self
.
min_sizes
=
np
.
array
(
self
.
min_sizes
).
astype
(
'
int64
'
)
self
.
min_sizes
=
np
.
array
(
self
.
min_sizes
).
astype
(
'
float32
'
)
self
.
max_sizes
=
[
5
,
10
]
self
.
max_sizes
=
np
.
array
(
self
.
max_sizes
).
astype
(
'
int64
'
)
self
.
max_sizes
=
np
.
array
(
self
.
max_sizes
).
astype
(
'
float32
'
)
self
.
aspect_ratios
=
[
2.0
,
3.0
]
self
.
flip
=
True
self
.
real_aspect_ratios
=
[
1
,
2.0
,
1.0
/
2.0
,
3.0
,
1.0
/
3.0
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录