0

I am using Android studio with OpenCV sdk 3.2 version. I am trying to train using readymade API available for SVM

Part of my code retrieves the image from the internal memory of the phone, and helps to train using SVM

    File mnist_images_file = new File(path);
    FileInputStream images_reader = new FileInputStream(mnist_images_file);

    Mat training_images = null;
    try {
        ...
        training_images = new Mat(total_images, px_count, CvType.CV_8U);
        for (int i = 0; i < total_images; i++) {
            byte[] image = new byte[px_count];
            images_reader.read(image, 0, px_count);
            training_images.put(i, 0, image);
        }
        training_images.convertTo(training_images,
                CvType.CV_32FC1);
        //Read Labels
        Mat training_labels = null;
        byte[] labels_data = new byte[total_images];

        File mnist_labels_file = new File(labels_path);
        FileInputStream labels_reader = new
                FileInputStream(mnist_labels_file);

        try{
            training_labels = new Mat(total_images, 1,
                    CvType.CV_8U);
            Mat temp_labels = new Mat(1, total_images,
                    CvType.CV_8U);
            header = new byte[8];
            //Read the header
            labels_reader.read(header, 0, 8);

            //Read all the labels at once
            labels_reader.read(labels_data,0,total_images);
            temp_labels.put(0,0, labels_data);

            //Take a transpose of the image
            Core.transpose(temp_labels, training_labels);
            training_labels.convertTo(training_labels,
                    CvType.CV_32FC1);
            labels_reader.close();
        }

        catch (IOException e)
        {
            Log.i("MNIST Read Error:", "" + e.getMessage());
        }

        images_reader.close();

        SVM svm = SVM.create();
        svm.setType(SVM.C_SVC);
        svm.setKernel(SVM.POLY);
        svm.setGamma(3);
        svm.train(training_images, Ml.ROW_SAMPLE,training_labels);

I am trying to train using available dataset that is already present in the device in the form of images . but calling this train() gives me the following error:

03-01 17:10:24.359 12035-12035/com.example.vankit.knnexample E/cv::error(): OpenCV Error: One of arguments' values is out of range (The kernel parameter must be positive) in void cv::ml::SVMImpl::checkParams(), file /home/maksim/workspace/android-pack/opencv/modules/ml/src/svm.cpp, line 1302 03-01 17:10:24.361 12035-12035/com.example.vankit.knnexample E/org.opencv.ml: ml::train_10() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/ml/src/svm.cpp:1302: error: (-211) The kernel parameter must be positive in function void cv::ml::SVMImpl::checkParams() 03-01 17:10:24.362 12035-12035/com.example.vankit.knnexample E/AndroidRuntime: FATAL EXCEPTION: main

Could you please suggest a way out for this?

1 Answers1

0

I don't know if this problem is still current, but as far as I know SVM module in newest version of OpenCV (I mean 3.0+) is not working perfectly while using other kernel than LINEAR. Try maybe using linear kernel, or changing version of opencv to 2.4.13.2 (newest of 2+) and see if you encounter any problems.

P.S. I would comment but due to my reputation I can't do that...

Infinito
  • 85
  • 1
  • 3
  • 10