IMAGE CLASSIFICATION with Yolov8 custom dataset | Computer vision tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey my name is Felipe and welcome to my  channel in this video I'm going to show you   how to make an image classifier using Yolo  V8 on your own custom data I'm going to show   you every single step of this process from how  to organize the data so it complies with Yolo V8   how to do the training in your local computer  and also from a Google Colab how to validate the   performance of the model you trained and finally  how to take the image classifier in order to   make new predictions I'm going to show you the  entire process this is going to be an amazing   tutorial and now let's get started so on today's  tutorial I'm going to show you how to train an   image classifier using yolo V8 on your own  custom data set so let's get started and the   first thing I'm going to do is to show you the  data I am going to use in this tutorial which is   a weather related dataset let me show you the  different categories we have and let me show   you all the different images how they look like  we have four different categories and they are   cloudy, rain, shine and sunrise now let me show you  each one of these categories for example the cloudy   category this is how the images look like you  can see that in each one of these images we have a   sky which is completely cloudy right we have many  different clouds for each one of these images now   the sunrise category it's basically many different  pictures of sunrises so this is how this category   look like and now for the shine category we have a  sky which is completely completely clear and with   a super super bright sun right you have the sun  in each one of these images and it's super super   bright and this is the rainy category and you can  see these are many different pictures of super   rainy days so this is basically the dara set I am  going to use in this tutorial but obviously you   can apply absolutely everything I'm going to show  you today to absolutely any type of data set you   are going to be able to build any type of image  classifier with everything I'm going to say in   this tutorial now let me show you the structure  you need for your data because if you're going   to train an image specifier or if you're going  to use yolo V8 yes the data is super super   important but you also need to structure to give  like a format to all of your data so it complies   with the way yolo V8 expects your data to be  right yolo V8 requires your data to be in a   given format in a given structure so I'm going  to show you exactly how to structure your file   system so everything looks the way it shloud to train  an image classifier using yolo V8 so if I show   you I have a directory which is called weather  data set this is going to be the root directory   you can call this directory whatever you want but  you need a directory which is going to be your   root directory and inside this directory you can  see we have two different folders one of them is   called train and the other one is called val and  this is exactly where you are going to have your   training dataset and your validation dataset right  it's very important you name these directories   exactly like this one of them should be called  train and the other one val now if I show you within   the train directory this is where we are  going to have our four directories containing   all the different images for all of our categories  basically you need to have as many directories as   categories you want to classify with your model so  in my case I want to classify an image into four   different categories and this is why I have four  different directories   each one of these directories is named as the category  I want to classify my images in right when one   of them is called cloudy the ther one  is called rain then shine and then sunrise and   these are the categories I want to classify all my  images and then within these directories, these   folders is where I have all my data within cloudy  is where I have all my data related to the Cloudy   category and so on right the same happens for  the rain and the shine and the sunrise category   so this is basically the structure you need for  your data the structure you need for your file   system in order to comply with what yolo v8 is expecting for your data and then if I go   to the val folder you can see I have exactly the  same structure I have four different directories   and they are named under the categories I want  to classify all my images and then if I open   this directory it's exactly the same you can  see that I only have different images for that   specific category now this is very important  because from now on everything is going to be   super super straightforward if you have created  this structure for your file system if your data   is exactly in the structure I show you there  is going to be super simple to train an image   classifier in yolov8 so this is very very  very important now I'm going to show you three   different ways in which you can train an image  classifier using yolo V8 so let's start with the   first way which is using a python script we are  going to make a very very simple script in Python   in order to train this model and let me show  you how to do it so let's go to pycharm this is   a pycharm project I created for todays tutorial and the first thing you should do  if you want to work with yolo V8 is to install a couple of dependencies a couple of python packages  these are two packages we are going to use in   this tutorial one of them is ultralytics and the  other one is numpy, ultralytics is very very very   very super important because this is exactly the  library you need in order to import yolo, in   order to train this model using yolo V8 so you  definitely need these two packages, now in order to   install these packages this is how we are going to  do it I'm going to show you a way to install these   packages which is going to work with whatever your  OS right if you are a Linux user or if you are a   Windows user if you use Mac it doesn't matter it's  going to work anyway so you need to go to file   then settings and then you have to select python  interpreter right this is the python interpreter   we are going to use you can see that I'm using  python 3.8 and then you need to click on plus   and this is where you're going to find... you're  going to search for the packages you want to   install in my case I'm going to search for  ultralytics and the version I'm going to use   let me copy the version first it's this one so I'm  just going to file setting then Ultralytics again   and then the version is this one okay and then I  click on install package in my case I have already   installed this dependency so nothing is going  to happen on my computer but please remember   to do it on your computer because otherwise you  will not be able to use or you're not going to   be able to do anything of what we are going  to be doing today now let's see numpy what's   exactly the version we are going to use we're  going to use 1.24.2 so file settings Plus numpy 1.4 24.2 so everything is okay now install package  this is like uh everything it's okay numpy has   been installed successfully so now we are ready  to continue once we you have installed these two   dependencies these two packages now you're ready  to continue and now you're ready to install your   own image classifier using yolo V8 so let's  go to main this is the file we are going to   use in order to code everything we need in order  to train this classifier and let me show you    exactly what's the code you need to type in order  to do this training I'm going over here to the   GitHub repository of yolo V8 and I'm going to  select the classification section right I'm going   for the classification and then here I'm going  to click on classification docs this is going   to open a new file a new URL a new website a new  page and this is exactly the all the information   we need in order to train this image classifier  I'm just going scroll down and I'm going to the   train section and this is what we're going to do I'm  going to copy and paste this line which is the one   in the middle the one that says load a pre-trained  model recommended for training I'm just going to   copy and then I'm going back to Pycharm and I'm  just going to paste it obviously we need to import   yolo otherwise this is not going to work  so I'm going to say from ultralytics import YOLO and that's pretty much all you can see now we  are creating our model, we are creating the   object we are going to use as our model and  then I'm just going to copy and paste this last   line which is model.train I'm going to paste it  here and then I'm going to make a few edits I'm   going to leave this value I'm going to leave  the image size in 64 but then for the number of   epochs I'm going to set it in 1 right because  the first thing we're going to do is we're going   to do a very very dummy training in order to make  sure everything works as expected in order to make   sure everything works properly and once we are  completely and 100% sure everything is okay we are   going to move forward with a more deeper training  and with a more real training right but for now   let's just do the training for one Epoch and let's  see how it goes then for data this is where you're   going to specify the absolute part to the data  you are going to train this model with right in   my case it's going to be this weather dataset so I'm  just going to copy and paste the absolute path of   this data set which is this... I'm going to copy this  path and I'm going to paste it here right this is the  data I am going to use remember that you need to  specify the absolute path to the root directory   of your data and remember you need to structure  your data into the exact format that I already   mentioned right otherwise this is not going to  work and that's everything we need in order to   train this image classifier so the only thing I'm  going to do is to press play I'm going to run this   script so let's see what happens remember we are  running this training we are doing this process   for only one Epoch because we need to make sure  everything works properly and once everything   is working properly we are just going to edit  this value we're going to make this training   for more epochs but you can see everything seems  to be working properly so everything seems to be   okay and everything seems to be completed and  everything seems to be ready so that's it and   you can see that the results have been saved here  in run/classify/train12 so let me show you   exactly where this directory where this location  is in my file system if I go to the project the   pycharm project I created into my file system  this is exactly the project I created this is the   file we are currently working in the main.py file  and this is where my data is located and this is   where the runs directory the runs folder will be  located this is where it will be created you   can see that within runs we have another directory  which is called classify and here is where you   will have many many many folders for each one of  your training processes and you can see that in   my case I have trained this classifier many many  many different times while I Was preparing this   video so there are many directories for me but  this is exactly the one which was just created   the train12 right train12 this is exactly  the directory which was just created and if   I open this directory you can see we have another  directory and then we have two files I'm going to   explain what exactly all these different files and  all these different folders are and exactly what's   the information we have in all these files but  I'm going to do it later on this tutorial when   we are validating this training process right  for now just remember all the results will be   saved here will be saved within  this folder within the runs folder and then within   classify and then a new directory a new folder  will be created for the training process you have   just executed right this is something you need to  remember for now but later on this tutorial I'm   going to show you exactly how you can validate the  training using the information that's within this   directory but for now let's continue I'm going  to show you now a different way in which you can   train this image classifier using yolo V8 I'm  going to do... I'm going to show you how to do it   using the command line using this utility and this  is actually like a very very straightforward way   to do this training let me show you you can see that  we have three different examples I'm just going to   select this one I'm going to copy and paste this  instruction this line and I'm going to show you   how it I'm just going to paste it here and you can  see that we have many different parameters right   in the first word is yolo this is the utility  we are going to execute then classify this is   the task we are going to execute we are going to  train an image classifier and then we are going   to train it so we need... we have another keyword  which is train and then we have these arguments   data model and epochs and also image size I'm  going to do exactly the same with image size   I'm just going to leave this value in 64 but then  I'm going to edit all the other values so actually   I'm going to the number of epochs and I'm  also going to edit data for the number of epochs   let's do something similar I'm just going to do  it for one epoch so we make sure everything runs   smoothly and everything runs properly and then  we can do like a more serious training a more   real training for more epochs this is exactly  the model I'm going to use so I'm not going to   edit this keyword either and then I'm going to  edit this argument and I'm just going to say   this is the absolute path to my data so this is  going to be exactly the same as I have over here something like this okay and that's pretty much  all the only thing I need to do now I'm going to   copy and paste this sentence and I'm just going  to a terminal and I'm going to do something like   this right I'm just copying I have just copy and  paste that sentence and you can see that that's   all we need to do in order to train this image  classifiers using yolov8 you can see that the model...   the training process has started and everything  is running super super smoothly so everything is   going super super well that's all right that's a  very very quick way and a very straightforward way   to do this training you can see the training has  just been completed and this is exactly where the   results have been saved to runs/classify/train13,  so everything is completed everything is ready   you can see how simple how fast is to train an  image classifier just by running this command   now I'm going to show you another way to do this  training which is using a google colab we are going   to use a Jupiter notebook we are going to use a  notebook in a Google collab in order to train this   model and this is also like a very good way to do  it so let me show you how to do it so basically   you need to go to google drive you need to go to  your Google Drive you need to select new then more   google collaboratory and this is going to  open a new notebook this is going to open   a new notebook in Google Colab and  is exactly what you need to do in order   to use this notebook to train yolo v8  now I'm going to show you a notebook   I have already created in order to train  this model which is this one is called train.ipymb and obviusly I'm going to give you  exactly this notebook in the GitHub repository   of today's video of today's tutorial so you can  just use this notebook if you want now I'm going   to show you all these different cells everything  that's already writen on this notebook so   you can... so you understand how exactly to use it  and how it works and what exactly you are doing   at each step so let's start with the first step  another thing you need to do if you want to   train this image classifier is to upload all  the data with all the images and with all your   categories into Google Drive obviously for  example in my case this is where I have my   weather data set you can see that this directory  is exactly this same directory I have over here   weather data set within weather data set there  are two directories which are train and val   if I if I open this directory you can see we  also have traiin and val so this is exactly   exactly the same data as in my local computer now  this is something very important because remember   to do it because you need the data in your Google  Drive in order to train this model using a Google   collab this is a very very important step please  remember to upload your data into Google Drive   now once your data is in Google Drive then you  need to be able to access your data from the   Google collab and in order to do that you need  to execute this cell if I click enter you can see   that now I'm going to be asked if I want  to connect Google collab with Google Drive and the   only thing I need to do is to say I accept there  you can see that it's requesting for my permission   I say connect to Google Drive and then   I select my account and then basically is to   scroll down to the bottom of this page and to  click allow and it's going to allow Google collab   to access all the data you have in your Google  Drive so this is a very very very important step   now something that's very important is that  you need to be able to access your data so you need   to know where your data is located in the Google  drive right you need to know exactly what's the path   what's the location of your data in Google Drive  in my case let me show you my Google Drive you   can see that my data is located into a directory  this is my root directory which is my drive then   I have another directory which is called computer  vision engineer then another directory which is   image classification yolo V8 and then data and  then this is where my weather data set is located   in your case it's going to be different obviously  it depends on where exactly you have uploaded your   data so something you may want to do is just to  click this... ls you can say something like ls and   then you say something like content my Gdrive  my drive right you execute this command and if I   execute this command you're going to see a very  very long list of files which are basically all   the files which are in my root directory in  Google Drive and for example this is where   I have the directory which is called computer  video engineer and if I do ls you're going to   see all these different directories if I say  something like image classification yolo V8   then this is data train.ipymb which is  exactly this notebook and then if I say data this is exactly where the weather data set is  located right so do something like that because   you definitely need to know what is the path of  your data in Google collab right you definitely   need to do it in order to continue to The  Next Step this is very important because if   you haven't set your data properly if your data  location is not set properly then yolo V8 will   not be able to train your model this is very very  important so in my case this is exactly where the   data the weather data set is located right this is  the path to the weather dataset so this is the the cell   I am going to execute and this is the value I'm  going to save in this value in data dir now   I'm going to continue then we need to pip install  ultralytics which is the library we need in order   to train this model in order to use yolo V8 now  the only thing you need to do is to execute this   cell and everything will run super smoothly you  can see that we have already completed this   process now I'm going to continue and the only  thing we need to do now is to execute this cell   and you can see that the code we have in this  cell is very very similar to the code we have   over here right basically we are running a python  script from a Google collab that's all we're doing   so you can see we are importing OS and also we  are importing the YOLO Library we are importing   from ultralytics we're importing yolo and  then we are doing exactly the same as we are   doing before and this is where we are using the  data directory the data dir variable we have   defined over here right so this is why it's  very very important you set this variable properly   so the only thing I'm going to do... I'm going to  do exactly the same as before I'm just going to   do this training for only one Epoch so we make  sure everything's okay I'm going to press enter   and that should be it in order to do all  this training the first time you execute   this training it may take a little longer because  you are downloading all the weights and you're   downloading the models and everything but uh after  that everything should be much much quicker okay   so you can see that now the training process is  in progress everything is going super super   well and from now on the only thing we need to do  is to edit the number of epochs so we do like a   more deeper training but I will say everything is  working super super properly so now let's move to   the other cells so I show you what exactly you  need to do once everything is completed once   everything is completed the only thing you need to  do is to run this cell so you are copying you are   going to copy all your results which were saved  on this directory you're going to copy everything   on your Google drive right because remember you  are working on a Google colab you're working   on an environment which is your Google collab  environment if you don't do something like this   it's going to be super super hard for you to get  the data you have just trained right to get your   results to get your model your weights is going to  be super super hard because everything is located   in your Google collab environment and long story  short is going to be much much simpler and much   much better if you just do something like this  and you just copy everything all the results which   were saved in this directory into your Google  Drive it's going to be much much better because   it's going to be much easier to download the weights  to download the results and so on so now I'm just   going to wait a couple of minutes so everything is  completed over here and then I can show you how to   copy the results into your Google Drive okay now  the training process has been completed and you   can see that the results have been saved into runs  classify train so this has a very similar output   to the one we just noticed when we were training on  our local environment now the only thing we need   to do is to copy everything into our Google Drive  so everything is much much simpler if you want   to download these results or to do whatever we  want so the only thing I'm going to do is to run   this cell and everything will be copied into this  directory which is the same directory where I have   my data and where I have my my Google collab right  now you can see that everything has been copied   already this is the directory I have just copied  this is the time this is the current time so this   is the result of the cell I have just executed and  if I go to runs classify train you can see that   these are all the results we have generated this  is the CSV file containing many different results   which I'm going to show you in a few minutes  and these are the weights and so on so from now   on if we want to get this data or if we want to  analyze this data the only thing we need to do is   to select runs and then we just need to click  download and it is going to download all this   directory into your local drive right you can see  everything is being zipping and once everything   is zipped this directory will be downloaded  into my local computer and you can see that this   directory has just been downloaded so everything  is working just fine now this is pretty much all in   order to show you three different ways in which  you can train an image classifier using yolo V8   and now let's do the deeper training right I'm  just going to take this script and I'm going to   edit the number of epochs so we do this training  for something like 20 epochs I have already been   doing some tests and 20 epochs is just enough for  this dataset for the data set I am using in this   tutorial so 20 will be just fine now the only  thing we need to do is to click on run I'm just   going to run this script as it is and everything  will be exactly the same as before everything will   be exactly the same right we are just we just need  to wait until this process is completely we don't   need to do anything from now on but this process  will be executed for 20 epochs so the only thing   I'm going to do is to wait until this process is  completed and once everything is completed we are   going to validate this training process I'm going  to show you how to analyze if all this process was   done successfully or not if you have successfully  trained a good image classifier or not so I'm   just going to pause the recording here and I'm  going to fast forward until this is completed   okay so the training process has been completed  and now let me show you all the results which   were saved here into runs classify and train14  now let me show you this directory this   folder in my local computer if I go to runs  classify and then train14 this is where all   the results have been saved and this is everything  we are going to analyze now, now we are going to   decide if the model we have trained is a good  model or not we are going to decide if this   is a model we can use or not so you can see that  there are two files args.yaml and results.csv and   another directory called weights let's start  by args.yaml if I open this file you can see   that this is something like a config file and  this is exactly the entire configuration file   which we have just used in order to train this  model this is very important because this is a   super super comprehensive list of all the hyper  parameters we have used in order to train this   model and for example the only parameters we have  specified are image size number of epochs and then   data the location of the data we have just used  and you can see that we have a keyword which is   data then epochs then image size and then we  have many many many other keywords as well   this is very important because these are  absolutely all the keywords we have used   we have used all these default values which were  set for all these different keywords and this is   important in case we want to train a new model  and we want to make some changes into some of   these hyper parameters now let me show you the  other file which is the results.csv file I would    say this is much more important this is like the  file containing all the information we need   in order to decide if this is a good model or not  and you can see that we have many different rows   each row for one of our training epochs right we  have trained this model for 20 epochs and you can   see that we have 20 rows for each one of these  epochs and for each one of these rows we have all   this different information and we are going to  focus on these three values on the training lose   the accuracy, this is the accuracy of the  validation set and then also the validation   loss right these are the three keywords in which  we are going to focus on this tutorial in order   to validate this model and I'm going to give you  like a very very quick tip like a very quick way   in order to analyze this training process which  is make sure the training loss and the validation   loss are going down through this training process  and also make sure the accuracy goes up and I know   you're thinking hey this is a very simple way  to analyze this process felpe yeah I agree with   you this is a very simple way but at the same  time it's very robust this is like a a very   simple but at the same time very powerful way to  decide if you have a good model or not now we can   analyze all these numbers but I think it's going  to be much much better and it's going to be much   much prettier if we make a plot with all these  numbers right because we have epochs in this   um in this column in this coordinate and  we also have all these different values   and we can definitely plot these values across all  these different epochs so let me show you a python   file I have created and this is exactly what  this python file does this file is called   plot_metrics and if I open this file you can see that  it basically we need to set the path to our results.csv   file in our case I'm going to set it to  train14 and you can see this is run/classify/train14   and thenresults.csv and then this is only  like some logic some very simple logic to take all   the data from these results.csv file and to do  some plots with it right that's all we are doing   we're just taking the data and doing some plots  and this file will be available in the GitHub   repository of this project of this tutorial so you  can definitely take this file and you can just use   it to to plot your functions as well all I'm going  to do now is just press play and you can see that   if we wait only a few seconds we get all these  two plots right and this is all the information   in our CSV file right everything I showed you  over here it's summarized on these two plots   so this is exactly what I mean with make sure your  loss is going down this is your    loss in the training set and in the validation  set in the training set we are plotting the loss   in blue and in the validation set is red and you  can see that in both cases the loss is going down   right which is exactly what we expect it's exactly  what we want now this is a very very simple way to   analyze this process but trust me this is also a  very powerful way right this is something that's   very very healthy something that looks like  this it's very healthy and then for this other   plot which is how the validation accuracy evolves  through this training process you can see that the   evaluation accuracy goes up when we increase the  number of epochs right you can see that starting   from the 10th Epoch or so everything starts to be  like somehow iddle right we are not really gaining   a lot of accuracy from here but we are not losing  accuracy either right we are just in something   like a plateau and this this is exactly how a   validation accuracy plot should look like right we   are starting from a very low value and then we  are just increasing our accuracy until we reach   a very high value of accuracy right this is like  a very healthy training process now obviously we   could make this process even better if we just  tune if we just change some of these parameters   and if we do like a more customized training I'm  sure we are... we will be able to have a better model   right because remember we are using all the default  values so as it usually goes if we make like a   more customized training and  we try different parameters and so on we should   be able to get like a better model but obviously  we're not going to do it in this tutorial because   I just wanted to show you like the end-to-end of  how to train this image classifier but remember   you could do it even better than this if you make  like a more custom model so this is pretty much   all for analyzing these plots which are the  validation accuracy and the loss function in   order to validate your training and then it's like  this directory which is the weights directory   you can see that this directory is called weights  and this is exactly where the models will be saved   this is very important because you have trained  a model and now obviously you want this model in   order to use it in your images in your data and  this is exactly where you are going to find this   model and you can see that you have two different  files one of them is called last.pt another one   is called best.pt now let me explain exactly what  these two files are and exactly what they mean so   remember how this training process works right  remember that you have a model you have a deep   learning model which is comprised of many many  many different weights and the way it goes is   that at the end of every Epoch right at the end of  the first Epoch of the second epoch of the third   epoch and so on you are updating the weights  of your model you are updating the weights of   your architecture of your deep learning model so  the way it works is that at the end of every   Epoch you have a model available which is a model  you have trained so far with all the process you   have followed so far so last.pt means that  you are taking the model which was the result of   the last Epoch of your training process right  remember at the end of absolutely every single   Epoch you have a model available which you can  definitely use if you want to in order to produce   your inferences and so on, so last.pt only  means that you are taking the last Model the model   which was produced at the end of your training  process at the end of the last Epoch in your   training process so at the end of the 20th Epoch  in our training process we are producing this model   Which is last.pt but you may Wonder hey Felipe  yeah it's great because at the end of our   training process our accuracy is something  like a 93% right a 93% it's   a very good accuracy but if we take the accuracy  if we take the model at the end of the 16th Epoch   for example our accuracy it's higher it's a  94.9 % maybe it makes more sense to take   that model instead right because we have an even  better accuracy we have an even higher accuracy   and if you ask me something like that I would say  yeah you're perfectly right you're you're super   super right that's a very valid argument and  that's exactly what the best.pt model is right   we are saving the weights of the best model in  our entire training process so if we look at   our data the best model in our training process is  this one if I'm not mistaken right it's the model   we produced at the end of the 16th Epoch and our  accuracy our validation accuracy was 94.9%   so this is definitely higher than the accuracy  we got at the end of this training process   which has which was a 93.5 and if we will take  the best model we have produced in the entire   process in the entire training process then  we will definitely need to take this model so   this is exactly what best.pt represents is the  best training the best model you have trained   in your training process and if you ask me what I  usually do is take in the model which was produced   at the end of the training process right what  I usually do is take in the last,pt file   because I consider that if this is a model we  have produced at the end of the training process   in this model we are summarizing much more  information right because we are considering   much more data we are considering much more  everything in all this training process many   things are going on many many things are going  on and remember there's a lot of Randomness in   this training process so I, me, personally  I consider that if I take the model which was   trained at the end of this process is a much  better option that if I choose a another one   if I choose like the best model or the model which  got the highest accuracy but it's not the last   Model that's what I usually do I usually take  the last model which was produced at the end of   the training process but if you want to take the  best model if you want to take best.pt  it also makes sense because you are taking the model  which produced the highest accuracy right   so you can do either one of them and I think it's  a very a good option that's why you have these   two files because you can use one of them or you  can use the other one and I would say that making   like a very very like the best decision on which  mode to use depends on many different variables   depends on many different things depends on your  data depends on your problem depends on your use   case depends on your training process  depends on many many different things which is   the best option right so remember you have these  two models and it's all up to you it's all up to   your specific project and it's all up to your  preferences which model you want to use right   if the best model which you have produced through  the entire training process or if you want to use   the last Model the model which you have produced  at the end of your training process so now let's   go back to pycharm because now it's time to make  our inferences now it's time to predict new    samples right and we are going to input an image  and we're going to use our image classifier in order to   predict which category this image belongs to so  let me show you how to do it I'm going to import   from ultralytics import YOLO and then  let's go back to this page because now we   are going to move to the predict section and  the only thing I'm going to do is to copy   this sentence... going to paste it here and then  I'm going to specify the path the absolute path   to the model which we have trained right  we don't really need to make it like the   absolute path we can use the relative path  so I'm going to do something like this right   sorry something like this so this is the path to  the model we have just trained right this is the   last model which we produce at the end of this  training process and this is the model I'm going   to use in order to show you how this works and  now let's copy this additional sentence which is   results = model and the model path the image  path right you can see that you can use an image   in your local computer in your file system or you  can also use something like an URL for example   in this case in this example which is in the  yolo V8 website you can see that the example is   using an URL and this is also going to work so  in my case I'm going to use an image in my local   computer I'm going to use one of the images I used  for training because I only want to show you how   this works but obviously you can use whatever  data whatever image you want so this is the   image I am going to use I'm just going to use I'm  just going to inference this image right which is   the first image in my Sunrise category data so  this is going to be something like sunrise1.jpg and this is pretty much all so these are  the results the first thing I'm going to   do is just trying to run this code and let's see  what happens everything should run smoothly but   this is where we are going to see if we have an  error or something like that we may need to wait a   couple seconds and everything seems to be working  fine because we didn't get an error so what I'm   going to do now is I'm going to print results  because I want to show you a couple of things   so this is the entire information we are getting  when we are printing results right you can see   that this is a lot of information we have these  probabilities which is the inferences we are   making this is exactly the result of applying  our image classifier and then we have a lot of   information another object or another result  which is very important is this one which are   the names of the categories we have just trained  our image classifier on right you can see this is   cloudy rain shine sunrise and also you can see  that we have different integer values for each one   of these categories so this is something like a  dictionary because we are going to have a result   from applying our image classifier and then  with this result which is going to be an integer   we are going to call this dictionary we're  going to call this object because we want to   know exactly what's the name of the category we  have just inferenced right so this is how we're   going to do it I'm going to call another variable  which is going to be names something like   names dictionary names_dict and this is results  zero because results is a list in this case   we only want to access the first element because  we are only predicting an individual image so this   is the element we want and then we are  going to call Dot names and that's pretty much   all then I'm going to Define another variable  which is props and this is results 0 dot props   and this is the probability Vector of all the  different categories we are trying to classify   right so we are going to have a length 4 array  with the probabilities of the different classes   we are classifying right so let me show you how  props looks like I'm going to print props and I'm   going to do something else I'm going to say to  list so we make this object into a list we are   using yolo which is based on pytorch so if we  don't do this if we don't call this method we will   be working with a torch object right with a tensor  so we don't really want to do that so that's why   I'm doing this tolist now I'm going to print  props so I show you how it looks like and I'll   show you how to continue from here okay you can  see that this is a result we got from applying from   printing props and you can see that this is a list  with four elements one two three and four and each   one of these elements are the probabilities  of this image to be one of these categories   right let's print the names too so we have all  the information in our screen I want to show   you I want to show you something so I'm going to  print sorry this wasn't names this was names dict and now let's wait a couple of seconds I want  to show you not only the probabilities but   also the class names so it's a little more clear what  exactly I'm going to show you now so this means   that this number is the probability for this  image to be cloudy right this other number is   the probability for this image to be rain this  other number is probability to be shine and   then this last number is the probability to be  sunrise and you can see by the values that we   are definitely classifying this image as Sunrise  right because this is almost a one this is almost   like a super super confident and absolutely  confident classification so this is exactly   the category we are classifying for this image and  this is how to make sense of this information so   what I'm going to do now is to print names dicts  and then I'm going to call np dot arg max and then I'm going to input the probability list  I just showed you and obviously I need to import numpy as np otherwise is not going to work and  basically what we are doing here is that we   are looking at this list the one containing all  four probabilities we are taking a look at the   maximum number which in this case is this one and  we are taking the index of this maximum number so   in this case this is the first element so this is  the index 0 this is one this is two and this is   three right so from this um from calling np dot  arg max props we are getting three and then we   are calling the third element of the names_dicts  object so we go here and we see that 3 belongs to   the sunrise category and if we look at this image  again we are going to see we are in fact plotting   a sunrise let me show you so everything seems to  be working fine and this is going to be all for   today this is exactly how you can train an image  classifier using yolo V8 in your own custom data   and this is going to be all for this tutorial  so this is going to be all for today if you   enjoyed this video remember to click the like button  and also remember to subscribe to my channel my   name is Felipe I'm a computer vision engineer  and these are exactly the type of projects I   make in this channel so this is going to be  all for today and see you on my next video
Info
Channel: Computer vision engineer
Views: 24,990
Rating: undefined out of 5
Keywords:
Id: ZeLg5rxLGLg
Channel Id: undefined
Length: 45min 7sec (2707 seconds)
Published: Wed Apr 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.