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
d72625d5
Commit
d72625d5
authored
May 19, 2016
by
CSBVision
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
neural_network.cpp added
parent
764a1f59
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
0 deletions
+65
-0
neural_network.cpp
samples/cpp/neural_network.cpp
+65
-0
No files found.
samples/cpp/neural_network.cpp
0 → 100644
View file @
d72625d5
#include <opencv2/ml/ml.hpp>
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
ml
;
int
main
()
{
//create random training data
Mat_
<
float
>
data
(
100
,
100
);
randn
(
data
,
Mat
::
zeros
(
1
,
1
,
data
.
type
()),
Mat
::
ones
(
1
,
1
,
data
.
type
()));
//half of the samples for each class
Mat_
<
float
>
responses
(
data
.
rows
,
2
);
for
(
int
i
=
0
;
i
<
data
.
rows
;
++
i
)
{
if
(
i
<
data
.
rows
/
2
)
{
data
(
i
,
0
)
=
1
;
data
(
i
,
1
)
=
0
;
}
else
{
data
(
i
,
0
)
=
0
;
data
(
i
,
1
)
=
1
;
}
}
/*
//example code for just a single response (regression)
Mat_<float> responses(data.rows, 1);
for (int i=0; i<responses.rows; ++i)
responses(i, 0) = i < responses.rows / 2 ? 0 : 1;
*/
//create the neural network
Mat_
<
int
>
layerSizes
(
1
,
3
);
layerSizes
(
0
,
0
)
=
data
.
cols
;
layerSizes
(
0
,
1
)
=
20
;
layerSizes
(
0
,
2
)
=
responses
.
cols
;
Ptr
<
ANN_MLP
>
network
=
ANN_MLP
::
create
();
network
->
setLayerSizes
(
layerSizes
);
network
->
setActivationFunction
(
ANN_MLP
::
SIGMOID_SYM
,
0.1
,
0.1
);
network
->
setTrainMethod
(
ANN_MLP
::
BACKPROP
,
0.1
,
0.1
);
Ptr
<
TrainData
>
trainData
=
TrainData
::
create
(
data
,
ROW_SAMPLE
,
responses
);
network
->
train
(
trainData
);
if
(
network
->
isTrained
())
{
printf
(
"Predict one-vector:
\n
"
);
Mat
result
;
network
->
predict
(
Mat
::
ones
(
1
,
data
.
cols
,
data
.
type
()),
result
);
cout
<<
result
<<
endl
;
printf
(
"Predict training data:
\n
"
);
for
(
int
i
=
0
;
i
<
data
.
rows
;
++
i
)
{
network
->
predict
(
data
.
row
(
i
),
result
);
cout
<<
result
<<
endl
;
}
}
return
0
;
}
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