Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenCV
opencv
提交
6674a024
O
opencv
项目概览
OpenCV
/
opencv
上一次同步 9 个月
通知
992
Star
71100
Fork
55581
代码
文件
提交
分支
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 搜索 >>
提交
6674a024
编写于
1月 07, 2018
作者:
A
Alexander Alekhin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dnn: add OPENCV_DNN_DISABLE_MEMORY_OPTIMIZATIONS runtime option
replaces REUSE_DNN_MEMORY compile-time option
上级
ee35263e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
61 addition
and
57 deletion
+61
-57
modules/dnn/CMakeLists.txt
modules/dnn/CMakeLists.txt
+0
-5
modules/dnn/src/dnn.cpp
modules/dnn/src/dnn.cpp
+61
-52
未找到文件。
modules/dnn/CMakeLists.txt
浏览文件 @
6674a024
...
...
@@ -97,8 +97,3 @@ if(BUILD_PERF_TESTS)
endif
()
endif
()
endif
()
ocv_option
(
${
the_module
}
_REUSE_MEMORY
"Enable reusing strategy of memory management"
ON
)
if
(
${
the_module
}
_REUSE_MEMORY
)
add_definitions
(
-DREUSE_DNN_MEMORY=1
)
endif
()
modules/dnn/src/dnn.cpp
浏览文件 @
6674a024
...
...
@@ -51,10 +51,15 @@
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utils/configuration.private.hpp>
namespace
cv
{
namespace
dnn
{
CV__DNN_EXPERIMENTAL_NS_BEGIN
// this option is usefull to run valgrind memory errors detection
static
bool
DNN_DISABLE_MEMORY_OPTIMIZATIONS
=
utils
::
getConfigurationParameterBool
(
"OPENCV_DNN_DISABLE_MEMORY_OPTIMIZATIONS"
,
false
);
using
std
::
vector
;
using
std
::
map
;
using
std
::
make_pair
;
...
...
@@ -369,40 +374,42 @@ public:
void
reuseOrCreate
(
const
MatShape
&
shape
,
const
LayerPin
&
lp
,
Mat
&
dst
)
{
#ifdef REUSE_DNN_MEMORY
Mat
bestBlob
;
LayerPin
bestBlobPin
;
if
(
!
DNN_DISABLE_MEMORY_OPTIMIZATIONS
)
{
Mat
bestBlob
;
LayerPin
bestBlobPin
;
std
::
map
<
LayerPin
,
Mat
>::
iterator
hostIt
;
std
::
map
<
LayerPin
,
int
>::
iterator
refIt
;
std
::
map
<
LayerPin
,
Mat
>::
iterator
hostIt
;
std
::
map
<
LayerPin
,
int
>::
iterator
refIt
;
const
int
targetTotal
=
total
(
shape
);
int
bestBlobTotal
=
INT_MAX
;
const
int
targetTotal
=
total
(
shape
);
int
bestBlobTotal
=
INT_MAX
;
for
(
hostIt
=
memHosts
.
begin
();
hostIt
!=
memHosts
.
end
();
++
hostIt
)
{
refIt
=
refCounter
.
find
(
hostIt
->
first
);
// Use only blobs that had references before because if not,
// it might be used as output.
if
(
refIt
!=
refCounter
.
end
()
&&
refIt
->
second
==
0
)
for
(
hostIt
=
memHosts
.
begin
();
hostIt
!=
memHosts
.
end
();
++
hostIt
)
{
Mat
&
unusedBlob
=
hostIt
->
second
;
if
(
unusedBlob
.
total
()
>=
targetTotal
&&
unusedBlob
.
total
()
<
bestBlobTotal
)
refIt
=
refCounter
.
find
(
hostIt
->
first
);
// Use only blobs that had references before because if not,
// it might be used as output.
if
(
refIt
!=
refCounter
.
end
()
&&
refIt
->
second
==
0
)
{
bestBlobPin
=
hostIt
->
first
;
bestBlob
=
unusedBlob
;
bestBlobTotal
=
unusedBlob
.
total
();
Mat
&
unusedBlob
=
hostIt
->
second
;
if
(
unusedBlob
.
total
()
>=
targetTotal
&&
unusedBlob
.
total
()
<
bestBlobTotal
)
{
bestBlobPin
=
hostIt
->
first
;
bestBlob
=
unusedBlob
;
bestBlobTotal
=
unusedBlob
.
total
();
}
}
}
if
(
!
bestBlob
.
empty
())
{
reuse
(
bestBlobPin
,
lp
);
dst
=
bestBlob
.
reshape
(
1
,
1
).
colRange
(
0
,
targetTotal
).
reshape
(
1
,
shape
);
return
;
}
}
if
(
!
bestBlob
.
empty
())
{
reuse
(
bestBlobPin
,
lp
);
dst
=
bestBlob
.
reshape
(
1
,
1
).
colRange
(
0
,
targetTotal
).
reshape
(
1
,
shape
);
}
else
#endif // REUSE_DNN_MEMORY
{
// if dst already has been allocated with total(shape) elements,
// it won't be recrreated and pointer of dst.data remains the same.
...
...
@@ -413,40 +420,42 @@ public:
void
reuseOrCreate
(
const
MatShape
&
shape
,
const
LayerPin
&
lp
,
UMat
&
umat_dst
)
{
#ifdef REUSE_DNN_MEMORY
UMat
bestBlob
;
LayerPin
bestBlobPin
;
if
(
!
DNN_DISABLE_MEMORY_OPTIMIZATIONS
)
{
UMat
bestBlob
;
LayerPin
bestBlobPin
;
std
::
map
<
LayerPin
,
UMat
>::
iterator
hostIt
;
std
::
map
<
LayerPin
,
int
>::
iterator
refIt
;
std
::
map
<
LayerPin
,
UMat
>::
iterator
hostIt
;
std
::
map
<
LayerPin
,
int
>::
iterator
refIt
;
const
int
targetTotal
=
total
(
shape
);
int
bestBlobTotal
=
INT_MAX
;
const
int
targetTotal
=
total
(
shape
);
int
bestBlobTotal
=
INT_MAX
;
for
(
hostIt
=
umat_memHosts
.
begin
();
hostIt
!=
umat_memHosts
.
end
();
++
hostIt
)
{
refIt
=
refCounter
.
find
(
hostIt
->
first
);
// Use only blobs that had references before because if not,
// it might be used as output.
if
(
refIt
!=
refCounter
.
end
()
&&
refIt
->
second
==
0
)
for
(
hostIt
=
umat_memHosts
.
begin
();
hostIt
!=
umat_memHosts
.
end
();
++
hostIt
)
{
UMat
&
unusedBlob
=
hostIt
->
second
;
if
(
unusedBlob
.
total
()
>=
targetTotal
&&
unusedBlob
.
total
()
<
bestBlobTotal
)
refIt
=
refCounter
.
find
(
hostIt
->
first
);
// Use only blobs that had references before because if not,
// it might be used as output.
if
(
refIt
!=
refCounter
.
end
()
&&
refIt
->
second
==
0
)
{
bestBlobPin
=
hostIt
->
first
;
bestBlob
=
unusedBlob
;
bestBlobTotal
=
unusedBlob
.
total
();
UMat
&
unusedBlob
=
hostIt
->
second
;
if
(
unusedBlob
.
total
()
>=
targetTotal
&&
unusedBlob
.
total
()
<
bestBlobTotal
)
{
bestBlobPin
=
hostIt
->
first
;
bestBlob
=
unusedBlob
;
bestBlobTotal
=
unusedBlob
.
total
();
}
}
}
if
(
!
bestBlob
.
empty
())
{
reuse
(
bestBlobPin
,
lp
);
umat_dst
.
create
(
shape
,
CV_32F
);
return
;
}
}
if
(
!
bestBlob
.
empty
())
{
reuse
(
bestBlobPin
,
lp
);
umat_dst
.
create
(
shape
,
CV_32F
);
}
else
#endif // REUSE_DNN_MEMORY
{
// if dst already has been allocated with total(shape) elements,
// it won't be recrreated and pointer of dst.data remains the same.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录