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
17a2480a
Commit
17a2480a
authored
Apr 19, 2011
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
integrated parallel SVM prediction; fixed warnings after meanshift integration
parent
537a3611
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
133 additions
and
10 deletions
+133
-10
ml.hpp
modules/ml/include/opencv2/ml/ml.hpp
+3
-2
svm.cpp
modules/ml/src/svm.cpp
+39
-1
matching.cpp
modules/objdetect/src/matching.cpp
+1
-1
ms_grouping.cpp
modules/objdetect/src/ms_grouping.cpp
+10
-4
letter_recog.cpp
samples/cpp/letter_recog.cpp
+80
-2
No files found.
modules/ml/include/opencv2/ml/ml.hpp
View file @
17a2480a
...
...
@@ -543,7 +543,8 @@ public:
bool
balanced
=
false
);
virtual
float
predict
(
const
CvMat
*
sample
,
bool
returnDFVal
=
false
)
const
;
virtual
float
predict
(
const
CvMat
*
samples
,
CvMat
*
results
)
const
;
#ifndef SWIG
CV_WRAP
CvSVM
(
const
cv
::
Mat
&
trainData
,
const
cv
::
Mat
&
responses
,
const
cv
::
Mat
&
varIdx
=
cv
::
Mat
(),
const
cv
::
Mat
&
sampleIdx
=
cv
::
Mat
(),
...
...
@@ -563,7 +564,7 @@ public:
CvParamGrid
coeffGrid
=
CvSVM
::
get_default_grid
(
CvSVM
::
COEF
),
CvParamGrid
degreeGrid
=
CvSVM
::
get_default_grid
(
CvSVM
::
DEGREE
),
bool
balanced
=
false
);
CV_WRAP
virtual
float
predict
(
const
cv
::
Mat
&
sample
,
bool
returnDFVal
=
false
)
const
;
CV_WRAP
virtual
float
predict
(
const
cv
::
Mat
&
sample
,
bool
returnDFVal
=
false
)
const
;
#endif
CV_WRAP
virtual
int
get_support_vector_count
()
const
;
...
...
modules/ml/src/svm.cpp
View file @
17a2480a
...
...
@@ -2081,7 +2081,7 @@ float CvSVM::predict( const CvMat* sample, bool returnDFVal ) const
CV_CALL
(
cvPreparePredictData
(
sample
,
var_all
,
var_idx
,
class_count
,
0
,
&
row_sample
));
result
=
predict
(
row_sample
,
get_var_count
(),
returnDFVal
);
__END__
;
if
(
sample
&&
(
!
CV_IS_MAT
(
sample
)
||
sample
->
data
.
fl
!=
row_sample
)
)
...
...
@@ -2090,6 +2090,44 @@ float CvSVM::predict( const CvMat* sample, bool returnDFVal ) const
return
result
;
}
struct
predict_body
{
predict_body
(
const
CvSVM
*
_pointer
,
float
*
_result
,
const
CvMat
*
_samples
,
CvMat
*
_results
)
{
pointer
=
_pointer
;
result
=
_result
;
samples
=
_samples
;
results
=
_results
;
}
const
CvSVM
*
pointer
;
float
*
result
;
const
CvMat
*
samples
;
CvMat
*
results
;
void
operator
()(
const
cv
::
BlockedRange
&
range
)
const
{
for
(
int
i
=
range
.
begin
();
i
<
range
.
end
();
i
++
)
{
CvMat
sample
;
cvGetRow
(
samples
,
&
sample
,
i
);
int
r
=
(
int
)
pointer
->
predict
(
&
sample
);
if
(
results
)
results
->
data
.
fl
[
i
]
=
r
;
if
(
i
==
0
)
*
result
=
r
;
}
}
};
float
CvSVM
::
predict
(
const
CvMat
*
samples
,
CV_OUT
CvMat
*
results
)
const
{
float
result
=
0
;
cv
::
parallel_for
(
cv
::
BlockedRange
(
0
,
samples
->
rows
),
predict_body
(
this
,
&
result
,
samples
,
results
)
);
return
result
;
}
CvSVM
::
CvSVM
(
const
Mat
&
_train_data
,
const
Mat
&
_responses
,
const
Mat
&
_var_idx
,
const
Mat
&
_sample_idx
,
CvSVMParams
_params
)
...
...
modules/objdetect/src/matching.cpp
View file @
17a2480a
...
...
@@ -1396,7 +1396,7 @@ int createSchedule(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all
const
int
threadsNum
,
int
*
kLevels
,
int
**
processingLevels
)
{
int
rootFilterDim
,
sumPartFiltersDim
,
i
,
numLevels
,
dbx
,
dby
,
numDotProducts
;
int
averNumDotProd
,
j
,
minValue
,
argMin
,
tmp
,
lambda
,
maxValue
,
k
;
int
averNumDotProd
,
j
,
minValue
,
argMin
,
lambda
,
maxValue
,
k
;
int
*
dotProd
,
*
weights
,
*
disp
;
if
(
H
==
NULL
||
all_F
==
NULL
)
{
...
...
modules/objdetect/src/ms_grouping.cpp
View file @
17a2480a
...
...
@@ -44,11 +44,17 @@
using
namespace
cv
;
MeanshiftGrouping
::
MeanshiftGrouping
(
const
Point3d
&
densKer
,
const
vector
<
Point3d
>&
posV
,
const
vector
<
double
>&
wV
,
double
modeEps
,
int
maxIter
)
:
densityKernel
(
densKer
),
weightsV
(
wV
),
positionsV
(
posV
),
positionsCount
(
posV
.
size
()),
meanshiftV
(
positionsCount
),
distanceV
(
positionsCount
),
modeEps
(
modeEps
),
iterMax
(
maxIter
)
const
vector
<
double
>&
wV
,
double
modeEps
,
int
maxIter
)
{
densityKernel
=
densKer
;
weightsV
=
wV
;
positionsV
=
posV
;
positionsCount
=
posV
.
size
();
meanshiftV
.
resize
(
positionsCount
);
distanceV
.
resize
(
positionsCount
);
modeEps
=
modeEps
;
iterMax
=
maxIter
;
for
(
unsigned
i
=
0
;
i
<
positionsV
.
size
();
i
++
)
{
meanshiftV
[
i
]
=
getNewValue
(
positionsV
[
i
]);
...
...
samples/cpp/letter_recog.cpp
View file @
17a2480a
...
...
@@ -9,7 +9,7 @@
void
help
()
{
printf
(
"
\n
The sample demonstrates how to train Random Trees classifier
\n
"
"(or Boosting classifier, or MLP, or Knearest, or Nbayes - see main()) using the provided dataset.
\n
"
"(or Boosting classifier, or MLP, or Knearest, or Nbayes
, or Support Vector Machines
- see main()) using the provided dataset.
\n
"
"
\n
"
"We use the sample database letter-recognition.data
\n
"
"from UCI Repository, here is the link:
\n
"
...
...
@@ -28,7 +28,7 @@ void help()
"The usage: letter_recog [-data <path to letter-recognition.data>]
\\\n
"
" [-save <output XML file for the classifier>]
\\\n
"
" [-load <XML file with the pre-trained classifier>]
\\\n
"
" [-boost|-mlp|-knearest|-nbayes
] # to use boost/mlp/knearest
classifier instead of default Random Trees
\n
"
);
" [-boost|-mlp|-knearest|-nbayes
|-svm] # to use boost/mlp/knearest/SVM
classifier instead of default Random Trees
\n
"
);
}
// This function reads data and responses from the file <filename>
...
...
@@ -630,6 +630,78 @@ int build_nbayes_classifier( char* data_filename )
return
0
;
}
static
int
build_svm_classifier
(
char
*
data_filename
)
{
CvMat
*
data
=
0
;
CvMat
*
responses
=
0
;
CvMat
train_data
;
int
nsamples_all
=
0
,
ntrain_samples
=
0
;
int
var_count
;
CvSVM
svm
;
int
ok
=
read_num_class_data
(
data_filename
,
16
,
&
data
,
&
responses
);
if
(
!
ok
)
{
printf
(
"Could not read the database %s
\n
"
,
data_filename
);
return
-
1
;
}
////////// SVM parameters ///////////////////////////////
CvSVMParams
param
;
param
.
kernel_type
=
CvSVM
::
LINEAR
;
param
.
svm_type
=
CvSVM
::
C_SVC
;
param
.
C
=
1
;
///////////////////////////////////////////////////////////
printf
(
"The database %s is loaded.
\n
"
,
data_filename
);
nsamples_all
=
data
->
rows
;
ntrain_samples
=
(
int
)(
nsamples_all
*
0.1
);
var_count
=
data
->
cols
;
// train classifier
printf
(
"Training the classifier (may take a few minutes)...
\n
"
);
cvGetRows
(
data
,
&
train_data
,
0
,
ntrain_samples
);
CvMat
*
train_resp
=
cvCreateMat
(
ntrain_samples
,
1
,
CV_32FC1
);
for
(
int
i
=
0
;
i
<
ntrain_samples
;
i
++
)
train_resp
->
data
.
fl
[
i
]
=
responses
->
data
.
fl
[
i
];
svm
.
train
(
&
train_data
,
train_resp
,
0
,
0
,
param
);
// classification
float
_sample
[
var_count
*
(
nsamples_all
-
ntrain_samples
)];
CvMat
sample
=
cvMat
(
nsamples_all
-
ntrain_samples
,
16
,
CV_32FC1
,
_sample
);
float
true_results
[
nsamples_all
-
ntrain_samples
];
for
(
int
j
=
ntrain_samples
;
j
<
nsamples_all
;
j
++
)
{
float
*
s
=
data
->
data
.
fl
+
j
*
var_count
;
for
(
int
i
=
0
;
i
<
var_count
;
i
++
)
{
sample
.
data
.
fl
[(
j
-
ntrain_samples
)
*
var_count
+
i
]
=
s
[
i
];
}
true_results
[
j
-
ntrain_samples
]
=
responses
->
data
.
fl
[
j
];
}
CvMat
*
result
=
cvCreateMat
(
1
,
nsamples_all
-
ntrain_samples
,
CV_32FC1
);
printf
(
"Classification (may take a few minutes)...
\n
"
);
(
int
)
svm
.
predict
(
&
sample
,
result
);
int
true_resp
=
0
;
for
(
int
i
=
0
;
i
<
nsamples_all
-
ntrain_samples
;
i
++
)
{
if
(
result
->
data
.
fl
[
i
]
==
true_results
[
i
])
true_resp
++
;
}
printf
(
"true_resp = %f%%
\n
"
,
(
float
)
true_resp
/
(
nsamples_all
-
ntrain_samples
)
*
100
);
cvReleaseMat
(
&
train_resp
);
cvReleaseMat
(
&
result
);
cvReleaseMat
(
&
data
);
cvReleaseMat
(
&
responses
);
return
0
;
}
int
main
(
int
argc
,
char
*
argv
[]
)
{
char
*
filename_to_save
=
0
;
...
...
@@ -672,6 +744,10 @@ int main( int argc, char *argv[] )
{
method
=
4
;
}
else
if
(
strcmp
(
argv
[
i
],
"-svm"
)
==
0
)
{
method
=
5
;
}
else
break
;
}
...
...
@@ -687,6 +763,8 @@ int main( int argc, char *argv[] )
build_knearest_classifier
(
data_filename
,
10
)
:
method
==
4
?
build_nbayes_classifier
(
data_filename
)
:
method
==
5
?
build_svm_classifier
(
data_filename
)
:
-
1
)
<
0
)
{
help
();
...
...
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