Approach to implement Multi class SVM classifier
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()
svmClassONE.train(redSigns,label(1,2,3,4),pos)
svmClassONE.train(!redSigns,label(0),neg)
CvSVM svmClassTWO = new CvSVM()
svmClassTWO.train(blueSigns,label(1,2,3,4),pos)
svmClassTWO.train(!blueSigns,label(0),neg)
Is that a proper way ? is it okay to create multiple instance of SVM's ?
I am using Java OpenCV btw Thanks
UPDATE WITH CODE
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));
}
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 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
forSpeed 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..updated code looks, like it should work !
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?
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)
I know that the negative samples would have a label too, but what to put in the negative sample as images?
@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?
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)
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?
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.