Hello everyone.
In this video tutorial we will see how we can train your V8 model for helmet detection and segmentation.
So this is going to be a complete end to end tutorial, so don't skip any part of the video and do watch
the complete video.
So for this project I will be using this helmet images dataset available publicly on roboflow.
So the dataset consists of around 272 training images and that 24 testing images, the validation set
images are not there.
So in this project we will consider the test images as the validation images.
So okay, so we will adjust it when we download this dataset into the Google CoLab notebook.
Okay.
So.
We have around to 72 images for the training and 24 images for the testing set, which we will consider
for the validation purpose.
Okay.
So if we see the dataset over here, so the dataset consist of only one class which is of helmet and
no other classes are there and the images consists of the helmet the person is wearing while riding
the bicycle and the handmade the person wears when while riding a motorbike.
Okay, so you can see over here the person is wearing a helmet while riding a motorcycle or by riding
a bicycle while on the other hand, the person is wearing a helmet while riding the bike.
So we have the images of the persons wearing helmet while riding the motorcycle or by riding the bicycle.
Okay, So you can see that the person is driving the car and he is wearing a helmet here as well.
So the dataset is very fine.
So now in order to export this dataset into our Google CoLab notebook, I will go to the dataset and
click on download and just click on show download folder right here and click on Continue.
So, for example, if you don't have an account or roboflow, you need to create an account Roboflow
by clicking, going to sign up.
So I've already have the account on Roboflow So I have just signed in and I can now export the dataset
from Roboflow into my Google CoLab notebook.
So just copy this command from here, like the Jupyter Kumar and you just need to paste this command
into the your Google CoLab notebook so that this dataset can be exported into your Google CoLab notebook.
Okay.
So here in the first step, we will import all the required libraries.
So here in the first we are importing the library.
A library in this project helps us to navigate between different files of the project.
Then we have the Globe Library to display any output predictions on test images.
So we need the Globe Library so that we can display the predictions of the or output images into the
Google CoLab notebook.
Then, if you want to display any input, image or any output image, we need the image library so that
we can display a single image into the Google CoLab notebook.
Okay, so in the first step, we need to check whether we have the access to GPU or not.
To check the access, I will just run this cell so you can see that GPU memory usage.
So please make sure before running the script that you have selected the runtime as GPU.
Okay, so you can just run this cell again.
Okay.
So YOLO, it can be implemented in two ways.
Like one is the PIP install method.
So this is the basically the first iteration of YOLO.
V eight is the first iteration of YOLO to have an official package.
Okay.
So YOLO V8 can be implemented by installing.
You can directly install the package which is PIP install and critics which will install the V8 or if
you want to for the development.
If you want to make some changes in the training script or prediction or validation, for example,
you want to add the tracking or if tracking or if you want to do the vehicle counting or the speed estimation,
then you can.
You need to clone the GitHub repo of the Ultralytics.
Okay.
So although I'm not doing any such making any change in the training but addition of prediction, but
I would be in this project, I will be cloning the GitHub repo by the easy ways.
You can simply install the package which is PIP, install ultralytics and run the command line interface.
But in this project I'm just learning the alternative hip hop rap over here.
So here I've just fought the rapper into my GitHub account, so it's here.
You can see my name on 197 for the for this alternative repo.
So just just run this wrapper so that the repo will be cloned over here.
Now you can see that.
Okay.
Just cancel this.
I will just.
So now you can see this.
See this?
Now we have the ultralytics here, the V8 over here.
So now we need to just set the current directory as this folder.
So just run this cell.
So now this cloned folder is set to our current directory.
Now we need to install all the required libraries or the dependencies to run this project.
So if you skip this step, so if you run the training validation or prediction step, you will face
the issue that the libraries, some of the libraries will not be installed.
So you will face the issue that hydro libraries Seaborn library, Matplotlib libraries not installed.
So it's better to install all the dependencies before running the training validation prediction step
so that you don't face this issue.
While at that time you will have to install the each of the libraries separately like PIP install Seaborn.
So by running the dependencies, by installing the dependencies, this will install all the required
libraries to run this script.
So we need to check whether ultra V8 is imported and it's working fine so we can see that we have done
the icon import ultralytics and predict checks.
So you can see that the ultra text or the V8 has been installed and it's working fine.
Okay.
So now we need to import the helmet segmentation dataset from the roboflow.
So we are just setting our directory as segmentation so you can see that Ultralytics Yolo.
Then we go to V8, then we go to segment.
So as we are doing segmentation in this project, so we just copy the path and set our current directory
as this segmentation folder.
Okay.
So now we will install the dataset from the roboflow.
So just go over here and just copy this command, which I have done so and just paste this command over
here just like this.
Control a and just paste this command over here.
Now just run this cell.
So it will download the dataset from roboflow into this segment folder.
Okay, so it might take few seconds, so let's wait and see, but the dataset will be downloaded into
your segmentation folder so we can see that the dataset is started to download.
It's, it will download in the zip format then, but it will automatically unzip it as well.
Like you can see that it's downloading in the zip format.
So it's 89, 88, 100% done.
So now we can see the dataset over here, helmet dataset.
Okay, so now we will train the V8 model on this dataset.
So just make sure you need to just make things one thing ahead, like please dash to remove and open
the data dot yml and just.
Okay, you just go over here and click on.
Okay.
So just.
Go over here and open the data dot yml file and just set the training and validation images path so
we don't have the testing images.
So we will.
Okay.
So we have the testing images, but we don't have the validation set images which we have seen there
as well.
Like you can see over here, our data set does not contain the validation set images.
It contains the training and testing set images.
But in this project we will consider the testing, set images as the validation set images.
So simply remove the test images and just rename this test folder as.
That edition for the.
Okay.
So just click on this now and just copy this path and just.
Is spot were here.
And in the validation case, now we have renamed this folder as the validation folder.
So just go over here.
Copy path and just paste this path over here and control S So now the training and validation paths
have been set because we have removed the testing, change the testing to validation because we need
the validation for image, we need some images in the validation folder so that we can validate our
model.
And plus, while training, we also need some validation images.
Okay.
So to validate our model on some or test images.
So we have just converted a.
Training images are testing images to of validation.
Okay so it's appearing from dot detect dot train.
So let me fix this issue.
So I will just go to I think there is an update in the ultralytics, so I will just go to ultralytics,
YOLO V8.
So and then Ultralytics YOLO V8 is under high development, so if there are any change occur.
So we are sometimes face this issue.
So I will just go to YOLO V8 segment and I will go to train.py.
So as the alternative is under the development, so sometimes the packages get updated and we face this
issue.
So just copy this from here, Ctrl c and just go to the train.py script and just add this over here.
So just go to over here and just Ctrl v, ctrl s and just now run this trip now.
Okay.
So where we were.
So where you are here.
So just now run this trip.
So we are going to train our model on 80 epochs.
Let's see if there is any error so we can fix it.
If it starts working fine, then we will wait for that training to complete.
Okay.
So that is first of all, it is rolling downloading the the Pre-trained model, which is trained with
the Coco dataset.
Okay.
File not found has helmet to data dot yml.
Okay.
What is our dataset provocation.
Okay.
Just means set this.
Just give me a minute now.
It will be working fine.
Okay.
Now just run this because I missed these two cells, so that's why this error was appearing.
But I hope it will work fine.
So now the training is going to start.
So the training might take a 30 to 40 minutes because the dataset is not very large.
So the training to complete.
So I will pause the video as the training gets start and as the training complete, I will be back.
So the training is about to start and let that start from the first epoch so that I can pause the video
and that the training updates.
I will be back.
So it's around.
Okay, training has started, but it will take some time.
So the ASR training completes.
I will be back and then we will see the results and test our model on some demo videos.
Well, guys, the training of the model is complete on helmet dataset for the segmentation.
We have trained our model on 80 epochs and we have set the image size.
614 So here are the results.
You can see that the model is trained on eight epochs.
So at the start we have a mean average precision with 50 at 0.5661 and the mean average precision with
1595 is 0.431.
But as the training progresses, we can see that the mean average precision begins to improve for both
the 50 and 50 to 95.
So in last we have mean average precision with IOU 50 as 0.58 and with mean average precision with iron,
50 to 95 is 0.462 and we have the best weights of the model saved as well.
And the last weight loss is the model weights on the last epoch, which is 80th epoch.
Okay.
So here is the model summary as well.
And you can see that we have only one class, which is the heavyweight class, and we have a mean average
precision with 50 as 0.674 and mean average precision with 50 to 95 is 0.548.
So these results are good.
Like these are not the bad results.
These are very fine results.
Okay, so our results are saved in run segment, train port.
So let me show you where our results are saved.
So keyboard to YOLO.
Then we click on V8 and then we go to segment and in the run.
Rain bullets got here in this border.
We have the weights file which contains the best and last weights.
And here are the confusion matrix for the predictions on the validation batch.
So let us see what different files we have over here.
So.
For this.
We will check what different files we have over here so you can see that we have the confusion matrix
master F1 curve.
We have the reserve store CSV file which contains the model performance or the performance on each of
each epoch and dessert store.
Contain the training and validation losses and validation.
Batch prediction contain the model predictions on the validation batch.
So the validation images are not used for the training.
So it always better to see what model or model predicts on the validation batch and the grids file contain
the best and last weights.
Okay, so next, let's see the confusion matrix.
So confusion matrix basically shows how our model handles different plastic.
But in this case, we have only one class, which is a pyramid.
So from the confusion matrix, we can see that 92% of time our model detected correctly that the person
is wearing a helmet, while 8% time when the person is wearing the helmet.
The model was unable to detect that the person is wearing the helmet.
Now here we have the training and validation losses.
So you can see that the training and validation loss begins to decrease as the training progresses.
While so this means that currently we have trained our model for 18 epochs.
So if we train our model for 350 to 400 epochs, we can definitely say that the loss will further decrease
and the mean average precision will further improve.
Like you can see that the mean average precision is increasing.
Okay, so here are the model predictions on the validation batch.
So the results are very good.
Like model was able to predict correctly the bicycle helmet.
You can see over here that the person wearing the bicycle is wearing the helmet.
So and the person riding the motorbike is also leading the handbrake.
So the model was able to detect correctly the helmet on the validation.
So these are the model predictions on the validation batch.
So validation batch is not used for the all the validation images are not used for the training.
So it's always better to have a look and see how the model performs.
Okay, so now here we are validating the system model on validation dataset images.
So there are 24 images and on which we are validating the model and the model results which we obtain
a mean average precision with 50 0.69 and mean average precision with 50 to 95 with 0.562.
Okay, so let's test our model on a demo video and see how our model performs.
So I have just downloaded a demo video from Pixels and paste into my drive and I'm just downloading
the demo video directly from my drive.
And now I will test the model on this demo video.
Okay, so just done this and now checking the current directory.
And now let's test our model on this demo video.
It might take two seconds.
Open a model to run on this demo video.
Like you can see that their model has divided a complete video into 1462 frames, and it's processing
each of the frame one by one.
So if and we can see the predictions that the model is rejecting the helmet as well.
So as this process completes and let me display the demo video in the Google CoLab, then I will be
back for ten.
Then I'm just pausing the video.
So that model.
So the model has possessed the demo video which we have passed as an input.
And here I have displayed the demo video over here and let's see what results do we get.
So it's a just 15 second so we can see that the model is able to detect the helmet.
But let me download this demo video and let's play it and show you the results live as well.
So just give me a second.
The video downloaded, okay, So the video is downloaded.
Let me just play it.
Electing to shift my strain towards it.
So.
You can see that the model is able to detect the helmet successfully.
The border of DICOM has a disabled model of detecting the helmet.
Okay, so the reserves are fine.
Next test our model on some other demo videos and see what results do we get over there.
We're just going back to the One Laptop by now.
Let's test the model on some more demo videos.
So here is the demo video too.
So let me download it from the drive and test the model on this demo video.
Well, now I will test the model on the demo video too.
So I will download the demo video directly from the drive.
I have placed the demo video on the drive, so I am just downloading it and now I will run the prediction
script on this demo video and see what results do we get here.
I have passed the model best weights and here is the source in the source.
I have passed the video on which I want to test the model so the predictions will be done frame by frame.
So it might take some time for the prediction to process each of the frame.
So it has started.
So the model.
So we have won 70 frames from this video.
So what is the process?
Each of the frame and we can see the prediction that one has made two helmet.
So it may might take some time.
So I will pause the video.
And as the video is ready to display on the Google CoLab notebook, I will just resume the video.
Okay.
Against the spirit has run successfully.
And here is our output video in the Google CoLab notebook.
So let me play this output demo video.
Okay, so you can see that the model is able to detect the helmet and we have done segmentation as well.
The mask you can see as well.
Let me download this video and let's play this video.
So it might take few seconds.
Okay.
Let me now play this video.
Okay.
So.
Just opening the video over here.
Just give me a minute so I can shift my screen.
Okay, so just let me share you this output demo.
So here you can see that the model is able to detect the helmet as well as we have the mask segmentation
as well.
Okay, so let's test our model on a demo video three and let's validate our results on the demo video.
Three.
So now we're testing our model on the demo video three.
So I will directly download this video from the Google Drive into the notebook.
So actually a place the videos on the drive so when I ever need it.
So I directly download into the Google CoLab notebook.
So the name of the video of demo video three is Bike to dot mp4.
So let me just run the script on this demo video.
So I'm running the prediction.py script where by passing the model best weights over here and on the
source is the bike to dot mp4.
So the video might take some time to process.
So as the output is ready to display on the Google colab notebook, I will be back.
So just stopping over here and I will back as the video is ready.
Well, guys, here are our results.
You can see that.
Let me download this video and show you the results and then we can see what results do we get.
So I'm just downloading the video and let me open it as well as it downloads complete.
So just opening it now and just let me shift the screen towards it.
So just give me a few minutes.
And so now you can see the screen.
So here are our results.
You can see that our model was able to detect successfully the helmet and you can see the mask segmentation
of the helmet as well.
Okay.
So the model is working very good and the results are very fine as well.
So that's all from this video tutorial.
See you all in the next video tutorial with some new projects.
Till then, bye bye.