From d72625d506f564afea3e57cf309644b76f9bf9ef Mon Sep 17 00:00:00 2001 From: CSBVision Date: Thu, 19 May 2016 15:40:42 +0200 Subject: [PATCH] neural_network.cpp added --- samples/cpp/neural_network.cpp | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 samples/cpp/neural_network.cpp diff --git a/samples/cpp/neural_network.cpp b/samples/cpp/neural_network.cpp new file mode 100644 index 0000000000..e0ef6fa9ae --- /dev/null +++ b/samples/cpp/neural_network.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; +using namespace cv; +using namespace cv::ml; + +int main() +{ + //create random training data + Mat_ 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_ responses(data.rows, 2); + for (int i = 0; i responses(data.rows, 1); + for (int i=0; i layerSizes(1, 3); + layerSizes(0, 0) = data.cols; + layerSizes(0, 1) = 20; + layerSizes(0, 2) = responses.cols; + + Ptr 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::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; ipredict(data.row(i), result); + cout << result << endl; + } + } + + return 0; +}