OpenCV中神经网络介绍与使用

2022-09-28 14:49:27 浏览数 (1)

OpenCV中神经网络介绍与使用

一:神经网络介绍

人工神经网络(ANN) 简称神经网络(NN),最早它的产生跟并行计算有关系,主要是学习生物神经元互联触发实现学习、完成对输入数据的分类与识别。最基本的单元是神经元,有一个输入值,一个输出值,神经元本身根据激活函数来说决定输出值,最简单例子就是感知器

上述在开始的时候通过随机初始化生成权重,然后通过对数据X的训练迭代更新权重直到收敛,过程表示如下:

上述就是最简单的单个感知器工作原理。而在实际情况下,神经网络会有多个感知器,多个层级,我们把输入数据X的层称为输入层,最终输出结果的层称为输出层,中间各个层级统统称为隐藏层。一个典型的多层感知器(MLP)网络如下:

这个时候我们选择的激活函数就不能选择简单的二分类函数,OpenCV中支持的激活函数有三个:

上述网络中的权重值是未知的,只有通过训练我们才可以得到这些权重值,生成可用网络模型,OpenCV中支持的两种训练算法分别是:

  • 反向传播算法
  • RPROP算法

二:OpenCV中创建神经网络

首先创建多层感知器的层数:

代码语言:javascript复制
Mat_<int> layerSizes(1, 3);
layerSizes(0, 0) = data.cols;
layerSizes(0, 1) = 20;
layerSizes(0, 2) = responses.cols;

上面几行代码是创建一个三层的感知器,输入层跟数据维度有关系,隐藏层有20个神经元、最后是输出层,一般是类别表示。

代码语言:javascript复制
Ptr<ANN_MLP> network = ANN_MLP::create();
network->setLayerSizes(layerSizes);
network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0.1, 0.1);
network->setTrainMethod(ANN_MLP::BACKPROP, 0.1, 0.1);

上述代码是创建神经网络,设置层数、激活函数、训练方法等参数。

代码语言:javascript复制
Ptr<TrainData> trainData = TrainData::create(data, ROW_SAMPLE, responses);
network->train(trainData);

上述代码是创建训练数据,执行网络训练

三:代码演示

OpenCV3.4中的sample的代码演示如下:

代码语言:javascript复制
#include<opencv2/ml/ml.hpp>

usingnamespace std;
usingnamespace cv;
usingnamespace 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();
    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;
        }
    }

    return0;
}

四:基于神经网络的实现mnist数据集训练

代码语言:javascript复制
Mat train_images = readImages(0);
normalize(train_images, train_images, -1.0, 1.0, NORM_MINMAX, -1);
Mat train_labels = readLabels(0);
printf("n read mnist train dataset successfully...n");
Mat response = Mat::zeros(Size(10, train_labels.rows), CV_32FC1);
for(int i = 0; i < train_labels.rows; i  ) {
    int digit = train_labels.at<int>(i, 0);
    response.at<float>(i, digit) = 1;
}

//create the neural network
Mat_<int> layerSizes(1, 3);
layerSizes(0, 0) = train_images.cols;
layerSizes(0, 1) = 100;
layerSizes(0, 2) = 10;

Ptr<ANN_MLP> network = ANN_MLP::create();
network->setLayerSizes(layerSizes);
network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0.1, 0.1);
network->setTrainMethod(ANN_MLP::BACKPROP, 0.1, 0.1);
network->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 1000, 1e-6));
Ptr<TrainData> trainData = TrainData::create(train_images, ROW_SAMPLE, response);
printf("start network trainning...n");
network->train(trainData);
if(network->isTrained())
{
    printf("ready to save network model data...n");
    network->save("D:/vcprojects/images/mnist/ann_knowledge.yml");
}

test_ann_minist();
waitKey(0);
return0;

0 人点赞