opencv - Assertion Failed (Layer_sizes!=0) CvANN_MLP::predict -
i trying classify input image classify function .xml file have made. training artificial neural network(ann) don't know wrong trying code book"practical projects opencv" full code of book available in github: https://github.com/masteringopencv/code/tree/master/chapter5_numberplaterecognition
technically used own way extract number segments full picture , created ocr.xlm file.
i have no idea why when try classify input segmented image(mat input array) see error : assertion failed (layer_sizes!=0) cvann_mlp::predict
here code
char const strcharacters[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int const numcharacters = 9; cvann_mlp ann; void train(mat traindata, mat classes, int nlayers){ filestorage fs; fs.open("ocr.xml", filestorage::read); mat traindata; fs["trainingdata"] >> traindata; fs["classes"] >> classes; mat layers(1, 3, cv_32sc1); layers.at<int>(0,0) = traindata.cols;//input layer layers.at<int>(1,0) = nlayers;//hidden layer layers.at<int>(2,0) = numcharacters;//output layer int buffer[] = { traindata.cols, 16, numcharacters }; ann.create(layers, cvann_mlp::sigmoid_sym, 1, 1); //prepare trainclases //create mat n trained data m classes mat trainclasses; trainclasses.create(traindata.rows, numcharacters, cv_32f); (int = 0; < trainclasses.rows; i++) { (int k = 0; k < trainclasses.cols; k++) { //if class of data same k class if (k == classes.at<int>(i)) trainclasses.at<float>(i, k) = 1; else trainclasses.at<float>(i, k) = 0; } } mat weights(1, traindata.rows, cv_32fc1, scalar::all(1)); //learn classifier ann.train(traindata, trainclasses, weights); } int classify(mat f){ float result = -1; mat output(1, numcharacters, cv_32fc1); ann.predict(f, output); point maxloc; double maxval; minmaxloc(output, 0, &maxval, 0, &maxloc); //we need know in output max val, x (cols) class. // result = output.at < float >(0, 0); return maxloc.x; }
i call calssify in main code:
int character = classify(roiresized);
i appreciate help. suggestion?
i know post old, answer can useful other one. must add layers in neuronal network before using prediction.
e.g :
// define parameters neural network (mlp) // set network 3 layer 256->10->10 // - 1 input node per attribute in sample // - 10 hidden nodes // - 1 output node per class int layers_d[] = { attributes_per_sample, 10, number_of_classes}; cvmat* layers = cvcreatematheader(1,3,cv_32sc1); cvinitmatheader(layers, 1,3,cv_32sc1, layers_d); // create network using sigmoid function alpha , beta // parameters 0.6 , 1 specified respectively (refer manual) cvann_mlp* nnetwork = new cvann_mlp; nnetwork->create(layers, cvann_mlp::sigmoid_sym, 0.6, 1);
source : https://github.com/arnaudgelas/opencvexamples/blob/master/neuralnetwork/neuralnetwork.cpp
Comments
Post a Comment