Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

ANN_MLP bad argument error

i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that

OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]

but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code

void run() throws IOException {
        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/4.jpg");
        Mat trainingData = new Mat();
        Mat trainingLabels = new Mat();

        //// here i prepare the neural network//////////
        ANN_MLP knn = ANN_MLP.create();
        TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);

        knn.setTrainMethod(ANN_MLP.BACKPROP);
        knn.setBackpropWeightScale(0.05f);
        knn.setBackpropMomentumScale(0.05f);
        knn.setTermCriteria(s);

     " ///////////here i identify that 3 layer 1 input 5 hidden 1 output   ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
        Mat layers = new Mat(3,1,CvType.CV_32SC1);
        layers.row(0).setTo(new Scalar(1));
        layers.row(1).setTo(new Scalar(5));
        layers.row(2).setTo(new Scalar(1));

        knn.setLayerSizes(layers);


        int i = 0;
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat image = getMat(file.getAbsolutePath());

            Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
            Core.addWeighted(image, 1.5, image, 0, 0, image);
            image.convertTo(image, CvType.CV_32F); // Convert to float
            Imgproc.resize(image, image, new Size(60, 74));

            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);

            int nImagesPerClass = 5;
            int id = 1 + (i / nImagesPerClass);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
            i++;
            System.out.println(trainingData);
        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32F); // Convert to float
        System.out.println(trainingData.cols());
        knn.train(trainingData, Ml.ROW_SAMPLE, response);  <<-- "error point here  when i output trainiData.rows=45 and   trainiData.rows.cols = 4440   so i guess the bad argument is for the cols ? " 

        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(label);

ANN_MLP bad argument error

i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that

OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]

but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code

void run() throws IOException {
        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/4.jpg");
        Mat trainingData = new Mat();
        Mat trainingLabels = new Mat();

        //// here i prepare the neural network//////////
        ANN_MLP knn = ANN_MLP.create();
        TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);

        knn.setTrainMethod(ANN_MLP.BACKPROP);
        knn.setBackpropWeightScale(0.05f);
        knn.setBackpropMomentumScale(0.05f);
        knn.setTermCriteria(s);

     " ///////////here i identify that 3 layer 1 input 5 hidden 1 output   ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
        Mat layers = new Mat(3,1,CvType.CV_32SC1);
        layers.row(0).setTo(new Scalar(1));
        layers.row(1).setTo(new Scalar(5));
        layers.row(2).setTo(new Scalar(1));

        knn.setLayerSizes(layers);


        int i = 0;
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat image = getMat(file.getAbsolutePath());

            Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
            Core.addWeighted(image, 1.5, image, 0, 0, image);
            image.convertTo(image, CvType.CV_32F); // Convert to float
            Imgproc.resize(image, image, new Size(60, 74));

            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);

            int nImagesPerClass = 5;
            int id = 1 + (i / nImagesPerClass);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
            i++;
            System.out.println(trainingData);
        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32F); // Convert to float
        System.out.println(trainingData.cols());
        knn.train(trainingData, Ml.ROW_SAMPLE, response);  <<-- "error point here  when i output trainiData.rows=45 and   trainiData.rows.cols = 4440   so i guess the bad argument is for the cols ? " 

        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(label);

UPDATE so i made some changes suggested by @berak

 here the code is 

        void run() throws IOException {
        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/4.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);
        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(5));

        knn.setLayerSizes(layers);
        trainingLabels = new Mat(45, 5, 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));


            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);
        }
      "the output of this for loop is the traininglabel which looks like my answer below "
        int index = 0;
        int numImgPerClass = 5;
        int rowstart = 0;
        for (int i = 0; i < 5; i++) {
            trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
            rowstart += 5;
            numImgPerClass += 5;
        }
        "here where i get an error that i cant understand the error is below  cuz when i delete the 3 lines of code below the code executes but the output is ...result.dump =[-nan, -nan, -nan, -nan, -nan] and for the label =0 "    

         Mat tmp=new Mat(); 
        trainingLabels = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(trainingLabels, CvType.CV_32F); // Convert to float

        knn.train(trainingData, Ml.ROW_SAMPLE, tmp);


        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(results.dump());

ERROR

OpenCV Error: Bad argument (output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 675 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:675: error: (-5) output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer in function prepare_to_train ]

ANN_MLP bad argument error

i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that

OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]

but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code

