svm_train.exe has stopped working

asked 2017-09-13 18:12:15 -0500

I am trying to train SVM classifier using SIFT feature and Bag Of Word (BOW). But my program is not running, but it is showing no error in my code. I am not able to figure out my faults. Can anybody give me any suggestion? Thanks in advance. Here is a piece of code by which I have declared variables and SVM classifier parameter.

Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");


std::vector<KeyPoint> keypoints;
Mat desc;

//defining terms for bowkmeans trainer
int cluster = 5;
int retries = 1;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.00001);
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(cluster, tc, retries, flags);
Mat vocubulary;
BOWImgDescriptorExtractor bowDE(descriptor, matcher);

CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

Mat features1, features2;
Mat bowDescriptor, bowDescriptor2;

int dictionarySize=100 ;
vector<string>names;
string folder= argv[1];
glob(folder, names);
Mat trainme(0, dictionarySize, CV_32FC1);
Mat labels(0, 1, CV_32FC1);

By the following part I tried to obtain features from images.

for(unsigned int i =0; i<names.size();i++)
{
    Mat img = imread( names[i], CV_LOAD_IMAGE_GRAYSCALE );

    if(!img.data)
    { cout<< " --(!) Error reading images " << endl; return -1; }

    features->detect(img, keypoints);
    descriptor->compute(img, keypoints, features1);
    bowDE.compute(img, keypoints, bowDescriptor);
    trainme.push_back(bowDescriptor);
    float label = 1.0;
    labels.push_back(label);
    bowTrainer.add(features1);
    cout<<"Program is running"<<endl;
}

By the following part, I trained SVM classifier

Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);

CvSVM SVM;
SVM.train(trainme,labels);
SVM.save("classifier.xml");

Mat tryme(0, dictionarySize, CV_32FC1);
Mat tryDescriptor;
Mat test = imread("C:\\Users\\ASHRAF\\Desktop\\Oslo\\ResizedSamples\\24.bmp", 0);
vector<KeyPoint> keypoints3;
features->detect(test, keypoints3);
bowDE.compute(test, keypoints3, tryDescriptor);
tryme.push_back(tryDescriptor);
cout<<SVM.predict(tryme)<<endl;

}

edit retag flag offensive close merge delete

Comments

the labels should be integer, not float

(unfortunately, i cannot test your outdated 2.4 code)

berak gravatar imageberak ( 2017-09-13 19:56:08 -0500 )edit
1

try to set: cv::setBreakOnError(true);

maybe this gives you some more information

VxW gravatar imageVxW ( 2017-09-14 02:28:21 -0500 )edit