digit recognition using ANN_MLP opencv 3.0 error

asked 2016-12-30 09:37:59 -0500

shreya gravatar image

updated 2016-12-30 12:55:03 -0500

[C:\fakepath\csv file.png] (/upfiles/1483124022567734.png)C:\fakepath\error.png C:\fakepath\error.pngthis related to the previous question i had asked: http://answers.opencv.org/question/12... i am trying to build digit recognition project using ANN_MLP open cv 3.0 and the code is as follows:

C:\fakepath\csv file.png

void main()
{

int inputLayerSize = 1;
int outputLayerSize = 1;
int numSamples = 2;
Mat layers = Mat(3, 1, CV_32S);
layers.row(0) =Scalar(35591) ;
layers.row(1) = Scalar(100);
layers.row(2) = Scalar(10);
vector<int> layerSizes;
layerSizes[0]=35591;
layerSizes[1]=1000;
layerSizes[2]=10;

Ptr<ANN_MLP> nnPtr =ANN_MLP::create();


//nnPtr->setLayerSizes(3);
nnPtr->setLayerSizes(layers);
nnPtr->setTrainMethod(ANN_MLP::BACKPROP);

nnPtr->setTermCriteria(TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 1000, 0.00001f));

nnPtr->setActivationFunction(ANN_MLP::SIGMOID_SYM, 1, 1);
nnPtr->setBackpropWeightScale(0.5f);
nnPtr->setBackpropMomentumScale(0.5f);

Ptr<TrainData> tdata = TrainData::loadFromCSV("C:/Users/Shreya Srivastava/Desktop/train.csv",
       0, // lines to skip
       0, // 1st elem is the label
      -1); // only 1 response per line


Mat trainData = tdata->getTrainSamples();
Mat trainLabels = tdata->getTrainResponses();
int numClasses = 10; // assuming mnist
Mat hot(trainLabels.rows, numClasses, CV_32F, 0.0f); // all zero, initially
for (int i=0; i<trainLabels.rows; i++)
{
        int id = (int)trainLabels.at<float>(i);
        hot.at<float>(i, id) = 1.0f; 
}
nnPtr->train(trainData,0,hot);

bool m = nnPtr->isTrained();
if (m)
    std::cout << "training complete\n\n";


Ptr<TrainData> mdata = TrainData::loadFromCSV("C:/Users/Shreya Srivastava/Desktop/test.csv", 0,0,-1);
Mat testData = mdata->getTrainSamples();
Mat testLabels = mdata->getTrainResponses();
Mat testResults;
nnPtr->predict(testData, testResults);
float accuracy = float(countNonZero(testResults == testLabels)) / testLabels.rows;

printf("the accuracy is %f",accuracy);
}

i dont understand why i am getting assertion failed error

edit retag flag offensive close merge delete

Comments

aand the error is ?

berak gravatar imageberak ( 2016-12-30 09:53:16 -0500 )edit
1

vector<int> layerSizes; layerSizes[0]=35591; layerSizes[1]=1000; layerSizes[2]=10; most probably something wrong here

shreya gravatar imageshreya ( 2016-12-30 10:18:36 -0500 )edit

indeed ! try:

vector<int> layerSizes(3);

(you need to allocate space there, before you can use the [] operator !)

on the other hand, that vector is never used ! (just delete that part)

berak gravatar imageberak ( 2016-12-30 10:25:33 -0500 )edit

also, it could simply be:

Mat_<int> layers(3, 1);
layers << 35591, 1000, 10;
berak gravatar imageberak ( 2016-12-30 10:29:35 -0500 )edit

what method should be used for model selection of ann? like the no of layers and nodes? is it hit and trial method and see maximum accuracy?

shreya gravatar imageshreya ( 2016-12-30 10:40:00 -0500 )edit

exactly, - trial & error. (and lots of time ...)

since the step from 35591 to 1000 is so large, maybe insert another layer there, like:

35591, 10000, 1000, 10;

but again, trial & error ...

berak gravatar imageberak ( 2016-12-30 10:53:29 -0500 )edit

i edited my code but i am still getting the following error: http://answers.opencv.org/upfiles/148... most probably because of the load command

shreya gravatar imageshreya ( 2016-12-30 11:31:12 -0500 )edit

can you give us a text version of that ?

berak gravatar imageberak ( 2016-12-30 11:50:00 -0500 )edit

the text version: OpenCV Error Assertion failed (nvars==(int)rowvals.size()) in cv ml TrainDataImpl::loadCSV,file(then the path till ml/src/data.cpp,line 587

are you unable to view the images ? if so next time i will give the text version

shreya gravatar imageshreya ( 2016-12-30 12:08:46 -0500 )edit