Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Opencv
提交
2aeb32d2
O
Opencv
项目概览
Greenplum
/
Opencv
大约 1 年 前同步成功
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
Opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2aeb32d2
编写于
3月 22, 2020
作者:
Y
YashasSamaga
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix segfaults, support bias in untrainable mode, support batches in untrainable mode
上级
4cdb4652
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
113 addition
and
30 deletion
+113
-30
modules/dnn/src/cuda4dnn/primitives/scale_shift.hpp
modules/dnn/src/cuda4dnn/primitives/scale_shift.hpp
+70
-22
modules/dnn/src/layers/scale_layer.cpp
modules/dnn/src/layers/scale_layer.cpp
+41
-6
modules/dnn/test/test_darknet_importer.cpp
modules/dnn/test/test_darknet_importer.cpp
+2
-2
未找到文件。
modules/dnn/src/cuda4dnn/primitives/scale_shift.hpp
浏览文件 @
2aeb32d2
...
...
@@ -19,25 +19,51 @@
namespace
cv
{
namespace
dnn
{
namespace
cuda4dnn
{
struct
ScaleShiftConfiguration
{
enum
class
OpMode
{
NONE
,
TRAINABLE
,
/* use a pretrained blob */
UNTRAINABLE
/* use another input */
};
OpMode
scaleMode
;
OpMode
shiftMode
;
std
::
size_t
axis
;
};
template
<
class
T
>
class
ScaleShiftOp
final
:
public
CUDABackendNode
{
public:
using
wrapper_type
=
GetCUDABackendWrapperType
<
T
>
;
ScaleShiftOp
(
csl
::
Stream
stream_
,
std
::
size_t
axis
,
const
cv
::
Mat
&
weights
,
const
cv
::
Mat
&
bias
)
:
stream
(
std
::
move
(
stream_
)),
axis
{
axis
}
ScaleShiftOp
(
csl
::
Stream
stream_
,
const
ScaleShiftConfiguration
&
config
,
const
cv
::
Mat
&
weights
,
const
cv
::
Mat
&
bias
)
:
stream
(
std
::
move
(
stream_
)),
axis
{
config
.
axis
}
{
if
(
!
weights
.
empty
())
scaleMode
=
config
.
scaleMode
;
if
(
scaleMode
==
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
)
{
CV_Assert
(
!
weights
.
empty
());
weightsTensor
=
csl
::
makeTensorHeader
<
T
>
(
weights
);
csl
::
copyMatToTensor
<
T
>
(
weights
,
weightsTensor
,
stream
);
}
if
(
!
bias
.
empty
())
shiftMode
=
config
.
shiftMode
;
if
(
shiftMode
==
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
)
{
CV_Assert
(
!
bias
.
empty
());
biasTensor
=
csl
::
makeTensorHeader
<
T
>
(
bias
);
csl
::
copyMatToTensor
<
T
>
(
bias
,
biasTensor
,
stream
);
}
CV_Assert
(
scaleMode
!=
ScaleShiftConfiguration
::
OpMode
::
NONE
||
shiftMode
!=
ScaleShiftConfiguration
::
OpMode
::
NONE
);
if
(
scaleMode
==
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
&&
shiftMode
==
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
)
{
CV_Error
(
cv
::
Error
::
StsNotImplemented
,
"scale and shift both in untrainable mode is not supported"
);
}
}
void
forward
(
...
...
@@ -53,40 +79,60 @@ namespace cv { namespace dnn { namespace cuda4dnn {
auto
output_wrapper
=
outputs
[
0
].
dynamicCast
<
wrapper_type
>
();
auto
output
=
output_wrapper
->
getSpan
();
/* number of batches in the weights/bias
* trainable mode: same for all batches
* untrainable mode: could be different for different batch samples
*/
std
::
size_t
parameter_batch_size
=
1
;
csl
::
TensorView
<
T
>
weights
;
if
(
weightsTensor
.
empty
()
&&
biasTensor
.
empty
())
if
(
scaleMode
==
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
)
{
CV_Assert
(
!
weightsTensor
.
empty
());
weights
=
csl
::
TensorView
<
T
>
(
weightsTensor
);
}
else
if
(
scaleMode
==
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
)
{
CV_Assert
(
inputs
.
size
()
==
2
);
/* no explicit scale/shift values provided; use the second input as weights */
auto
wrapper
=
inputs
[
1
].
dynamicCast
<
wrapper_type
>
();
weights
=
wrapper
->
getView
();
parameter_batch_size
=
weights
.
get_axis_size
(
0
);
CV_Assert
(
parameter_batch_size
==
input
.
get_axis_size
(
0
));
}
else
if
(
!
weightsTensor
.
empty
())
csl
::
TensorView
<
T
>
bias
;
if
(
shiftMode
==
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
)
{
weights
=
csl
::
TensorSpan
<
T
>
(
weightsTensor
);
CV_Assert
(
!
biasTensor
.
empty
());
bias
=
csl
::
TensorView
<
T
>
(
biasTensor
);
}
else
if
(
shiftMode
==
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
)
{
CV_Assert
(
inputs
.
size
()
==
2
);
auto
wrapper
=
inputs
[
1
].
dynamicCast
<
wrapper_type
>
();
bias
=
wrapper
->
getView
();
csl
::
TensorView
<
T
>
bias
;
if
(
!
biasTensor
.
empty
())
bias
=
csl
::
TensorSpan
<
T
>
(
biasTensor
);
parameter_batch_size
=
bias
.
get_axis_size
(
0
)
;
CV_Assert
(
parameter_batch_size
==
input
.
get_axis_size
(
0
));
}
const
auto
numParams
=
!
weights
.
empty
()
?
weights
.
size
()
:
bias
.
size
();
CV_Assert
(
numParams
!=
0
);
if
(
!
weightsTensor
.
empty
()
&&
!
biasTensor
.
empty
())
CV_Assert
(
!
weights
.
empty
()
||
!
bias
.
empty
());
if
(
!
weights
.
empty
()
&&
!
bias
.
empty
())
{
CV_CheckEQ
(
weights
.
size
(),
bias
.
size
(),
"
weights and bias size are not equal
"
);
CV_CheckEQ
(
weights
.
size
(),
bias
.
size
(),
"
different broadcasting options for weights and bias is not supported
"
);
}
/* the weights/bias might require broadcasting to scale/shift */
const
auto
num_parameters
=
!
weights
.
empty
()
?
weights
.
size
()
:
bias
.
size
();
const
auto
mid_size
=
num_parameters
/
parameter_batch_size
;
/* the scale shift operation might require broadcasting */
const
int
end_axis
=
[
&
]
{
for
(
int
endAxis
=
axis
+
1
;
endAxis
<=
input
.
rank
();
endAxis
++
)
{
std
::
size_t
size
=
input
.
size_range
(
axis
,
endAxis
);
if
(
size
==
numParams
)
for
(
int
endAxis
=
axis
+
1
;
endAxis
<=
input
.
rank
();
endAxis
++
)
{
if
(
input
.
size_range
(
axis
,
endAxis
)
==
mid_size
)
return
endAxis
;
}
CV_Assert
(
0
/*
invalid weights matrix
*/
);
CV_Assert
(
0
/*
failed to find a broadcast config
*/
);
}();
std
::
size_t
inner_size
=
input
.
size_range
(
end_axis
,
input
.
rank
());
...
...
@@ -103,6 +149,8 @@ namespace cv { namespace dnn { namespace cuda4dnn {
csl
::
Stream
stream
;
csl
::
Tensor
<
T
>
weightsTensor
,
biasTensor
;
std
::
size_t
axis
;
ScaleShiftConfiguration
::
OpMode
scaleMode
,
shiftMode
;
};
}}}
/* namespace cv::dnn::cuda4dnn */
...
...
modules/dnn/src/layers/scale_layer.cpp
浏览文件 @
2aeb32d2
...
...
@@ -159,14 +159,49 @@ public:
CV_Assert
(
!
blobs
.
empty
()
||
inputs
.
size
()
==
2
);
cv
::
Mat
weightsMat
=
hasWeights
?
blobs
[
0
]
:
Mat
();
auto
weightsMat
=
Mat
(),
biasMat
=
Mat
();
cuda4dnn
::
ScaleShiftConfiguration
config
;
if
(
hasWeights
)
{
if
(
blobs
.
empty
())
{
config
.
scaleMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
;
}
else
{
weightsMat
=
blobs
[
0
];
config
.
scaleMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
;
}
}
else
{
config
.
scaleMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
NONE
;
}
if
(
hasBias
)
{
if
(
blobs
.
empty
())
{
config
.
shiftMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
UNTRAINABLE
;
}
else
{
/* if the weights are provided, bias will be in blobs[1]; otherwise, it will be in blobs[0]
* in either case, it is at the end of the blobs vector => bias = blobs.back()
*/
cv
::
Mat
biasMat
=
hasBias
?
blobs
.
back
()
:
Mat
();
biasMat
=
blobs
.
back
();
config
.
shiftMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
TRAINABLE
;
}
}
else
{
config
.
shiftMode
=
cuda4dnn
::
ScaleShiftConfiguration
::
OpMode
::
NONE
;
}
config
.
axis
=
axis
;
return
make_cuda_node
<
cuda4dnn
::
ScaleShiftOp
>
(
preferableTarget
,
std
::
move
(
context
->
stream
),
axis
,
weightsMat
,
biasMat
);
return
make_cuda_node
<
cuda4dnn
::
ScaleShiftOp
>
(
preferableTarget
,
std
::
move
(
context
->
stream
),
config
,
weightsMat
,
biasMat
);
}
#endif
...
...
modules/dnn/test/test_darknet_importer.cpp
浏览文件 @
2aeb32d2
...
...
@@ -580,8 +580,8 @@ TEST_P(Test_Darknet_layers, convolutional)
TEST_P
(
Test_Darknet_layers
,
scale_channels
)
{
// TODO: test fails for batches due to a bug/missing feature in ScaleLayer
testDarknetLayer
(
"scale_channels"
,
false
,
false
);
bool
testBatches
=
backend
==
DNN_BACKEND_CUDA
;
testDarknetLayer
(
"scale_channels"
,
false
,
testBatches
);
}
TEST_P
(
Test_Darknet_layers
,
connected
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录