为什么我的C ++ OpenCV 3.4.1神经网络预测如此糟糕?

时间:2018-06-01 17:21:25

标签: c++ opencv machine-learning neural-network opencv3.1

我正在尝试使用OpenCV 3.4.1在C ++中开发一个人工神经网络,目的是能够识别33个不同的字符,包括数字和字母,但我得到的结果总是错误的。

我已经使用不同的参数值测试了我的代码,例如我用于训练的sigmoid函数的alpha和beta,反向传播参数或隐藏节点的数量但是,虽然结果有时变化,但它通常会趋于成为以下形状的矢量:

分类结果:

  

[20.855789,-0.033862107,-0.0053131776,0.026316155,-0.0032050854,   0.036046479,-0.025410429,-0.017537225,0.015429396,-0.023276867,0.013653283,-0.025660357,-0.051959664,-0.0032470606,0.032143779,-0.011631044,0.022339549,0.041757714,0.04414707,-0.044756029,0.042280547,0.012204648,0.026924053,0.016814215,-0.028257577,0.05190875 ,-0.0070033628,-0.0084492415,-0.040644459,0.00022287761,-0.0376678,-0.0021550131,-0.015310903]

也就是说,无论我测试哪个字符,它总是预测分析的字符是字符向量的第一个位置,它对应于数字'1'。

训练数据来自我创建的.XML,其中包含474个样本(行),每个样本有265个属性(cols)。至于培训课程,根据我在本论坛上一个问题中找到的一些建议,它是从另一个.XML文件中获得的,该文件包含474行,每个训练样本一个,33列,每个字符/类一个。 / p>

我附上下面的代码,以便您可以好好猜测我做错了什么,我非常感谢您提供任何帮助! :)

//Create the Neural Network
Mat_<int> layerSizes(1, 3);
layerSizes(0, 0) = numFeaturesPerSample;
layerSizes(0, 1) = nlayers;
layerSizes(0, 2) = numClasses;

//Set ANN params
Ptr<ANN_MLP> network = ANN_MLP::create();
network->setLayerSizes(layerSizes);
network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0.6, 1); 
network->setTrainMethod(ANN_MLP::BACKPROP, 0.1, 0.1);
Ptr<TrainData> trainData = TrainData::create(TrainingData, ROW_SAMPLE, classes);
network->train(trainData);

//Predict
if (network->isTrained())
{
    trained = true;
    Mat results;
    cout << "Predict:" << endl;
    network->predict(features, results); 
    cout << "Prediction done!" << endl;
    cout << endl << "Classification result: " << endl << results << endl;

    //We need to know where in output is the max val, the x (cols) is the class.
    Point maxLoc;
    double maxVal;
    minMaxLoc(results, 0, &maxVal, 0, &maxLoc);

    return maxLoc.x;
}

0 个答案:

没有答案