Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

SVM predict Sizes of input arguments do not match

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? ]