Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
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
Commits
5496dedd
Commit
5496dedd
authored
Feb 10, 2016
by
Marina Noskova
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed warnings.
parent
0307dd19
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
29 deletions
+30
-29
ml.hpp
modules/ml/include/opencv2/ml.hpp
+2
-2
svmsgd.cpp
modules/ml/src/svmsgd.cpp
+16
-15
test_svmsgd.cpp
modules/ml/test/test_svmsgd.cpp
+6
-6
train_svmsgd.cpp
samples/cpp/train_svmsgd.cpp
+6
-6
No files found.
modules/ml/include/opencv2/ml.hpp
View file @
5496dedd
...
@@ -1640,9 +1640,9 @@ public:
...
@@ -1640,9 +1640,9 @@ public:
CV_WRAP
virtual
void
setOptimalParameters
(
int
svmsgdType
=
SVMSGD
::
ASGD
,
int
marginType
=
SVMSGD
::
SOFT_MARGIN
)
=
0
;
CV_WRAP
virtual
void
setOptimalParameters
(
int
svmsgdType
=
SVMSGD
::
ASGD
,
int
marginType
=
SVMSGD
::
SOFT_MARGIN
)
=
0
;
/** @brief %Algorithm type, one of SVMSGD::SvmsgdType. */
/** @brief %Algorithm type, one of SVMSGD::SvmsgdType. */
/** @see set
Algorithm
Type */
/** @see set
Svmsgd
Type */
CV_WRAP
virtual
int
getSvmsgdType
()
const
=
0
;
CV_WRAP
virtual
int
getSvmsgdType
()
const
=
0
;
/** @copybrief get
AlgorithmType @see getAlgorithm
Type */
/** @copybrief get
SvmsgdType @see getSvmsgd
Type */
CV_WRAP
virtual
void
setSvmsgdType
(
int
svmsgdType
)
=
0
;
CV_WRAP
virtual
void
setSvmsgdType
(
int
svmsgdType
)
=
0
;
/** @brief %Margin type, one of SVMSGD::MarginType. */
/** @brief %Margin type, one of SVMSGD::MarginType. */
...
...
modules/ml/src/svmsgd.cpp
View file @
5496dedd
...
@@ -172,7 +172,8 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
...
@@ -172,7 +172,8 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
average
=
Mat
(
1
,
featuresCount
,
samples
.
type
());
average
=
Mat
(
1
,
featuresCount
,
samples
.
type
());
for
(
int
featureIndex
=
0
;
featureIndex
<
featuresCount
;
featureIndex
++
)
for
(
int
featureIndex
=
0
;
featureIndex
<
featuresCount
;
featureIndex
++
)
{
{
average
.
at
<
float
>
(
featureIndex
)
=
mean
(
samples
.
col
(
featureIndex
))[
0
];
Scalar
scalAverage
=
mean
(
samples
.
col
(
featureIndex
))[
0
];
average
.
at
<
float
>
(
featureIndex
)
=
static_cast
<
float
>
(
scalAverage
[
0
]);
}
}
for
(
int
sampleIndex
=
0
;
sampleIndex
<
samplesCount
;
sampleIndex
++
)
for
(
int
sampleIndex
=
0
;
sampleIndex
<
samplesCount
;
sampleIndex
++
)
...
@@ -182,7 +183,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
...
@@ -182,7 +183,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
double
normValue
=
norm
(
samples
);
double
normValue
=
norm
(
samples
);
multiplier
=
s
qrt
(
samples
.
total
())
/
normValue
;
multiplier
=
s
tatic_cast
<
float
>
(
sqrt
(
samples
.
total
())
/
normValue
)
;
samples
*=
multiplier
;
samples
*=
multiplier
;
}
}
...
@@ -228,11 +229,11 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const
...
@@ -228,11 +229,11 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const
for
(
int
samplesIndex
=
0
;
samplesIndex
<
trainSamplesCount
;
samplesIndex
++
)
for
(
int
samplesIndex
=
0
;
samplesIndex
<
trainSamplesCount
;
samplesIndex
++
)
{
{
Mat
currentSample
=
trainSamples
.
row
(
samplesIndex
);
Mat
currentSample
=
trainSamples
.
row
(
samplesIndex
);
float
dotProduct
=
currentSample
.
dot
(
weights_
);
float
dotProduct
=
static_cast
<
float
>
(
currentSample
.
dot
(
weights_
)
);
bool
firstClass
=
isFirstClass
(
trainResponses
.
at
<
float
>
(
samplesIndex
));
bool
firstClass
=
isFirstClass
(
trainResponses
.
at
<
float
>
(
samplesIndex
));
int
index
=
firstClass
?
0
:
1
;
int
index
=
firstClass
?
0
:
1
;
float
signToMul
=
firstClass
?
1
:
-
1
;
float
signToMul
=
firstClass
?
1
.
f
:
-
1.
f
;
float
curDistance
=
dotProduct
*
signToMul
;
float
curDistance
=
dotProduct
*
signToMul
;
if
(
curDistance
<
distanceToClasses
[
index
])
if
(
curDistance
<
distanceToClasses
[
index
])
...
@@ -263,7 +264,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
...
@@ -263,7 +264,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
if
(
areEmpty
.
first
||
areEmpty
.
second
)
if
(
areEmpty
.
first
||
areEmpty
.
second
)
{
{
weights_
=
Mat
::
zeros
(
1
,
featureCount
,
CV_32F
);
weights_
=
Mat
::
zeros
(
1
,
featureCount
,
CV_32F
);
shift_
=
areEmpty
.
first
?
-
1
:
1
;
shift_
=
areEmpty
.
first
?
-
1
.
f
:
1.
f
;
return
true
;
return
true
;
}
}
...
@@ -329,7 +330,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
...
@@ -329,7 +330,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
if
(
params
.
marginType
==
SOFT_MARGIN
)
if
(
params
.
marginType
==
SOFT_MARGIN
)
{
{
shift_
=
extendedWeights
.
at
<
float
>
(
featureCount
)
-
weights_
.
dot
(
average
);
shift_
=
extendedWeights
.
at
<
float
>
(
featureCount
)
-
static_cast
<
float
>
(
weights_
.
dot
(
average
)
);
}
}
else
else
{
{
...
@@ -363,8 +364,8 @@ float SVMSGDImpl::predict( InputArray _samples, OutputArray _results, int ) cons
...
@@ -363,8 +364,8 @@ float SVMSGDImpl::predict( InputArray _samples, OutputArray _results, int ) cons
for
(
int
sampleIndex
=
0
;
sampleIndex
<
nSamples
;
sampleIndex
++
)
for
(
int
sampleIndex
=
0
;
sampleIndex
<
nSamples
;
sampleIndex
++
)
{
{
Mat
currentSample
=
samples
.
row
(
sampleIndex
);
Mat
currentSample
=
samples
.
row
(
sampleIndex
);
float
criterion
=
currentSample
.
dot
(
weights_
)
+
shift_
;
float
criterion
=
static_cast
<
float
>
(
currentSample
.
dot
(
weights_
)
)
+
shift_
;
results
.
at
<
float
>
(
sampleIndex
)
=
(
criterion
>=
0
)
?
1
:
-
1
;
results
.
at
<
float
>
(
sampleIndex
)
=
(
criterion
>=
0
)
?
1
.
f
:
-
1.
f
;
}
}
return
result
;
return
result
;
...
@@ -530,9 +531,9 @@ void SVMSGDImpl::setOptimalParameters(int svmsgdType, int marginType)
...
@@ -530,9 +531,9 @@ void SVMSGDImpl::setOptimalParameters(int svmsgdType, int marginType)
params
.
svmsgdType
=
SGD
;
params
.
svmsgdType
=
SGD
;
params
.
marginType
=
(
marginType
==
SOFT_MARGIN
)
?
SOFT_MARGIN
:
params
.
marginType
=
(
marginType
==
SOFT_MARGIN
)
?
SOFT_MARGIN
:
(
marginType
==
HARD_MARGIN
)
?
HARD_MARGIN
:
ILLEGAL_MARGIN_TYPE
;
(
marginType
==
HARD_MARGIN
)
?
HARD_MARGIN
:
ILLEGAL_MARGIN_TYPE
;
params
.
lambda
=
0.0001
;
params
.
lambda
=
0.0001
f
;
params
.
gamma0
=
0.05
;
params
.
gamma0
=
0.05
f
;
params
.
c
=
1
;
params
.
c
=
1
.
f
;
params
.
termCrit
=
TermCriteria
(
TermCriteria
::
COUNT
+
TermCriteria
::
EPS
,
100000
,
0.00001
);
params
.
termCrit
=
TermCriteria
(
TermCriteria
::
COUNT
+
TermCriteria
::
EPS
,
100000
,
0.00001
);
break
;
break
;
...
@@ -540,9 +541,9 @@ void SVMSGDImpl::setOptimalParameters(int svmsgdType, int marginType)
...
@@ -540,9 +541,9 @@ void SVMSGDImpl::setOptimalParameters(int svmsgdType, int marginType)
params
.
svmsgdType
=
ASGD
;
params
.
svmsgdType
=
ASGD
;
params
.
marginType
=
(
marginType
==
SOFT_MARGIN
)
?
SOFT_MARGIN
:
params
.
marginType
=
(
marginType
==
SOFT_MARGIN
)
?
SOFT_MARGIN
:
(
marginType
==
HARD_MARGIN
)
?
HARD_MARGIN
:
ILLEGAL_MARGIN_TYPE
;
(
marginType
==
HARD_MARGIN
)
?
HARD_MARGIN
:
ILLEGAL_MARGIN_TYPE
;
params
.
lambda
=
0.00001
;
params
.
lambda
=
0.00001
f
;
params
.
gamma0
=
0.05
;
params
.
gamma0
=
0.05
f
;
params
.
c
=
0.75
;
params
.
c
=
0.75
f
;
params
.
termCrit
=
TermCriteria
(
TermCriteria
::
COUNT
+
TermCriteria
::
EPS
,
100000
,
0.00001
);
params
.
termCrit
=
TermCriteria
(
TermCriteria
::
COUNT
+
TermCriteria
::
EPS
,
100000
,
0.00001
);
break
;
break
;
...
...
modules/ml/test/test_svmsgd.cpp
View file @
5496dedd
...
@@ -124,7 +124,7 @@ void CV_SVMSGDTrainTest::makeTrainData(Mat weights, float shift)
...
@@ -124,7 +124,7 @@ void CV_SVMSGDTrainTest::makeTrainData(Mat weights, float shift)
cv
::
Mat
responses
=
cv
::
Mat
::
zeros
(
datasize
,
1
,
CV_32FC1
);
cv
::
Mat
responses
=
cv
::
Mat
::
zeros
(
datasize
,
1
,
CV_32FC1
);
for
(
int
sampleIndex
=
0
;
sampleIndex
<
datasize
;
sampleIndex
++
)
for
(
int
sampleIndex
=
0
;
sampleIndex
<
datasize
;
sampleIndex
++
)
{
{
responses
.
at
<
float
>
(
sampleIndex
)
=
decisionFunction
(
samples
.
row
(
sampleIndex
),
weights
,
shift
)
>
0
?
1
:
-
1
;
responses
.
at
<
float
>
(
sampleIndex
)
=
decisionFunction
(
samples
.
row
(
sampleIndex
),
weights
,
shift
)
>
0
?
1
.
f
:
-
1.
f
;
}
}
data
=
TrainData
::
create
(
samples
,
cv
::
ml
::
ROW_SAMPLE
,
responses
);
data
=
TrainData
::
create
(
samples
,
cv
::
ml
::
ROW_SAMPLE
,
responses
);
...
@@ -146,7 +146,7 @@ void CV_SVMSGDTrainTest::makeTestData(Mat weights, float shift)
...
@@ -146,7 +146,7 @@ void CV_SVMSGDTrainTest::makeTestData(Mat weights, float shift)
for
(
int
i
=
0
;
i
<
testSamplesCount
;
i
++
)
for
(
int
i
=
0
;
i
<
testSamplesCount
;
i
++
)
{
{
testResponses
.
at
<
float
>
(
i
)
=
decisionFunction
(
testSamples
.
row
(
i
),
weights
,
shift
)
>
0
?
1
:
-
1
;
testResponses
.
at
<
float
>
(
i
)
=
decisionFunction
(
testSamples
.
row
(
i
),
weights
,
shift
)
>
0
?
1
.
f
:
-
1.
f
;
}
}
}
}
...
@@ -175,7 +175,7 @@ CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDat
...
@@ -175,7 +175,7 @@ CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDat
float
CV_SVMSGDTrainTest
::
decisionFunction
(
const
Mat
&
sample
,
const
Mat
&
weights
,
float
shift
)
float
CV_SVMSGDTrainTest
::
decisionFunction
(
const
Mat
&
sample
,
const
Mat
&
weights
,
float
shift
)
{
{
return
s
ample
.
dot
(
weights
)
+
shift
;
return
s
tatic_cast
<
float
>
(
sample
.
dot
(
weights
)
)
+
shift
;
}
}
void
CV_SVMSGDTrainTest
::
run
(
int
/*start_from*/
)
void
CV_SVMSGDTrainTest
::
run
(
int
/*start_from*/
)
...
@@ -217,7 +217,7 @@ void makeWeightsAndShift(int featureCount, Mat &weights, float &shift)
...
@@ -217,7 +217,7 @@ void makeWeightsAndShift(int featureCount, Mat &weights, float &shift)
double
upperLimit
=
1
;
double
upperLimit
=
1
;
rng
.
fill
(
weights
,
RNG
::
UNIFORM
,
lowerLimit
,
upperLimit
);
rng
.
fill
(
weights
,
RNG
::
UNIFORM
,
lowerLimit
,
upperLimit
);
shift
=
rng
.
uniform
(
-
featureCount
,
featureCount
);
shift
=
static_cast
<
float
>
(
rng
.
uniform
(
-
featureCount
,
featureCount
)
);
}
}
...
@@ -319,7 +319,7 @@ TEST(ML_SVMSGD, twoPoints)
...
@@ -319,7 +319,7 @@ TEST(ML_SVMSGD, twoPoints)
float
realShift
=
-
500000.5
;
float
realShift
=
-
500000.5
;
float
normRealWeights
=
norm
(
realWeights
);
float
normRealWeights
=
static_cast
<
float
>
(
norm
(
realWeights
)
);
realWeights
/=
normRealWeights
;
realWeights
/=
normRealWeights
;
realShift
/=
normRealWeights
;
realShift
/=
normRealWeights
;
...
@@ -330,7 +330,7 @@ TEST(ML_SVMSGD, twoPoints)
...
@@ -330,7 +330,7 @@ TEST(ML_SVMSGD, twoPoints)
Mat
foundWeights
=
svmsgd
->
getWeights
();
Mat
foundWeights
=
svmsgd
->
getWeights
();
float
foundShift
=
svmsgd
->
getShift
();
float
foundShift
=
svmsgd
->
getShift
();
float
normFoundWeights
=
norm
(
foundWeights
);
float
normFoundWeights
=
static_cast
<
float
>
(
norm
(
foundWeights
)
);
foundWeights
/=
normFoundWeights
;
foundWeights
/=
normFoundWeights
;
foundShift
/=
normFoundWeights
;
foundShift
/=
normFoundWeights
;
CV_Assert
((
norm
(
foundWeights
-
realWeights
)
<
0.001
)
&&
(
abs
((
foundShift
-
realShift
)
/
realShift
)
<
0.05
));
CV_Assert
((
norm
(
foundWeights
-
realWeights
)
<
0.001
)
&&
(
abs
((
foundShift
-
realShift
)
/
realShift
)
<
0.05
));
...
...
samples/cpp/train_svmsgd.cpp
View file @
5496dedd
...
@@ -97,7 +97,7 @@ bool findCrossPointWithBorders(const Mat &weights, float shift, const std::pair<
...
@@ -97,7 +97,7 @@ bool findCrossPointWithBorders(const Mat &weights, float shift, const std::pair<
if
(
xMin
==
xMax
&&
weights
.
at
<
float
>
(
1
)
!=
0
)
if
(
xMin
==
xMax
&&
weights
.
at
<
float
>
(
1
)
!=
0
)
{
{
x
=
xMin
;
x
=
xMin
;
y
=
st
d
::
floor
(
-
(
weights
.
at
<
float
>
(
0
)
*
x
+
shift
)
/
weights
.
at
<
float
>
(
1
));
y
=
st
atic_cast
<
int
>
(
std
::
floor
(
-
(
weights
.
at
<
float
>
(
0
)
*
x
+
shift
)
/
weights
.
at
<
float
>
(
1
)
));
if
(
y
>=
yMin
&&
y
<=
yMax
)
if
(
y
>=
yMin
&&
y
<=
yMax
)
{
{
crossPoint
.
x
=
x
;
crossPoint
.
x
=
x
;
...
@@ -108,7 +108,7 @@ bool findCrossPointWithBorders(const Mat &weights, float shift, const std::pair<
...
@@ -108,7 +108,7 @@ bool findCrossPointWithBorders(const Mat &weights, float shift, const std::pair<
else
if
(
yMin
==
yMax
&&
weights
.
at
<
float
>
(
0
)
!=
0
)
else
if
(
yMin
==
yMax
&&
weights
.
at
<
float
>
(
0
)
!=
0
)
{
{
y
=
yMin
;
y
=
yMin
;
x
=
st
d
::
floor
(
-
(
weights
.
at
<
float
>
(
1
)
*
y
+
shift
)
/
weights
.
at
<
float
>
(
0
));
x
=
st
atic_cast
<
int
>
(
std
::
floor
(
-
(
weights
.
at
<
float
>
(
1
)
*
y
+
shift
)
/
weights
.
at
<
float
>
(
0
)
));
if
(
x
>=
xMin
&&
x
<=
xMax
)
if
(
x
>=
xMin
&&
x
<=
xMax
)
{
{
crossPoint
.
x
=
x
;
crossPoint
.
x
=
x
;
...
@@ -149,8 +149,8 @@ void redraw(Data data, const Point points[2])
...
@@ -149,8 +149,8 @@ void redraw(Data data, const Point points[2])
Scalar
color
;
Scalar
color
;
for
(
int
i
=
0
;
i
<
data
.
samples
.
rows
;
i
++
)
for
(
int
i
=
0
;
i
<
data
.
samples
.
rows
;
i
++
)
{
{
center
.
x
=
data
.
samples
.
at
<
float
>
(
i
,
0
);
center
.
x
=
static_cast
<
int
>
(
data
.
samples
.
at
<
float
>
(
i
,
0
)
);
center
.
y
=
data
.
samples
.
at
<
float
>
(
i
,
1
);
center
.
y
=
static_cast
<
int
>
(
data
.
samples
.
at
<
float
>
(
i
,
1
)
);
color
=
(
data
.
responses
.
at
<
float
>
(
i
)
>
0
)
?
Scalar
(
128
,
128
,
0
)
:
Scalar
(
0
,
128
,
128
);
color
=
(
data
.
responses
.
at
<
float
>
(
i
)
>
0
)
?
Scalar
(
128
,
128
,
0
)
:
Scalar
(
0
,
128
,
128
);
circle
(
data
.
img
,
center
,
radius
,
color
,
5
);
circle
(
data
.
img
,
center
,
radius
,
color
,
5
);
}
}
...
@@ -163,8 +163,8 @@ void addPointRetrainAndRedraw(Data &data, int x, int y, int response)
...
@@ -163,8 +163,8 @@ void addPointRetrainAndRedraw(Data &data, int x, int y, int response)
{
{
Mat
currentSample
(
1
,
2
,
CV_32F
);
Mat
currentSample
(
1
,
2
,
CV_32F
);
currentSample
.
at
<
float
>
(
0
,
0
)
=
x
;
currentSample
.
at
<
float
>
(
0
,
0
)
=
(
float
)
x
;
currentSample
.
at
<
float
>
(
0
,
1
)
=
y
;
currentSample
.
at
<
float
>
(
0
,
1
)
=
(
float
)
y
;
data
.
samples
.
push_back
(
currentSample
);
data
.
samples
.
push_back
(
currentSample
);
data
.
responses
.
push_back
(
response
);
data
.
responses
.
push_back
(
response
);
...
...
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