2

I'm using LIBSVM within MatLab to try and classify images.

I understand that SVM is a binary Classification Model, however I'm wondering how I would go about using it as multi-class Classification Model.

Is it possible to train pairs of data (ie car and non car, horse and non horse, person and non person) and then predict which class an image belongs to by comparing the image to all three models? If so, how could I achieve this? What would my test label vector be?

1
  • 1
    Thanks for your response. In the case I mentioned above - having generated 3 models, would I then perform svmpredict three times one for each model and accept the one with the highest predicted accuracy score? Commented Mar 5, 2014 at 14:34

2 Answers 2

3

Yes your suggestion is a good approach. It's called the one-vs-all strategy.

You need to train separate SVMs for each class. The output data would be a binary variable equal to 1 if is in that class and 0 otherwise. Then in order to classify a new item, run it through each of your SVMs and choose the one with the highest output (output closest to 1).

Sign up to request clarification or add additional context in comments.

Comments

2

As a supplementary answer of @Dan's, below is the relevant code from my previous post:

model = cell(NumofClass,1);  % NumofClass = 3 in your case
for k = 1:NumofClass
    model{k} = svmtrain(double(trainingLabel==k), trainingData, '-c 1 -g 0.2 -b 1');
end

%% calculate the probability of different labels

pr = zeros(1,NumofClass);
for k = 1:NumofClass
    [~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
    pr(:,k) = p(:,model{k}.Label==1);    %# probability of class==k
end

%% your label prediction will be the one with highest probability:

[~,predctedLabel] = max(pr,[],2);

4 Comments

Thanks for your help. Could you please explain why you use double(trainingLabel==k) as opposed to just double(trainingLabel)? Also - when generating the model, I use 15 instances of say a car (1 in the corresponding position in the training label) and 15 instances of a non-car (-1 in the corresponding position in the training label). Can I use the same 15 random objects for every non instance of aclass - ie non-horse, non-person?
Thank you Gwenji. trainingLabel==K indicates that you want to get the probability that the model predicts your instance belongs to K, after that you select the K number with largest probability. Your question two, the answer is yes.
One final question - when it comes to running svmpredict, say I have a class of 20 cars and I want to decide which of the three classes they belong to - can I just use the 20 cars and a test label vector of twenty "1"'s? Or must I use 20 cars and 20 non-cars in order to use a test label vector of forty '1s and -1s'? Many thanks for all your help.
I would suggest you use 40 of them since you may need to get the false positive/negative of your model predictability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.