Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenCV
opencv
提交
45b5b3c1
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 搜索 >>
提交
45b5b3c1
编写于
7月 20, 2018
作者:
A
Alexander Alekhin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dnn: check layer output for NaN/Inf
上级
5336b9ad
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
141 addition
and
8 deletion
+141
-8
modules/dnn/include/opencv2/dnn/shape_utils.hpp
modules/dnn/include/opencv2/dnn/shape_utils.hpp
+20
-6
modules/dnn/src/dnn.cpp
modules/dnn/src/dnn.cpp
+121
-2
未找到文件。
modules/dnn/include/opencv2/dnn/shape_utils.hpp
浏览文件 @
45b5b3c1
...
...
@@ -44,7 +44,9 @@
#include <opencv2/core.hpp>
#include <opencv2/core/types_c.h>
#include <iostream>
#include <ostream>
#include <sstream>
namespace
cv
{
namespace
dnn
{
...
...
@@ -178,13 +180,25 @@ static inline MatShape concat(const MatShape& a, const MatShape& b)
return
c
;
}
inline
void
print
(
const
MatShape
&
shape
,
const
String
&
name
=
""
)
static
inline
std
::
string
toString
(
const
MatShape
&
shape
,
const
String
&
name
=
""
)
{
printf
(
"%s: ["
,
name
.
c_str
());
size_t
i
,
n
=
shape
.
size
();
for
(
i
=
0
;
i
<
n
;
i
++
)
printf
(
" %d"
,
shape
[
i
]);
printf
(
" ]
\n
"
);
std
::
ostringstream
ss
;
if
(
!
name
.
empty
())
ss
<<
name
<<
' '
;
ss
<<
'['
;
for
(
size_t
i
=
0
,
n
=
shape
.
size
();
i
<
n
;
++
i
)
ss
<<
' '
<<
shape
[
i
];
ss
<<
" ]"
;
return
ss
.
str
();
}
static
inline
void
print
(
const
MatShape
&
shape
,
const
String
&
name
=
""
)
{
std
::
cout
<<
toString
(
shape
,
name
)
<<
std
::
endl
;
}
static
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
MatShape
&
shape
)
{
out
<<
toString
(
shape
);
return
out
;
}
inline
int
clamp
(
int
ax
,
int
dims
)
...
...
modules/dnn/src/dnn.cpp
浏览文件 @
45b5b3c1
...
...
@@ -74,6 +74,10 @@ static int PARAM_DNN_BACKEND_DEFAULT = (int)utils::getConfigurationParameterSize
#endif
);
// Additional checks (slowdowns execution!)
static
bool
DNN_CHECK_NAN_INF
=
utils
::
getConfigurationParameterBool
(
"OPENCV_DNN_CHECK_NAN_INF"
,
false
);
static
bool
DNN_CHECK_NAN_INF_DUMP
=
utils
::
getConfigurationParameterBool
(
"OPENCV_DNN_CHECK_NAN_INF_DUMP"
,
false
);
static
bool
DNN_CHECK_NAN_INF_RAISE_ERROR
=
utils
::
getConfigurationParameterBool
(
"OPENCV_DNN_CHECK_NAN_INF_RAISE_ERROR"
,
false
);
using
std
::
vector
;
using
std
::
map
;
...
...
@@ -2053,10 +2057,75 @@ struct Net::Impl
{
if
(
preferableBackend
==
DNN_BACKEND_OPENCV
&&
IS_DNN_OPENCL_TARGET
(
preferableTarget
))
{
std
::
vector
<
UMat
>
umat_inputBlobs
=
OpenCLBackendWrapper
::
getUMatVector
(
ld
.
inputBlobsWrappers
);
std
::
vector
<
UMat
>
umat_outputBlobs
=
OpenCLBackendWrapper
::
getUMatVector
(
ld
.
outputBlobsWrappers
);
layer
->
forward
(
OpenCLBackendWrapper
::
getUMatVector
(
ld
.
inputBlobsWrappers
),
std
::
vector
<
UMat
>
umat_internalBlobs
=
OpenCLBackendWrapper
::
getUMatVector
(
ld
.
internalBlobsWrappers
);
layer
->
forward
(
umat_inputBlobs
,
umat_outputBlobs
,
OpenCLBackendWrapper
::
getUMatVector
(
ld
.
internalBlobsWrappers
));
umat_internalBlobs
);
if
(
DNN_CHECK_NAN_INF
)
{
bool
fail
=
false
;
for
(
size_t
i
=
0
;
i
<
umat_outputBlobs
.
size
();
++
i
)
{
UMat
&
u
=
umat_outputBlobs
[
i
];
Mat
m
;
if
(
u
.
depth
()
==
CV_16S
)
// FP16
convertFp16
(
u
,
m
);
else
m
=
u
.
getMat
(
ACCESS_READ
);
if
(
!
checkRange
(
m
))
{
std
::
cerr
<<
"WARNING: NaN detected in layer output: id="
<<
ld
.
id
<<
" name="
<<
layer
->
name
<<
std
::
endl
;
std
::
cerr
<<
"output id="
<<
i
<<
" output shape="
<<
shape
(
m
)
<<
std
::
endl
;
fail
=
true
;
}
else
if
(
!
checkRange
(
m
,
true
,
NULL
,
-
1e6
,
1e6
))
{
std
::
cerr
<<
"WARNING: Inf detected in layer output: id="
<<
ld
.
id
<<
" name="
<<
layer
->
name
<<
std
::
endl
;
std
::
cerr
<<
"output id="
<<
i
<<
" output shape="
<<
shape
(
m
)
<<
std
::
endl
;
fail
=
true
;
}
}
if
(
fail
)
{
for
(
size_t
i
=
0
;
i
<
umat_inputBlobs
.
size
();
++
i
)
{
UMat
&
u
=
umat_inputBlobs
[
i
];
Mat
m
;
if
(
u
.
depth
()
==
CV_16S
)
// FP16
convertFp16
(
u
,
m
);
else
m
=
u
.
getMat
(
ACCESS_READ
);
std
::
cout
<<
"INPUT "
<<
i
<<
" "
<<
cv
::
typeToString
(
u
.
type
())
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
for
(
size_t
i
=
0
;
i
<
umat_outputBlobs
.
size
();
++
i
)
{
UMat
&
u
=
umat_outputBlobs
[
i
];
Mat
m
;
if
(
u
.
depth
()
==
CV_16S
)
// FP16
convertFp16
(
u
,
m
);
else
m
=
u
.
getMat
(
ACCESS_READ
);
std
::
cout
<<
"OUTPUT "
<<
i
<<
" "
<<
cv
::
typeToString
(
u
.
type
())
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
for
(
size_t
i
=
0
;
i
<
umat_internalBlobs
.
size
();
++
i
)
{
UMat
&
u
=
umat_internalBlobs
[
i
];
Mat
m
;
if
(
u
.
depth
()
==
CV_16S
)
// FP16
convertFp16
(
u
,
m
);
else
m
=
u
.
getMat
(
ACCESS_READ
);
std
::
cout
<<
"INTERNAL "
<<
i
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
cv
::
typeToString
(
u
.
type
())
<<
" "
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
if
(
DNN_CHECK_NAN_INF_RAISE_ERROR
)
CV_Assert
(
!
fail
);
}
}
OpenCLBackendWrapper
::
update
(
ld
.
outputBlobsWrappers
,
umat_outputBlobs
);
}
else
...
...
@@ -2069,6 +2138,56 @@ struct Net::Impl
layer
->
forward
(
ld
.
inputBlobs
,
ld
.
outputBlobs
,
ld
.
internals
);
if
(
DNN_CHECK_NAN_INF
)
{
bool
fail
=
false
;
for
(
size_t
i
=
0
;
i
<
ld
.
outputBlobs
.
size
();
++
i
)
{
const
Mat
&
m
=
ld
.
outputBlobs
[
i
];
if
(
!
checkRange
(
m
))
{
std
::
cerr
<<
"WARNING: NaN detected in layer output: id="
<<
ld
.
id
<<
" name="
<<
layer
->
name
<<
std
::
endl
;
std
::
cerr
<<
"output id="
<<
i
<<
" output shape="
<<
shape
(
m
)
<<
std
::
endl
;
fail
=
true
;
}
else
if
(
!
checkRange
(
m
,
true
,
NULL
,
-
1e6
,
1e6
))
{
std
::
cerr
<<
"WARNING: Inf detected in layer output: id="
<<
ld
.
id
<<
" name="
<<
layer
->
name
<<
std
::
endl
;
std
::
cerr
<<
"output id="
<<
i
<<
" output shape="
<<
shape
(
m
)
<<
std
::
endl
;
fail
=
true
;
}
}
if
(
fail
)
{
for
(
size_t
i
=
0
;
i
<
ld
.
inputBlobs
.
size
();
++
i
)
{
const
Mat
*
pM
=
ld
.
inputBlobs
[
i
];
if
(
!
pM
)
{
std
::
cout
<<
"INPUT "
<<
i
<<
" is NULL"
<<
std
::
endl
;
continue
;
}
const
Mat
&
m
=
*
pM
;
std
::
cout
<<
"INPUT "
<<
i
<<
" "
<<
cv
::
typeToString
(
m
.
type
())
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
for
(
size_t
i
=
0
;
i
<
ld
.
outputBlobs
.
size
();
++
i
)
{
const
Mat
&
m
=
ld
.
outputBlobs
[
i
];
std
::
cout
<<
"OUTPUT "
<<
i
<<
" "
<<
cv
::
typeToString
(
m
.
type
())
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
for
(
size_t
i
=
0
;
i
<
ld
.
internals
.
size
();
++
i
)
{
const
Mat
&
m
=
ld
.
internals
[
i
];
std
::
cout
<<
"INTERNAL "
<<
i
<<
" "
<<
cv
::
typeToString
(
m
.
type
())
<<
" "
<<
shape
(
m
)
<<
std
::
endl
;
if
(
DNN_CHECK_NAN_INF_DUMP
)
std
::
cout
<<
m
.
reshape
(
1
,
1
)
<<
std
::
endl
;
}
if
(
DNN_CHECK_NAN_INF_RAISE_ERROR
)
CV_Assert
(
!
fail
);
}
}
for
(
int
i
=
0
,
n
=
ld
.
outputBlobsWrappers
.
size
();
i
<
n
;
++
i
)
{
if
(
!
ld
.
outputBlobsWrappers
[
i
].
empty
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录