Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenCV
opencv
提交
35ff9af6
O
opencv
项目概览
OpenCV
/
opencv
上一次同步 8 个月
通知
981
Star
71099
Fork
55580
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
35ff9af6
编写于
12月 02, 2021
作者:
A
Alexander Alekhin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21162 from rogday:softmax_simplification
上级
dad2b9aa
82941072
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
59 addition
and
11 deletion
+59
-11
modules/dnn/src/onnx/onnx_graph_simplifier.cpp
modules/dnn/src/onnx/onnx_graph_simplifier.cpp
+59
-11
未找到文件。
modules/dnn/src/onnx/onnx_graph_simplifier.cpp
浏览文件 @
35ff9af6
...
...
@@ -107,17 +107,10 @@ private:
opencv_onnx
::
GraphProto
&
net
;
};
class
SoftMaxSubgraph
:
public
Subgraph
class
SoftMaxSubgraph
Base
:
public
Subgraph
{
public:
SoftMaxSubgraph
()
:
axis
(
1
)
{
int
input
=
addNodeToMatch
(
""
);
int
inpExp
=
addNodeToMatch
(
"Exp"
,
input
);
int
sum
=
addNodeToMatch
(
"ReduceSum"
,
inpExp
);
addNodeToMatch
(
"Div"
,
inpExp
,
sum
);
setFusedNode
(
"Softmax"
,
input
);
}
SoftMaxSubgraphBase
()
:
axis
(
1
),
id
(
-
1
)
{}
virtual
bool
match
(
const
Ptr
<
ImportGraphWrapper
>&
net
,
int
nodeId
,
std
::
vector
<
int
>&
matchedNodesIds
,
...
...
@@ -125,7 +118,8 @@ public:
{
if
(
Subgraph
::
match
(
net
,
nodeId
,
matchedNodesIds
,
targetNodesIds
))
{
Ptr
<
ImportNodeWrapper
>
sum
=
net
->
getNode
(
matchedNodesIds
[
1
]);
CV_Assert
(
id
>=
0
&&
id
<
matchedNodesIds
.
size
());
Ptr
<
ImportNodeWrapper
>
sum
=
net
->
getNode
(
matchedNodesIds
[
id
]);
opencv_onnx
::
NodeProto
*
node
=
sum
.
dynamicCast
<
ONNXNodeWrapper
>
()
->
node
;
for
(
int
i
=
0
;
i
<
node
->
attribute_size
();
i
++
)
...
...
@@ -153,8 +147,60 @@ public:
attr
->
set_i
(
axis
);
}
pr
ivate
:
pr
otected
:
int
axis
;
int
id
;
};
class
SoftMaxSubgraph
:
public
SoftMaxSubgraphBase
{
public:
SoftMaxSubgraph
()
{
int
input
=
addNodeToMatch
(
""
);
int
inpExp
=
addNodeToMatch
(
"Exp"
,
input
);
int
sum
=
addNodeToMatch
(
"ReduceSum"
,
inpExp
);
id
=
1
;
addNodeToMatch
(
"Div"
,
inpExp
,
sum
);
setFusedNode
(
"Softmax"
,
input
);
}
};
class
SoftMaxSubgraph2
:
public
SoftMaxSubgraphBase
{
public:
SoftMaxSubgraph2
()
{
int
input
=
addNodeToMatch
(
""
);
int
reducemax
=
addNodeToMatch
(
"ReduceMax"
,
input
);
id
=
0
;
int
sub
=
addNodeToMatch
(
"Sub"
,
input
,
reducemax
);
int
exp
=
addNodeToMatch
(
"Exp"
,
sub
);
int
reducesum
=
addNodeToMatch
(
"ReduceSum"
,
exp
,
addNodeToMatch
(
""
));
addNodeToMatch
(
"Div"
,
exp
,
reducesum
);
setFusedNode
(
"Softmax"
,
input
);
}
};
class
LogSoftMaxSubgraph
:
public
SoftMaxSubgraphBase
{
public:
LogSoftMaxSubgraph
()
{
int
input
=
addNodeToMatch
(
""
);
int
reducemax
=
addNodeToMatch
(
"ReduceMax"
,
input
);
id
=
0
;
int
sub_1
=
addNodeToMatch
(
"Sub"
,
input
,
reducemax
);
int
exp
=
addNodeToMatch
(
"Exp"
,
sub_1
);
int
reducesum
=
addNodeToMatch
(
"ReduceSum"
,
exp
,
addNodeToMatch
(
""
));
int
log
=
addNodeToMatch
(
"Log"
,
reducesum
);
addNodeToMatch
(
"Sub"
,
sub_1
,
log
);
setFusedNode
(
"LogSoftmax"
,
input
);
}
};
class
NormalizeSubgraphBase
:
public
Subgraph
...
...
@@ -574,6 +620,8 @@ void simplifySubgraphs(opencv_onnx::GraphProto& net)
subgraphs
.
push_back
(
makePtr
<
ResizeSubgraph1
>
());
subgraphs
.
push_back
(
makePtr
<
ResizeSubgraph2
>
());
subgraphs
.
push_back
(
makePtr
<
SoftMaxSubgraph
>
());
subgraphs
.
push_back
(
makePtr
<
SoftMaxSubgraph2
>
());
subgraphs
.
push_back
(
makePtr
<
LogSoftMaxSubgraph
>
());
subgraphs
.
push_back
(
makePtr
<
NormalizeSubgraph1
>
());
subgraphs
.
push_back
(
makePtr
<
NormalizeSubgraph2
>
());
subgraphs
.
push_back
(
makePtr
<
NormalizeSubgraph2_2
>
());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录