Yolov8 Image Classification on Custom Dataset | Integrated with Streamlit App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this tutorial I'll be teaching you how to train your custom Yu VH classification model from scratch I'll take you through how to structure your data I'll take you through step by step the folder structure and how to train this model on our local machine we are not going to be using Google collab I'll teach you how to train this model with your own local machine then at the end of this tutorial after we train the model we test this model by integrating it with a streamlit app where you just drag and drop images and get your predictions on them if you like this tutorial if you like computer vision Ai and python related projects subscribe to this Channel and also share this Channel with others so that they also benefit from tutorials like this enough with myself s promotion staffs let's roll into it and train our custom classification model [Music] all righty guys we start off by looking at the data and the folder structure we need to train this model so for data we'll be using images of cats and dog in your case you can use anything you want to train for provided you have the images so let's say you want to classify carts and horses you want to classify bottles and let's say um ants you can go ahead and do it provide that you have the image all you need to do is to follow this tutorial step by step and make sure each and every steps is implemented as well so on my desktop I have a folder called cat and dog and this cat and dog is the root folder so this is our main folder in this case so inside it we have train and validation so you create a root folder called cat and dog in your case you can name it anything you like then inside it you have a folder name train and you have a folder name V or validation so the training folder will contain the images we are going to use to train our model and the validation folder will contain the images we are going to use to validate our model during the training process so when I open train you can see we have the classes cat and dog so when you open cats we have some images of cat and in this case I have 0 images for each class when you open dogs so you can see we have the same so you have a root folder called cat dog and inside it you have three TR and validation and inside train you have your classes inside validation you also have the same thing C and do just that your validation data is always less than your training data so I have only 20 images for validation in the case of both cat and dog so this is how you need to structure your data for training a classification model so in case you are having many classes maybe you have three classes you can go ahead and just add them here so it can be Cuts dogs goats bottles and whatever you have you can just put it right here but in our case I'm just doing only cats and dog okay so that it so you'll be like okay fell after training the model what images will we use to test the model I have that images here in a test folder so you can see we have some few images here of dogs and cats and we use these images to test the model after training so this images the model has not seen before so use it to test it to see how well it performs on this images so since we'll be training with our local machine we'll go ahead and create a new folder and I'll just name this folder YOLO classification so all I'll do is to open this folder with Visual Studio code or vs code and in here the first thing I'll do is to create a main.py file our main.py file is ready and I'll just run this it will prompt me to select a python interpretor so over here I have python 3.7 version installed I'll just choose it run again and that is it so now we have the terminal and the first thing we go ahead and do is to create a virtual environment I have a tutorial on Virtual environment why it's important you the link of this tutorial will be in the description so that you know why you should always create virtual environments for your projects so to create a virtual environment you just type python I in M V and V and then you give the name of the virtual environment you want to create so I'll type YOLO in our case this will go ahead and create it and you will get a prompt the virtual environment has been created do you want to use it I said yes I want to use the virtual environment the next is to activate this virtual environments and a simple way you can do this is to kill the terminal and rerun your code and now you can see our virtual environment is being activated you can see it right here y okay so now go ahead and install some some few packages for this particular tutorial and the first package we install is orality so I install atis and this will go ahead and install it so this this is the installation process it will install thetis Library together with other packages for us so that we can train our model so you can see we are installing pandas T Vision opcv n and so many other packages as well all right guys so our installation is done and the process is done so we can continue by training our model let me CLE the terminal and okay over here the first thing we need is to import yolu from orality so that we can instantiate it to train our model so we just start from AAL import yow the next thing you need is your data P the P to your root folder guys pay attention to this you need the path to your root folder so we just Define a variable here called Data path and this will be equal to the part to our root folder so I'll just move over to the desktop open this root folder here and then I will copy the address right here or the path right here head over back to vs code and all I have to do is to paste in this path here so this is the path to my root folder so the next step we need is to instantiate a pre-rain model we need in training our model so for that we can say our model and this is equal to our instance of our yo model and in here we yo have a lot of pre-rain model so we have to choose from whichever one you like in this case we'll be using the Nano version of the model so all we need to type is Yol V8 and H CLS so this is the particular pre-train model we'll be using and this is the Nano version of the Yol V8 models so if you want to use the medium model you can just change the end there to M if you want to use the large you can use l if you want to use the small you can just use x but in our case we are using the Nano version to train our model so now we have instantiated the model we want to use we'll go ahead now and train this model so training the model is only one line of code usually so we can just say result is equal to our model do strin and in here we just have to provide some couple of parameters for our model to train so the first thing we need is our data and this data will be equal to our data path then we need the number of a then you can put this to some number of a you want so you can normally the rule of th is that you make you increase this number because you V8 will automatically apply early stopping if your model performance do not increase over time as it's training so um you just have to put a higher number here so you can put any number you want want in my case I can just put 500 and I know the 4 500 EPO will elapse ear stopping will kick in and that's it okay the next thing you would like to put here is the image size so imgz and this I will set to 640 so you just have to put it like that and that's all so when we run this code we kick off our training process and everything will go on successfully so you have to follow this particular guidelines in order to train your custom modu so let me run this so it will go ahead and download the pre-train model for us we are specifying the pre-rain model here so it will download that pre-train model and then it pick up the whole training process so you can see the pre-train model has been downloaded right here and the next step is our model to train okay guys so we run into some errors here and if I should check correctly since we forget to add X here so it's image and Z so make sure the spelling here is actually correct and then we'll run this once again all righty so guys you can see our training process has begun we have the number of EOG GPU memory and our LW and everything so you can see we are one out of 500 EPS I'm not going to wait for this model to train because I went ahead and train one model which we will use for this particular tutorial but what I'm glad about the model is training and all the process we've gone through is working so you can see now two out of 500 so this will go ahead and train our model for 500 EPO I'll go over how to get your model after training so over here you can see we have the folder called R here so immediately the model start training this R folder will be created and inside runs you can see we have weights ask result. CSV train badge and whatsoever but what is important here is the weight and inside the weight you can see we have. PT and large. PT so the best. PT is your best performing model that you use so after the training process you just have to use this best. PT model for running your prediction or is the model you use the last. PT also works but mostly you have to use the best. PT because the best version of your model now over here result. CSV also gives you some results so I said I'm not going to wait till the model trains because I've trained some of these models which we use for our testing TX so I'll just cancel the training process and what I'll do is that um create another python script and this script I'll call test.py which we use for testing our model test.py is created but what I do is to show you the model of train when you come to the test data you can see we have another round folder here this is the model I've created before this tutorial inside this we have classify you open classify we have train and inside train you can see we have this we have our weight so this is the model we be using we have best. PT and what last. PT so over here we'll be using the best. PT so I'll just copy it path and it's the model we'll be using and I'll go back to PS code and I'll just say model path and this is equal to the particular part to our model right from here we also import you from aalis and that is it so this our model PA and fromes we are importing YOLO what I also do is to um bring in one image that we use for texting the model then we go ahead and create a stream L app where you can drag and drop images and get prediction so for that image I'll just say image and this is equal to or let's make it image back and this is also so you go to some image I'm going to copy the path so we just copy the path of one of the test images right here I'll copy the PA and I'll paste it inside here okay so I'll go ahead and also import CV I import CV2 and this installs by default when you install orality so you don't need to install it again and then um I'll read the image using opcv so image is to CV2 IM read and over we are reading our image path then what we'll do is to instantiate our model here so we can say our model is to yellow and the model will be using is our custom model which is our model path here okay so now we can run predictions using this particular model so we can just write your model and then we feed it our image to get some prediction okay so that's it we have the image here which we are reading with opcv so we just call our model and then we fed in some image and this will give us some prediction so we'll store these predictions in result and this is equal to our model then I think we can run this to see whatever we are getting okay I'm not printing it so let's print result you can see we have a lot of outputs here so within this output all we need is the probability key so we have to extract this probability and the names so these are the two most important thing we need the probabilities and then the names the class names so I close this and we have to go ahead and extract that and that is it so we can go ahead and print both the names and the probability so I'll go ahead and run this and over here you can see our names retains cats and dogs so um the key of zero is a dictionary the key of zero here stands for cut and the key of one here stands for dog so when we detect a class of zero that means it is a cat and we detect a class of one that means it's a dog and then our probability here is also retaining something you can see it right here so this rains a p tensor so data and it's a tensor so we can even grab the data there so prop dot data and that's it and one thing to I like to do is to convert this to non Pi instead of Pi tou tensor so by okay so um let's close this and also run it once again and here we go so the image particular image we are using is an image of a card so you can see data and it's it's a cat let me show you the particular image we are using so we are using this particular image and it's an image of a cat so the output here we have 0.99 for cat and 2 something for dog so you can see strongly that what we are predicting here is a cat and the reason I'm saying that is that the first key here which is zero stands for the class of cat so the first key here also corresponds to the first Index right here so this believes that we are actually predicting a cat okay so now that we are able to run predictions on our model it's time we just go ahead and integrate this with a stream Le app where we can just drag and drop images and see actual predictions in the textual for so for that I'll go ahead and install stream l so let me run this once again so that I get the terminal install stream l so I'll say peep install stream lit and hit enter this will go ahead and install stream L for us the installation process is done and I can close this teral what we will do is to import stream l so we import stream L as St so now we've imported stream L the first thing I do is to create a title so can see st. title stream L is very easy to use and over here so I'll just put something like drop your image for prediction so should it be drop or it should be insert let's just use insert instead of drop so inser your image for prediction then over here we can put it into parenthesis that dog and cat okay so I think this looks good then after this you have to create a file uploader so we will call this our image and this was equals to st. file uploader so inside here we can give it a label that okay upload your image and the next thing I do is to specify some types of image we want so say types and this takes it as a list so we need PNG we accept PNG images JPEG and so on so these are the image extensions we'll be accepting right here PNG jpeg jpg and GF I don't know how to pronounce that well so G okay so now I have to check whether someone has dropped an image or somebody has uploaded an image so we say if there is an image if there's an image we are goinging to read this image so we can use the pil Library so from op so right here what you will do is that I'll say my image is equal to image. open and over here we are taking our image so once we grab this image we can now run this image through our model and then we get some prediction so all of this code can now come up and then after here all we need is to bring in okay our model loading should also go up we can bring in this right here so our model and then we are inserting this particular image so let me copy this then we can do the other things we are doing and that's it so this all we need to do I think I can even comment the image reading path using the opcv because we don't need it anymore so we can comment this we can also comment this and with this our stream lad app is ready so let's run the code and then and then click this app up and see how it runs so to run a stream L app you just have to write stream L run then the name of your app that is test.py and you hit enter so this will just open your browser and then this app will run by default so you can see inser your image for prediction cat then over here we can just insert any of the images so go to desktop and then our test data and you have some images here so we can just upload an image of a card and this will run through the model okay so one thing we can also do is that after receiving the image you can also put this image on the screen so we can say st. image and over here we just pass in our image and that's is it so when I save this automatically to update click on always run and you can see the image is now here so now we just have to put our prediction right under the image and that's it okay so over here we can use nonp so let's import nonp we are importing nonp as MP and then we can just come down here and I'll say our prediction is equal to mp. Max and inside here you just put in your probabilities and what we do here is that if we can say if our prediction but before we do that let's even write that so we use St WR and then we write our prediction so you can see it returns one right here which means it's a dog because from the beginning we know the class zero or the key zero here belongs to cat and the key one here belongs to dog so I think we can also write that st. WR and what we are going to write here is the names so that we can keep track of that then we can add if statement here saying that if our prediction here is equal to zero then we'll write that our result is a cut so this is a cut not a dog otherwise our prediction is up so if the number return by our agmax function is zero then it's a cat but any other number it retains it's a dog so if it's not zero it will be one so if anything one then we know we are predicting a dog and that's what we are doing right here so let me open this once again and you can see this is a dog but for the moment of truth let's update one cut in ageny so this will go through the model and you can see this is a cut so we have zero for cats and one for dog so by with this you can just open up dates with some images and get some results so this is a dog so this model is working good and it's working great because I've trained this with about 100 and something EPO is it 120 or so EPO and then early stopping kicks in because the model have successfully trained and the performance is not increasing over time so early stopping kicks in that's it but after testing the model I'm very happy because the results is fantastic I just put in any image at of either dog or cat and is able to classify it so you can see this is a cat so guys this is all for this tutorial the code including the files and everything we use will be on my gtop repo and you know the link will be in the description for you guys to use this for yourself if you have any comments just put it in the comment section I'll be glad to answer your comment and also help you out for you to make this particular tutorial work in your use case thanks for watching we see you in the next tutorial
Info
Channel: Tech Watt
Views: 473
Rating: undefined out of 5
Keywords: yolov8, image classfication, custom dataset, ai, machine learning, computer vision, deep learning
Id: MrZHnaI4Nlk
Channel Id: undefined
Length: 22min 0sec (1320 seconds)
Published: Sun Jan 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.