training neural net..
I am using opencv 2.4 with visual studio 2012...
here is the code ..
using namespace std;
using namespace cv;
using namespace ml;
int val=0;
float a[]={0,0,1,1};
float b[]={0,1,0,1};
float C[]={0,0,0,1};
void main()
{
Ptr<ANN_MLP>neural_net=ANN_MLP::create(); //empty neural model..
neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM); //neural activation function...
neural_net->setTrainMethod(ANN_MLP::BACKPROP); ///training algorithm...
Mat layers(3,1,CV_32FC1);
layers.row(0)=2;
layers.row(1)=2;
layers.row(2)=1;
std::cout<<"neural layers\n"<<layers;
neural_net->setLayerSizes(layers);
TermCriteria Term(CV_TERMCRIT_ITER,10000,0.1);
neural_net->setTermCriteria(Term);
Mat trainig_data(4,2,CV_32FC1);
Mat sampleDat(4,1,CV_32FC1);
Mat trainDat(4,2,CV_32FC1);
int count=0;
for(int r=0;r<trainDat.rows;r++)
{
for(int c=0;c<trainDat.cols;c++)
{
trainDat.at<float>(r,0)=a[count];
trainDat.at<float>(r,1)=b[count];
}
count+=1;
}
std::cout<<"\n"<<trainDat;
count=0;
for(int r=0;r<sampleDat.rows;r++)
{
for(int c=0;c<sampleDat.cols;c++)
{
sampleDat.at<float>(r,c)=C[count];
}
count+=1;
}
std::cout<<"\n"<<sampleDat;
Ptr<TrainData>TrainingData=TrainData::create(trainDat,ROW_SAMPLE,sampleDat);
printf("training...");
neural_net->train(TrainingData);
printf("..done!");
Mat test(1,2,CV_32FC1);
test.at<float>(0,0)=1;
test.at<float>(0,1)=1;
printf("neural output %f",neural_net->predict(test));
_getch();
}
This is the code for my neural net implementation...
Actuallty my program is crashing in runtime..when I am calling prediction function().It is encountering (memory access violation ) I don't know why..???
Thank you..