天天看点

opencv实现ann mlp(多层感知机)识别手写数字 一

突然想用opencv实现一下mlp,说动手就动手。

首先查看了opencv410版本自带的mlp的例子。neural_network。这个例子就是告诉ml模块的可以实现mlp。这伞兵例子不知谁加的,可以说屁用没有。

注释过的例子代码如下

#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main()
{
    //create random training data
    Mat_<float> 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_<float> responses(data.rows, 2);
    for (int i = 0; i < data.rows; ++i)
    {
        if (i < data.rows / 2)
        {
            responses(i, 0) = 1;
            responses(i, 1) = 0;
        }
        else
        {
            responses(i, 0) = 0;
            responses(i, 1) = 1;
        }
    }
    //制作标签
    /*
    //example code for just a single response (regression)
    Mat_<float> responses(data.rows, 1);
    for (int i=0; i<responses.rows; ++i)
        responses(i, 0) = i < responses.rows / 2 ? 0 : 1;
    */

    //create the neural network
    Mat_<int> layerSizes(1, 3);
    layerSizes(0, 0) = data.cols;
    layerSizes(0, 1) = 20;
    layerSizes(0, 2) = responses.cols;//层数

    Ptr<ANN_MLP> network = ANN_MLP::create();//初始化一个ann mlp
    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 = 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; i < data.rows; ++i)
        {
            network->predict(data.row(i), result);
            cout << result << endl;
        }
        //整个流程可以说比较简单
    }

    return 0;
}
           

可以,除了告诉你函数能运行之外,没有任何参考意义。

需要再仔细解释的函数两个: setActivationFunction setTrainMethod

  1. CV_WRAP virtual void setActivationFunction(int type, double param1 = 0, double param2 = 0) = 0;

根据官方文档第一个参数type有四种:

opencv实现ann mlp(多层感知机)识别手写数字 一

除了SIGMOID_SYM(对称sigmoid)都比较容易理解。SIGMOID_SYM也是opencv默认和唯一完全支持的激活函数。

只说SIGMOID_SYM,后面两个参数指的是公式中的beta和阿尔法。文档说后两个参数默认值为零。

但是从上面的公式看,都为0时,激活函数fx=0,好像看着是这样。这点看源码可以知道,阿尔法=0会被修正为2.0/3 .beta会被修正为1.7159;。这令人误解的写法。

if( fabs(_f_param1) < FLT_EPSILON )
                _f_param1 = 2./3;
            if( fabs(_f_param2) < FLT_EPSILON )
                _f_param2 = 1.7159;
           
  1. CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;

    设置训练方法和通用参数。

训练方法有三个:

opencv实现ann mlp(多层感知机)识别手写数字 一

反向传播算法、RPROP算法 、模拟退火算法。三个老当益壮的老东西。

后面两个参数都是服务这三个优化算法的。对这俩参数的说明我直接复制opencv文档里的

method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.

param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.

param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.

对于反向传播算法而言。param1服务于bp的权重比例,可以理解为学习率、权重更新率等等吧(名字多,但是意思是一个)。param2服务bp的动量更新率。opencv的反向传播是带动量。

从这个例子源码里,能看到的就那么多了。

基本上,这篇是准备工作,分析了一下参考代码

继续阅读