Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
505bbf36
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
505bbf36
编写于
5月 22, 2018
作者:
E
eclipsess
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add box_coder and test
上级
93800ac9
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
549 addition
and
9 deletion
+549
-9
src/operators/box_coder_op.cpp
src/operators/box_coder_op.cpp
+54
-0
src/operators/box_coder_op.h
src/operators/box_coder_op.h
+56
-0
src/operators/kernel/arm/box_coder_kernel.cpp
src/operators/kernel/arm/box_coder_kernel.cpp
+141
-0
src/operators/kernel/arm/concat_kernel.cpp
src/operators/kernel/arm/concat_kernel.cpp
+1
-2
src/operators/kernel/arm/prior_box_kernel.cpp
src/operators/kernel/arm/prior_box_kernel.cpp
+4
-7
src/operators/kernel/box_coder_kernel.h
src/operators/kernel/box_coder_kernel.h
+37
-0
src/operators/op_param.h
src/operators/op_param.h
+51
-0
test/CMakeLists.txt
test/CMakeLists.txt
+4
-0
test/operators/test_box_coder_op.cpp
test/operators/test_box_coder_op.cpp
+201
-0
未找到文件。
src/operators/box_coder_op.cpp
0 → 100644
浏览文件 @
505bbf36
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#include "operators/box_coder_op.h"
#include <vector>
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
Dtype
,
typename
T
>
void
BoxCoderOp
<
Dtype
,
T
>::
InferShape
()
const
{
auto
input_priorbox_dims
=
param_
.
InputPriorBox
()
->
dims
();
auto
input_priorboxvar_dims
=
param_
.
InputPriorBoxVar
()
->
dims
();
auto
input_targetbox_dims
=
param_
.
InputTargetBox
()
->
dims
();
auto
code_type
=
param_
.
CodeType
();
if
(
code_type
==
"encode_center_size"
)
{
if
(
input_targetbox_dims
.
size
()
!=
2
)
{
LOG
(
kLOG_ERROR
)
<<
" The rank of Input of TargetBox must be 2"
;
}
if
(
input_targetbox_dims
[
1
]
!=
4
)
{
LOG
(
kLOG_ERROR
)
<<
" The shape of TargetBox is [M, 4]"
;
}
}
if
(
code_type
==
"decode_center_size"
)
{
if
(
input_targetbox_dims
.
size
()
!=
3
)
{
LOG
(
kLOG_ERROR
)
<<
"The rank of Input of TargetBox must be 3"
;
}
if
(
input_targetbox_dims
[
1
]
!=
input_priorbox_dims
[
0
]
||
input_targetbox_dims
[
2
]
!=
input_priorbox_dims
[
1
])
{
LOG
(
kLOG_ERROR
)
<<
" dimension not match"
;
}
}
param_
.
OutputBox
()
->
Resize
(
framework
::
make_ddim
(
{
input_targetbox_dims
[
0
],
input_priorbox_dims
[
0
],
4
}));
}
template
class
BoxCoderOp
<
CPU
,
float
>;
}
// namespace operators
}
// namespace paddle_mobile
src/operators/box_coder_op.h
0 → 100644
浏览文件 @
505bbf36
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#pragma once
#include <string>
#include "framework/operator.h"
#include "operators/kernel/box_coder_kernel.h"
#include "operators/op_param.h"
namespace
paddle_mobile
{
namespace
operators
{
using
paddle_mobile
::
framework
::
Tensor
;
template
<
typename
DeviceType
,
typename
T
>
class
BoxCoderOp
:
public
framework
::
OperatorWithKernel
<
DeviceType
>
{
public:
BoxCoderOp
(
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
attrs
,
std
::
shared_ptr
<
framework
::
Scope
>
scope
)
:
framework
::
OperatorWithKernel
<
DeviceType
>
(
type
,
inputs
,
outputs
,
attrs
,
scope
),
param_
(
inputs
,
outputs
,
attrs
,
*
scope
)
{}
void
Run
()
const
{
operators
::
BoxCoderKernel
<
DeviceType
,
T
>
kernel
;
kernel
.
Compute
(
param_
);
}
using
framework
::
OperatorWithKernel
<
DeviceType
>::
OperatorWithKernel
;
void
InferShape
()
const
override
;
protected:
BoxCoderParam
param_
;
};
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/arm/box_coder_kernel.cpp
0 → 100644
浏览文件 @
505bbf36
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#pragma once
#include "operators/kernel/box_coder_kernel.h"
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
T
>
void
EncodeCenterSize
(
const
framework
::
Tensor
&
target_box
,
const
framework
::
Tensor
&
prior_box
,
const
framework
::
Tensor
&
prior_box_var
,
T
*
output
)
{
int64_t
row
=
target_box
.
dims
()[
0
];
int64_t
col
=
prior_box
.
dims
()[
0
];
int64_t
len
=
prior_box
.
dims
()[
1
];
auto
*
target_box_data
=
target_box
.
data
<
T
>
();
auto
*
prior_box_data
=
prior_box
.
data
<
T
>
();
auto
*
prior_box_var_data
=
prior_box_var
.
data
<
T
>
();
for
(
int64_t
i
=
0
;
i
<
row
;
++
i
)
{
for
(
int64_t
j
=
0
;
j
<
col
;
++
j
)
{
T
prior_box_width
=
prior_box_data
[
j
*
len
+
2
]
-
prior_box_data
[
j
*
len
];
T
prior_box_height
=
prior_box_data
[
j
*
len
+
3
]
-
prior_box_data
[
j
*
len
+
1
];
T
prior_box_center_x
=
(
prior_box_data
[
j
*
len
+
2
]
+
prior_box_data
[
j
*
len
])
/
2
;
T
prior_box_center_y
=
(
prior_box_data
[
j
*
len
+
3
]
+
prior_box_data
[
j
*
len
+
1
])
/
2
;
T
target_box_center_x
=
(
target_box_data
[
i
*
len
+
2
]
+
target_box_data
[
i
*
len
])
/
2
;
T
target_box_center_y
=
(
target_box_data
[
i
*
len
+
3
]
+
target_box_data
[
i
*
len
+
1
])
/
2
;
T
target_box_width
=
target_box_data
[
i
*
len
+
2
]
-
target_box_data
[
i
*
len
];
T
target_box_height
=
target_box_data
[
i
*
len
+
3
]
-
target_box_data
[
i
*
len
+
1
];
size_t
offset
=
i
*
col
*
len
+
j
*
len
;
output
[
offset
]
=
(
target_box_center_x
-
prior_box_center_x
)
/
prior_box_width
/
prior_box_var_data
[
j
*
len
];
output
[
offset
+
1
]
=
(
target_box_center_y
-
prior_box_center_y
)
/
prior_box_height
/
prior_box_var_data
[
j
*
len
+
1
];
output
[
offset
+
2
]
=
std
::
log
(
std
::
fabs
(
target_box_width
/
prior_box_width
))
/
prior_box_var_data
[
j
*
len
+
2
];
output
[
offset
+
3
]
=
std
::
log
(
std
::
fabs
(
target_box_height
/
prior_box_height
))
/
prior_box_var_data
[
j
*
len
+
3
];
}
}
}
template
<
typename
T
>
void
DecodeCenterSize
(
const
framework
::
Tensor
&
target_box
,
const
framework
::
Tensor
&
prior_box
,
const
framework
::
Tensor
&
prior_box_var
,
T
*
output
)
{
int64_t
row
=
target_box
.
dims
()[
0
];
int64_t
col
=
prior_box
.
dims
()[
0
];
int64_t
len
=
prior_box
.
dims
()[
1
];
auto
*
target_box_data
=
target_box
.
data
<
T
>
();
auto
*
prior_box_data
=
prior_box
.
data
<
T
>
();
auto
*
prior_box_var_data
=
prior_box_var
.
data
<
T
>
();
for
(
int64_t
i
=
0
;
i
<
row
;
++
i
)
{
for
(
int64_t
j
=
0
;
j
<
col
;
++
j
)
{
size_t
offset
=
i
*
col
*
len
+
j
*
len
;
T
prior_box_width
=
prior_box_data
[
j
*
len
+
2
]
-
prior_box_data
[
j
*
len
];
T
prior_box_height
=
prior_box_data
[
j
*
len
+
3
]
-
prior_box_data
[
j
*
len
+
1
];
T
prior_box_center_x
=
(
prior_box_data
[
j
*
len
+
2
]
+
prior_box_data
[
j
*
len
])
/
2
;
T
prior_box_center_y
=
(
prior_box_data
[
j
*
len
+
3
]
+
prior_box_data
[
j
*
len
+
1
])
/
2
;
T
target_box_center_x
=
prior_box_var_data
[
j
*
len
]
*
target_box_data
[
offset
]
*
prior_box_width
+
prior_box_center_x
;
T
target_box_center_y
=
prior_box_var_data
[
j
*
len
+
1
]
*
target_box_data
[
offset
+
1
]
*
prior_box_height
+
prior_box_center_y
;
T
target_box_width
=
std
::
exp
(
prior_box_var_data
[
j
*
len
+
2
]
*
target_box_data
[
offset
+
2
])
*
prior_box_width
;
T
target_box_height
=
std
::
exp
(
prior_box_var_data
[
j
*
len
+
3
]
*
target_box_data
[
offset
+
3
])
*
prior_box_height
;
output
[
offset
]
=
target_box_center_x
-
target_box_width
/
2
;
output
[
offset
+
1
]
=
target_box_center_y
-
target_box_height
/
2
;
output
[
offset
+
2
]
=
target_box_center_x
+
target_box_width
/
2
;
output
[
offset
+
3
]
=
target_box_center_y
+
target_box_height
/
2
;
}
}
}
template
<
>
void
BoxCoderKernel
<
CPU
,
float
>::
Compute
(
const
BoxCoderParam
&
param
)
const
{
const
auto
*
input_priorbox
=
param
.
InputPriorBox
();
const
auto
*
input_priorboxvar
=
param
.
InputPriorBoxVar
();
const
auto
*
input_targetbox
=
param
.
InputTargetBox
();
const
auto
&
code_type
=
param
.
CodeType
();
auto
row
=
input_targetbox
->
dims
()[
0
];
auto
col
=
input_priorbox
->
dims
()[
0
];
auto
len
=
input_priorbox
->
dims
()[
1
];
Tensor
*
output_box
=
param
.
OutputBox
();
auto
*
output_box_dataptr
=
output_box
->
mutable_data
<
float
>
({
row
,
col
,
len
});
if
(
code_type
==
"encode_center_size"
)
{
EncodeCenterSize
<
float
>
(
*
input_targetbox
,
*
input_priorbox
,
*
input_priorboxvar
,
output_box_dataptr
);
}
if
(
code_type
==
"decode_center_size"
)
{
DecodeCenterSize
<
float
>
(
*
input_targetbox
,
*
input_priorbox
,
*
input_priorboxvar
,
output_box_dataptr
);
}
}
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/arm/concat_kernel.cpp
浏览文件 @
505bbf36
...
...
@@ -61,7 +61,7 @@ void StridedNumelCopyWithAxis(int64_t axis, T *dst,
int64_t
src_after
=
src_stride_numel
[
axis
];
int64_t
dst_after
=
dst_stride_numel
[
axis
];
///"src and dst tensor should have the same dims size."
///
"src and dst tensor should have the same dims size."
assert
(
src_stride_numel
.
size
()
==
dst_stride_numel
.
size
());
for
(
int64_t
i
=
0
;
i
<
axis
;
++
i
)
{
...
...
@@ -79,7 +79,6 @@ void StridedNumelCopyWithAxis(int64_t axis, T *dst,
assert
(
src_stride_numel
[
i
]
==
dst_stride_numel
[
i
]);
}
}
for
(
int64_t
i
=
0
;
i
<
before
;
++
i
)
{
memory
::
Copy
(
dst
+
i
*
dst_after
,
src
+
i
*
src_after
,
sizeof
(
T
)
*
size
);
}
...
...
src/operators/kernel/arm/prior_box_kernel.cpp
浏览文件 @
505bbf36
...
...
@@ -131,12 +131,11 @@ void PriorBoxKernel<CPU, float>::Compute(const PriorBoxParam ¶m) const {
output_boxes_dataptr
,
clip_func
);
}
Tensor
var_t
;
var_t
.
mutable_data
<
float
>
(
make_ddim
({
1
,
static_cast
<
int
>
(
variances
.
size
())}));
if
((
variances
.
size
()
!=
4
))
{
LOG
(
kLOG_ERROR
)
<<
" variances.size() must be 4."
;
}
int
box_num
=
feature_height
*
feature_width
*
num_priors
;
// auto var_dim = output_variances->dims();
// output_variances->Resize({box_num, static_cast<int>(variances.size())});
int64_t
box_num
=
feature_height
*
feature_width
*
num_priors
;
for
(
int
i
=
0
;
i
<
box_num
;
i
++
)
{
output_variances_dataptr
[
4
*
i
]
=
variances
[
0
];
...
...
@@ -144,8 +143,6 @@ void PriorBoxKernel<CPU, float>::Compute(const PriorBoxParam ¶m) const {
output_variances_dataptr
[
4
*
i
+
2
]
=
variances
[
2
];
output_variances_dataptr
[
4
*
i
+
3
]
=
variances
[
3
];
}
// output_variances->Resize(var_dim);
}
}
// namespace operators
...
...
src/operators/kernel/box_coder_kernel.h
0 → 100644
浏览文件 @
505bbf36
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#include <vector>
#include "framework/operator.h"
#include "operators/math/transform.h"
#include "operators/op_param.h"
#pragma once;
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
DeviceType
,
typename
T
>
class
BoxCoderKernel
:
public
framework
::
OpKernelBase
<
DeviceType
,
BoxCoderParam
>
{
public:
void
Compute
(
const
BoxCoderParam
&
param
)
const
;
};
}
// namespace operators
}
// namespace paddle_mobile
src/operators/op_param.h
浏览文件 @
505bbf36
...
...
@@ -18,6 +18,7 @@ SOFTWARE.
#pragma once
#include <string>
#include "common/log.h"
#include "common/type_define.h"
#include "framework/lod_tensor.h"
...
...
@@ -69,6 +70,22 @@ class OpParam : PaddleMobileObject {
static
T
*
InputImageFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"Image"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
T
*
InputPriorBoxFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"PriorBox"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
T
*
InputPriorBoxVarFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"PriorBoxVar"
,
inputs
,
scope
);
}
// LoDTensor but now use Tensor
template
<
typename
T
>
static
T
*
InputTargetBoxFrom
(
const
VariableNameMap
&
inputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"TargetBox"
,
inputs
,
scope
);
}
template
<
typename
T
>
static
std
::
vector
<
T
*>
InputMultiFrom
(
const
VariableNameMap
&
inputs
,
...
...
@@ -97,6 +114,11 @@ class OpParam : PaddleMobileObject {
return
GetVarValue
<
T
>
(
"Boxes"
,
outputs
,
scope
);
}
template
<
typename
T
>
static
T
*
OutputBoxFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
return
GetVarValue
<
T
>
(
"OutputBox"
,
outputs
,
scope
);
}
template
<
typename
T
>
static
T
*
OutputVariancesFrom
(
const
VariableNameMap
&
outputs
,
const
Scope
&
scope
)
{
...
...
@@ -458,5 +480,34 @@ class PriorBoxParam : public OpParam {
float
step_h_
;
float
offset_
;
};
class
BoxCoderParam
:
public
OpParam
{
public:
BoxCoderParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
,
const
framework
::
Scope
&
scope
)
{
input_priorbox_
=
InputPriorBoxFrom
<
framework
::
Tensor
>
(
inputs
,
scope
);
input_priorboxvar_
=
InputPriorBoxVarFrom
<
framework
::
Tensor
>
(
inputs
,
scope
);
input_targetbox_
=
InputTargetBoxFrom
<
framework
::
Tensor
>
(
inputs
,
scope
);
output_box_
=
OutputBoxFrom
<
framework
::
Tensor
>
(
outputs
,
scope
);
code_type_
=
GetAttr
<
std
::
string
>
(
"code_type"
,
attrs
);
}
const
Tensor
*
InputPriorBox
()
const
{
return
input_priorbox_
;
}
const
Tensor
*
InputPriorBoxVar
()
const
{
return
input_priorboxvar_
;
}
const
Tensor
*
InputTargetBox
()
const
{
return
input_targetbox_
;
}
Tensor
*
OutputBox
()
const
{
return
output_box_
;
}
const
std
::
string
&
CodeType
()
const
{
return
code_type_
;
}
private:
Tensor
*
input_priorbox_
;
Tensor
*
input_priorboxvar_
;
Tensor
*
input_targetbox_
;
Tensor
*
output_box_
;
std
::
string
code_type_
;
};
}
// namespace operators
}
// namespace paddle_mobile
test/CMakeLists.txt
浏览文件 @
505bbf36
...
...
@@ -27,6 +27,10 @@ target_link_libraries(test-batchnorm-op paddle-mobile)
ADD_EXECUTABLE
(
test-priorbox-op operators/test_prior_box_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-priorbox-op paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-boxcoder-op operators/test_box_coder_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-boxcoder-op paddle-mobile
)
# gen test log
ADD_EXECUTABLE
(
test-log common/test_log.cpp
)
target_link_libraries
(
test-log paddle-mobile
)
...
...
test/operators/test_box_coder_op.cpp
0 → 100644
浏览文件 @
505bbf36
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#pragma once
#include "../test_include.h"
#include "operators/box_coder_op.h"
namespace
paddle_mobile
{
namespace
framework
{
template
<
typename
Dtype
>
class
TestBoxCoderOp
{
public:
explicit
TestBoxCoderOp
(
const
Program
<
Dtype
>
p
)
:
program_
(
p
)
{
if
(
use_optimize_
)
{
to_predict_program_
=
program_
.
optimizeProgram
;
}
else
{
to_predict_program_
=
program_
.
originProgram
;
}
const
std
::
vector
<
std
::
shared_ptr
<
BlockDesc
>>
blocks
=
to_predict_program_
->
Blocks
();
// DLOG << " **block size " << blocks.size();
for
(
auto
block_desc
:
blocks
)
{
std
::
vector
<
std
::
shared_ptr
<
OpDesc
>>
ops
=
block_desc
->
Ops
();
// DLOG << " ops " << ops.size();
for
(
auto
op
:
ops
)
{
if
(
op
->
Type
()
==
"box_coder"
&&
op
->
Input
(
"PriorBox"
)[
0
]
==
"concat_0.tmp_0"
)
{
DLOG
<<
" mul attr size: "
<<
op
->
GetAttrMap
().
size
();
DLOG
<<
" inputs size: "
<<
op
->
GetInputs
().
size
();
DLOG
<<
" outputs size: "
<<
op
->
GetOutputs
().
size
();
DLOG
<<
" Input PriorBox is : "
<<
op
->
Input
(
"PriorBox"
)[
0
];
DLOG
<<
" Input PriorBoxVar is : "
<<
op
->
Input
(
"PriorBoxVar"
)[
0
];
DLOG
<<
" Input TargetBox is : "
<<
op
->
Input
(
"TargetBox"
)[
0
];
DLOG
<<
" OutputBox is : "
<<
op
->
Output
(
"OutputBox"
)[
0
];
DLOG
<<
" code_type : "
<<
op
->
GetAttrMap
().
at
(
"code_type"
).
Get
<
std
::
string
>
();
std
::
shared_ptr
<
operators
::
BoxCoderOp
<
Dtype
,
float
>>
boxcoder
=
std
::
make_shared
<
operators
::
BoxCoderOp
<
Dtype
,
float
>>
(
op
->
Type
(),
op
->
GetInputs
(),
op
->
GetOutputs
(),
op
->
GetAttrMap
(),
program_
.
scope
);
ops_of_block_
[
*
block_desc
.
get
()].
push_back
(
boxcoder
);
}
}
}
}
std
::
shared_ptr
<
Tensor
>
predict_boxcoder
(
const
Tensor
&
t1
,
const
Tensor
&
t2
,
const
Tensor
&
t3
)
{
// feed
auto
scope
=
program_
.
scope
;
Variable
*
prior_box
=
scope
->
Var
(
"concat_0.tmp_0"
);
auto
tensor_x1
=
prior_box
->
GetMutable
<
Tensor
>
();
tensor_x1
->
ShareDataWith
(
t1
);
Variable
*
prior_box_var
=
scope
->
Var
(
"concat_1.tmp_0"
);
auto
tensor_x2
=
prior_box_var
->
GetMutable
<
Tensor
>
();
tensor_x2
->
ShareDataWith
(
t2
);
Variable
*
target_box
=
scope
->
Var
(
"concat_2.tmp_0"
);
auto
tensor_x3
=
target_box
->
GetMutable
<
Tensor
>
();
tensor_x3
->
ShareDataWith
(
t3
);
Variable
*
boxes_output
=
scope
->
Var
(
"box_coder_0.tmp_0"
);
auto
*
boxes_output_tensor
=
boxes_output
->
GetMutable
<
Tensor
>
();
boxes_output_tensor
->
mutable_data
<
float
>
({
1
,
1917
,
4
});
// DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims();
std
::
shared_ptr
<
Tensor
>
outbox_tensor
=
std
::
make_shared
<
LoDTensor
>
();
outbox_tensor
.
reset
(
boxes_output_tensor
);
predict_boxcoder
(
t1
,
t2
,
t3
,
0
);
return
outbox_tensor
;
}
private:
const
framework
::
Program
<
Dtype
>
program_
;
std
::
shared_ptr
<
ProgramDesc
>
to_predict_program_
;
std
::
map
<
framework
::
BlockDesc
,
std
::
vector
<
std
::
shared_ptr
<
OperatorBase
<
Dtype
>>>>
ops_of_block_
;
bool
use_optimize_
=
false
;
void
predict_boxcoder
(
const
Tensor
&
t1
,
const
Tensor
&
t2
,
const
Tensor
&
t3
,
int
block_id
)
{
std
::
shared_ptr
<
BlockDesc
>
to_predict_block
=
to_predict_program_
->
Block
(
block_id
);
for
(
int
j
=
0
;
j
<
ops_of_block_
[
*
to_predict_block
.
get
()].
size
();
++
j
)
{
auto
op
=
ops_of_block_
[
*
to_predict_block
.
get
()][
j
];
DLOG
<<
"op -> run()"
;
op
->
Run
();
}
}
};
template
class
TestBoxCoderOp
<
CPU
>;
}
// namespace framework
}
// namespace paddle_mobile
int
main
()
{
DLOG
<<
"----------**********----------"
;
DLOG
<<
"begin to run BoxCoderOp Test"
;
paddle_mobile
::
Loader
<
paddle_mobile
::
CPU
>
loader
;
auto
program
=
loader
.
Load
(
std
::
string
(
"../../test/models/mobilenet+ssd"
));
paddle_mobile
::
framework
::
Tensor
priorbox
;
SetupTensor
<
float
>
(
&
priorbox
,
{
1917
,
4
},
static_cast
<
float
>
(
0
),
static_cast
<
float
>
(
1
));
auto
*
priorbox_ptr
=
priorbox
.
data
<
float
>
();
paddle_mobile
::
framework
::
Tensor
priorboxvar
;
SetupTensor
<
float
>
(
&
priorboxvar
,
{
1917
,
4
},
static_cast
<
float
>
(
0.1
),
static_cast
<
float
>
(
0.2
));
auto
*
priorboxvar_ptr
=
priorboxvar
.
data
<
float
>
();
paddle_mobile
::
framework
::
Tensor
targetbox
;
SetupTensor
<
float
>
(
&
targetbox
,
{
1
,
1917
,
4
},
static_cast
<
float
>
(
0
),
static_cast
<
float
>
(
1
));
auto
*
targetbox_ptr
=
targetbox
.
data
<
float
>
();
paddle_mobile
::
framework
::
TestBoxCoderOp
<
paddle_mobile
::
CPU
>
testBoxCoderOp
(
program
);
auto
output_boxcoder
=
testBoxCoderOp
.
predict_boxcoder
(
priorbox
,
priorboxvar
,
targetbox
);
auto
output_boxcoder_ptr
=
output_boxcoder
->
data
<
float
>
();
for
(
int
i
=
0
;
i
<
output_boxcoder
->
numel
();
i
++
)
{
DLOG
<<
output_boxcoder_ptr
[
i
];
}
DLOGF
(
"
\n
"
);
/// testing 25th bbox.
DLOG
<<
"PriorBox**************"
;
DLOG
<<
priorbox_ptr
[
100
];
DLOG
<<
priorbox_ptr
[
101
];
DLOG
<<
priorbox_ptr
[
102
];
DLOG
<<
priorbox_ptr
[
103
];
DLOG
<<
"PriorBoxVar**************"
;
DLOG
<<
priorboxvar_ptr
[
100
];
DLOG
<<
priorboxvar_ptr
[
101
];
DLOG
<<
priorboxvar_ptr
[
102
];
DLOG
<<
priorboxvar_ptr
[
103
];
DLOG
<<
"TargetBox***************"
;
DLOG
<<
targetbox_ptr
[
100
];
DLOG
<<
targetbox_ptr
[
101
];
DLOG
<<
targetbox_ptr
[
102
];
DLOG
<<
targetbox_ptr
[
103
];
DLOG
<<
"OutputBox**************"
;
DLOG
<<
output_boxcoder_ptr
[
100
];
DLOG
<<
output_boxcoder_ptr
[
101
];
DLOG
<<
output_boxcoder_ptr
[
102
];
DLOG
<<
output_boxcoder_ptr
[
103
];
DLOG
<<
"***********----------------------**************"
;
auto
priorbox_w
=
priorbox_ptr
[
102
]
-
priorbox_ptr
[
100
];
auto
priorbox_h
=
priorbox_ptr
[
103
]
-
priorbox_ptr
[
101
];
auto
priorbox_center_x
=
(
priorbox_ptr
[
100
]
+
priorbox_ptr
[
102
])
/
2
;
auto
priorbox_center_y
=
(
priorbox_ptr
[
101
]
+
priorbox_ptr
[
103
])
/
2
;
DLOG
<<
"prior box width : "
<<
priorbox_w
;
DLOG
<<
"prior box height : "
<<
priorbox_h
;
DLOG
<<
"prior box center x : "
<<
priorbox_center_x
;
DLOG
<<
"prior box center y : "
<<
priorbox_center_y
;
auto
target_box_center_x
=
priorboxvar_ptr
[
100
]
*
targetbox_ptr
[
100
]
*
priorbox_w
+
priorbox_center_x
;
DLOG
<<
"target_box_center_x : "
<<
target_box_center_x
;
auto
target_box_center_y
=
priorboxvar_ptr
[
101
]
*
targetbox_ptr
[
101
]
*
priorbox_h
+
priorbox_center_y
;
DLOG
<<
"target_box_center_y : "
<<
target_box_center_y
;
auto
target_box_width
=
std
::
exp
(
priorboxvar_ptr
[
102
]
*
targetbox_ptr
[
102
])
*
priorbox_w
;
DLOG
<<
"target_box_width : "
<<
target_box_width
;
auto
target_box_height
=
std
::
exp
(
priorboxvar_ptr
[
103
]
*
targetbox_ptr
[
103
])
*
priorbox_h
;
DLOG
<<
"target_box_height : "
<<
target_box_height
;
DLOG
<<
"pre x min : "
<<
target_box_center_x
-
target_box_width
/
2
;
DLOG
<<
"pre y min : "
<<
target_box_center_y
-
target_box_height
/
2
;
DLOG
<<
"pre x max : "
<<
target_box_center_x
+
target_box_width
/
2
;
DLOG
<<
"pre y max : "
<<
target_box_center_y
+
target_box_height
/
2
;
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录