Svm Vs Random Forest | 68b – Svm Vs. Random Forest For Image Segmentation


Subscribe Here





68b - Svm Vs. Random Forest For Image Segmentation


Hey, guys, you’re watching. Python tutorial videos on my Youtube channel Python from across campus in today’s tutorial, let’s look at how performing image segmentation, using support vector machines or SVM for short in the last few tutorials. In fact, in the last tutorial, I introduced the concept of support vector machines at a high level. You know, we discussed or you know how the algorithm actually works. But the previous few two videos, we performed image segmentation, using random forests again. The way we did, This was, we took a training image and corresponding mask and for each of the labels in the mask, we extracted a whole bunch of features, okay, and then we trained the random forest algorithm using these features and created a model and then use that model to segment well, first of all that the training images and then also to segment any other images that look very similar to the image that we have used for training. Okay to summarize it. We’ve used random forest algorithm to generate a model using training images. Okay, and then that model got used for segmenting other images. So in this tutorial, let’s switch random forests with support vector machines. That’s it, okay, very simple exercise. Let’s switch, let’s take the same code. I don’t want to repeat exactly what I’ve done. You know, in the last few video, so please go through these videos in this in this playlist, but let’s simply switch the random forest part with support vector machines and see how the result looks like. And then, of course we can compare both SVM and random forests to see which one is better for these type of image segmentation. You know, tasks, so lets a directly jump into the code and have a quick look at it. So here is the code that we have written to read an image, a trained image or a training image and also read a training mask, corresponding mask and extract features from the training image and train an algorithm in this case, random for classifier to create a model and then predict it. Okay, so just to show you this trained or training image that we have used looks somewhat like this. Okay, and the training mask looks like this, okay, so on the left hand side, we have the original image, so let me actually try to decrease the size to match their dimensions. So on the left hand side is the training image. So we said okay. This dark area is let’s say represented by blue here. The bright area represented in yellow and this this, this medium gray, with some texture is represented by green and red. Is this medium gray? That has no texture, okay, or very flat, relatively flat region. So this is a training mask and again. Training mask is nothing, but of course, it’s a segmented image meaning. All the pixels in the blue. Let’s say, have a value of zero. All the pixels of red have a value of one. All the pixels in yellow have a value of two and so on so each region is correspondent by a unique value. So so this is this is what we started by reading and right, so in this code, Okay, Our input image or the training image is converted to the gray level image, and then we started off by creating an empty data frame that we sequentially filled with corresponding features that we extracted from this input image, the feature number one being the pixels from original image itself, right, so we actually took the actual image and we converted that to gray values and then that gray value represents my original image, and then we took that image initial, the actual original image and applied a whole bunch of filters, in this example generated a whole bunch of COBOL filters, and then the filtered image pixels are added to the data frame. Okay, and then we also applied traditional filters like canny edge and Roberts and Pruitt and gaussians and so on median, and we kept adding those to the data frame and finally, this is obviously we need also do for each pixel. We need to at least label them, so that’s where we actually loaded the training image or a training mask, which is nothing, but this colorful image that I showed you earlier and then at every pixel now we have a label. Is it blue? Is it red? Is it yellow? Is it green well? I shouldn’t use the color names. But is it zero one two three four, right so that those values are actually again appended or added. I should say to the data frame so now at the end of this. I have a data frame that’s ready. Okay, a data frame that has if you look up here. 42 columns and the column names our original image cabal, one couple, two, three, four and all of these columns with the last column being labels. Okay, so now the data is ready for machine learning algorithm. And then we define that, okay. My, why is the column that’s labeled label. Okay, so that is my. Y Y is nothing, but this is the these are the values that we are trying to predict, right. Y is nothing, but my labels and X are all the independent variables. Y is the dependent variable that we are trying to predict. X is the independent variables, which is everything except for the column name label, which includes the original image, The Gabor, one Gabor, 2 million. Gaussian all of this, All of these are used are part of my X, And then we split the data and we used random forests. We imported the random forest classifier and then fit the model and then we did the prediction and reported the accuracy values. Okay, and because random Forest Classifier reports the feature priorities or feature importances. We printed them out support vector machines. Doesn’t do that so. I need to comment these out. When which is switch this from random forests to support vector machines and the the model that we actually created here, we pickled it meaning. We saved it. I should say, and then we load that model to actually segment this image that we are that we just used for training. Of course, that model can be used to segment other images. But this is the code that we have written in the previous tutorial again. I encourage you to watch the tutorial itself, so you understand exactly every line here. So now our task is to replace this random force classifier. In fact, you see the result on the right-hand side from the random Forest Classifier, so we used 10 estimators and we got an accuracy of 98.1 in fact. Let me go ahead and run this so you can see how fast it is. Random forest is much faster, compared to support vector machines. I should say, okay. I know this appears a bit slow. I’m recording this on my laptop It takes you know about. I would say, like 20 seconds or so which sounds very long, but this is still pretty fast, actually for training support vector machines. I’ll probably speed up the video when I when I do that. Support Vector machine training. But it is, it is an order of magnitude, at least slower compared to random forests. Okay, so this is the report result that we got now. How do we switch this random forest with support vector machines? So while that’s running, let me go ahead and comment these two off. I’m gonna leave everything as is okay. The only thing I’m gonna change is the classifier itself, So I’m gonna comment that, and I’m gonna comment the model, okay, and by the way, this is done, all you are seeing. Is that okay. We got ninety eight point. One percent accuracy and here is how the final image looks like once it’s segmented. So now lets let’s use support vector machine. So you can import it from scikit-learn dot? Svm okay from SK learn. Svm, let’s import linear SVC. Okay, this is how you import your support vector machines, and now all we need to do is just apply it. That’s it model equals two linear. Svc, okay, and then now we have to give how many iterations. Okay, so let me just say by default. If you don’t provide this value. The maximum number of iterations is 100 I believe, but I’m gonna just provide that anyway. Okay, so my model. I am initiating. This model, and this is nothing but linear Spc. Previously my model was random Forest classifier, right, and the parameters that the random forest took was the number of estimators and then some seed for a random state, so it doesn’t change every time you run it, but here linear. Spc. I’m just gonna give OK 100 iterations. Everything else I can keep. I believe the same, except let’s comment out this feature list, like I said, Feature importance is not part of support vector machines, so that’s it, so let’s go ahead and run this so the way to implement Svm. Since it’s so easy, you know, in terms of implementing it, I encourage you to do both random forests and Spc to see to see how the results look like to compare them. And how fast is the computing time, right and then pick the right one? That works for your use case. OK, so there you go. After all that time, first of all, it says, linear failed to converge, increase the number of iterations, so apparently, 100 iterations is not enough. And then it says that, OK? The accuracy is 93.9% which sounds great. But if you look at this image. This is horrible, OK? So let’s actually compare this results later on, but first let me change this number of iterations to 1,000 and see what happens. OK, let’s run it so there you go. Now, we see slightly better result and it says 96.3% accuracy. After all of that time, right, so now, let’s actually compare the results between random forest and 100 iterations for SVM support vector machines and then 1,000 iterations. OK, so first, let me open the original image, so we can actually compare the results and here it is for lets. Which one do we open? So let’s open the SVM 100 iterations, okay. I cleaned up this a bit so here. It is and let me resize, so we can actually compare. Okay and let me bring the other. Let’s see one thousand iterations. So this is one thousand iterations. So first of all. You can see that, actually. I think it’s better if we compare it with the labeled image, I mean, sorry. For the colors, we could have actually changed the colors, but this blue is represented in dark blue here and the red is represented in light blue, so don’t be confused, just look at these different regions, so the yellow it did pretty good with 100 iterations and also with 1,000 iterations. Right, It’s the it’s this green region where it actually struggled. It is green area, so not much. I mean here. It’s represented in yellow, so we know that 100 iterations is ridiculous. I mean, this is not useful. This is not accurate at all and now looking at 1,000 iterations there. This is okay, although still not as good as you know. Let me open random for us. So now we can actually see so very synonym for this is random forest with 10 estimators on the bottom and I should also have 100 estimators random forest, so as you can see random forest with 1 I mean, with 10 estimators is already pretty good much better than what I got with. Svm 100 iterations, or 1,000 iterations, in fact, with 100 iterations. I’m not surprised it looks this way because it did not converge. Okay, so it didn’t finish so we increased it to 1,000 and still, it’s not good. Maybe if I go to 10,000 it would be better, but then I would wait forever, You know, to for to get my result now with random forest. This is like literally a few seconds, you know, And with 10 estimators, it’s already pretty good, OK? In fact, I don’t even see much of a difference between 10 estimators or 100 estimators Okay, so well. I thought I opened 100 estimators just a second, so 100 estimators. But either way this is as you can see. Random forest is definitely much better than support vector machines as you can see here and at least for most of the image processing, you know examples? I have seen for my own work, not for just this video it. I mean random. Forests seem to be much much better choice compared to any other algorithm out there. So this tutorial is about support vector machines. But I’m telling you there. Trenhol’m Forest is better than this, but other this is the only way we can learn by looking at how the result looks like for SVM. Okay, so although that doesn’t mean support, vector machines is useless. I mean, for pixel segmentation, support vector machine is probably not the right choice, but if you have image classification, for example, a support vector machine actually does a very good job, so in the next tutorial. I’m going to talk about how to classify your images, not just pixel segmentation, okay, using a technique or an approach called bag-of-word’s again, you’ll learn that in the next tutorial and for that, we will be using support vector machines. Okay, so think of next tutorial as an extension of this one so. I hope you learned something from this tutorial. If you liked this video, please go ahead and like it and please subscribe to this channel definitely keeps me encouraged to teach you more about image processing using Python. Thank you very much.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more