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
e13f6ded
Commit
e13f6ded
authored
Aug 31, 2018
by
berak
Committed by
Vadim Pisarevsky
Aug 31, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ml: fix adjusting K in KNearest (#12358)
parent
4b03a4a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
4 deletions
+27
-4
knearest.cpp
modules/ml/src/knearest.cpp
+5
-4
test_emknearestkmeans.cpp
modules/ml/test/test_emknearestkmeans.cpp
+22
-0
No files found.
modules/ml/src/knearest.cpp
View file @
e13f6ded
...
...
@@ -140,13 +140,12 @@ public:
String
getModelName
()
const
CV_OVERRIDE
{
return
NAME_BRUTE_FORCE
;
}
int
getType
()
const
CV_OVERRIDE
{
return
ml
::
KNearest
::
BRUTE_FORCE
;
}
void
findNearestCore
(
const
Mat
&
_samples
,
int
k
0
,
const
Range
&
range
,
void
findNearestCore
(
const
Mat
&
_samples
,
int
k
,
const
Range
&
range
,
Mat
*
results
,
Mat
*
neighbor_responses
,
Mat
*
dists
,
float
*
presult
)
const
{
int
testidx
,
baseidx
,
i
,
j
,
d
=
samples
.
cols
,
nsamples
=
samples
.
rows
;
int
testcount
=
range
.
end
-
range
.
start
;
int
k
=
std
::
min
(
k0
,
nsamples
);
AutoBuffer
<
float
>
buf
(
testcount
*
k
*
2
);
float
*
dbuf
=
buf
.
data
();
...
...
@@ -215,7 +214,7 @@ public:
float
*
nr
=
neighbor_responses
->
ptr
<
float
>
(
testidx
+
range
.
start
);
for
(
j
=
0
;
j
<
k
;
j
++
)
nr
[
j
]
=
rbuf
[
testidx
*
k
+
j
];
for
(
;
j
<
k
0
;
j
++
)
for
(
;
j
<
k
;
j
++
)
nr
[
j
]
=
0.
f
;
}
...
...
@@ -224,7 +223,7 @@ public:
float
*
dptr
=
dists
->
ptr
<
float
>
(
testidx
+
range
.
start
);
for
(
j
=
0
;
j
<
k
;
j
++
)
dptr
[
j
]
=
dbuf
[
testidx
*
k
+
j
];
for
(
;
j
<
k
0
;
j
++
)
for
(
;
j
<
k
;
j
++
)
dptr
[
j
]
=
0.
f
;
}
...
...
@@ -307,6 +306,7 @@ public:
{
float
result
=
0.
f
;
CV_Assert
(
0
<
k
);
k
=
std
::
min
(
k
,
samples
.
rows
);
Mat
test_samples
=
_samples
.
getMat
();
CV_Assert
(
test_samples
.
type
()
==
CV_32F
&&
test_samples
.
cols
==
samples
.
cols
);
...
...
@@ -363,6 +363,7 @@ public:
{
float
result
=
0.
f
;
CV_Assert
(
0
<
k
);
k
=
std
::
min
(
k
,
samples
.
rows
);
Mat
test_samples
=
_samples
.
getMat
();
CV_Assert
(
test_samples
.
type
()
==
CV_32F
&&
test_samples
.
cols
==
samples
.
cols
);
...
...
modules/ml/test/test_emknearestkmeans.cpp
View file @
e13f6ded
...
...
@@ -702,4 +702,26 @@ TEST(ML_EM, accuracy) { CV_EMTest test; test.safe_run(); }
TEST
(
ML_EM
,
save_load
)
{
CV_EMTest_SaveLoad
test
;
test
.
safe_run
();
}
TEST
(
ML_EM
,
classification
)
{
CV_EMTest_Classification
test
;
test
.
safe_run
();
}
TEST
(
ML_KNearest
,
regression_12347
)
{
Mat
xTrainData
=
(
Mat_
<
float
>
(
5
,
2
)
<<
1
,
1.1
,
1.1
,
1
,
2
,
2
,
2.1
,
2
,
2.1
,
2.1
);
Mat
yTrainLabels
=
(
Mat_
<
float
>
(
5
,
1
)
<<
1
,
1
,
2
,
2
,
2
);
Ptr
<
KNearest
>
knn
=
KNearest
::
create
();
knn
->
train
(
xTrainData
,
ml
::
ROW_SAMPLE
,
yTrainLabels
);
Mat
xTestData
=
(
Mat_
<
float
>
(
2
,
2
)
<<
1.1
,
1.1
,
2
,
2.2
);
Mat
zBestLabels
,
neighbours
,
dist
;
// check output shapes:
int
K
=
16
,
Kexp
=
std
::
min
(
K
,
xTrainData
.
rows
);
knn
->
findNearest
(
xTestData
,
K
,
zBestLabels
,
neighbours
,
dist
);
EXPECT_EQ
(
xTestData
.
rows
,
zBestLabels
.
rows
);
EXPECT_EQ
(
neighbours
.
cols
,
Kexp
);
EXPECT_EQ
(
dist
.
cols
,
Kexp
);
// see if the result is still correct:
K
=
2
;
knn
->
findNearest
(
xTestData
,
K
,
zBestLabels
,
neighbours
,
dist
);
EXPECT_EQ
(
1
,
zBestLabels
.
at
<
float
>
(
0
,
0
));
EXPECT_EQ
(
2
,
zBestLabels
.
at
<
float
>
(
1
,
0
));
}
}}
// namespace
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