OpenCV——ANN神经网络
ANN—— Artificial Neural Networks 人工神经网络
//定义人工神经网络 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.;
两种训练方法:BACKPROP 与 RPROP
BACKPROP的两个参数:
RPROP的四个参数:
// 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); // layerSizes设置了有三个隐含层的网络结构:输入层,三个隐含层,输出层。输入层和输出层节点数均为5,中间隐含层每层有两个节点 Mat layerSizes=(Mat_<int>(1,5) << 5,2,2,2,5); //create第二个参数可以设置每个神经节点的激活函数,默认为CvANN_MLP::SIGMOID_SYM,即Sigmoid函数 //同时提供的其他激活函数有Gauss(CvANN_mlp::GAUSSIAN)和阶跃函数(CvANN_MLP::IDENTITY)。
bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM); //CvANN_MLP::SIGMOID_SYM
bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params);
//预测新节点 Mat sampleMat = (Mat_<float>(1,5) << i,j,0,0,0); Mat responseMat; bp.predict(sampleMat,responseMat);
float CvANN_MLP::predict(constMat&inputs,Mat&outputs)
图像进行特征提取,把它保存在inputs里,通过调用predict函数,我们得到一个输出向量,它是一个1*nClass的行向量,
其中每一列说明它与该类的相似程度(0-1之间),也可以说是置信度。我们只用对output求一个最大值,就可得到结果。
完整代码:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/ml/ml.hpp> #include <iostream> #include <string> using namespace std; using namespace cv; int main() { CvANN_MLP bp; CvANN_MLP_TrainParams params; params.train_method=CvANN_MLP_TrainParams::BACKPROP; //(Back Propagation,BP)反向传播算法 params.bp_dw_scale=0.1; params.bp_moment_scale=0.1; float labels[10][2] = {{0.9,0.1},{0.1,0.9},{0.9,0.1},{0.1,0.9},{0.9,0.1},{0.9,0.1},{0.1,0.9},{0.1,0.9},{0.9,0.1},{0.9,0.1}}; //这里对于样本标记为0.1和0.9而非0和1,主要是考虑到sigmoid函数的输出为一般为0和1之间的数,只有在输入趋近于-∞和+∞才逐渐趋近于0和1,而不可能达到。 Mat labelsMat(10, 2, CV_32FC1, labels); float trainingData[10][2] = { {11,12},{111,112}, {21,22}, {211,212},{51,32}, {71,42}, {441,412},{311,312}, {41,62}, {81,52} }; Mat trainingDataMat(10, 2, CV_32FC1, trainingData); Mat layerSizes=(Mat_<int>(1,5) << 2, 2, 2, 2, 2); //5层:输入层,3层隐藏层和输出层,每层均为两个perceptron bp.create(layerSizes,CvANN_MLP::SIGMOID_SYM); bp.train(trainingDataMat, labelsMat, Mat(),Mat(), params); int width = 512, height = 512; Mat image = Mat::zeros(height, width, CV_8UC3); Vec3b green(0,255,0), blue (255,0,0); for (int i = 0; i < image.rows; ++i) { for (int j = 0; j < image.cols; ++j) { Mat sampleMat = (Mat_<float>(1,2) << i,j); Mat responseMat; bp.predict(sampleMat,responseMat); float* p=responseMat.ptr<float>(0); // if (p[0] > p[1]) { 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(111, 112), 5, Scalar( 0, 0, 0), thickness, lineType); circle( image, Point(211, 212), 5, Scalar( 0, 0, 0), thickness, lineType); circle( image, Point(441, 412), 5, Scalar( 0, 0, 0), thickness, lineType); circle( image, Point(311, 312), 5, Scalar( 0, 0, 0), thickness, lineType); circle( image, Point(11, 12), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(21, 22), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(51, 32), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(71, 42), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(41, 62), 5, Scalar(255, 255, 255), thickness, lineType); circle( image, Point(81, 52), 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); return 0; }