gpt4 book ai didi

c++ - ANN OPENCV 错误断言失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:16 26 4
gpt4 key购买 nike

我正在尝试在 QT 中使用 opencv 制作一个简单的 ANN 网络,稍后再开发它, 我尝试使用简单的数据,但收到一条错误消息:OpenCV Error : asserion failed ((unsigned)(i1 *datatype<_tp>::channels)) < unsigned(size.p[1]* channels())) in cv::mat::at

这是我写的代码

#include <iostream>
#include <opencv2/ml.hpp>
#include <opencv/cv.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "nnet.h"
using namespace std;
using namespace cv;


int main()
{
string filename="data.csv";
Ptr<cv::ml::TrainData> tdata = cv::ml::TrainData::loadFromCSV(filename,0,-1,-1);

Mat trainData = tdata->getTrainSamples();
Mat trainLabels = tdata->getTrainResponses();
int numClasses = 3;

Mat hot(trainLabels.rows, numClasses, CV_32F, 0.0f);

for (int i=0; i<trainLabels.rows; i++) {
int id = (int)trainLabels.at<float>(i);
hot.at<float>(i, id) = 1.0f;
}

int input_neurons = 5;
int hidden_neurons = 5;
int output_neurons = 3;

Mat layerSizes = Mat(3, 1, CV_32SC1);
layerSizes.row(0) = Scalar(input_neurons);
layerSizes.row(1) = Scalar(hidden_neurons);
layerSizes.row(2) = Scalar(output_neurons);

Ptr<cv::ml::ANN_MLP> myNetwork = cv::ml::ANN_MLP::create();

myNetwork->setLayerSizes(layerSizes);
myNetwork->setTrainMethod(ml::ANN_MLP::SIGMOID_SYM);
myNetwork->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 1000, 0.00001f));
myNetwork->setTrainMethod(ml::ANN_MLP::BACKPROP,0.1f,0.1f);
myNetwork->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM, 1, 1);

myNetwork->train(trainData, 0, hot);

string testfilename="test-data.csv";

Ptr<cv::ml::TrainData> testdata = cv::ml::TrainData::loadFromCSV(testfilename, 0,0,-1);

Mat testData = testdata->getTrainSamples();
Mat testLabels = testdata->getTrainResponses();
Mat testResults;

myNetwork->predict(testData, testResults);
float accuracy = float(countNonZero(testResults == testLabels)) / testLabels.rows;
printf("%f",accuracy);

return 0;
}

对于我拥有的数据集

data.csv 包含

1,2,3,7,2
7,1,7,7,5
9,7,5,3,2
12,21,32,71,8

和 data-test.csv 包含:

1,2,1,1,2,
9,1,2,12,5,
11,28,14,50,8,
3,1,2,12,5,
11,28,24,20,8,

预先感谢您的帮助。

最佳答案

我找到了我的问题的解决方案,在 csv 文件中我有 3 个类并且响应值应该在 [0..2] 之间并且我给了随机数 5 和 8 所以改变它们解决了这个问题

关于c++ - ANN OPENCV 错误断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42424324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com