Loading a .yml file in a SVM

asked 2020-07-08 10:06:20 -0500

albeh95 gravatar image

Hi everyone, I trained an SVM and saved the result in a .yml file thanks to svm->save(filename). However when on the second run I use

Ptr<SVM> svm = SVM::load(filename);

the whole program doesn't give me the same result. What I do on the second run is commenting these 2 parts:

Mat trainingData;
int k = 0;

Mat labels;

vector<String> fna;
glob("C:/Users/albma/Desktop/train_originale/*.png", fna, true);

for (size_t i = 0; i < fna.size(); i++) { //trees: label = 1

    printf("Training Image = %s\n", fna[i]);
    img = imread(fna[i]);
    vector<KeyPoint> keypoint;
    Mat bowDescriptor;
    detector1->detect(img, keypoint); //detect keypoints
    bowDE.compute(img, keypoint, bowDescriptor); //compute descriptors
    trainingData.push_back(bowDescriptor);
    if(!bowDescriptor.empty())
        labels.push_back((int)1);
}
trainingData.convertTo(trainingData, CV_32FC1);

And this:

int dictSize = 1500;
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::ONE_CLASS);
svm->setNu(0.5);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
printf("Training SVM\n");
Ptr<TrainData> td = TrainData::create(trainingData, ROW_SAMPLE, labels); //start training SVM
svm->train(td);
svm->save("mySVM.yml");

Substituting it all with the load() mentioned above. What's wrong with the code?

edit retag flag offensive close merge delete

Comments

did you look at the yml ? are your params saved correctly ?

can you explain, why you use SVM::ONE_CLASS ?

(and what again was the goal of the whole thing ?)

berak gravatar imageberak ( 2020-07-08 10:16:26 -0500 )edit
1

Sure, it starts like this:

 - opencv_ml_svm:    format: 3   
   svmType: ONE_CLASS    kernel:
         type: LINEAR    nu: 5.0000000000000000e-01    term_criteria: { iterations:100 }   
   var_count: 1500    class_count: 1   
   sv_total: 1    support_vectors:

and then begins all the list of the computed values

albeh95 gravatar imagealbeh95 ( 2020-07-08 10:20:59 -0500 )edit
1

I used one_class because i just need to classificate one object class (the goal is to classified pictures as tree pictures or not)

albeh95 gravatar imagealbeh95 ( 2020-07-08 10:34:03 -0500 )edit

I've just noticed I have a lot of values like these:

uncompressed_sv_total: 246
   uncompressed_support_vectors:
      - [ 0., 0., 2.89017335e-03, 0., 0., 0., 2.89017335e-03, 0., 0.,
          5.78034669e-03, 2.89017335e-03, 0., 0., 2.89017335e-03, 0., 0.,

and so on. Could this be the problem?

albeh95 gravatar imagealbeh95 ( 2020-07-08 11:39:10 -0500 )edit