I have trained MLP as shown in the code below to classify images into 4 classes. My class labels are 1,2,3,4
.This trains the model successfully but it thorws an Exception vector out of range during prediction
void train::trainANN(Mat hists, vector<int> labels)
{
int cols = hists.cols;//size of input layer must be equal to this number of cols
int rows = hists.rows;//used to determine rows in Mat of responses
Mat_<float> responses = Mat(labels).reshape(0, rows);
Ptr<TrainData> trainData = TrainData::create(hists, ROW_SAMPLE, responses);
Ptr<ANN_MLP> ann = ml::ANN_MLP::create();
vector<int> layers = { cols, 500, 1 };
ann->setLayerSizes(layers);
ann->setActivationFunction(ml::ANN_MLP::ActivationFunctions::SIGMOID_SYM, 1.0, 1.0);
ann->setTrainMethod(ANN_MLP::TrainingMethods::BACKPROP);
ann->setBackpropMomentumScale(0.1);
ann->setBackpropWeightScale(0.1);
ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 10000, 0.00001));
ann->train(trainData);
}
This is how I predict the model
float train::predictANN(Mat hist)
{
Mat results(1, 4, CV_32FC1);
float pred = ann->predict(hist, results);//hist is a row matrix with one sample
return pred;
}
I have tried to debug this code but have not fixed the error. Kindly help with why the prediction throws vector out of range exception. Thank you. NB:Am using different number of images per class during training. class 1 = 300 images, class 2 = 340 images etc