Approach to implement Multi class SVM classifier

asked 2018-04-28 08:49:28 -0600

Macho gravatar image

updated 2018-04-28 10:04:58 -0600

I am trying to classify road signs which means i have classes and sub classes. For example i have a class for Speed sign limits and it includes sub classes (ex: 30 speed signs, 50 speed signs, 80 speed signs, etc...).

My problem what i am facing is how to proper model my system, i want to implement One vs All technique.

I already have implemented One class SVM as well as Multi class single SVM so i am not asking about how to code.

Should the model be smth like this:

For Red Speed limit signs super class i intialize one SVM and train it with more than one label that represents sub classes as a positives and put all the remaining data set as a negatives.

Then i intialize/create another instance of another SVM to represent another super class for an example lets say Blue Speed limit signs class, and so on?

In pseudo code would look like:

CvSVM svmClassONE = new CvSVM()

CvSVM svmClassTWO = new CvSVM()

Is that a proper way ? is it okay to create multiple instance of SVM's ?

I am using Java OpenCV btw Thanks


for(int z = 1; z <= 3; z++) {

// Train positives

String p = "/Users/Desktop/ml/pos" + z; 
 File[] file = new File(p).listFiles();

 for(int i = 0; i < file.length; i++) {
  Mat img = Highgui.imread(file[i].getAbsolutePath(), Highgui.CV_LOAD_IMAGE_COLOR);
  Imgproc.resize(img, img, new Size(32,32));
  hog.compute(img, descriptors);

  trainingData.push_back(descriptors.reshape(1, 1));
  trainingLabels.push_back(new Mat(new Size(1,1),CvType.CV_32FC1,new Scalar(z)));

// Training Negatives

 File[] file2 = new File(PATH_NEGATIVE).listFiles();
 for(int i = 0; i < file2.length; i++) {
  Mat img = Highgui.imread(file2[i].getAbsolutePath(), Highgui.CV_LOAD_IMAGE_COLOR);
  Imgproc.resize(img, img, new Size(32,32));
  hog.compute(img, descriptors);

  trainingData.push_back(descriptors.reshape(1, 1));
  trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));
edit retag flag offensive close merge delete


your pseudo code does not make sense.

there is no concept of "subclasses" with an SVM, if you wanted a "binary" (one-vs-all) classification, you would have to feed all your positives and negatives in one go (one train() run), and there would be exactly 2 labels (e.g. +1,-1), not many.

for multiple binary SVM's, your traindata wouldbe always the same, just differently labelled (another part of your trainset plays the part of the "negatives").

also a multi-class SVM (that's what you should use, imho) would split it up into multiple binary SVM's internally, so there's nothing won with your approach.

berak gravatar imageberak ( 2018-04-28 09:19:41 -0600 )edit

@berak Thank you again for trying to help :)).

What i have done for testing purposes (i updated the question with my code), i constructed Multi class SVM for Speed limit signs i used (20,30,50 speed signs) as a positives and labeled them as 1,2,3 respectively and (other speed limit signs like 60,70,80) as negatives and labeled them as 0, predictions showed good results. Now back to your answer i don't have negative dataset that contain no sign what i thought is that for an example when i try to train for (30 speed limit) i would put (20 speed limit and others) as a negative and vice versa when it comes to train the (20 speed limit), following..

Macho gravatar imageMacho ( 2018-04-28 10:05:38 -0600 )edit

updated code looks, like it should work !

berak gravatar imageberak ( 2018-04-28 10:06:29 -0600 )edit

By your answer as long as i understand i will use different labels for each and every grouped positive sample (ex: if i have 5 speed limit signs so i will have 5 labels, and if i have 6 triangular sign i will have 6 more labels i.e 11 labels total) , but what about the negatives in this case?

Macho gravatar imageMacho ( 2018-04-28 10:06:41 -0600 )edit

Yea the code works but not the way i want it only classify (20,30,50) as pos and (all other signs) as negative, in my case i also want to classify (the all other signs too)

Macho gravatar imageMacho ( 2018-04-28 10:08:07 -0600 )edit

I know that the negative samples would have a label too, but what to put in the negative sample as images?

Macho gravatar imageMacho ( 2018-04-28 10:09:33 -0600 )edit

@berak sorry for late reply, I got that binary is 2 labels that wouldn't help in my case. But yet i didn't get how to achieve my model. Lets assume i have 11 classes of signs which they are all have different types and each must be classified , practically speaking i will give them 1,2,3,......,11 labels but know what about the negative? i mean what data should be trained in the negative training?

Macho gravatar imageMacho ( 2018-04-29 02:11:40 -0600 )edit

well, you can build (several) multi-class SVM's, where your positive labels for svm1 are: (20,30,40) and all other images are labellled -1, and svm2 like: (no-park, no-halt) and again all other images labelled as -1.

but i do not think, this is feasible at all, since then you also have to deal with conflicting predictions between svm1, svm2, svm3, ...

last, just saying, it's 2018 now, and there are more advanced ideas for classifying traffic signs, like taking a pretrained imagenet trained inception dnn, and retrain it on your traffic signs (transfer learning)

berak gravatar imageberak ( 2018-04-29 02:27:17 -0600 )edit

I know it is 2018 but i am a student tbh don't have time to do research again, so lets say i got stuck into SVM`s

You mean that i can initialize multiple instance of CvSVM ?

CvSVM svm1 = new CvSVM()

CvSVM svm2 = new CvSVM()

CvSVM svm3 = new CvSVM()

and each one can be multiple class svm is that right?

Macho gravatar imageMacho ( 2018-04-29 02:41:13 -0600 )edit

sure you can try that.

there's a HOGSVM tag here, are you using that ? note, that it will discard all color information, and only go for shape. maybe not a good idea.

berak gravatar imageberak ( 2018-04-29 02:51:11 -0600 )edit