void run() throws IOException {
        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/4.jpg");
        Mat trainingData = new Mat();
        Mat trainingLabels = new Mat();

        //// here i prepare the neural network//////////
        ANN_MLP knn = ANN_MLP.create();
        TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);

        knn.setTrainMethod(ANN_MLP.BACKPROP);
        knn.setBackpropWeightScale(0.05f);
        knn.setBackpropMomentumScale(0.05f);
        knn.setTermCriteria(s);

     " ///////////here i identify that 3 layer 1 input 5 hidden 1 output   ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
        Mat layers = new Mat(3,1,CvType.CV_32SC1);
        layers.row(0).setTo(new Scalar(1));
        layers.row(1).setTo(new Scalar(5));
        layers.row(2).setTo(new Scalar(1));

        knn.setLayerSizes(layers);


        int i = 0;
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat image = getMat(file.getAbsolutePath());

            Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
            Core.addWeighted(image, 1.5, image, 0, 0, image);
            image.convertTo(image, CvType.CV_32F); // Convert to float
            Imgproc.resize(image, image, new Size(60, 74));

            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);

            int nImagesPerClass = 5;
            int id = 1 + (i / nImagesPerClass);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
            i++;
            System.out.println(trainingData);
        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32F); // Convert to float
        System.out.println(trainingData.cols());
        knn.train(trainingData, Ml.ROW_SAMPLE, response);  <<-- "error point here  when i output trainiData.rows=45 and   trainiData.rows.cols = 4440   so i guess the bad argument is for the cols ? " 

        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(label);

UPDATE so i made some changes suggested by @berak

 here the code is 

        void run() throws IOException {
        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/4.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);
        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(5));

        knn.setLayerSizes(layers);
        trainingLabels = new Mat(45, 5, 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));


            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);
        }
      "the " the output of this for loop is the traininglabel which looks like my answer below "
        int index = 0;
        int numImgPerClass = 5;
        int rowstart = 0;
        for (int i = 0; i < 5; i++) {
            trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
            rowstart += 5;
            numImgPerClass += 5;
        }
        "here " here where i get an error that i cant understand the error is below  cuz when i delete the 3 lines of code below the code executes but the output is ...result.dump =[-nan, -nan, -nan, -nan, -nan] and for the label =0 "    

         Mat tmp=new Mat(); 
        trainingLabels = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(trainingLabels, CvType.CV_32F); // Convert to float

        knn.train(trainingData, Ml.ROW_SAMPLE, tmp);


        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(results.dump());

ERROR

OpenCV Error: Bad argument (output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 675 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:675: error: (-5) output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer in function prepare_to_train ]

ANN_MLP bad argument error

i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that

OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]

but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code

void run() throws IOException {
        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/4.jpg");
        Mat trainingData = new Mat();
        Mat trainingLabels = new Mat();

        //// here i prepare the neural network//////////
        ANN_MLP knn = ANN_MLP.create();
        TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);

        knn.setTrainMethod(ANN_MLP.BACKPROP);
        knn.setBackpropWeightScale(0.05f);
        knn.setBackpropMomentumScale(0.05f);
        knn.setTermCriteria(s);

     " ///////////here i identify that 3 layer 1 input 5 hidden 1 output   ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
        Mat layers = new Mat(3,1,CvType.CV_32SC1);
        layers.row(0).setTo(new Scalar(1));
        layers.row(1).setTo(new Scalar(5));
        layers.row(2).setTo(new Scalar(1));

        knn.setLayerSizes(layers);


        int i = 0;
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat image = getMat(file.getAbsolutePath());

            Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
            Core.addWeighted(image, 1.5, image, 0, 0, image);
            image.convertTo(image, CvType.CV_32F); // Convert to float
            Imgproc.resize(image, image, new Size(60, 74));

            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);

            int nImagesPerClass = 5;
            int id = 1 + (i / nImagesPerClass);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
            i++;
            System.out.println(trainingData);
        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32F); // Convert to float
        System.out.println(trainingData.cols());
        knn.train(trainingData, Ml.ROW_SAMPLE, response);  <<-- "error point here  when i output trainiData.rows=45 and   trainiData.rows.cols = 4440   so i guess the bad argument is for the cols ? " 

        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(label);

UPDATE so i made some changes suggested by @berak

 here the code is 

        void run() throws IOException {
        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/4.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);
        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(5));
