Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is anyone using "ONE CLASS SVM"?

I'm new at dealing with SVM and i created successfully multi0class svm examples. I have tried many times to implement ONE-CLASS SVM, but it always returns zero. I have all labels of sample filled with 1, though one class svm seems that it doesn't need to label samples. If there is complete example using one class svm, could you refer the link?

Here's my code

include<iostream>

include<vector>

include<fstream>

include<opencv2\core.hpp>

include<opencv2\imgproc.hpp>

include<opencv2\highgui.hpp>

include<opencv2\xfeatures2d.hpp>

include<opencv2\ml.hpp>

using namespace std; using namespace cv; using namespace xfeatures2d; using namespace ml;

void main() {

Mat groups;
Mat samples;
vector<KeyPoint> keypoints1;

//ORB Detector
Ptr<ORB> detector = ORB::create(20, 1.2f, 2, 31, 0, 2, ORB::HARRIS_SCORE, 31);

Mat descriptors1, descriptors2;

Ptr<SURF> extractor = SURF::create();

//Samples of similar images
for (int i = 1; i <= 1; ++i) {
    stringstream nn;
    nn << "models/" << i << ".png";

    Mat img = imread(nn.str());
    cvtColor(img, img, COLOR_BGR2GRAY);

    detector->detect(img, keypoints1);

    extractor->compute(img, keypoints1, descriptors1);

    samples.push_back(descriptors1.reshape(1, 1));
    keypoints1.clear();
}

for (int j = 1; j <= 40; ++j) {
    groups.push_back(1);
}

Ptr<SVM> classifierSVM = SVM::create();

ifstream existence("trainingData.yml");

if (existence.good()) {
    classifierSVM = Algorithm::load<SVM>("trainingData.yml");
}
else {
    classifierSVM->setType(SVM::ONE_CLASS);
    classifierSVM->setKernel(SVM::LINEAR);
    classifierSVM->setDegree(3);
    classifierSVM->setGamma(1);
    classifierSVM->setCoef0(0);
    classifierSVM->setC(1);
    classifierSVM->setNu(0.5);
    classifierSVM->setP(0);
    classifierSVM->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER,
                                                 500, FLT_EPSILON));

    classifierSVM->train(samples, ml::ROW_SAMPLE, groups);
    classifierSVM->save("trainingData.yml");
}

//Test for 10 test samples - 7 & 9 must be classified.
for (int i = 1; i <= 10; ++i) {
    stringstream nn;

    nn << "testers/" << "unknown" << i << ".png";

    Mat unknown = imread(nn.str());
    cvtColor(unknown, unknown, COLOR_BGR2GRAY);

    detector->detect(unknown, keypoints1);

    extractor->compute(unknown, keypoints1, descriptors2);

    float result = classifierSVM->predict(descriptors2.reshape(1, 1));

    cout << nn.str() << ": class " << result << endl;
}

waitKey(10);

}

click to hide/show revision 2
No.2 Revision

updated 2017-02-17 00:59:37 -0600

berak gravatar image

Is anyone using "ONE CLASS SVM"?

I'm new at dealing with SVM and i created successfully multi0class svm examples. I have tried many times to implement ONE-CLASS SVM, but it always returns zero. I have all labels of sample filled with 1, though one class svm seems that it doesn't need to label samples. If there is complete example using one class svm, could you refer the link?

Here's my code

include<iostream>

include<vector>

include<fstream>

include<opencv2\core.hpp>

include<opencv2\imgproc.hpp>

include<opencv2\highgui.hpp>

include<opencv2\xfeatures2d.hpp>

include<opencv2\ml.hpp>

#include<iostream>
#include<vector>
#include<fstream>

#include<opencv2\core.hpp>
#include<opencv2\imgproc.hpp>
#include<opencv2\highgui.hpp>
#include<opencv2\xfeatures2d.hpp>
#include<opencv2\ml.hpp>

using namespace std;
using namespace cv;
using namespace xfeatures2d;
using namespace ml;

