I know everyone in this video tutorial.
We will see how we can train our YOLO V8 model for traffic, light detection and color recognition.
So for this project I will be using that traffic light data set available on Roboflow.
This is a publicly available dataset, so how you can access this dataset.
Let me show you.
You just need to go over here and write Roboflow and just click on the first link which appears over
here.
And if you haven't created your account, you can create your account on roboflow by clicking on sign
up.
So I have already created an account, so I will just click on sign in so it will redirect me to the
side like I want to sign it with my google email.
Okay.
So it might take few seconds.
Okay.
Just sign in with your.
I'm just going to sign in with my Google email.
Okay.
So it might take few seconds.
So let's say.
Okay.
But so I am able to sign in.
So I will just go to Universe over here.
Then I will.
Here.
Right.
Traffic lights.
Better said.
In the.
This the name of the dataset?
We do one.
Okay.
So.
So here is the dataset.
Basically, the the name of the dataset is on Roboflow is Cinta V2 and dash one represents the version
one.
Okay.
So if you click over here, we have around 1000 images in our dataset and we have around three classes.
If we can see over here, let me show you.
So you can see that we have red, green and yellow glass over here.
So we have three classes.
If it is a red signal, it will appear that it is red.
If it is a green signal, it will appear green.
And if it is a yellow signal, the label will appear yellow.
Okay.
So and as far as the health check of the model is concerned, we can see that the yellow class is underrepresented
while all other classes are balanced.
Okay.
And we have around 103 hundred 1320 annotation like each image contains around 1 to 2 annotated annotations.
Okay.
And we have three classes in our dataset.
Okay, so let's export this data set into our Google CoLab notebook and let's implement this project.
Well, guys, here is our code.
In the first step, I am importing the library.
So library basically through library, I will create a helper variable which helps me to navigate between
different file paths.
Okay, so here I have created a helper variable using library, so it helps me navigate between the
different file paths.
In the next step, I am importing the Globe Library.
Okay, so Globe Library basically returns me all file paths that match the specific pattern.
So here I'm using Globe Library when I test my train model on traffic light dataset on some test images
to display the output of the test images I will use on Google CoLab notebook.
I will use Globe Library.
Next, I will have the importing the image and display libraries.
So to display any input, image or output image into the Google CoLab notebook, I require the image
and the display library.
Okay, so in the next step we will check whether we have the access to GPU or not.
So by just running this cell you can check that there is GPU memory usage, which means that you are
using the GPU and you have the access to GPU.
So.
Next we will using using OS library.
I'm checking my path.
Home path.
So it's this path over here.
So in the next step I will install ultralytics.
Okay.
So ultralytics can be installed in two ways from the source or PIP.
So this is the first iteration of the YOLO to have its own official package.
So it is always recommended to use PIP install ultralytics.
But for the development you will need to clone the github repo.
So I'm just cloning the latest GitHub repo over here.
So.
But if you want to simply train, test or validate your model.
So it's always better to use the command line interface and just to PIP install the tragedies.
But in this project I'm going to change the bounding box color so I will be doing some development in
the plotting.py file.
So here I will be using the I will be cloning the github repo in this project.
So I am cloning the github repo ultralytics over in this project over here.
So by just running this cell we will be able to clone the GitHub repo over here and you can see that
the GitHub repo is cloned and it's appearing over here.
So I have just forked the latest GitHub repo into my GitHub account.
So it's just appearing there for here you can see my name, so I've just popped the GitHub repo.
Okay.
So here now I have just uh, here you can see that ultralytics GitHub repo.
So basically my current directory is set at this path now to set the current directory as this folder.
So you just need to go here and just rename folder and copy this and just write over here.
So your current directory will be set to this folder now.
Now, basically we need to install all the requirements or the dependencies so it's necessary that we
install all the required libraries to run our project.
Okay.
So.
With.
So if we don't skip this step or don't install all the dependencies, then you might face the issue
that when you run the training script.
So you might get the error that the following library is not installed or the following library is not
installed.
So like Matplotlib library is not installed.
Library is not installed.
Seaborn library is not installed, so it's always better to install all the dependencies.
So when we run the training prediction or validation script, we don't face this error that the following
library is not installed like matplotlib or anyone.
So if you run install all the dependencies, it will install all the required libraries and you don't
face this issue any later libraries issue later.
So now we just need to check whether Ultralytics is installed and it's working fine.
So just click on import ultralytics and ultralytics checks which will tell us.
Okay, so I'll tell you this, you'll be getting setup is a complete and it's working fine.
Okay.
So next.
Basically we need to go to the ultralytics.
Ultralytics Yolo.
Then when we go to paid and then we go to the detect folder.
Okay.
So basically in this case we are doing the detection, not the segmentation or classification.
So I will set this as my current directory, which you can see over here.
So you just need to go here, copy path and just paste this path over here.
So I'm just setting this folder as my current directory.
In the next step, I will download this data set from Roboflow, which you can see over here.
So just go over here, go to the images or go to the datasets and click on download and select a format
so you can select the V8 or YOLO V5 by just format because YOLO, V8 and YOLO V5 both are released by
the ULTRALYTICS so you can choose any of the format.
So just copy this from here, click on copy and just paste this over here, Ctrl V and just.
Run this now.
So it might take.
Okay.
I made a mistake.
I have not run this cell.
So just run this cell first and then get back and run.
Because I need to download the data set in this folder.
So now the data set will be downloaded over here.
Okay, guys.
So it might take a few seconds for the dataset.
We downloaded it like you can see that it's downloaded in the zip format and after this it will automatically
unzip the dataset.
So first it's downloading the zip format.
You can see that downloading data was set version, zip send out, send out.
We do dash one represents the version one, and this is the name of the dataset which the user, which
user who has uploaded this dataset has named it.
Okay.
So you can see here the dataset is 100% downloaded and will now be able to see the dataset over here.
Let's refresh it and just see.
Okay.
So we can see that we have the data set over here in the V2 version one.
And if we go over here and we have the data dot yml file which contains the training and validation
part.
So just rename this folder, remove that dash one from here because it might create an error when we
run the training script and go to.
Okay, so just click over here now and just go over here.
Just close this now and just reopen the data dot yml file and just go over here.
Click on training.
Copy the images path and just.
Okay, so just control V over here and just then go to the validation folder and just click over here.
Copy path and control C and control V and just save it now.
Okay, that's good.
In the next step, just see what our current directory is.
So.
Okay.
And the data set location we are checking out.
Okay.
Just look over here and write dataset dot location.
Okay.
And just add the columns over here and just copy this path.
And just paste it over here and just run this and now just run this cell over here again.
And now let's set.
So we have set the data set dot location as well.
Our home path is set and everything is fine.
So let's start that training of our YOLO V8 model on traffic, light detection and color recognition
data set.
So first it is downloading YOLO V8 and dot model dot model over here and the training will now start.
We can see that we have around 2100 images for that training and 200 images for the validation.
Okay.
And our results will be saved in that data train folder which is created over here.
We just refresh it.
So here in this folder, our weights file will be saved.
In this folder, our weights file will save.
And here also in the train folder, we can see the confusion matrix validation batch results and the
F1 cars precision recall girls after training completes.
So the training is about to start.
So it might take it will take around 2 to 3 hours for the model trained on this data set.
Okay.
So as the training stop starts, I will pause the video and when the training completes, I will be
back.
So I'm just waiting for the training to start.
Okay, so it might take a few seconds.
Well, guys, it's taking time.
So as the training completes, I will be back and I will explain you the how the complete process flow,
the remaining process flow, and we'll discuss the results as well.
So let's get back when the training completes.
So guys, our YOLO V8 model has completed training on traffic light dataset.
So here are the results.
We have trained our model on 80 epochs and so here are our results.
So it is starting from first epoch and you can see that we have at the first epoch we have a mean average
precision with 50 is 0.535.
While mean average precision with IOU varies from 50 to 95 is 0.252.
So you can see that as the training progresses, the mean average precision begins to improve.
So it's continuously improving.
And when the training lasts on the eight, nine 80th epoch.
So there we get the mean average precision which is given over here.
We have a mean average precision with 50 is 0.983 while mean average precision with IOU 50 to 95 is
0.641%, which are which is very good.
And at the end of the training we got our best players on which our model gives us the best results
and we have the best of on the last epoch, which is eight epoch.
So these are the weights on the last epoch, the last.is the weights on the last epoch, which is on
the 80th epoch, while best dot contains the weight on, on that epoch, on which model gives us the
best results.
So the model took around 2.2 hours and 30 minutes to train, so it took around one 50 minutes for the
training and here are our results final.
And you can see that on each class, like on the green one, the green class, we have a mean average
precision with IOU 50 as 0.976.
On the red class, we have our mean average precision with IOU 50 as 0.988.
And on the yellow class we have a mean average precision with IOU 50 as 0.986.
And similarly, we have a mean average precision where IOU varies from 50 to 95 is given here as well.
So the results are very satisfying and our results are saved in over here we can go to.
Ultralytics Yolo.
Then we will go to V8 and then we go to detect.
And then we go to runs.
And then if we go to the detect and then if we go to the train tool.
So here we can see that our results are saved.
Here we have the weights folder which contains our best and last weights while on the last epoch and
best weights file is on that epoch on which the model gives us the best results.
So here we have the best weights file and the last epoch weights file.
And here we also have the F1 curves curves.
So to display or to see all the files, what different files we have over here.
So I will just write this home path, set the home path and just add the path of this folder.
Like just copy the path and paste it over here and just run this cell so it will show you all the files
which we have over here.
We have the training batch results, we have the predictions on the validation batch which are given.
Over here.
We have the F1 gov.
Gov.
We have the confusion matrix as well.
So the most important thing is the confusion matrix.
So confusion matrix is the chart that shows how our models handles different classes.
So in this case we have three different classes green, red and yellow glass.
So let me see what results do we get with the confusion matrix?
So like you can see that in green we have 96% of the time over here if you see over here.
So 96% are dying.
The model detected correctly that currently the green light is on or the traffic signal is on the green
light while 2% were dying.
We get the bounding box, but the green light over here is incorrectly classified as the red light,
while 1%.
Over time we also get the bounding box.
But the green light is incorrectly classified as the yellow light, while 1% of the remaining 1% of
time when the green light is on.
But our model is unable to detect anything.
Okay, so this is a very detailed example of how we can look, what we can judge from the confusion
matrix.
So our results are very good.
Like 96 of the percent of the time our model has detected correctly that the green light is on, while
2% of the time our model incorrectly classified the green light as the red light, while 1% of the time
the green light is incorrectly classified as the yellow light while remaining 1% of the time, our model
is unable to detect anything.
Okay.
So here are the training and validation loss results so you can see that the loss is being converging.
So if we train our model to 250 or 300 epochs, we can say that the result will further improve.
While you can see that mean average precision is a bit increasing, you can see over here, okay, so
these are the model predictions on the validation batch.
So as the validation batch is not used in the training, so it's always better to have a look at the
predictions on the validation batch.
So you can see that the model is detecting correctly that the red light is on here, the yellow and
here is the green.
But you can see that when the green light is on the bounding box, color is also red.
Okay?
So you can see that here we also have the when we have the green light, you can see that over here,
the bounding box color and the label color is also red.
But think when the green light is on the bounding box color and the label color should be green.
And when like here, when the yellow light is on, it shouldn't be the bounding box.
Color shouldn't be orange, it should be yellow.
Okay.
And when the red light is on, it should the color should not be light pink, which you can see over
here, it should be red.
So we need to make these changes.
So to make this changes, I will go to the.
I would just go click over here and just will go to the predict.py file.
Let's me go to first.
I will go to.
I need to make a change on the utils and from here I will go to the plotting.py file and here I will
set the colors over here.
I have already set the colors, but let me explain you.
So set the colors first.
You need to go to the a data set.
So here we have the data set and set open the data dot yml file.
So you just see over here the first class is green, the second class is red and the third class is
yellow.
Okay, so just copy green.
So green coloring hacks just type on Google, green color on hacks.
So just copy this from here, Ctrl C and just open the plotting dot pie.
Okay.
And just paste this color over here because it is taking the input as the color, as the it is, taking
the color in the form of hex.
Then go to the second color, which is red.
So just copy this and write red color in hex.
So just copy this from here and just paste this over here.
And I'll just go over here and see yellow.
So now you write yellow color in Hex, so you will go class by class.
You will add the color class by class.
So just copy this from here and in the plotting.py just paste this over here.
So you need to add colors class by class.
Like first the green.
There is a green label over here in the data dot.
So first we will add the x color of the green.
Then we have the yellow red color over here.
So then we will add the hex form of the red.
Or we have the red color over here.
Then in last we have the yellow color.
So we'll add the hex form of the yellow color in the last.
Okay.
So just save this and close this from here.
Okay.
So you don't need to make any further change.
So just here we are doing the predictions.
Or you can say that we are validating our model on the validation images.
Okay.
So.
Here we are just varied, so I'm just running the validation.py script and we are taking the best weights
and we are taking the validation sets of images and see how our model performs on those images.
So here we will get the performance with respect to each of the class.
So you can see that within the green class we have a mean average precision with 50 as 0.976.
With the red class, we have a mean average precision with 50 as 0.988.
And with the yellow class we have a mean average precision with 50 as 0.986.
And we have also mean average precision when varies from 50 to 95 is also given over here.
So you can see that the results are very impressive of our model.
So now we will test our model on test data, set images, which is here.
So we will evaluate our model on the test data, set images.
So just copy path over here.
And just paste this path over here and here.
We have passed the best weights of our model.
And here I'm just running the prediction.py script.
So just run this cell now so it might take few moments to completely run.
So it might take some time.
So let's see.
But it will not take very much time as we are using the GPU.
So you can see that processing the image is very fast and we are done.
Okay, so now our results are saved in.
Let me go to.
Okay.
Our results are saved in YOLO V8.
And detect.
Runs.
You tanked.
And train 11.
So here our output images are saved.
So just copy this path.
Okay.
And just.
Paste this part over here.
Okay.
And we just want to see the results of the top five images because we have 100 images and it will not
be possible to display all the 100 output images over here.
So just see the output on the first five images.
You can see the output on the last five images from 5 to 10 images.
So you can just set the sequence on which sort of images you want to see the output and just run this
cell now.
Okay.
So here you can see that.
And now if we.
Okay, So you can see here we have the green light.
So the moderator correctly that there is a green light.
Plus you can see that we have the bounding box of the color green as well.
Okay.
So here you can see that we have a red light.
So bounding box color is also red and the model detected as red.
And here we also have a green light.
So you can see that bounding box color is green.
And the model directory bracket that this is a green light.
In this case the there is a red light.
So model detected correctly that there is a red light.
In this case there was no yellow light, but in this case the model make the wrong prediction.
But by on all other four images, the model made the right prediction and the as per the If there is
a red color the of the light the bounding box color you can see is also red.
Okay, so now we will test our model on some demo images.
So I have placed this demo videos on my Google Drive, so I'm just downloading those images from that
drive and just running the script on those images and see what results do we get on these images, on
these videos.
Okay, so I'm just downloading some demo videos from the drive and testing my model on those demo videos
and seeing what results do we get on those demo videos?
So it might take some time.
Okay.
And this model has run on the demo video and it has done the predictions frame by frame.
Okay.
The predictions are being done frame by frame.
Please remember this, that all the predictions are done frame by frame.
So this is important.
We can see that the model has divided the video in out in 183 frames and it has processed each of the
frames separately.
So now we are just displaying our output demo video in the Google CoLab notebook.
So it might take few seconds to display the demo output video over here.
So let's see, we will have the demo output video over here very soon.
It will not take very much time.
Then we will see.
What results do we get over here?
We will also test our model on two other demo videos as well.
So we will validate our model.
Completely.
Well, here is the output video, so let me download it first.
So just click on download and.
And let me open this video for you.
Okay.
So now you can see that video.
So let me play it from start.
So you can see over here in the start there was model was not able to detect correctly very perfectly.
But later on it detects that the red light is on.
Okay, so let's test our model on some other demo videos and see what results do we get on them.
Okay, so this is the.
Demo video to first download it from the drive.
So I've just placed the videos on the drive so I can easily download into my Google CoLab notebook.
Okay, so just run this cell.
I've already placed the video to MP4 path over here, so just doing the prediction on the demo video
too.
So it might take a few seconds.
So let's wait and see.
What results do we get?
So it has divided the whole video into 20 frames and it is doing the predictions frame by frame.
So the prediction is done.
Let's display the output demo video over here.
Just change it to 13 and just run this cell now.
So it might take a few seconds to display the demo output video here.
So I'm just pausing the video and as we get the demo output video, I will resume this video and show
you what the results do we get.
Guys.
The water has run on the demo video too, and here are the results.
So let me download this video and show you what results do we got over here?
So the video is downloading and let me open it and show you the results.
Okay.
So.
So you can see here the model is able to detect correctly the red light over here.
So the model is working fine, very good.
And the model is giving very good results in this case on the demo video to it, as it detects correctly
that there is a red light.
Now test our model on the demo video three.
So just click on yes over here.
And I have placed the demo video three on the drive as well.
So I will just download the video from the drive and just run this.
Okay, so the video has been downloaded from the drive and let me test on the demo video three.
Okay, so it might take few seconds.
So let me pause the video.
And as the video processing completes, like you can see here, the model has the okay, So the model
has divided this complete video into 412 frames and it is processing each frame, each frame by frame.
It is processing the complete video frame by frame.
So as this processing completed.
Let me get back and show you the output demo video.
Well, the processing or the predictions on the demo video three has been completed and here you can
see that the model has is able to detect the lights one red, two reds, one red.
And now I'm trying to just play the demo video over here.
So as the demo video is ready to display over here, I will show you the results.
So just pausing the video until the demo output video is ready to display over here.
I guess due to the large pie sign, the model was I was unable to display the output demo video over
here into the Google CoLab notebook.
So I've just downloaded the output demo video and let me show you the, uh, the results.
Let me share my screen.
Just give me a minute.
Okay, So now you can see the results.
So you can see that the model is able to detect correctly that the green light is there.
Like you can see that we have a green bounding bounding box with a green color around the line.
And now when there is a yellow color, the model was able to detect correctly.
But you can see over here, like when the traffic light blinks to yellow, the model was able to detect
correctly that the yellow signal is on and when the red signal is on, the model was also able to detect
correctly that there is the red signal is on.
So I hope you have learned a lot from this video.
See you all in the next video tutorial with some new amazing projects.
Till then, bye bye.