Scalar(9));

        knn.setLayerSizes(layers);
        trainingLabels = new Mat(45, 5, 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));


            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);
        }
      " the output of this for loop is the traininglabel which looks like my answer below "
        int index = 0;
        int numImgPerClass = 5;
        int rowstart = 0;
        for (int i = 0; i < 5; 9; i++) {
            trainingLabels.submat(rowstart, numImgPerClass, i, i + 1).setTo(new Scalar(1));
            rowstart += 5;
            numImgPerClass += 5;
        }
        " here where i get an error that i cant understand the error is below  cuz when i delete the 3 lines of code below the code executes but the output is ...result.dump =[-nan, -nan, -nan, -nan, -nan] and for the label =0 "    

         Mat tmp=new Mat(); 
        trainingLabels = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(trainingLabels, CvType.CV_32F); // Convert to float

        knn.train(trainingData, Ml.ROW_SAMPLE, tmp);


        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(results.dump());

ERROR

OpenCV Error: Bad argument (output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 675 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:675: error: (-5) output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer in function prepare_to_train ]

ANN_MLP bad argument error

i am going straigt to the point i am doing a neural network and after setting the ANN i get this error saying that

OpenCV Error: Bad argument (input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 668 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:668: error: (-5) input training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of 0-th (input) layer in function prepare_to_train ]

but i dont get why cuz the numbers seems to be okay or what i think they are soo here is the code

void run() throws IOException {
        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/4.jpg");
        Mat trainingData = new Mat();
        Mat trainingLabels = new Mat();

        //// here i prepare the neural network//////////
        ANN_MLP knn = ANN_MLP.create();
        TermCriteria s = new TermCriteria(TermCriteria.MAX_ITER + TermCriteria.EPS, 100, 0.00001);

        knn.setTrainMethod(ANN_MLP.BACKPROP);
        knn.setBackpropWeightScale(0.05f);
        knn.setBackpropMomentumScale(0.05f);
        knn.setTermCriteria(s);

     " ///////////here i identify that 3 layer 1 input 5 hidden 1 output   ////not sure here how to put the input since i have only 45 images for training so need help here and i think 5 is also a not good example for hidden so yaa as well here "
        Mat layers = new Mat(3,1,CvType.CV_32SC1);
        layers.row(0).setTo(new Scalar(1));
        layers.row(1).setTo(new Scalar(5));
        layers.row(2).setTo(new Scalar(1));

        knn.setLayerSizes(layers);


        int i = 0;
        for (File file : new File(PATH_numbers).listFiles()) {
            Mat image = getMat(file.getAbsolutePath());

            Imgproc.GaussianBlur(image, image, new Size(5, 5), 0);
            Core.addWeighted(image, 1.5, image, 0, 0, image);
            image.convertTo(image, CvType.CV_32F); // Convert to float
            Imgproc.resize(image, image, new Size(60, 74));

            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);

            int nImagesPerClass = 5;
            int id = 1 + (i / nImagesPerClass);
            trainingLabels.push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(id)));
            i++;
            System.out.println(trainingData);
        }

        Mat response = new Mat();
        Mat tmp;
        tmp = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32F); // Convert to float
        System.out.println(trainingData.cols());
        knn.train(trainingData, Ml.ROW_SAMPLE, response);  <<-- "error point here  when i output trainiData.rows=45 and   trainiData.rows.cols = 4440   so i guess the bad argument is for the cols ? " 

        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(label);

UPDATE so i made some changes suggested by @berak

 here the code is 

        void run() throws IOException {
        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/4.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);
        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);
        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));


            Mat imgresized = image.reshape(1, 1); // make continous
            trainingData.push_back(imgresized);
        }
      " the output of this for loop is the traininglabel which looks like my answer below "
        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;
        }
        " here where i get an error that i cant understand the error is below  cuz when i delete the 3 lines of code below the code executes but the output is ...result.dump =[-nan, -nan, -nan, -nan, -nan] and for the label =0 "    

         Mat tmp=new Mat(); 
        trainingLabels = trainingLabels.reshape(1, 1); // make continuous
        tmp.convertTo(trainingLabels, CvType.CV_32F); // Convert to float

        knn.train(trainingData, Ml.ROW_SAMPLE, tmp);


        Mat test = new Mat();
        Imgproc.cvtColor(img, test, Imgproc.COLOR_RGB2GRAY);
        Imgproc.GaussianBlur(test, test, new Size(5, 5), 0);
        Core.addWeighted(test, 0.5, test, 0, 0, test);
        Imgproc.resize(test, test, new Size(60, 74));
        test.convertTo(test, CvType.CV_32F);
        Mat results = new Mat();
        int label = (int) knn.predict(test.reshape(1, 1), results, 0);
        System.out.println(results.dump());

ERROR

OpenCV Error: Bad argument (output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp, line 675 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/tomna/Desktop/opencv-3.1.0/modules/ml/src/ann_mlp.cpp:675: error: (-5) output training data should be a floating-point matrix with the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer in function prepare_to_train ]