Ask Your Question

doubt in multiclass svm

asked 2018-12-28 23:25:48 -0500

arti gravatar image

Hello all, i'm doing classification using one to all multiclass svm. my data set have 10 classes like running, walking ,biking riding, waving, walking etc. and each class have 20 videos. my doubt in what should be label for each class. should i have to take labels like---

 float labels[10] = {0, 1, 2, 3,.......10};
 Mat labelsMat(10, 1, CV_32FC1, labels);

and training data for each class

float trainingData1[4] [n]= { feature1, feature2, feature3, feature4};
Mat trainingDataMat1(4, n, CV_32FC1, trainingData1);

then create svm for each class such as

Ptr<SVM> svm1 = SVM::create();

svm1->train(trainingDataMat1, ROW_SAMPLE, labelsMat);

here each svm should be trained with same labelsMat or i have to define different labels for each class?

and at the time testing i have to apply predict function with each svm1...svmN like

float response1 = svm1->predict(sampleMat); . . . . float responseK = svmN->predict(sampleMat);

then accuracy is calculated?? please clear my doubts. Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-29 02:55:28 -0500

berak gravatar image

updated 2018-12-29 02:56:51 -0500

try with a multiclass SVM first, it's far easier than juggling multiple SVM instances.

Mat trainData, trainLabels; // initially empty
for each video in the trainset {
       // extract the feature, put it into a Mat
       feature.convertTo(feature, CV_32F); // make sure we got float data
       trainData.push_back(feature.reshape(1,1)); // as a flat column
       trainLabels.push_back(7); // the classlabel of your video as *integer* number

at this point, your trainData should be [sizeOfYourFeatures x numVideos] (CV_32F)

and the trainLabels should be [1 x numVideos] (CV_32S)

Ptr<SVM> svm = SVM::create();
svm->train(trainData, ROW_SAMPLE, trainLabels);

testing goes similar:

int correct = 0;
for each video in the testset {
       // extract the feature, put it into a Mat
       feature.convertTo(feature, CV_32F); // make sure we got float data
       int predicted = svm->predict(feature.reshape(1,1)); // as a flat column
       int groundtruth = ??? // the expected classlabel for your video
       correct += (predicted == groundtruth);

float accuracy = (float)correct / numTestVideos;
edit flag offensive delete link more


Thanks a lot sir for the explanation.

arti gravatar imagearti ( 2018-12-30 23:52:06 -0500 )edit

Question Tools

1 follower


Asked: 2018-12-28 23:25:48 -0500

Seen: 74 times

Last updated: Dec 29 '18