ml; void main() {

{
Mat groups;
 Mat samples;
 vector<KeyPoint> keypoints1;
 //ORB Detector
 Ptr<ORB> detector = ORB::create(20, 1.2f, 2, 31, 0, 2, ORB::HARRIS_SCORE, 31);
 Mat descriptors1, descriptors2;
 Ptr<SURF> extractor = SURF::create();
 //Samples of similar images
 for (int i = 1; i <= 1; ++i) {
 stringstream nn;
 nn << "models/" << i << ".png";
  Mat img = imread(nn.str());
  cvtColor(img, img, COLOR_BGR2GRAY);
 detector->detect(img, keypoints1);
  extractor->compute(img, keypoints1, descriptors1);
 samples.push_back(descriptors1.reshape(1, 1));
 keypoints1.clear();
 }
 for (int j = 1; j <= 40; ++j) {
 groups.push_back(1);
 }
 Ptr<SVM> classifierSVM = SVM::create();
 ifstream existence("trainingData.yml");
 if (existence.good()) {
 classifierSVM = Algorithm::load<SVM>("trainingData.yml");
 }
 else {
 classifierSVM->setType(SVM::ONE_CLASS);
 classifierSVM->setKernel(SVM::LINEAR);
 classifierSVM->setDegree(3);
 classifierSVM->setGamma(1);
 classifierSVM->setCoef0(0);
 classifierSVM->setC(1);
 classifierSVM->setNu(0.5);
 classifierSVM->setP(0);
 classifierSVM->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER,
  500, FLT_EPSILON));
  classifierSVM->train(samples, ml::ROW_SAMPLE, groups);
 classifierSVM->save("trainingData.yml");
 }
 //Test for 10 test samples - 7 & 9 must be classified.
 for (int i = 1; i <= 10; ++i) {
 stringstream nn;
 nn << "testers/" << "unknown" << i << ".png";
  Mat unknown = imread(nn.str());
  cvtColor(unknown, unknown, COLOR_BGR2GRAY);
 detector->detect(unknown, keypoints1);
  extractor->compute(unknown, keypoints1, descriptors2);
 float result = classifierSVM->predict(descriptors2.reshape(1, 1));
  cout << nn.str() << ": class " << result << endl;
 }
 waitKey(10);

}

}

Is anyone using "ONE CLASS SVM"?

I'm new at dealing with SVM and i created successfully multi0class svm examples. I have tried many times to implement ONE-CLASS SVM, but it always returns zero. I have all labels of sample filled with 1, though one class svm seems that it doesn't need to label samples. If there is complete example using one class svm, could you refer the link?

Here's my code

#include<iostream>
#include<vector>
#include<fstream>

#include<opencv2\core.hpp>
#include<opencv2\imgproc.hpp>
#include<opencv2\highgui.hpp>
#include<opencv2\xfeatures2d.hpp>
#include<opencv2\ml.hpp>

using namespace std;
using namespace cv;
using namespace xfeatures2d;
using namespace ml;

void main() {

    Mat groups;
    Mat samples;
    vector<KeyPoint> keypoints1;

    //ORB Detector
    Ptr<ORB> detector = ORB::create(20, 1.2f, 2, 31, 0, 2, ORB::HARRIS_SCORE, 31);

    Mat descriptors1, descriptors2;

    Ptr<SURF> extractor = SURF::create();

    //Samples //Sample of similar images
    for (int i = 1; i <= 1; 40; ++i) {
        stringstream nn;
        nn << "models/" << i << ".png";

        Mat img = imread(nn.str());
        cvtColor(img, img, COLOR_BGR2GRAY);

        detector->detect(img, keypoints1);

        extractor->compute(img, keypoints1, descriptors1);

        samples.push_back(descriptors1.reshape(1, 1));
        keypoints1.clear();
    }

    for (int j = 1; j <= 40; ++j) {
        groups.push_back(1);
    }

    Ptr<SVM> classifierSVM = SVM::create();

    ifstream existence("trainingData.yml");

    if (existence.good()) {
        classifierSVM = Algorithm::load<SVM>("trainingData.yml");
    }
    else {
        classifierSVM->setType(SVM::ONE_CLASS);
        classifierSVM->setKernel(SVM::LINEAR);
        classifierSVM->setDegree(3);
        classifierSVM->setGamma(1);
        classifierSVM->setCoef0(0);
        classifierSVM->setC(1);
        classifierSVM->setNu(0.5);
        classifierSVM->setP(0);
        classifierSVM->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER,
                                                     500, FLT_EPSILON));

        classifierSVM->train(samples, ml::ROW_SAMPLE, groups);
        classifierSVM->save("trainingData.yml");
    }

    //Test for 10 test samples - 7 & 9 must be classified.
    for (int i = 1; i <= 10; ++i) {
        stringstream nn;

        nn << "testers/" << "unknown" << i << ".png";

        Mat unknown = imread(nn.str());
        cvtColor(unknown, unknown, COLOR_BGR2GRAY);

        detector->detect(unknown, keypoints1);

        extractor->compute(unknown, keypoints1, descriptors2);

        float result = classifierSVM->predict(descriptors2.reshape(1, 1));

        cout << nn.str() << ": class " << result << endl;
    }

    waitKey(10);

}

