opencv2笔记04-人工神经网络(ANN)
2016-11-04 09:07
330 查看
关于单个神经元的感知机和最基本的BP神经网络的模型,以及它们的推导过程,已经在之前的博文给出。点击打开链接 点击打开链接
这里参考CSDN上的博客,利用opencv接口,实现了神经网络的train和predict过程。
代码实现:
效果图:
Reference:
[1] http://blog.csdn.net/xiaowei_cqu/article/details/9027617
这里参考CSDN上的博客,利用opencv接口,实现了神经网络的train和predict过程。
代码实现:
int cv_ann() { //Setup the BPNetwork CvANN_MLP bp; // Set up BPNetwork's parameters 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; // Set up training data 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);//CvANN_MLP::SIGMOID_SYM ,选用sigmoid作为激励函数 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 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; }
效果图:
Reference:
[1] http://blog.csdn.net/xiaowei_cqu/article/details/9027617
相关文章推荐
- OpenCV2学习笔记04:图像的读取与显示
- [数据挖掘课程笔记]人工神经网络(ANN)
- opencv 笔记04Core_Blending
- 【OpenCV笔记 04】OpenCV中显式创建Mat对象的5种方法
- OpenCV学习笔记04--图片和视频的保存
- opencv机器学习 Haar特征 LBP特征 adaboost集成学习 级联分类器 支持向量机SVM 主成分分析PCA 人工神经网络(ANN) 笑脸检测 SVM分类 笑脸 检测
- 机器学习笔记之人工神经网络(ANN)
- OpenCV学习笔记(04):Mat类详解(一)
- FreeBSD学习笔记04-限制SSH登录
- 人工神经网络(Artificial Neural Netwroks)笔记-消除样本顺序的BP算法
- 人工神经网络(Artificial Neural Netwroks)笔记--离散多输出感知器训练算法
- 人工神经网络(Artificial Neural Netwroks)笔记-基本的非确定性统计训练算法
- Cxcore基础结构[OpenCV笔记1]
- TGE学习笔记04 - billboard
- 人工神经网络(Artificial Neural Netwroks)笔记-粒子群优化(Partical Swarm Optimization - PSO)
- 人工神经网络(Artificial Neural Netwroks)笔记-连续多输出感知器算法
- OpenCV学习笔记(一)
- jQuery学习笔记 04
- Taglib笔记--摘自http://tech.acnow.net/Html/Program/Java/2002-04/10/092611588.shtml
- 老友记台词学习笔记 SE01EP04(一)