{"action":"created","issue":{"url":"https://api.github.com/repos/Itseez/opencv/issues/5054","repository_url":"https://api.github.com/repos/Itseez/opencv","labels_url":"https://api.github.com/repos/Itseez/opencv/issues/5054/labels{/name}","comments_url":"https://api.github.com/repos/Itseez/opencv/issues/5054/comments","events_url":"https://api.github.com/repos/Itseez/opencv/issues/5054/events","html_url":"https://github.com/Itseez/opencv/issues/5054","id":97443775,"number":5054,"title":"SVM write/load problems with kernels other than linear","user":{"login":"opencv-pushbot","id":2051563,"avatar_url":"https://avatars.githubusercontent.com/u/2051563?v=3","gravatar_id":"","url":"https://api.github.com/users/opencv-pushbot","html_url":"https://github.com/opencv-pushbot","followers_url":"https://api.github.com/users/opencv-pushbot/followers","following_url":"https://api.github.com/users/opencv-pushbot/following{/other_user}","gists_url":"https://api.github.com/users/opencv-pushbot/gists{/gist_id}","starred_url":"https://api.github.com/users/opencv-pushbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opencv-pushbot/subscriptions","organizations_url":"https://api.github.com/users/opencv-pushbot/orgs","repos_url":"https://api.github.com/users/opencv-pushbot/repos","events_url":"https://api.github.com/users/opencv-pushbot/events{/privacy}","received_events_url":"https://api.github.com/users/opencv-pushbot/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Itseez/opencv/labels/affected:%20master","name":"affected: master","color":"ededed"},{"url":"https://api.github.com/repos/Itseez/opencv/labels/auto-transferred","name":"auto-transferred","color":"ededed"},{"url":"https://api.github.com/repos/Itseez/opencv/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/Itseez/opencv/labels/category:%20ml","name":"category: ml","color":"ededed"},{"url":"https://api.github.com/repos/Itseez/opencv/labels/priority:%20normal","name":"priority: normal","color":"A38700"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-07-27T11:38:19Z","updated_at":"2016-03-01T02:34:48Z","closed_at":null,"body":"Transferred from http://code.opencv.org/issues/4455\r\n```\n|| Jens Garstka on 2015-07-01 08:32\n|| Priority: Normal\n|| Affected: branch 'master' (3.0-dev)\n|| Category: ml\n|| Tracker: Bug\n|| Difficulty: Easy\n|| PR: \n|| Platform: x64 / Linux\n```\n\nSVM write/load problems with kernels other than linear\n-----------\n```\nWithin the implementation of the SVMs write and read methods (modules/ml/src/svm.cpp, lines 2022 ff and 2139 ff), the author makes the assumption, that support vector indices are not required in 2-class problems. That is probably correct if a linear kernel is used (i.e., when you have only one support vector). But with other kernels (in my case with an RBF kernel), the support vector indices are definitly required.\r\n\r\nI've added a git diff where this questionable conditions are commented out. Without these lines everything works fine. \r\n\r\nIMHO the lines should be removed.\n```\n\nHistory\n-------\n##### Hyunjun Kim on 2015-07-03 07:35\n```\nHi, I was able to reproduce the error. I confirmed that cv::Algorithm::load() works well only with SVM::LINEAR kernel.\n\n<pre>\n#include <opencv2/core.hpp>\n#include <opencv2/imgproc.hpp>\n#include \"opencv2/imgcodecs.hpp\"\n#include <opencv2/highgui.hpp>\n#include <opencv2/ml.hpp>\n\nusing namespace cv;\nusing namespace cv::ml;\n\nint main(int, char**)\n{\n    // Data for visual representation\n    int width = 512, height = 512;\n    Mat image = Mat::zeros(height, width, CV_8UC3);\n\n    // Set up training data\n    int labels[4] = {1, -1, -1, -1};\n    Mat labelsMat(4, 1, CV_32SC1, labels);\n\n    float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };\n    Mat trainingDataMat(4, 2, CV_32FC1, trainingData);\n\n    // Set up SVM's parameters\n    Ptr<ml::SVM> svm = ml::SVM::create();\n    svm->setType(ml::SVM::C_SVC);\n    svm->setKernel(SVM::INTER); // Algorithm::load() works well with SVM::LINEAR\n    svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));\n\n    // Train the SVM\n    svm->train(ml::TrainData::create(trainingDataMat, ml::ROW_SAMPLE, labelsMat));\n\n    // Save and load SVM\n    svm->save(\"ex_svm.xml\");\n    svm= cv::Algorithm::load<ml::SVM>(\"ex_svm.xml\"); // something is wrong\n\n\n    Vec3b green(0,255,0), blue (255,0,0);\n    // Show the decision regions given by the SVM\n    for (int i = 0; i < image.rows; ++i)\n        for (int j = 0; j < image.cols; ++j)\n        {\n            Mat sampleMat = (Mat_<float>(1,2) << j,i);\n            float response = svm->predict(sampleMat);\n\n            if (response == 1)\n                image.at<Vec3b>(i,j)  = green;\n            else if (response == -1)\n                image.at<Vec3b>(i,j)  = blue;\n        }\n\n    // Show the training data\n    int thickness = -1;\n    int lineType = 8;\n    circle( image, Point(501,  10), 5, Scalar(  0,   0,   0), thickness, lineType );\n    circle( image, Point(255,  10), 5, Scalar(255, 255, 255), thickness, lineType );\n    circle( image, Point(501, 255), 5, Scalar(255, 255, 255), thickness, lineType );\n    circle( image, Point( 10, 501), 5, Scalar(255, 255, 255), thickness, lineType );\n\n    // Show support vectors\n    thickness = 2;\n    lineType  = 8;\n\n    Mat sv = svm->getSupportVectors();\n\n    for (int i = 0; i < sv.rows; ++i)\n    {\n        const float* v = sv.ptr<float>(i);\n        circle( image,  Point( (int) v[0], (int) v[1]),   6,  Scalar(128, 128, 128), thickness, lineType);\n    }\n\n    imwrite(\"result.png\", image);        // save the image\n\n    imshow(\"SVM Simple Example\", image); // show it to the user\n    waitKey(0);\n\n}\n</pre>\n```\n##### Maksim Shabunin on 2015-07-13 08:56\n```\n-   Assignee set to Maksim Shabunin\n```"},"comment":{"url":"https://api.github.com/repos/Itseez/opencv/issues/comments/190501494","html_url":"https://github.com/Itseez/opencv/issues/5054#issuecomment-190501494","issue_url":"https://api.github.com/repos/Itseez/opencv/issues/5054","id":190501494,"user":{"login":"Bardo91","id":3602335,"avatar_url":"https://avatars.githubusercontent.com/u/3602335?v=3","gravatar_id":"","url":"https://api.github.com/users/Bardo91","html_url":"https://github.com/Bardo91","followers_url":"https://api.github.com/users/Bardo91/followers","following_url":"https://api.github.com/users/Bardo91/following{/other_user}","gists_url":"https://api.github.com/users/Bardo91/gists{/gist_id}","starred_url":"https://api.github.com/users/Bardo91/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bardo91/subscriptions","organizations_url":"https://api.github.com/users/Bardo91/orgs","repos_url":"https://api.github.com/users/Bardo91/repos","events_url":"https://api.github.com/users/Bardo91/events{/privacy}","received_events_url":"https://api.github.com/users/Bardo91/received_events","type":"User","site_admin":false},"created_at":"2016-03-01T02:34:48Z","updated_at":"2016-03-01T02:34:48Z","body":"Is it fixed? I'm trying to reproduce it with current master but I can't.  https://github.com/Bardo91/opencvSvmLoadError"}}