Is anyone using "ONE CLASS SVM"?

I'm new at dealing with SVM and i created successfully multi0class svm examples. I have tried many times to implement ONE-CLASS SVM, but it always returns zero. I have all labels of sample filled with 1, though one class svm seems that it doesn't need to label samples. If there is complete example using one class svm, could you refer the link?

Here's my code

#include<iostream>
#include<vector>
#include<fstream>

#include<opencv2\core.hpp>
#include<opencv2\imgproc.hpp>
#include<opencv2\highgui.hpp>
#include<opencv2\xfeatures2d.hpp>
#include<opencv2\ml.hpp>

using namespace std;
using namespace cv;
using namespace xfeatures2d;
using namespace ml;

void main() {

    Mat groups;
    Mat samples;
    vector<KeyPoint> keypoints1;

    //ORB Detector
    Ptr<ORB> detector = ORB::create(20, 1.2f, 2, 31, 0, 2, ORB::HARRIS_SCORE, 31);

    Mat descriptors1, descriptors2;

    Ptr<SURF> extractor = SURF::create();

    //Sample of similar images
    for (int i = 1; i <= 40; ++i) {
        stringstream nn;
        nn << "models/" << i << ".png";

        Mat img = imread(nn.str());
        cvtColor(img, img, COLOR_BGR2GRAY);

        detector->detect(img, keypoints1);

        extractor->compute(img, keypoints1, descriptors1);

        samples.push_back(descriptors1.reshape(1, 1));
        keypoints1.clear();
    }

    for (int j = 1; j <= 40; ++j) {
        groups.push_back(1);
    }

    Ptr<SVM> classifierSVM = SVM::create();

    ifstream existence("trainingData.yml");

    if (existence.good()) {
        classifierSVM = Algorithm::load<SVM>("trainingData.yml");
    }
    else {
        classifierSVM->setType(SVM::ONE_CLASS);
        classifierSVM->setKernel(SVM::LINEAR);
        classifierSVM->setDegree(3);
        classifierSVM->setGamma(1);
        classifierSVM->setCoef0(0);
        classifierSVM->setC(1);
        classifierSVM->setNu(0.5);
        classifierSVM->setP(0);
        classifierSVM->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER,
                                                     500, FLT_EPSILON));

        classifierSVM->train(samples, ml::ROW_SAMPLE, groups);
        classifierSVM->save("trainingData.yml");
    }

    //Test for 10 test samples image sample - 7 & 9 must be classified.
    for (int i = 1; i <= 10; ++i) {
        stringstream nn;

        nn << "testers/" << "unknown" << i << ".png";

        Mat unknown = imread(nn.str());
        cvtColor(unknown, unknown, COLOR_BGR2GRAY);

        detector->detect(unknown, keypoints1);

        extractor->compute(unknown, keypoints1, descriptors2);

        float result = classifierSVM->predict(descriptors2.reshape(1, 1));

        cout << nn.str() << ": class " << result << endl;
    }

    waitKey(10);

}