使用神经网络与opencv 3.1.0

时间:2016-03-13 16:22:43

标签: c++ opencv

我试图用OpenCV 3.1.0实现神经网络。在预测时,我得到一个带有-1。#QNAN值的向量。我做错了什么?

// train
Ptr<ANN_MLP> ann = ml::ANN_MLP::create();
Mat layers(1, 3, CV_32F);
layers.at<float>(0) = features.cols;
layers.at<float>(1) = nlayers;
layers.at<float>(2) = numLabels;
ann->setActivationFunction(ANN_MLP::SIGMOID_SYM);
ann->setLayerSizes(layers);
Mat trainClasses;
trainClasses.create(features.rows, numLabels, CV_32F);
for (int i = 0; i < trainClasses.rows; i++)
{
    for (int k = 0; k < trainClasses.cols; k++)
    {
        if (k == labels[i])
            trainClasses.at<float>(i, k) = 1;
        else
            trainClasses.at<float>(i, k) = 0;
    }

}
Mat weights(1, features.rows, CV_32F, Scalar::all(1));
Ptr<TrainData> tdata = TrainData::create(features, ROW_SAMPLE,
trainClasses, Mat(), Mat(), weights, Mat());
ann->train(tdata);

// predict
Mat output(1, numLabels, CV_32F);
ann->predict(test_data, output);

1 个答案:

答案 0 :(得分:-1)

我是神经网络新手,但我猜你的输入图层应该有神经元等于整体属性大小。

假设您有5张10 * 10大小的图像用于训练,那么您应该为每个像素输入神经元。也就是说,5 * 10 * 10 = 500。