OpenCV的ml模块实现了人工神经网络(Artificial Neural Networks, ANN)最典型的多层感知器(multi-layer perceptrons, MLP)模型。由于ml模型实现的算法都继承自统一的CvStatModel基类,其训练和预测的接口都是train(),predict(),非常简单。
下面来看神经网络 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_<int>(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);
layerSizes设置了有三个隐含层的网络结构:输入层,三个隐含层,输出层。输入层和输出层节点数均为5,中间隐含层每层有两个节点。
create第二个参数可以设置每个神经节点的激活函数,默认为CvANN_MLP::SIGMOID_SYM,即Sigmoid函数,同时提供的其他激活函数有Gauss和阶跃函数。
使用训练好的网络结构分类新的数据:
然后直接使用predict函数,就可以预测新的节点:
Mat sampleMat = (Mat_<float>(1,5) << i,j,0,0,0); Mat responseMat; bp.predict(sampleMat,responseMat);
完整程序代码:
int CCvMLP::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_<int>(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_<float>(1,5) << i,j,0,0,0); Mat responseMat; bp.predict(sampleMat,responseMat); float* p=responseMat.ptr<float>(0); int response=0; for(int i=0;i<5;i++){ // cout<<p[i]<<" "; response+=p[i]; } if (response >2) image.at<Vec3b>(j, i) = green; else image.at<Vec3b>(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); }
运行结果:
二:MLP用于图像分类:
二:MLP的Python版本: