Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
自由之枫~
opencv
提交
a9d7b6ea
O
opencv
项目概览
自由之枫~
/
opencv
与 Fork 源项目一致
Fork自
OpenCV / opencv
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a9d7b6ea
编写于
10月 11, 2021
作者:
S
Smirnov Egor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix const - input and remove unimplemented function
上级
62252d15
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
52 addition
and
22 deletion
+52
-22
modules/dnn/src/onnx/onnx_importer.cpp
modules/dnn/src/onnx/onnx_importer.cpp
+45
-22
modules/dnn/test/test_onnx_importer.cpp
modules/dnn/test/test_onnx_importer.cpp
+7
-0
未找到文件。
modules/dnn/src/onnx/onnx_importer.cpp
浏览文件 @
a9d7b6ea
...
...
@@ -53,7 +53,6 @@ class ONNXImporter
Mat
getBlob
(
const
std
::
string
&
input_name
);
LayerParams
getLayerParams
(
const
opencv_onnx
::
NodeProto
&
node_proto
);
bool
isCeilMode
(
const
LayerParams
&
layerParams
);
void
addConstant
(
const
std
::
string
&
name
,
const
Mat
&
blob
);
void
addLayer
(
LayerParams
&
layerParams
,
...
...
@@ -61,6 +60,7 @@ class ONNXImporter
void
expandMid
(
const
std
::
string
&
prefix
,
opencv_onnx
::
NodeProto
&
node_proto
,
const
std
::
string
&
input
,
size_t
n
);
void
addNegation
(
const
LayerParams
&
layerParams
,
opencv_onnx
::
NodeProto
&
node_proto
,
int
input_id
);
public:
ONNXImporter
(
Net
&
net
,
const
char
*
onnxFile
)
...
...
@@ -460,6 +460,32 @@ void ONNXImporter::expandMid(const std::string& prefix, opencv_onnx::NodeProto&
}
}
/** @brief Multiply one of node_proto inputs by -1
* @param layerParams parameters of the node
* @param node_proto node which input will be replaced
* @param input_id id of input to be multiplied by -1
*/
void
ONNXImporter
::
addNegation
(
const
LayerParams
&
layerParams
,
opencv_onnx
::
NodeProto
&
node_proto
,
int
input_id
)
{
LayerParams
powerParams
;
powerParams
.
name
=
layerParams
.
name
+
"/neg"
;
powerParams
.
type
=
"Power"
;
powerParams
.
set
(
"scale"
,
-
1.
f
);
//Create Power layer
int
id
=
dstNet
.
addLayer
(
powerParams
.
name
,
powerParams
.
type
,
powerParams
);
//Connect to input
IterLayerId_t
layerId
=
layer_id
.
find
(
node_proto
.
input
(
input_id
));
CV_Assert
(
layerId
!=
layer_id
.
end
());
dstNet
.
connect
(
layerId
->
second
.
layerId
,
layerId
->
second
.
outputId
,
id
,
0
);
//Add shape
layer_id
.
insert
(
std
::
make_pair
(
powerParams
.
name
,
LayerInfo
(
id
,
0
)));
outShapes
[
powerParams
.
name
]
=
outShapes
[
node_proto
.
input
(
input_id
)];
//Replace input to Power
node_proto
.
set_input
(
input_id
,
powerParams
.
name
);
}
void
ONNXImporter
::
addConstant
(
const
std
::
string
&
name
,
const
Mat
&
blob
)
{
constBlobs
.
insert
(
std
::
make_pair
(
name
,
blob
));
...
...
@@ -918,29 +944,42 @@ void ONNXImporter::parseBias(LayerParams& layerParams, const opencv_onnx::NodePr
else
if
(
is_const_0
||
is_const_1
)
{
int
const_blob_id
=
is_const_0
?
0
:
1
;
int
input_id
=
1
-
const_blob_id
;
Mat
blob
=
getBlob
(
node_proto
,
const_blob_id
);
int
blob_total
=
blob
.
total
();
const
float
inputScale
=
isSub
&&
is_const_0
?
-
1.
f
:
1.
f
;
const
float
constScale
=
isSub
&&
is_const_1
?
-
1.
f
:
1.
f
;
if
(
blob_total
==
1
)
{
layerParams
.
type
=
"Power"
;
layerParams
.
set
(
"shift"
,
(
isSub
?
-
1
:
1
)
*
blob
.
ptr
<
float
>
()[
0
]);
layerParams
.
set
(
"scale"
,
inputScale
);
layerParams
.
set
(
"shift"
,
constScale
*
blob
.
ptr
<
float
>
()[
0
]);
}
else
{
MatShape
inpShape
=
outShapes
[
node_proto
.
input
(
1
-
const_blob
_id
)];
MatShape
inpShape
=
outShapes
[
node_proto
.
input
(
input
_id
)];
if
(
shape
(
blob
)
==
inpShape
)
{
LayerParams
constParams
;
constParams
.
name
=
layerParams
.
name
+
"/const"
;
constParams
.
type
=
"Const"
;
constParams
.
blobs
.
push_back
(
(
isSub
?
-
1
:
1
)
*
blob
);
constParams
.
blobs
.
push_back
(
blob
);
int
id
=
dstNet
.
addLayer
(
constParams
.
name
,
constParams
.
type
,
constParams
);
layer_id
.
insert
(
std
::
make_pair
(
constParams
.
name
,
LayerInfo
(
id
,
0
)));
outShapes
[
constParams
.
name
]
=
shape
(
blob
);
layerParams
.
type
=
"Eltwise"
;
float
coeffs
[]
=
{
1.
,
isSub
?
-
1.
f
:
1.
f
};
layerParams
.
set
(
"coeff"
,
DictValue
::
arrayReal
<
float
*>
(
coeffs
,
2
));
node_proto
.
set_input
(
const_blob_id
,
constParams
.
name
);
}
else
{
if
(
inputScale
<
0.
f
)
{
addNegation
(
layerParams
,
node_proto
,
input_id
);
}
layerParams
.
type
=
"Scale"
;
layerParams
.
set
(
"bias_term"
,
true
);
int
axis
=
1
;
...
...
@@ -955,7 +994,7 @@ void ONNXImporter::parseBias(LayerParams& layerParams, const opencv_onnx::NodePr
}
layerParams
.
set
(
"axis"
,
axis
);
blob
=
blob
.
reshape
(
1
,
1
);
layerParams
.
blobs
.
push_back
(
(
isSub
?
-
1
:
1
)
*
blob
);
layerParams
.
blobs
.
push_back
(
constScale
*
blob
);
}
}
}
...
...
@@ -972,23 +1011,7 @@ void ONNXImporter::parseBias(LayerParams& layerParams, const opencv_onnx::NodePr
{
if
(
isSub
)
{
LayerParams
powerParams
;
powerParams
.
name
=
layerParams
.
name
+
"/neg"
;
powerParams
.
type
=
"Power"
;
powerParams
.
set
(
"scale"
,
-
1
);
//Create Power layer
int
id
=
dstNet
.
addLayer
(
powerParams
.
name
,
powerParams
.
type
,
powerParams
);
//Connect to input
IterLayerId_t
layerId
=
layer_id
.
find
(
node_proto
.
input
(
1
));
CV_Assert
(
layerId
!=
layer_id
.
end
());
dstNet
.
connect
(
layerId
->
second
.
layerId
,
layerId
->
second
.
outputId
,
id
,
0
);
//Add shape
layer_id
.
insert
(
std
::
make_pair
(
powerParams
.
name
,
LayerInfo
(
id
,
0
)));
outShapes
[
powerParams
.
name
]
=
outShapes
[
node_proto
.
input
(
1
)];
//Replace input to Power
node_proto
.
set_input
(
1
,
powerParams
.
name
);
addNegation
(
layerParams
,
node_proto
,
1
);
}
layerParams
.
type
=
"Scale"
;
layerParams
.
set
(
"bias_term"
,
true
);
...
...
modules/dnn/test/test_onnx_importer.cpp
浏览文件 @
a9d7b6ea
...
...
@@ -931,6 +931,13 @@ TEST_P(Test_ONNX_layers, ConvResizePool1d)
testONNXModels
(
"conv_resize_pool_1d"
);
}
TEST_P
(
Test_ONNX_layers
,
SubFromConst
)
{
testONNXModels
(
"sub_from_const1"
);
testONNXModels
(
"sub_from_const_eltwise"
);
testONNXModels
(
"sub_from_const_broadcast"
);
}
INSTANTIATE_TEST_CASE_P
(
/*nothing*/
,
Test_ONNX_layers
,
dnnBackendsAndTargets
());
class
Test_ONNX_nets
:
public
Test_ONNX_layers
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录