Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Loading a .yml file in a SVM

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?