Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv_contrib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv_contrib
Commits
b51ffe3e
Commit
b51ffe3e
authored
Aug 02, 2016
by
Vitaliy Lyudvichenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding of public interfaces and refactoring of Reshape and MVN layer.
parent
4f578068
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
222 additions
and
210 deletions
+222
-210
all_layers.hpp
modules/dnn/include/opencv2/dnn/all_layers.hpp
+18
-0
blob.hpp
modules/dnn/include/opencv2/dnn/blob.hpp
+2
-0
blob.inl.hpp
modules/dnn/include/opencv2/dnn/blob.inl.hpp
+5
-0
shape_utils.hpp
modules/dnn/include/opencv2/dnn/shape_utils.hpp
+2
-25
blob.cpp
modules/dnn/src/blob.cpp
+106
-50
layer_loaders.cpp
modules/dnn/src/caffe/layer_loaders.cpp
+42
-0
layer_loaders.hpp
modules/dnn/src/caffe/layer_loaders.hpp
+3
-0
init.cpp
modules/dnn/src/init.cpp
+4
-4
lrn_layer.cpp
modules/dnn/src/layers/lrn_layer.cpp
+3
-2
mvn_layer.cpp
modules/dnn/src/layers/mvn_layer.cpp
+18
-14
mvn_layer.hpp
modules/dnn/src/layers/mvn_layer.hpp
+3
-5
reshape_layer.cpp
modules/dnn/src/layers/reshape_layer.cpp
+11
-101
reshape_layer.hpp
modules/dnn/src/layers/reshape_layer.hpp
+5
-9
No files found.
modules/dnn/include/opencv2/dnn/all_layers.hpp
View file @
b51ffe3e
...
...
@@ -275,8 +275,26 @@ namespace dnn
static
Ptr
<
InnerProductLayer
>
create
(
int
axis
=
1
);
};
class
CV_EXPORTS_W
MVNLayer
:
public
Layer
{
public
:
double
eps
;
bool
normVariance
,
acrossChannels
;
static
Ptr
<
MVNLayer
>
create
(
bool
normVariance
=
true
,
bool
acrossChannels
=
false
,
double
eps
=
1e-9
);
};
/* Reshaping */
class
CV_EXPORTS_W
ReshapeLayer
:
public
Layer
{
public
:
BlobShape
newShapeDesc
;
Range
newShapeRange
;
static
Ptr
<
ReshapeLayer
>
create
(
const
BlobShape
&
newShape
,
Range
applyingRange
=
Range
::
all
());
};
class
CV_EXPORTS_W
ConcatLayer
:
public
Layer
{
public
:
...
...
modules/dnn/include/opencv2/dnn/blob.hpp
View file @
b51ffe3e
...
...
@@ -115,6 +115,8 @@ namespace dnn
/** @brief Returns pointer to the first element of continuous size array. */
const
int
*
ptr
()
const
;
/** @overload */
int
*
ptr
();
bool
equal
(
const
BlobShape
&
other
)
const
;
//!< Checks equality of two shapes.
bool
operator
==
(
const
BlobShape
&
r
)
const
;
//!< @sa equal()
...
...
modules/dnn/include/opencv2/dnn/blob.inl.hpp
View file @
b51ffe3e
...
...
@@ -208,6 +208,11 @@ inline const int *BlobShape::ptr() const
return
sz
;
}
inline
int
*
BlobShape
::
ptr
()
{
return
sz
;
}
inline
bool
BlobShape
::
equal
(
const
BlobShape
&
other
)
const
{
if
(
this
->
dims
()
!=
other
.
dims
())
...
...
modules/dnn/include/opencv2/dnn/shape_utils.hpp
View file @
b51ffe3e
...
...
@@ -57,6 +57,7 @@ inline std::ostream &operator<< (std::ostream &s, cv::Range &r)
}
//Reshaping
//TODO: add -1 specifier for automatic size inferring
template
<
typename
Mat
>
void
reshape
(
Mat
&
m
,
const
BlobShape
&
shape
)
...
...
@@ -129,31 +130,7 @@ Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, co
return
m
(
&
ranges
[
0
]);
}
//Traits for switching in ploymorphic implementations
template
<
typename
XMat
>
struct
MatTraits
{
};
template
<>
struct
MatTraits
<
cv
::
Mat
>
{
enum
{
IS_MAT
=
1
,
IS_UMAT
=
0
,
};
};
template
<>
struct
MatTraits
<
cv
::
UMat
>
{
enum
{
IS_MAT
=
0
,
IS_UMAT
=
1
,
};
};
BlobShape
computeShapeByReshapeMask
(
const
BlobShape
&
srcShape
,
const
BlobShape
&
maskShape
,
Range
srcRange
=
Range
::
all
());
}
}
...
...
modules/dnn/src/blob.cpp
View file @
b51ffe3e
...
...
@@ -40,25 +40,26 @@
//M*/
#include "precomp.hpp"
#include <opencv2/dnn/shape_utils.hpp>
namespace
cv
{
namespace
dnn
{
Blob
::
Blob
()
{
Blob
::
Blob
()
{
CV_DNN_UMAT_ONLY
(
state
=
UNINITIALIZED
);
}
}
Blob
::
Blob
(
const
BlobShape
&
shape
,
int
type
,
int
allocFlags
)
{
Blob
::
Blob
(
const
BlobShape
&
shape
,
int
type
,
int
allocFlags
)
{
CV_DNN_UMAT_ONLY
(
state
=
UNINITIALIZED
);
this
->
create
(
shape
,
type
,
allocFlags
);
}
}
Blob
::
Blob
(
InputArray
data
)
{
Blob
::
Blob
(
InputArray
data
)
{
#ifndef CV_DNN_UMAT
m
=
data
.
getMat
();
#else
...
...
@@ -74,10 +75,10 @@ namespace dnn
state
=
HEAD_AT_UMAT
;
}
#endif
}
}
void
Blob
::
create
(
const
BlobShape
&
shape
,
int
type
,
int
allocFlags
)
{
void
Blob
::
create
(
const
BlobShape
&
shape
,
int
type
,
int
allocFlags
)
{
#ifndef CV_DNN_UMAT
CV_Assert
(
allocFlags
&
ALLOC_MAT
);
m
.
create
(
shape
.
dims
(),
shape
.
ptr
(),
type
);
...
...
@@ -99,10 +100,10 @@ namespace dnn
state
=
HEAD_AT_UMAT
;
}
#endif
}
}
void
Blob
::
fill
(
InputArray
in
)
{
void
Blob
::
fill
(
InputArray
in
)
{
#ifdef CV_DNN_UMAT
CV_Assert
(
in
.
isMat
()
||
in
.
isUMat
());
if
(
in
.
isMat
())
...
...
@@ -119,15 +120,15 @@ namespace dnn
CV_Assert
(
in
.
isMat
());
m
=
in
.
getMat
();
#endif
}
}
static
inline
int
getMatChannels
(
const
Mat
&
mat
)
{
static
inline
int
getMatChannels
(
const
Mat
&
mat
)
{
return
(
mat
.
dims
<=
2
)
?
mat
.
channels
()
:
mat
.
size
[
0
];
}
}
static
BlobShape
getBlobShape
(
std
::
vector
<
Mat
>
&
vmat
,
int
requestedCn
=
-
1
)
{
static
BlobShape
getBlobShape
(
std
::
vector
<
Mat
>
&
vmat
,
int
requestedCn
=
-
1
)
{
BlobShape
shape
(
BlobShape
::
all
(
4
));
int
cnSum
=
0
,
matCn
;
...
...
@@ -163,10 +164,10 @@ namespace dnn
shape
[
0
]
=
cnSum
/
shape
[
-
3
];
return
shape
;
}
}
static
std
::
vector
<
Mat
>
extractMatVector
(
InputArray
in
)
{
static
std
::
vector
<
Mat
>
extractMatVector
(
InputArray
in
)
{
if
(
in
.
isMat
()
||
in
.
isUMat
())
{
return
std
::
vector
<
Mat
>
(
1
,
in
.
getMat
());
...
...
@@ -186,10 +187,10 @@ namespace dnn
CV_Assert
(
in
.
isMat
()
||
in
.
isMatVector
()
||
in
.
isUMat
()
||
in
.
isUMatVector
());
return
std
::
vector
<
Mat
>
();
}
}
}
void
Blob
::
batchFromImages
(
InputArray
image
,
int
dstCn
)
{
void
Blob
::
batchFromImages
(
InputArray
image
,
int
dstCn
)
{
CV_Assert
(
dstCn
==
-
1
||
dstCn
>
0
);
std
::
vector
<
Mat
>
inMats
=
extractMatVector
(
image
);
BlobShape
dstShape
=
getBlobShape
(
inMats
,
dstCn
);
...
...
@@ -223,17 +224,17 @@ namespace dnn
dstPtr
+=
elemSize
*
inMat
.
total
();
}
}
}
}
Blob
Blob
::
fromImages
(
InputArray
image
,
int
dstCn
)
{
Blob
Blob
::
fromImages
(
InputArray
image
,
int
dstCn
)
{
Blob
res
;
res
.
batchFromImages
(
image
,
dstCn
);
return
res
;
}
}
void
Blob
::
fill
(
const
BlobShape
&
shape
,
int
type
,
void
*
data
,
bool
deepCopy
)
{
void
Blob
::
fill
(
const
BlobShape
&
shape
,
int
type
,
void
*
data
,
bool
deepCopy
)
{
if
(
deepCopy
)
{
create
(
shape
,
type
);
...
...
@@ -244,10 +245,10 @@ namespace dnn
m
=
Mat
(
shape
.
dims
(),
shape
.
ptr
(),
type
,
data
);
}
CV_DNN_UMAT_ONLY
(
state
=
HEAD_AT_MAT
);
}
}
void
Blob
::
setTo
(
InputArray
value
,
int
allocFlags
)
{
void
Blob
::
setTo
(
InputArray
value
,
int
allocFlags
)
{
#ifdef CV_DNN_UMAT
if
(
allocFlags
==
-
1
)
{
...
...
@@ -285,10 +286,10 @@ namespace dnn
#else
m
.
setTo
(
value
);
#endif
}
}
void
Blob
::
updateMat
(
bool
syncData
)
const
{
void
Blob
::
updateMat
(
bool
syncData
)
const
{
#ifdef CV_DNN_UMAT
if
(
state
==
UNINITIALIZED
||
state
==
SYNCED
||
state
==
HEAD_AT_MAT
)
{
...
...
@@ -309,10 +310,10 @@ namespace dnn
#else
(
void
)
syncData
;
#endif
}
}
void
Blob
::
updateUMat
(
bool
syncData
)
const
{
void
Blob
::
updateUMat
(
bool
syncData
)
const
{
#ifdef CV_DNN_UMAT
if
(
state
==
UNINITIALIZED
||
state
==
SYNCED
||
state
==
HEAD_AT_UMAT
)
{
...
...
@@ -332,21 +333,23 @@ namespace dnn
#else
(
void
)
syncData
;
#endif
}
}
void
Blob
::
sync
()
const
{
void
Blob
::
sync
()
const
{
updateMat
();
updateUMat
();
}
}
Vec4i
Blob
::
shape4
()
const
{
Vec4i
Blob
::
shape4
()
const
{
return
Vec4i
(
num
(),
channels
(),
rows
(),
cols
());
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
stream
,
const
BlobShape
&
shape
)
{
//BlobShape
std
::
ostream
&
operator
<<
(
std
::
ostream
&
stream
,
const
BlobShape
&
shape
)
{
stream
<<
"["
;
for
(
int
i
=
0
;
i
<
shape
.
dims
()
-
1
;
i
++
)
...
...
@@ -355,6 +358,59 @@ namespace dnn
stream
<<
shape
[
-
1
];
return
stream
<<
"]"
;
}
BlobShape
computeShapeByReshapeMask
(
const
BlobShape
&
srcShape
,
const
BlobShape
&
maskShape
,
Range
srcRange
/*= Range::all()*/
)
{
if
(
srcRange
==
Range
::
all
())
srcRange
=
Range
(
0
,
srcShape
.
dims
());
CV_Assert
(
0
<=
srcRange
.
start
&&
srcRange
.
start
<=
srcRange
.
end
&&
srcRange
.
end
<=
srcShape
.
dims
());
Shape
dstShape
(
srcShape
.
dims
()
-
srcRange
.
size
()
+
maskShape
.
dims
(),
nullptr
);
std
::
copy
(
srcShape
.
ptr
(),
srcShape
.
ptr
()
+
srcRange
.
start
,
dstShape
.
ptr
());
std
::
copy
(
srcShape
.
ptr
()
+
srcRange
.
end
,
srcShape
.
ptr
()
+
srcShape
.
dims
(),
dstShape
.
ptr
()
+
srcRange
.
start
+
maskShape
.
dims
());
int
inferDim
=
-
1
;
for
(
int
i
=
0
;
i
<
maskShape
.
dims
();
i
++
)
{
if
(
maskShape
[
i
]
>
0
)
{
dstShape
[
srcRange
.
start
+
i
]
=
maskShape
[
i
];
}
else
if
(
maskShape
[
i
]
==
0
)
{
if
(
srcRange
.
start
+
i
>=
srcShape
.
dims
())
CV_Error
(
Error
::
StsBadArg
,
format
(
"Copy dim[%d] (which has zero size) is out of the source shape bounds"
,
srcRange
.
start
+
i
));
dstShape
[
srcRange
.
start
+
i
]
=
srcShape
[
srcRange
.
start
+
i
];
}
else
if
(
maskShape
[
i
]
==
-
1
)
{
if
(
inferDim
!=
-
1
)
CV_Error
(
Error
::
StsAssert
,
"Duplicate of inferred dim (which is denoted by -1)"
);
inferDim
=
srcRange
.
start
+
i
;
dstShape
[
inferDim
]
=
1
;
}
else
CV_Error
(
Error
::
StsBadArg
,
"maskShape[i] >= -1"
);
}
if
(
inferDim
!=
-
1
)
{
ptrdiff_t
srcTotal
=
srcShape
.
total
();
ptrdiff_t
dstTotal
=
dstShape
.
total
();
if
(
srcTotal
%
dstTotal
!=
0
)
CV_Error
(
Error
::
StsBackTrace
,
"Can't infer a dim denoted by -1"
);
dstShape
[
inferDim
]
=
(
int
)(
srcTotal
/
dstTotal
);
}
else
{
CV_Assert
(
srcShape
.
total
()
==
dstShape
.
total
());
}
return
dstShape
;
}
}
}
modules/dnn/src/caffe/layer_loaders.cpp
View file @
b51ffe3e
...
...
@@ -162,8 +162,45 @@ Ptr<Layer> createLayerFromCaffe<LRNLayer>(LayerParams& params)
return
Ptr
<
Layer
>
(
LRNLayer
::
create
(
type
,
size
,
alpha
,
beta
));
}
template
<>
Ptr
<
Layer
>
createLayerFromCaffe
<
MVNLayer
>
(
LayerParams
&
params
)
{
return
Ptr
<
Layer
>
(
MVNLayer
::
create
(
params
.
get
<
bool
>
(
"normalize_variance"
,
true
),
params
.
get
<
bool
>
(
"across_channels"
,
false
),
params
.
get
<
double
>
(
"eps"
,
1e-9
)
));
}
/* Reshape layers */
template
<>
Ptr
<
Layer
>
createLayerFromCaffe
<
ReshapeLayer
>
(
LayerParams
&
params
)
{
int
axis
=
params
.
get
<
int
>
(
"axis"
,
0
);
int
numAxes
=
params
.
get
<
int
>
(
"num_axes"
,
-
1
);
CV_Assert
(
numAxes
>=
-
1
);
Range
applyingRange
=
(
numAxes
==
-
1
)
?
Range
::
all
()
:
Range
(
axis
,
axis
+
numAxes
);
Shape
newShape
;
if
(
params
.
has
(
"dim"
))
{
const
DictValue
&
paramShape
=
params
.
get
(
"dim"
);
newShape
=
Shape
(
paramShape
.
size
(),
nullptr
);
for
(
int
i
=
0
;
i
<
paramShape
.
size
();
i
++
)
newShape
[
i
]
=
paramShape
.
get
<
int
>
(
i
);
}
else
newShape
=
Shape
::
all
(
0
);
return
Ptr
<
Layer
>
(
ReshapeLayer
::
create
(
newShape
,
applyingRange
));
}
Ptr
<
Layer
>
createFlattenLayerFromCaffe
(
LayerParams
&
)
{
return
Ptr
<
Layer
>
(
ReshapeLayer
::
create
(
Shape
(
0
,
-
1
)));
}
template
<>
Ptr
<
Layer
>
createLayerFromCaffe
<
ConcatLayer
>
(
LayerParams
&
params
)
{
...
...
@@ -239,6 +276,11 @@ template Ptr<Layer> createLayerFromCaffe<DeconvolutionLayer>(LayerParams&);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
SoftmaxLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
InnerProductLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
LRNLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
MVNLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
ConcatLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
SliceLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
SplitLayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
ReLULayer
>
(
LayerParams
&
);
template
Ptr
<
Layer
>
createLayerFromCaffe
<
SigmoidLayer
>
(
LayerParams
&
);
...
...
modules/dnn/src/caffe/layer_loaders.hpp
View file @
b51ffe3e
...
...
@@ -53,6 +53,8 @@ namespace dnn
template
<
typename
PublicLayer
>
Ptr
<
Layer
>
createLayerFromCaffe
(
LayerParams
&
);
Ptr
<
Layer
>
createFlattenLayerFromCaffe
(
LayerParams
&
);
}
}
#endif
\ No newline at end of file
modules/dnn/src/init.cpp
View file @
b51ffe3e
...
...
@@ -71,10 +71,8 @@ void initModule()
REG_RUNTIME_LAYER_FUNC
(
Slice
,
createLayerFromCaffe
<
SliceLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Split
,
createLayerFromCaffe
<
SplitLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Concat
,
createLayerFromCaffe
<
ConcatLayer
>
);
REG_RUNTIME_LAYER_CLASS
(
Reshape
,
ReshapeLayer
)
REG_RUNTIME_LAYER_FUNC
(
Flatten
,
createFlattenLayer
);
REG_RUNTIME_LAYER_CLASS
(
Dropout
,
BlankLayer
)
REG_RUNTIME_LAYER_CLASS
(
MVN
,
MVNLayer
)
REG_RUNTIME_LAYER_FUNC
(
Reshape
,
createLayerFromCaffe
<
ReshapeLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Flatten
,
createFlattenLayerFromCaffe
);
REG_RUNTIME_LAYER_FUNC
(
Convolution
,
createLayerFromCaffe
<
ConvolutionLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Deconvolution
,
createLayerFromCaffe
<
DeconvolutionLayer
>
);
...
...
@@ -82,6 +80,7 @@ void initModule()
REG_RUNTIME_LAYER_FUNC
(
LRN
,
createLayerFromCaffe
<
LRNLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
InnerProduct
,
createLayerFromCaffe
<
InnerProductLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Softmax
,
createLayerFromCaffe
<
SoftmaxLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
MVN
,
createLayerFromCaffe
<
MVNLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
ReLU
,
createLayerFromCaffe
<
ReLULayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Sigmoid
,
createLayerFromCaffe
<
SigmoidLayer
>
);
...
...
@@ -89,6 +88,7 @@ void initModule()
REG_RUNTIME_LAYER_FUNC
(
BNLL
,
createLayerFromCaffe
<
BNLLLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
AbsVal
,
createLayerFromCaffe
<
AbsLayer
>
);
REG_RUNTIME_LAYER_FUNC
(
Power
,
createLayerFromCaffe
<
PowerLayer
>
);
REG_RUNTIME_LAYER_CLASS
(
Dropout
,
BlankLayer
)
init
.
status
=
true
;
}
...
...
modules/dnn/src/layers/lrn_layer.cpp
View file @
b51ffe3e
...
...
@@ -42,11 +42,12 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
#include "lrn_layer.hpp"
#include "opencl_kernels_dnn.hpp"
#include "
modules/dnn/
opencl_kernels_dnn.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/dnn/shape_utils.hpp>
#include <algorithm>
#include <type_traits>
namespace
cv
{
...
...
@@ -220,7 +221,7 @@ void LRNLayerImpl::spatialNormalization_(Blob &srcBlob, Blob &dstBlob)
XMat
src
=
getPlane
(
srcMat
,
n
,
cn
);
XMat
dst
=
getPlane
(
dstMat
,
n
,
cn
);
if
(
MatTraits
<
XMat
>::
IS_UMAT
)
if
(
std
::
is_same
<
XMat
,
UMat
>::
value
)
{
cv
::
sqrBoxFilter
(
src
,
dst
,
dst
.
depth
(),
Size
(
size
,
size
),
Point
(
-
1
,
-
1
),
false
,
BORDER_CONSTANT
|
BORDER_ISOLATED
);
}
...
...
modules/dnn/src/layers/mvn_layer.cpp
View file @
b51ffe3e
...
...
@@ -42,20 +42,21 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
#include "mvn_layer.hpp"
#include <opencv2/dnn/shape_utils.hpp>
namespace
cv
{
namespace
dnn
{
MVNLayer
::
MVNLayer
(
LayerParams
&
params
)
:
Layer
(
params
)
MVNLayer
Impl
::
MVNLayerImpl
(
bool
normVariance_
,
bool
acrossChannels_
,
double
eps_
)
{
eps
=
params
.
get
<
double
>
(
"eps"
,
1e-9
)
;
acrossChannels
=
params
.
get
<
bool
>
(
"across_channels"
,
false
)
;
normalizeVariance
=
params
.
get
<
bool
>
(
"normalize_variance"
,
true
)
;
normVariance
=
normVariance_
;
acrossChannels
=
acrossChannels_
;
eps
=
eps_
;
}
void
MVNLayer
::
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
void
MVNLayer
Impl
::
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
{
outputs
.
resize
(
inputs
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
...
...
@@ -65,20 +66,17 @@ void MVNLayer::allocate(const std::vector<Blob *> &inputs, std::vector<Blob> &ou
}
}
void
MVNLayer
::
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
void
MVNLayer
Impl
::
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
{
for
(
size_t
inpIdx
=
0
;
inpIdx
<
inputs
.
size
();
inpIdx
++
)
{
Blob
&
inpBlob
=
*
inputs
[
inpIdx
];
Blob
&
outBlob
=
outputs
[
inpIdx
];
int
workSize
[
2
];
int
splitDim
=
(
acrossChannels
)
?
1
:
2
;
workSize
[
0
]
=
(
int
)
inpBlob
.
total
(
0
,
splitDim
);
workSize
[
1
]
=
(
int
)
inpBlob
.
total
(
splitDim
);
Mat
inpMat
=
inpBlob
.
matRef
().
reshape
(
1
,
2
,
workSize
);
Mat
outMat
=
outBlob
.
matRef
().
reshape
(
1
,
2
,
workSize
);
Shape
workSize
((
int
)
inpBlob
.
total
(
0
,
splitDim
),
(
int
)
inpBlob
.
total
(
splitDim
));
Mat
inpMat
=
reshaped
(
inpBlob
.
matRefConst
(),
workSize
);
Mat
outMat
=
reshaped
(
outBlob
.
matRef
(),
workSize
);
Scalar
mean
,
dev
;
for
(
int
i
=
0
;
i
<
workSize
[
0
];
i
++
)
...
...
@@ -86,12 +84,18 @@ void MVNLayer::forward(std::vector<Blob *> &inputs, std::vector<Blob> &outputs)
Mat
inpRow
=
inpMat
.
row
(
i
);
Mat
outRow
=
outMat
.
row
(
i
);
cv
::
meanStdDev
(
inpRow
,
mean
,
(
norm
alize
Variance
)
?
dev
:
noArray
());
double
alpha
=
(
norm
alize
Variance
)
?
1
/
(
eps
+
dev
[
0
])
:
1
;
cv
::
meanStdDev
(
inpRow
,
mean
,
(
normVariance
)
?
dev
:
noArray
());
double
alpha
=
(
normVariance
)
?
1
/
(
eps
+
dev
[
0
])
:
1
;
inpRow
.
convertTo
(
outRow
,
outRow
.
type
(),
alpha
,
-
mean
[
0
]
*
alpha
);
}
}
}
Ptr
<
MVNLayer
>
MVNLayer
::
create
(
bool
normVariance
,
bool
acrossChannels
,
double
eps
)
{
return
Ptr
<
MVNLayer
>
(
new
MVNLayerImpl
(
normVariance
,
acrossChannels
,
eps
));
}
}
}
modules/dnn/src/layers/mvn_layer.hpp
View file @
b51ffe3e
...
...
@@ -42,20 +42,18 @@
#ifndef __OPENCV_DNN_LAYERS_MVN_LAYER_HPP__
#define __OPENCV_DNN_LAYERS_MVN_LAYER_HPP__
#include "../precomp.hpp"
#include <opencv2/dnn/all_layers.hpp>
namespace
cv
{
namespace
dnn
{
class
MVNLayer
:
public
Layer
class
MVNLayer
Impl
:
public
MVN
Layer
{
double
eps
;
bool
acrossChannels
,
normalizeVariance
;
public
:
MVNLayer
(
LayerParams
&
params
);
MVNLayer
Impl
(
bool
normVariance_
=
true
,
bool
acrossChannels_
=
false
,
double
eps_
=
1e-9
);
void
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
);
void
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
);
};
...
...
modules/dnn/src/layers/reshape_layer.cpp
View file @
b51ffe3e
...
...
@@ -42,73 +42,33 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
#include "reshape_layer.hpp"
#include <opencv2/dnn/shape_utils.hpp>
namespace
cv
{
namespace
dnn
{
ReshapeLayer
::
ReshapeLayer
(
LayerParams
&
params
)
:
Layer
(
params
)
ReshapeLayer
Impl
::
ReshapeLayerImpl
(
const
BlobShape
&
newShape_
,
Range
applyingRange_
)
{
inAxis
=
params
.
get
<
int
>
(
"axis"
,
0
);
inNumAxes
=
params
.
get
<
int
>
(
"num_axes"
,
-
1
);
CV_Assert
(
inNumAxes
>=
-
1
);
autoAxisIdx
=
-
1
;
if
(
!
params
.
has
(
"dim"
))
{
shapeDesc
=
BlobShape
::
all
(
0
);
return
;
}
DictValue
paramShape
=
params
.
get
(
"dim"
);
shapeDesc
=
BlobShape
::
all
(
paramShape
.
size
());
for
(
int
i
=
0
;
i
<
paramShape
.
size
();
i
++
)
{
int
dim
=
paramShape
.
get
<
int
>
(
i
);
CV_Assert
(
dim
>=
-
1
);
if
(
dim
==
-
1
)
{
if
(
autoAxisIdx
!=
-
1
)
CV_Error
(
Error
::
StsBadArg
,
"New shape contains multiple -1 dims"
);
autoAxisIdx
=
i
;
}
shapeDesc
[
i
]
=
dim
;
}
newShapeDesc
=
newShape_
;
newShapeRange
=
applyingRange_
;
}
void
ReshapeLayer
::
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
void
ReshapeLayer
Impl
::
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
{
outputs
.
resize
(
inputs
.
size
());
outShapes
.
resize
(
inputs
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
Blob
&
inpBlob
=
*
inputs
[
i
];
Blob
&
outBlob
=
outputs
[
i
];
BlobShape
inpShape
=
inpBlob
.
shape
();
int
startAxis
=
(
inAxis
>=
0
)
?
inAxis
:
inpShape
.
dims
()
+
1
+
inAxis
;
int
endAxis
=
(
inNumAxes
==
-
1
)
?
inpShape
.
dims
()
:
startAxis
+
inNumAxes
;
CV_Assert
(
0
<=
startAxis
&&
startAxis
<=
inpShape
.
dims
());
CV_Assert
(
0
<=
endAxis
&&
endAxis
<=
inpShape
.
dims
());
int
newDims
=
inpShape
.
dims
()
-
(
endAxis
-
startAxis
)
+
shapeDesc
.
dims
();
BlobShape
outShape
=
BlobShape
::
all
(
newDims
);
computeOutputShape
(
startAxis
,
endAxis
,
inpShape
,
outShape
);
outShapes
[
i
]
=
outShape
;
outBlob
.
shareFrom
(
inpBlob
);
outBlob
.
reshape
(
outShape
);
outShapes
[
i
]
=
computeShapeByReshapeMask
(
inputs
[
i
]
->
shape
(),
newShapeDesc
,
newShapeRange
);
outputs
[
i
].
shareFrom
(
*
inputs
[
i
]);
outputs
[
i
].
reshape
(
outShapes
[
i
]);
}
}
void
ReshapeLayer
::
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
void
ReshapeLayer
Impl
::
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
)
{
for
(
size_t
i
=
0
;
i
<
outputs
.
size
();
i
++
)
{
...
...
@@ -117,61 +77,11 @@ void ReshapeLayer::forward(std::vector<Blob*> &inputs, std::vector<Blob> &output
}
}
void
ReshapeLayer
::
computeOutputShape
(
int
startAxis
,
int
endAxis
,
BlobShape
&
inpShape
,
BlobShape
&
outShape
)
Ptr
<
ReshapeLayer
>
ReshapeLayer
::
create
(
const
BlobShape
&
newShape
,
Range
applyingRange
/*= Range::all()*/
)
{
int
idx
=
0
;
for
(
int
i
=
0
;
i
<
startAxis
;
i
++
)
outShape
[
idx
++
]
=
inpShape
[
i
];
for
(
int
i
=
0
;
i
<
shapeDesc
.
dims
();
i
++
)
{
if
(
shapeDesc
[
i
]
==
0
)
{
int
inpAxisIdx
=
startAxis
+
i
;
if
(
inpAxisIdx
<
0
||
inpShape
.
dims
()
<=
inpAxisIdx
)
CV_Error
(
Error
::
StsOutOfRange
,
"copy dimension (which has zero size) is not presented into reshaped blob"
);
outShape
[
idx
++
]
=
inpShape
[
startAxis
+
i
];
}
else
{
outShape
[
idx
++
]
=
(
shapeDesc
[
i
]
>
0
)
?
shapeDesc
[
i
]
:
1
;
}
}
for
(
int
i
=
endAxis
;
i
<
inpShape
.
dims
();
i
++
)
outShape
[
idx
++
]
=
inpShape
[
i
];
if
(
autoAxisIdx
>=
0
)
{
size_t
total
=
inpShape
.
total
();
size_t
curTotal
=
1
;
for
(
int
i
=
0
;
i
<
outShape
.
dims
();
i
++
)
{
if
(
i
!=
startAxis
+
autoAxisIdx
)
curTotal
*=
outShape
[
i
];
}
CV_DbgAssert
(
curTotal
<=
total
&&
total
%
curTotal
==
0
);
outShape
[
startAxis
+
autoAxisIdx
]
=
(
int
)(
total
/
curTotal
);
}
if
(
inpShape
.
total
()
!=
outShape
.
total
())
{
CV_Error
(
Error
::
StsUnmatchedSizes
,
"Mismatch between input and output blob elements count"
);
}
return
Ptr
<
ReshapeLayer
>
(
new
ReshapeLayerImpl
(
newShape
,
applyingRange
));
}
Ptr
<
Layer
>
createFlattenLayer
(
LayerParams
&
)
{
LayerParams
params
;
int
shapeDesc
[]
=
{
0
,
-
1
};
params
.
set
(
"dim"
,
DictValue
::
arrayInt
(
shapeDesc
,
2
));
return
Ptr
<
Layer
>
(
new
ReshapeLayer
(
params
));
}
}
}
modules/dnn/src/layers/reshape_layer.hpp
View file @
b51ffe3e
...
...
@@ -42,27 +42,23 @@
#ifndef __OPENCV_DNN_LAYERS_RESHAPE_LAYER_HPP__
#define __OPENCV_DNN_LAYERS_RESHAPE_LAYER_HPP__
#include "../precomp.hpp"
#include <opencv2/dnn/all_layers.hpp>
namespace
cv
{
namespace
dnn
{
class
ReshapeLayer
:
public
Layer
class
ReshapeLayer
Impl
:
public
Reshape
Layer
{
std
::
vector
<
BlobShape
>
outShapes
;
public
:
ReshapeLayer
(
LayerParams
&
params
);
ReshapeLayer
Impl
(
const
BlobShape
&
newShape_
,
Range
applyingRange_
);
void
allocate
(
const
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
);
void
forward
(
std
::
vector
<
Blob
*>
&
inputs
,
std
::
vector
<
Blob
>
&
outputs
);
protected
:
BlobShape
shapeDesc
;
std
::
vector
<
BlobShape
>
outShapes
;
int
inAxis
,
inNumAxes
,
autoAxisIdx
;
void
computeOutputShape
(
int
startAxis
,
int
endAxis
,
BlobShape
&
inpShape
,
BlobShape
&
outShape
);
};
Ptr
<
Layer
>
createFlattenLayer
(
LayerParams
&
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment