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
d4848938
Commit
d4848938
authored
Feb 25, 2016
by
Marina Noskova
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deleted functions makeTrainData() and makeTestData() in test_svmsgd.cpp.
Added function makeData() in test_svmsgd.cpp.
parent
74c87a26
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
46 deletions
+31
-46
ml.hpp
modules/ml/include/opencv2/ml.hpp
+4
-4
svmsgd.cpp
modules/ml/src/svmsgd.cpp
+7
-5
test_svmsgd.cpp
modules/ml/test/test_svmsgd.cpp
+20
-37
No files found.
modules/ml/include/opencv2/ml.hpp
View file @
d4848938
...
...
@@ -1626,10 +1626,10 @@ public:
* stepDecreasingPower = 1;
* termCrit.maxCount = 100000;
* termCrit.epsilon = 0.00001;
* @param svmsgdType is the type of SVMSGD classifier. Legal values are S
vmsgdType::SGD and
SvmsgdType::ASGD.
* Recommended value is SvmsgdType::ASGD (by default).
* @param marginType is the type of margin constraint. Legal values are
MarginType::SOFT_MARGIN and
MarginType::HARD_MARGIN.
* Default value is MarginType::SOFT_MARGIN.
* @param svmsgdType is the type of SVMSGD classifier. Legal values are S
VMSGD::SvmsgdType::SGD and SVMSGD::
SvmsgdType::ASGD.
* Recommended value is S
VMSGD::S
vmsgdType::ASGD (by default).
* @param marginType is the type of margin constraint. Legal values are
SVMSGD::MarginType::SOFT_MARGIN and SVMSGD::
MarginType::HARD_MARGIN.
* Default value is
SVMSGD::
MarginType::SOFT_MARGIN.
*/
CV_WRAP
virtual
void
setOptimalParameters
(
int
svmsgdType
=
SVMSGD
::
ASGD
,
int
marginType
=
SVMSGD
::
SOFT_MARGIN
)
=
0
;
...
...
modules/ml/src/svmsgd.cpp
View file @
d4848938
...
...
@@ -142,6 +142,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
int
samplesCount
=
samples
.
rows
;
average
=
Mat
(
1
,
featuresCount
,
samples
.
type
());
CV_Assert
(
average
.
type
()
==
CV_32FC1
);
for
(
int
featureIndex
=
0
;
featureIndex
<
featuresCount
;
featureIndex
++
)
{
average
.
at
<
float
>
(
featureIndex
)
=
static_cast
<
float
>
(
mean
(
samples
.
col
(
featureIndex
))[
0
]);
...
...
@@ -170,11 +171,11 @@ void SVMSGDImpl::makeExtendedTrainSamples(const Mat &trainSamples, Mat &extended
cv
::
hconcat
(
normalizedTrainSamples
,
onesCol
,
extendedTrainSamples
);
}
void
SVMSGDImpl
::
updateWeights
(
InputArray
_sample
,
bool
firstClass
,
float
stepSize
,
Mat
&
weights
)
void
SVMSGDImpl
::
updateWeights
(
InputArray
_sample
,
bool
positive
,
float
stepSize
,
Mat
&
weights
)
{
Mat
sample
=
_sample
.
getMat
();
int
response
=
firstClass
?
1
:
-
1
;
// ensure that trainResponses are -1 or 1
int
response
=
positive
?
1
:
-
1
;
// ensure that trainResponses are -1 or 1
if
(
sample
.
dot
(
weights
)
*
response
>
1
)
{
...
...
@@ -197,6 +198,7 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const
Mat
trainResponses
=
_responses
.
getMat
();
CV_Assert
(
trainResponses
.
type
()
==
CV_32FC1
);
for
(
int
samplesIndex
=
0
;
samplesIndex
<
trainSamplesCount
;
samplesIndex
++
)
{
Mat
currentSample
=
trainSamples
.
row
(
samplesIndex
);
...
...
@@ -261,7 +263,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
RNG
rng
(
0
);
CV_Assert
(
params
.
termCrit
.
type
&
TermCriteria
::
COUNT
||
params
.
termCrit
.
type
&
TermCriteria
::
EPS
);
CV_Assert
(
(
params
.
termCrit
.
type
&
TermCriteria
::
COUNT
||
params
.
termCrit
.
type
&
TermCriteria
::
EPS
)
&&
(
trainResponses
.
type
()
==
CV_32FC1
)
);
int
maxCount
=
(
params
.
termCrit
.
type
&
TermCriteria
::
COUNT
)
?
params
.
termCrit
.
maxCount
:
INT_MAX
;
double
epsilon
=
(
params
.
termCrit
.
type
&
TermCriteria
::
EPS
)
?
params
.
termCrit
.
epsilon
:
0
;
...
...
@@ -300,7 +302,7 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
weights_
=
extendedWeights
(
roi
);
weights_
*=
multiplier
;
CV_Assert
(
params
.
marginType
==
SOFT_MARGIN
||
params
.
marginType
==
HARD_MARGIN
);
CV_Assert
(
(
params
.
marginType
==
SOFT_MARGIN
||
params
.
marginType
==
HARD_MARGIN
)
&&
(
extendedWeights
.
type
()
==
CV_32FC1
)
);
if
(
params
.
marginType
==
SOFT_MARGIN
)
{
...
...
@@ -332,7 +334,7 @@ float SVMSGDImpl::predict( InputArray _samples, OutputArray _results, int ) cons
else
{
CV_Assert
(
nSamples
==
1
);
results
=
Mat
(
1
,
1
,
CV_32F
,
&
result
);
results
=
Mat
(
1
,
1
,
CV_32F
C1
,
&
result
);
}
for
(
int
sampleIndex
=
0
;
sampleIndex
<
nSamples
;
sampleIndex
++
)
...
...
modules/ml/test/test_svmsgd.cpp
View file @
d4848938
...
...
@@ -62,8 +62,7 @@ public:
private
:
virtual
void
run
(
int
start_from
);
static
float
decisionFunction
(
const
Mat
&
sample
,
const
Mat
&
weights
,
float
shift
);
void
makeTrainData
(
Mat
weights
,
float
shift
);
void
makeTestData
(
Mat
weights
,
float
shift
);
void
makeData
(
int
samplesCount
,
Mat
weights
,
float
shift
,
RNG
rng
,
Mat
&
samples
,
Mat
&
responses
);
void
generateSameBorders
(
int
featureCount
);
void
generateDifferentBorders
(
int
featureCount
);
...
...
@@ -108,46 +107,28 @@ void CV_SVMSGDTrainTest::generateDifferentBorders(int featureCount)
}
}
void
CV_SVMSGDTrainTest
::
makeTrainData
(
Mat
weights
,
float
shift
)
float
CV_SVMSGDTrainTest
::
decisionFunction
(
const
Mat
&
sample
,
const
Mat
&
weights
,
float
shift
)
{
int
datasize
=
100000
;
int
featureCount
=
weights
.
cols
;
RNG
rng
(
0
);
cv
::
Mat
samples
=
cv
::
Mat
::
zeros
(
datasize
,
featureCount
,
CV_32FC1
);
for
(
int
featureIndex
=
0
;
featureIndex
<
featureCount
;
featureIndex
++
)
{
rng
.
fill
(
samples
.
col
(
featureIndex
),
RNG
::
UNIFORM
,
borders
[
featureIndex
].
first
,
borders
[
featureIndex
].
second
);
}
cv
::
Mat
responses
=
cv
::
Mat
::
zeros
(
datasize
,
1
,
CV_32FC1
);
for
(
int
sampleIndex
=
0
;
sampleIndex
<
datasize
;
sampleIndex
++
)
{
responses
.
at
<
float
>
(
sampleIndex
)
=
decisionFunction
(
samples
.
row
(
sampleIndex
),
weights
,
shift
)
>
0
?
1.
f
:
-
1.
f
;
}
data
=
TrainData
::
create
(
samples
,
cv
::
ml
::
ROW_SAMPLE
,
responses
);
return
static_cast
<
float
>
(
sample
.
dot
(
weights
))
+
shift
;
}
void
CV_SVMSGDTrainTest
::
make
TestData
(
Mat
weights
,
float
shift
)
void
CV_SVMSGDTrainTest
::
make
Data
(
int
samplesCount
,
Mat
weights
,
float
shift
,
RNG
rng
,
Mat
&
samples
,
Mat
&
responses
)
{
int
testSamplesCount
=
100000
;
int
featureCount
=
weights
.
cols
;
cv
::
RNG
rng
(
42
);
testSamples
.
create
(
testS
amplesCount
,
featureCount
,
CV_32FC1
);
samples
.
create
(
s
amplesCount
,
featureCount
,
CV_32FC1
);
for
(
int
featureIndex
=
0
;
featureIndex
<
featureCount
;
featureIndex
++
)
{
rng
.
fill
(
testS
amples
.
col
(
featureIndex
),
RNG
::
UNIFORM
,
borders
[
featureIndex
].
first
,
borders
[
featureIndex
].
second
);
rng
.
fill
(
s
amples
.
col
(
featureIndex
),
RNG
::
UNIFORM
,
borders
[
featureIndex
].
first
,
borders
[
featureIndex
].
second
);
}
testResponses
.
create
(
testS
amplesCount
,
1
,
CV_32FC1
);
responses
.
create
(
s
amplesCount
,
1
,
CV_32FC1
);
for
(
int
i
=
0
;
i
<
testS
amplesCount
;
i
++
)
for
(
int
i
=
0
;
i
<
s
amplesCount
;
i
++
)
{
testResponses
.
at
<
float
>
(
i
)
=
decisionFunction
(
testS
amples
.
row
(
i
),
weights
,
shift
)
>
0
?
1.
f
:
-
1.
f
;
responses
.
at
<
float
>
(
i
)
=
decisionFunction
(
s
amples
.
row
(
i
),
weights
,
shift
)
>
0
?
1.
f
:
-
1.
f
;
}
}
CV_SVMSGDTrainTest
::
CV_SVMSGDTrainTest
(
const
Mat
&
weights
,
float
shift
,
TrainDataType
_type
,
double
_precision
)
...
...
@@ -169,13 +150,16 @@ CV_SVMSGDTrainTest::CV_SVMSGDTrainTest(const Mat &weights, float shift, TrainDat
CV_Error
(
CV_StsBadArg
,
"Unknown train data type"
);
}
makeTrainData
(
weights
,
shift
);
makeTestData
(
weights
,
shift
);
}
RNG
rng
(
0
);
float
CV_SVMSGDTrainTest
::
decisionFunction
(
const
Mat
&
sample
,
const
Mat
&
weights
,
float
shift
)
{
return
static_cast
<
float
>
(
sample
.
dot
(
weights
))
+
shift
;
Mat
trainSamples
;
Mat
trainResponses
;
int
trainSamplesCount
=
10000
;
makeData
(
trainSamplesCount
,
weights
,
shift
,
rng
,
trainSamples
,
trainResponses
);
data
=
TrainData
::
create
(
trainSamples
,
cv
::
ml
::
ROW_SAMPLE
,
trainResponses
);
int
testSamplesCount
=
100000
;
makeData
(
testSamplesCount
,
weights
,
shift
,
rng
,
testSamples
,
testResponses
);
}
void
CV_SVMSGDTrainTest
::
run
(
int
/*start_from*/
)
...
...
@@ -205,7 +189,6 @@ void CV_SVMSGDTrainTest::run( int /*start_from*/ )
}
}
void
makeWeightsAndShift
(
int
featureCount
,
Mat
&
weights
,
float
&
shift
)
{
weights
.
create
(
1
,
featureCount
,
CV_32FC1
);
...
...
@@ -253,7 +236,7 @@ TEST(ML_SVMSGD, trainSameScale100)
float
shift
=
0
;
makeWeightsAndShift
(
featureCount
,
weights
,
shift
);
CV_SVMSGDTrainTest
test
(
weights
,
shift
,
CV_SVMSGDTrainTest
::
UNIFORM_SAME_SCALE
);
CV_SVMSGDTrainTest
test
(
weights
,
shift
,
CV_SVMSGDTrainTest
::
UNIFORM_SAME_SCALE
,
0.02
);
test
.
safe_run
();
}
...
...
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