SVM predict Sizes of input arguments do not match

asked 2015-04-15 04:30:13 -0600

prostykoks gravatar image

Hi everyone, i have problem with SVM. My traing look like this:

    for (File file : new File("path to positivies images").listFiles()) {
        Mat img = Highgui.imread(file.getAbsolutePath());
        Mat timg = new Mat();
        Imgproc.cvtColor(img, timg, Imgproc.COLOR_BGR2GRAY);
        timg = timg.reshape(1, timg.width()*timg.height());
        timg.convertTo(timg, CvType.CV_32FC1);
        trainingImages.push_back(timg);
        trainingLabels.push_back(Mat.ones(new Size(1, img.width()*img.height()), CvType.CV_32FC1));
    }

    for (File file : new File("path to negative images").listFiles()) {
        Mat img = Highgui.imread(file.getAbsolutePath());
        Mat timg = new Mat();
        Imgproc.cvtColor(img, timg, Imgproc.COLOR_BGR2GRAY);
        timg = timg.reshape(1, timg.width()*timg.height());
        timg.convertTo(timg, CvType.CV_32FC1);
        trainingImages.push_back(timg);
        trainingLabels.push_back(Mat.zeros(new Size(1, img.width()*img.height()), CvType.CV_32FC1));
    }

    trainingImages.copyTo(trainingData);
    trainingData.convertTo(trainingData, CvType.CV_32FC1);
    trainingLabels.copyTo(classes);

    CvSVMParams params = new CvSVMParams();
    params.set_kernel_type(CvSVM.LINEAR);

    clasificador = new CvSVM(trainingData, classes, new Mat(), new Mat(), params);
    clasificador.train(trainingData, classes);
    clasificador.save("path to save xml");

And the traing works fine no errors here. But when i want to use predict using xml generated by the traing i have the following error:

OpenCV Error: Sizes of input arguments do not match (The sample size is different from what has been used for training) in cvPreparePredictData, file ........\opencv\modules\ml\src\inner_functions.cpp, line 1114 Exception in thread "AWT-EventQueue-0" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\ml\src\inner_functions.cpp:1114: error: (-209) The sample size is different from what has been used for training in function cvPreparePredictData

My class using traing xml look like this:

    clasificador = new CvSVM();
    clasificador.load("path to xml generated earlier");
    Mat img = Highgui.imread("path to positive image used in traning");
    Mat timg = new Mat();
    Imgproc.cvtColor(img, timg, Imgproc.COLOR_BGR2GRAY);
    timg = timg.reshape(1, timg.width()*timg.height());
    timg.convertTo(timg, CvType.CV_32FC1);

    clasificador.predict(timg);

i want to use image from positivie only to check if the svm works correctly. Can someone help me? ]

edit retag flag offensive close merge delete

Comments

2

can you try a reshape(1,1) for both train and test images ? (not reshape(1,w*h))

berak gravatar imageberak ( 2015-04-15 04:48:15 -0600 )edit

for input (1,1) the algorithm works correctly. But i would like to make it works with images more than 1pixels size. Edit: Ok i saw on the debug that reshape create Mat with 1 row and many columns needed, now its work. Thanks

prostykoks gravatar imageprostykoks ( 2015-04-15 05:08:49 -0600 )edit