Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use clustering file after training

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

How to use clustering file after training

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

How to use clustering file after training

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

Edit :

Code after above code :

for (k = all_names.begin(); k != all_names.end(); ++k)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    Mat row_img_2 = cv::imread( Dir +*k, 0 );

    detector.detect( row_img_2, keypoints);

    RetainBestKeypoints(keypoints, 20);

    dextract.compute( row_img_2, keypoints, descriptors_1);

    Mat my_img = descriptors_1.reshape(1,1);

    my_img.convertTo( training_mat.row(count_2), CV_32FC1 );
    //training_mat.push_back(my_img);

    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1; // 1 for car, -1 otherwise
    ++count_2;
}



CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
Params.C=1000;

CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.xml");

How to use clustering file after training

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

Edit :

Code after above code :

for (k = all_names.begin(); k != all_names.end(); ++k)
{
    Dir=( (count_2 < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);

    Mat row_img_2 = cv::imread( Dir +*k, 0 );

    detector.detect( row_img_2, keypoints);

    RetainBestKeypoints(keypoints, 20);

    dextract.compute( row_img_2, keypoints, descriptors_1);

    Mat my_img = descriptors_1.reshape(1,1);

    my_img.convertTo( training_mat.row(count_2), CV_32FC1 );
    //training_mat.push_back(my_img);
 
    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1; // 1 for car, -1 otherwise
(count_2<nb_cars)?1:-1;
    ++count_2;
}
 CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
Params.C=1000;

CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.xml");

How to use clustering BOW file after trainingfor testing

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

Edit :

Code after above code :

for (k = all_names.begin(); k != all_names.end(); ++k)
{
    Mat row_img_2 = cv::imread( Dir +*k, 0 );

    detector.detect( row_img_2, keypoints);

    dextract.compute( row_img_2, keypoints, descriptors_1);

    Mat my_img = descriptors_1.reshape(1,1);

    my_img.convertTo( training_mat.row(count_2), CV_32FC1 );


    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1;
    ++count_2;
}
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
Params.C=1000;

CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.xml");

Edit :

I used this approach in the above code and got error :

FileStorage fs; 
fs.open("Vocabulary.xml", FileStorage::WRITE);

Mat vocabulary = bow.cluster();

fs["Vocabulary"]>>vocabulary ; 

dextract.setVocabulary(vocabulary);
fs.release();
cv::Mat training_mat(num_img , dictionarySize,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);

CvSVM svm;
svm.load( "trainsvm.xml" );

Error :

image description

How to use BOW file for testing

I trained the data using BOG with SVM , i trained it , now i just want to open it through xml , i don't want to train it again and again on every run , I trained the svm and now loading it using its loading function

CvSVM svm;
svm.load( "trainsvm.xml" );

But how to do the same with vocabulary file which i use for clustering using BOG algorithm , below is my code , which is training the data , but i want to open it now like svm

for (i = files.begin(); i != files.end(); ++i)
{
    Mat row_img = imread( Dir +*i, 0 );

    detector.detect( row_img, keypoints);

    extractor->compute( row_img, keypoints, descriptors_1);

    descriptors_1.reshape(1,1);

    bow.add(descriptors_1);

    ++count;
}


cout<<"CLUSTERING";
Mat vocabulary = bow.cluster();

dextract.setVocabulary(vocabulary);

Mat training_mat(num_img , dictionarySize,CV_32FC1);
Mat labels(num_img,1,CV_32FC1);

This is clustering the data , now i don't want to run it again and again because it takes too much time , when it trained once , i just want to use it , so that i can speed up my program

Edit :

Code after above code :

for (k = all_names.begin(); k != all_names.end(); ++k)
{
    Mat row_img_2 = cv::imread( Dir +*k, 0 );

    detector.detect( row_img_2, keypoints);

    dextract.compute( row_img_2, keypoints, descriptors_1);

    Mat my_img = descriptors_1.reshape(1,1);

    my_img.convertTo( training_mat.row(count_2), CV_32FC1 );


    labels.at< float >(count_2, 0) = (count_2<nb_cars)?1:-1;
    ++count_2;
}
CvSVMParams Params;
Params.svm_type=CvSVM::C_SVC;
Params.kernel_type=CvSVM::LINEAR;
Params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Params.gamma=3;
Params.C=1000;

CvSVM svm;
svm.train(training_mat,labels,cv::Mat(),cv::Mat(),Params);
svm.save("trainsvm.xml");

Edit :

I used this approach in the above code and got error :

FileStorage fs; 
fs.open("Vocabulary.xml", FileStorage::WRITE);

Mat vocabulary = bow.cluster();

fs["Vocabulary"]>>vocabulary ; 

dextract.setVocabulary(vocabulary);
fs.release();
cv::Mat training_mat(num_img , dictionarySize,CV_32FC1);
cv::Mat labels(num_img,1,CV_32FC1);

CvSVM svm;
svm.load( "trainsvm.xml" );

Error :

image description