so i have been successful with making ANN_MLP work and output results since i had a problem with the sigmoid not initialized however i am still not sure if there is a problem with having not enough training images or i have something wrong with my code making the results not true every time with different results is there a possible way to check if the problem is with the data set i have ?
so my input is this image
and the dataset used for training
so the result should be 6.0
but what i get is 5.0 [0.52028143, 0.44897014, 0.54281932, 0.50860959, 0.47123247, 0.54682046, 0.48321274, 0.54106969, 0.45284992]
and
when input this image the result should be 0.0
but it gives me also
5.0 [0.52415699, 0.44889876, 0.54271841, 0.51714647, 0.48295537, 0.54325002, 0.48789713, 0.53129482, 0.45263588]
so without further due here is the code
String PATH_numbers = "/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/chdataset/Numb";
Mat img = Imgcodecs.imread("/home/tomna/NetBeansProjects/Parking-tracking-system/src/mainclass/characters/8.jpg");
Mat trainingData = new Mat();
Mat trainingLabels = new Mat();
ANN_MLP knn = ANN_MLP.create();
TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);
knn.setTrainMethod(ANN_MLP.BACKPROP, 0.5, 0.5);
knn.setBackpropWeightScale(0.05f);
knn.setBackpropMomentumScale(0.05f);
knn.setTermCriteria(s);
Mat layers = new Mat(3, 1, CvType.CV_32SC1);
layers.row(0).setTo(new Scalar(4440));
layers.row(1).setTo(new Scalar(2000));
layers.row(2).setTo(new Scalar(9));
knn.setLayerSizes(layers);
knn.setActivationFunction(ANN_MLP.SIGMOID_SYM, 0.1, 0.1);
trainingLabels = new Mat(45, 9, CvType.CV_32F, Scalar.all(0));
for (File file : new File(PATH_numbers).listFiles()) {
Mat image = getMat(file.getAbsolutePath());
image.convertTo(image, CvType.CV_32F); // Convert to float
Imgproc.resize(image, image, new Size(60, 74));
Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
Core.addWeighted(image, 0.5, image, 0, 0, image);
Mat imgresized = image.reshape(1, 1); // make continous
trainingData.push_back(imgresized);
}
int index = 0;
int numImgPerClass = 5;
int rowstart = 0;
for (int i = 0; i < 9; i++) {
trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
rowstart += 5;
numImgPerClass += 5;
}
knn.train(trainingData, Ml.ROW_SAMPLE, trainingLabels);
Mat test = new Mat();
Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
Imgproc.resize(test, test, new Size(60, 74));
test.convertTo(test, CvType.CV_32FC1);
Mat results = new Mat();
test = test.reshape(1, 1);
float label = knn.predict(test.reshape(1, 1), results, 0);
System.out.println(label + " " + results.dump());