Ask Your Question
0

SVM Training Data with Bag-of-Word

asked 2016-12-18 14:08:14 -0600

marcob88 gravatar image

Hi, I've a question with training an SVM multiclass with bag of word model. I have 50 video of training and each video is represented by a matrix of words.

Now i have a vector<mat> that contains all training data. How can training svm with this data structure?

I think to use pca but i'm not sure if is the exactly way.

Thanks

edit retag flag offensive close merge delete

Comments

May be you can try this example

LBerger gravatar imageLBerger ( 2016-12-18 14:34:11 -0600 )edit

^^ for opencv2.4

berak gravatar imageberak ( 2016-12-19 03:58:47 -0600 )edit
2

for opencv 3.1 it's here

LBerger gravatar imageLBerger ( 2016-12-19 04:02:45 -0600 )edit

^^ cool :)

berak gravatar imageberak ( 2016-12-19 04:09:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-12-19 03:58:59 -0600

berak gravatar image

updated 2016-12-19 08:54:03 -0600

to train an SVM with opencv3, you'd go like this:

// preprocess data:
vector<Mat> fromBOW = ...;
Mat data, labels; // initially empty
for (size_t i=0; i<fromBOW.size(); i++) {
      Mat m;
      fromBOW.convertTo(m, CV_32F); // we need float features
      data.push_back(m.reshape(1,1)); // add as a single, flat row vec.

      int label = 17; // ??? how do you get your labels ????
      labels.push_back(label);
}

then we can train the SVM:

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setKernel(ml::SVM::LINEAR);
bool itWorked = svm->train(data, ml::ROW_SAMPLE, labels);

later, we can predict on bow features:

Mat bowfeature = ...
Mat m;
bowfeature .convertTo(m, CV_32F); // we need float features
int predicted = svm->predict(m.reshape(1,1)); // single, flat row vec.
edit flag offensive delete link more

Comments

But if every mat have different size? For example i have a mat with 7 rows (number frame of video) and 10 cols ( number of words) but another mat can have different size...how can train in this situation? is possible?

marcob88 gravatar imagemarcob88 ( 2016-12-20 15:31:57 -0600 )edit

hmm, can it be, you got the BoW idea wrong ?

berak gravatar imageberak ( 2016-12-20 16:38:03 -0600 )edit

could you try to edit your question, and be more explicit about the data you have ? what format ? a "matrix of words" is what, exactly ?

berak gravatar imageberak ( 2016-12-21 00:52:24 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-12-18 14:08:14 -0600

Seen: 289 times

Last updated: Dec 19 '16