Ask Your Question
0

ANN training error assertion failed

asked 2017-02-23 14:06:51 -0600

Aeyden gravatar image

i'm trying to make a simple ANN network with opencv in QT and develop it more later , i tried with simple data and i get an error says : OpenCV Error : asserion failed ((unsigned)(i1 datatype<_tp>::channels)) < unsigned(size.p[1] channels())) in cv::mat::at

here's the code i wrote

#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;
}
and for the data set i have

data.csv contains

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

and data-test.csv contains :

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,

thank you in advance for your help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-23 14:26:35 -0600

LBerger gravatar image

I think something is wrong in your data file (data.csv) :

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

trainData

[1, 2, 3, 7;
 7, 1, 7, 7;
 9, 7, 5, 3;
 12, 21, 32, 71]

trainLabels :

[2;
 5;
 2;
 8]

Train labels 5 and 8 with only 3 classes it is not possible.

exception : int id = (int)trainLabels.at<float>(i); hot.at<float>(i, id) = 1.0f;

id >=3 with only 3 columns

label for csv file is described here :

responseStartIdx Index of the first output variable. If -1, the function considers the last variable as the response

edit flag offensive delete link more

Comments

He sir , i didn't understand your answer , what do u suggest as a solution for this

Aeyden gravatar imageAeyden ( 2017-02-23 14:40:41 -0600 )edit
1

last column is responses. number of classes is 3. In this last column you have 2,5,2 and 8 value must be 0 1 or 2 because you have three classes

LBerger gravatar imageLBerger ( 2017-02-23 14:44:19 -0600 )edit
1

Thank you , that was helpful

Aeyden gravatar imageAeyden ( 2017-02-23 14:52:15 -0600 )edit

sorry to disturb u again but now after fixing it , i get another error that says : bad agrument(input training data should be a floating-point patrix with the number of rows equel to the number of training samples and the number of columns equal to the size of 0-th ( input ) layer ) in cv::ml::ANN_MLPImpl::prepare_to_train

Aeyden gravatar imageAeyden ( 2017-02-24 09:16:48 -0600 )edit

Check your data file : int input_neurons = 5; but you have only four columns (lats column is for label) input_neurons is four or add a column to csv file. of course you have to check test-data.csv file too

LBerger gravatar imageLBerger ( 2017-02-24 10:06:13 -0600 )edit

that means i have to make another file that contains the response ? or how do i separate them , my csv file contains 5 but the last one is the response , i've read in documentation that loadfromcsv will make the response is the last one if i put -1 in argument i did , and then i worked with data->getsamples and data->getlabels , i tought that would separate them

Aeyden gravatar imageAeyden ( 2017-02-24 10:49:26 -0600 )edit

add after

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

cout<<trainData <<endl;
cout<<trainLabels <endl;

you will see how opencv process your csv files

LBerger gravatar imageLBerger ( 2017-02-24 10:58:38 -0600 )edit

i did that and i found that opencv separes the data here is an image : http://zupimages.net/viewer.php?id=17/08/fvu1.png (http://zupimages.net/viewer.php?id=17...) of what i recive when i run

Aeyden gravatar imageAeyden ( 2017-02-24 11:06:06 -0600 )edit

the problem isn't in my train data , the problem i get it when i make predict i just commented the second part and i got no error

Aeyden gravatar imageAeyden ( 2017-02-24 11:10:59 -0600 )edit

i guess i knew where my error was , in the testing i didn't make a loop to test one by one my data-test had 5 rows and it should only have 1 to test , thank you so much for your help sir

Aeyden gravatar imageAeyden ( 2017-02-24 11:14:34 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-23 14:06:51 -0600

Seen: 306 times

Last updated: Feb 23 '17