下面来看神经网络CvANN_MLP 的使用~
//Setup the BPNetwork
CvANN_MLP bp;
// Set up BPNetwork's parameters
CvANN_MLP_TrainParams params;
params.train_method=CvANN_MLP_TrainParams::BACKPROP;
params.bp_dw_scale=0.1;
params.bp_moment_scale=0.1;
//params.train_method=CvANN_MLP_TrainParams::RPROP;
//params.rp_dw0 = 0.1;
//params.rp_dw_plus = 1.2;
//params.rp_dw_minus = 0.5;
//params.rp_dw_min = FLT_EPSILON;
//params.rp_dw_max = 50.;
可以直接定义CvANN_MLP神经网络,并设置其参数。BACKPROP表示使用back-propagation的训练方法,RPROP即最简单的propagation训练方法。
使用BACKPROP有两个相关参数:bp_dw_scale即bp_moment_scale:
使用PRPOP有四个相关参数:rp_dw0, rp_dw_plus, rp_dw_minus, rp_dw_min, rp_dw_max:
上述代码中为其默认值。
// Set up training data
float labels[3][5] = {{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0}};
Mat labelsMat(3, 5, CV_32FC1, labels);
float trainingData[3][5] = { {1,2,3,4,5},{111,112,113,114,115}, {21,22,23,24,25} };
Mat trainingDataMat(3, 5, CV_32FC1, trainingData);
Mat layerSizes=(Mat_(1,5) << 5,2,2,2,5);
bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM);//CvANN_MLP::SIGMOID_SYM
//CvANN_MLP::GAUSSIAN
//CvANN_MLP::IDENTITY
bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);
create第二个参数可以设置每个神经节点的激活函数,默认为CvANN_MLP::SIGMOID_SYM,即Sigmoid函数,同时提供的其他激活函数有Gauss和阶跃函数。
然后直接使用predict函数,就可以预测新的节点:
Mat sampleMat = (Mat_(1,5) << i,j,0,0,0);
Mat responseMat;
bp.predict(sampleMat,responseMat);
//The example of using BPNetwork in OpenCV
//Coded by L. Wei
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
//Setup the BPNetwork
CvANN_MLP bp;
// Set up BPNetwork's parameters
CvANN_MLP_TrainParams params;
params.train_method=CvANN_MLP_TrainParams::BACKPROP;
params.bp_dw_scale=0.1;
params.bp_moment_scale=0.1;
//params.train_method=CvANN_MLP_TrainParams::RPROP;
//params.rp_dw0 = 0.1;
//params.rp_dw_plus = 1.2;
//params.rp_dw_minus = 0.5;
//params.rp_dw_min = FLT_EPSILON;
//params.rp_dw_max = 50.;
// Set up training data
float labels[3][5] = {{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0}};
Mat labelsMat(3, 5, CV_32FC1, labels);
float trainingData[3][5] = { {1,2,3,4,5},{111,112,113,114,115}, {21,22,23,24,25} };
Mat trainingDataMat(3, 5, CV_32FC1, trainingData);
Mat layerSizes=(Mat_(1,5) << 5,2,2,2,5);
bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM);//CvANN_MLP::SIGMOID_SYM
//CvANN_MLP::GAUSSIAN
//CvANN_MLP::IDENTITY
bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);
// Data for visual representation
int width = 512, height = 512;
Mat image = Mat::zeros(height, width, CV_8UC3);
Vec3b green(0,255,0), blue (255,0,0);
// Show the decision regions given by the SVM
for (int i = 0; i < image.rows; ++i)
for (int j = 0; j < image.cols; ++j)
{
Mat sampleMat = (Mat_(1,5) << i,j,0,0,0);
Mat responseMat;
bp.predict(sampleMat,responseMat);
float* p=responseMat.ptr(0);
float response=0.0f;
for(int k=0;k<5;i++){
// cout<2)
image.at(j, i) = green;
else
image.at(j, i) = blue;
}
// Show the training data
int thickness = -1;
int lineType = 8;
circle( image, Point(501, 10), 5, Scalar( 0, 0, 0), thickness, lineType);
circle( image, Point(255, 10), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType);
circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType);
imwrite("result.png", image); // save the image
imshow("BP Simple Example", image); // show it to the user
waitKey(0);
}