Ask Your Question
0

Errors when predicting from a Neutral Network

asked 2016-04-22 17:57:54 -0600

valentine gravatar image

updated 2016-04-23 06:13:47 -0600

Hello everyone,

I am trying to train a neutral network in opencv using one hidden layer network. Everything seems fine but the program crashes when the predict function for the model is called. Below is my code:

float sc[6] = { 0.3, 0.5, 1, 1, 0.2, 0.4 };
float sc1[3] = { 0.75, 0.81, 0.93 };
float sc2[2] = { 0.2, 1};
Mat traindata = Mat(3,2,CV_32FC1,&sc);
Mat trainlabel = Mat(3, 1,CV_32FC1,&sc1);
Mat testdata = Mat(1, 2,CV_32FC1,&sc2);
Ptr<ml::ANN_MLP> mlp = ml::ANN_MLP::create();
mlp->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
mlp->setTrainMethod(ml::ANN_MLP::BACKPROP);
mlp->setBackpropMomentumScale(0.1);
mlp->setBackpropWeightScale(0.1);
mlp->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 1000, 1e-6));

Mat layers = Mat(3, 1, CV_32SC1);
layers.row(0) = Scalar(2);
layers.row(1) = Scalar(3);
layers.row(2) = Scalar(1);
//layers.row(3) = Scalar(5);

mlp->setLayerSizes(layers);

mlp->train(traindata,ml::ROW_SAMPLE,trainlabel);

Mat responses = Mat(3,1,CV_32FC1);
mlp->predict(testdata,responses);
float result = responses.at<float>(0, 0);

The error message is given below:

Unhandled exception at 0x00007FFE0076855C (opencv_world310d.dll) in fetchvideoapp.exe: 0xC0000005: Access violation reading location 0x000001D3A69DF000.

errormessage.png

Your assistance will be duly appreciated.

Regards.

edit retag flag offensive close merge delete

Comments

hi berak this only error message Unhandled exception at 0x00007FFE0076855C (opencv_world310d.dll) in fetchvideoapp.exe: 0xC0000005: Access violation reading location 0x000001D3A69DF000.

valentine gravatar imagevalentine ( 2016-04-23 06:35:56 -0600 )edit

weirdly, i can't reprocuce the crash, it just returns [-1.INF] (or [-nan] on linux) .

but i think, your traindata/labels are schrott not nice, for a classification you would need one output node per class (and a 2d, numFeatures x numClasses response Mat for training)

berak gravatar imageberak ( 2016-04-23 06:50:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-27 10:15:00 -0600

valentine gravatar image

Hi berak,

solved the problem using the same data. I basically removed the setMethod, setBackpropMomentumScale, setBackpropWeightscale, setTermCriteria and it worked. I dont know why but will like someone to explain. Here is the code below:

  int inputLayerSize = 2;
int outputLayerSize = 1;
int numSamples = 3;
int hiddenLayer = 3;
vector<int> layerSizes = { inputLayerSize,hiddenLayer, outputLayerSize };
Ptr<ml::ANN_MLP> nnPtr = ml::ANN_MLP::create();
nnPtr->setLayerSizes(layerSizes);
nnPtr->setActivationFunction(ANN_MLP::SIGMOID_SYM);
//nnPtr->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
/*nnPtr->setTrainMethod(ml::ANN_MLP::BACKPROP);
nnPtr->setBackpropMomentumScale(0.1);
nnPtr->setBackpropWeightScale(0.1);
nnPtr->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 100, 1e-4));*/

Mat samples(Size(inputLayerSize, numSamples), CV_32F);
samples.at<float>(Point(0, 0)) = 0.3f;
samples.at<float>(Point(1, 0)) = 1.0f;
samples.at<float>(Point(0, 1)) = 0.2f;
samples.at<float>(Point(1, 1)) = 0.2f;
samples.at<float>(Point(0, 2)) = 1.0f;
samples.at<float>(Point(1, 2)) = 0.4f;
Mat responses(Size(outputLayerSize, numSamples), CV_32F);
responses.at<float>(Point(0, 0)) = 0.75f;
//responses.at<float>(Point(0, 1)) = 0.64f;
responses.at<float>(Point(0, 1)) = 0.82f;
//responses.at<float>(Point(1, 1)) = 0.74f;
responses.at<float>(Point(0, 2)) = 0.93f;
//responses.at<float>(Point(2, 1)) = 0.84f;

cout << "samples:\n" << samples << endl;
cout << "\nresponses:\n" << responses << endl;

if (!nnPtr->train(samples, ml::ROW_SAMPLE, responses))
    return 1;
cout << "\nweights[0]:\n" << nnPtr->getWeights(0) << endl;
cout << "\nweights[1]:\n" << nnPtr->getWeights(1) << endl;
cout << "\nweights[2]:\n" << nnPtr->getWeights(2) << endl;
cout << "\nweights[3]:\n" << nnPtr->getWeights(3) << endl;

Mat output;
Mat samplestest(Size(inputLayerSize, 1), CV_32F);
samplestest.at<float>(Point(0, 0)) = 0.8f;
samplestest.at<float>(Point(1, 0)) = 0.6f;
//samplestest.at<float>(Point(0, 2)) = 0.3f;
nnPtr->predict(samplestest, output);
cout << "\noutput:\n" << output << endl;

and the output is given below:

samples: [0.30000001, 1; 0.2, 0.2; 1, 0.40000001]

responses: [0.75; 0.81999999; 0.93000001]

weights[0]: [2.809757458984826, -1.404878743448629, 2.941742042246543, -1.568929097965231]

weights[1]: [-0.2094534488630105, 0.5412198073856522, 1.658734440612611; -0.8916550488630105, -1.139978207385652, -0.7469871593873889; 0.7505247417458868, 0.7944425035100793, -0.1239852716789556]

weights[2]: [-0.3488449412441831; 0.8637626791966008; 1.178125967026814; -0.2092970630454225]

weights[3]: [0.09473684586976705, 0.8400000035762786]

output: [0.88864398]

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-22 17:57:54 -0600

Seen: 407 times

Last updated: Apr 27 '16