0

I am working on a project thumb recognition. following is code I am reading the 118 images of order 42 X 25 and storing them in training matrix.

training=zeros(118, 1050);

imagefiles = dir('*.png');      
nfiles = length(imagefiles);    
for ii=1:nfiles
    currentfilename = imagefiles(ii).name;
    I = imread(currentfilename);

    BW=im2bw(I,graythresh(I));    

    temp = reshape(BW,1,1050);
    training(ii,:)=temp;
end

Now I am creating a matrix of labelData to assign labels to images.

labelData = zeros(118,1);
labelData(1:50,:) = 0; 
labelData(51:83,:) = 1;
labelData(84:118,:) = 2;

Here i am training my system by giving training data and label data.

options=optimset('MaxIter',5000);
SVMStruct =  svmtrain(training,labelData,'Kernel_Function','linear','QuadProg_Opts',options);

BUT when I run this code it is giving me an error like

Error 1 : SVMTRAIN only supports classification into two groups. GROUP contains 3 groups.
Error 2 : SVMStruct = svmtrain(training,labelData,'Kernel_Function','linear','QuadProg_Opts',options);

Kindly help me what is the problem I used it before it was working fine but now I dont know what is going on. Thanks in advance.

3 Answers 3

3

Error 1 tells you what the problem is - the MATLAB built-in SVM only supports binary classification. You are assigning 3 classes.

Your options are:

  1. Construct three classifiers: 0 vs. 1,2 then 1 vs. 0,2 then 2 vs. 0,1 and look at the output of each.
  2. Construct 0 vs. not 0 and then 1 vs. 2
  3. Use a multi-class SVM trainer from LIBSVM or svmlight or other such packages.
5
  • There is lots of example code out there - just search for it. For your purposes I suggest the second option - just make two separate classifiers using the code structure you already have.
    – Ansari
    May 29, 2012 at 19:02
  • Brother, I am new in Matlab, sorry for disturbing, but I dont know how to create classifier. May 29, 2012 at 19:08
  • I'm sorry - this website will not hold your hand in writing code. You need to look for tutorials in other places. Creating a classifier simply means to train a classifier, like you're trying to do above.
    – Ansari
    May 29, 2012 at 19:10
  • thankyou so much. any reference for constructing 0 vs. not 0 and then 1 vs. 2 May 29, 2012 at 19:17
  • 1
    This is just logic - first label all your 0s as 0, and everything that is not a 0 as 1, then train your classifier. Then, separately, label your 1s as 1 and 2s as 2, and just train a classifier on those (forget the 0s). Now you have two classifiers - any new instance that comes in, first pass it through the first classifier and see if it is classified as 0. If it is, you have your answer. If it isn't, run it through the second classifier. It will tell you whether it's 1 or 2.
    – Ansari
    May 29, 2012 at 19:19
1

The error message is pretty clear. MATLAB's svmtrain does not support multiclass classification, that is only two classes are allowed.

So, you have two options: 1) write your own multiclass classifier as a wrapper around svmtrain. You can implement one-vs-all or one-vs-one strategies. 2) use a svm implementation that already supports multiclass classification such as libsvm.

2
  • is there any code available for multiclass classifier or libsvm. as I am new in Matlab programming. thanks for your response. May 29, 2012 at 19:00
  • libsvm supports multiclass problems by default. It implements the one-vs-one strategy. Yes, there is a matlab interface to libsvm, which is included in the libsvm package.
    – emrea
    May 29, 2012 at 19:01
0

Your problem is in the labelData vector ceck it and find the eror, yoy shoild OAA architector if hthe number of classes is more then .

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.