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
ff549527
Commit
ff549527
authored
Feb 15, 2016
by
Marina Noskova
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Corrected spelling mistakes
parent
5496dedd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
16 deletions
+15
-16
ml.hpp
modules/ml/include/opencv2/ml.hpp
+1
-1
svmsgd.cpp
modules/ml/src/svmsgd.cpp
+11
-12
test_svmsgd.cpp
modules/ml/test/test_svmsgd.cpp
+1
-1
train_svmsgd.cpp
samples/cpp/train_svmsgd.cpp
+2
-2
No files found.
modules/ml/include/opencv2/ml.hpp
View file @
ff549527
...
...
@@ -1535,7 +1535,7 @@ The margin type may have one of the following values: \ref SOFT_MARGIN or \ref H
- You should use \ref HARD_MARGIN type, if you have linearly separable sets.
- You should use \ref SOFT_MARGIN type, if you have non-linearly separable sets or sets with outliers.
- In the general case (if you know nothing about linear
ly
separability of your sets), use SOFT_MARGIN.
- In the general case (if you know nothing about linear separability of your sets), use SOFT_MARGIN.
The other parameters may be described as follows:
- \f$\lambda\f$ parameter is responsible for weights decreasing at each step and for the strength of restrictions on outliers
...
...
modules/ml/src/svmsgd.cpp
View file @
ff549527
...
...
@@ -11,7 +11,7 @@
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Copyright (C) 201
4
, Itseez Inc, all rights reserved.
// Copyright (C) 201
6
, Itseez Inc, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
...
...
@@ -103,7 +103,7 @@ public:
CV_IMPL_PROPERTY_S
(
cv
::
TermCriteria
,
TermCriteria
,
params
.
termCrit
)
private
:
void
updateWeights
(
InputArray
sample
,
bool
is
FirstClass
,
float
gamma
,
Mat
&
weights
);
void
updateWeights
(
InputArray
sample
,
bool
is
Positive
,
float
gamma
,
Mat
&
weights
);
std
::
pair
<
bool
,
bool
>
areClassesEmpty
(
Mat
responses
);
...
...
@@ -111,7 +111,7 @@ private:
void
readParams
(
const
FileNode
&
fn
);
static
inline
bool
is
FirstClass
(
float
val
)
{
return
val
>
0
;
}
static
inline
bool
is
Positive
(
float
val
)
{
return
val
>
0
;
}
static
void
normalizeSamples
(
Mat
&
matrix
,
Mat
&
average
,
float
&
multiplier
);
...
...
@@ -152,7 +152,7 @@ std::pair<bool,bool> SVMSGDImpl::areClassesEmpty(Mat responses)
for
(
int
index
=
0
;
index
<
limit_index
;
index
++
)
{
if
(
is
FirstClass
(
responses
.
at
<
float
>
(
index
)))
if
(
is
Positive
(
responses
.
at
<
float
>
(
index
)))
emptyInClasses
.
first
=
false
;
else
emptyInClasses
.
second
=
false
;
...
...
@@ -172,7 +172,7 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
average
=
Mat
(
1
,
featuresCount
,
samples
.
type
());
for
(
int
featureIndex
=
0
;
featureIndex
<
featuresCount
;
featureIndex
++
)
{
Scalar
scalAverage
=
mean
(
samples
.
col
(
featureIndex
))
[
0
]
;
Scalar
scalAverage
=
mean
(
samples
.
col
(
featureIndex
));
average
.
at
<
float
>
(
featureIndex
)
=
static_cast
<
float
>
(
scalAverage
[
0
]);
}
...
...
@@ -190,13 +190,13 @@ void SVMSGDImpl::normalizeSamples(Mat &samples, Mat &average, float &multiplier)
void
SVMSGDImpl
::
makeExtendedTrainSamples
(
const
Mat
&
trainSamples
,
Mat
&
extendedTrainSamples
,
Mat
&
average
,
float
&
multiplier
)
{
Mat
normali
s
edTrainSamples
=
trainSamples
.
clone
();
int
samplesCount
=
normali
s
edTrainSamples
.
rows
;
Mat
normali
z
edTrainSamples
=
trainSamples
.
clone
();
int
samplesCount
=
normali
z
edTrainSamples
.
rows
;
normalizeSamples
(
normali
s
edTrainSamples
,
average
,
multiplier
);
normalizeSamples
(
normali
z
edTrainSamples
,
average
,
multiplier
);
Mat
onesCol
=
Mat
::
ones
(
samplesCount
,
1
,
CV_32F
);
cv
::
hconcat
(
normali
s
edTrainSamples
,
onesCol
,
extendedTrainSamples
);
cv
::
hconcat
(
normali
z
edTrainSamples
,
onesCol
,
extendedTrainSamples
);
}
void
SVMSGDImpl
::
updateWeights
(
InputArray
_sample
,
bool
firstClass
,
float
gamma
,
Mat
&
weights
)
...
...
@@ -231,7 +231,7 @@ float SVMSGDImpl::calcShift(InputArray _samples, InputArray _responses) const
Mat
currentSample
=
trainSamples
.
row
(
samplesIndex
);
float
dotProduct
=
static_cast
<
float
>
(
currentSample
.
dot
(
weights_
));
bool
firstClass
=
is
FirstClass
(
trainResponses
.
at
<
float
>
(
samplesIndex
));
bool
firstClass
=
is
Positive
(
trainResponses
.
at
<
float
>
(
samplesIndex
));
int
index
=
firstClass
?
0
:
1
;
float
signToMul
=
firstClass
?
1.
f
:
-
1.
f
;
float
curDistance
=
dotProduct
*
signToMul
;
...
...
@@ -297,11 +297,10 @@ bool SVMSGDImpl::train(const Ptr<TrainData>& data, int)
int
randomNumber
=
rng
.
uniform
(
0
,
extendedTrainSamplesCount
);
//generate sample number
Mat
currentSample
=
extendedTrainSamples
.
row
(
randomNumber
);
bool
firstClass
=
isFirstClass
(
trainResponses
.
at
<
float
>
(
randomNumber
));
float
gamma
=
params
.
gamma0
*
std
::
pow
((
1
+
params
.
lambda
*
params
.
gamma0
*
(
float
)
iter
),
(
-
params
.
c
));
//update gamma
updateWeights
(
currentSample
,
firstClass
,
gamma
,
extendedWeights
);
updateWeights
(
currentSample
,
isPositive
(
trainResponses
.
at
<
float
>
(
randomNumber
))
,
gamma
,
extendedWeights
);
//average weights (only for ASGD model)
if
(
params
.
svmsgdType
==
ASGD
)
...
...
modules/ml/test/test_svmsgd.cpp
View file @
ff549527
...
...
@@ -134,7 +134,7 @@ void CV_SVMSGDTrainTest::makeTestData(Mat weights, float shift)
{
int
testSamplesCount
=
100000
;
int
featureCount
=
weights
.
cols
;
cv
::
RNG
rng
(
0
);
cv
::
RNG
rng
(
42
);
testSamples
.
create
(
testSamplesCount
,
featureCount
,
CV_32FC1
);
for
(
int
featureIndex
=
0
;
featureIndex
<
featureCount
;
featureIndex
++
)
...
...
samples/cpp/train_svmsgd.cpp
View file @
ff549527
...
...
@@ -6,8 +6,6 @@
using
namespace
cv
;
using
namespace
cv
::
ml
;
#define WIDTH 841
#define HEIGHT 594
struct
Data
{
...
...
@@ -17,6 +15,8 @@ struct Data
Data
()
{
const
int
WIDTH
=
841
;
const
int
HEIGHT
=
594
;
img
=
Mat
::
zeros
(
HEIGHT
,
WIDTH
,
CV_8UC3
);
imshow
(
"Train svmsgd"
,
img
);
}
...
...
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