You know everyone in this video tutorial, we will see how we can use YOLO V8 for crack detection and
segmentation.
So for this project I will be using the crack dataset which is available publicly on roboflow.
So you can see here the dataset consists of around 1551 images and we have only one class in this dataset
which is of crack and we don't need to go into the model health check because we don't have multi classes
in this dataset.
We have only one class and it consist of good number of images as well.
So we usually do the model health check when we have multi classes.
So we check that either our dataset is balanced or not.
Okay, so this is the crack dataset and here is the overview how our results will look like.
So you can see that this is the sample output image when so you can see that when the crack is detected,
the model is able to create a bounding box around the detected crack.
And as well as we are doing the segmentation on the crack as well, like you can see this red line.
So we are able to do the segmentation of the crack as well.
And here we have the label crack and here we have the confidence score.
Like how how much the model is confident that this is a crack.
So this is the confidence score 0.8, which represents 80%.
The model is confident and here we have the label crack, okay?
And here we have the bounding box around the crack.
And here you can see that we are doing the segmentation on the crack as well.
So let's move towards the code part.
So in the first step, I'm importing all the required libraries that are necessary to run this project.
So in the first step you can see that I am importing the library.
So library, basically we are using library to create a helper variable which allows us to navigate
between different file paths.
Like if I want to access the files in my training folder or if I want to go through the data dot yml
file.
So there I will start import library.
Then we have the import glob library import Glob library is used when we want to display the input and
output images and glob library.
Also return all file paths that basically match a specific pattern.
Okay.
Then we have the from ipython dot display import image display library.
So when we just want to display our input or output image into the Google CoLab notebook, then we require
the image and display library.
So let's run all these cells as well.
So first we need to make sure that we have access to a GPU or not.
So before running the script, please make sure that you have access to GPU so you can go to runtime
and click on change runtime and select the runtime is GPU, so that basically in GPU will help us to
do the training and processing becomes very faster while in CPU, that training takes a lot of time,
so we usually prefer, as you Google CoLab provides free GPU, so it's always better to use the GPU
to train your model and to do the predictions as well.
Okay.
So in to check whether we have access to GPU or not, just run this cell so you can see that we are
currently using the GPU memory usage.
And then here I'm defining my my own variable.
You can see that or you can say that I'm defining the current directory as content.
So YOLO V8 can be implemented in two ways.
Like we can install the package ultralytics over it.
Through this way we can implement YOLO V8 or we can clone the GitHub repo of YOLO V8.
So here you have also written YOLO.
V8 can be installed in two ways from the source or VIP source means like you can clone the GitHub repo
or vip means like if you inside pip install ultralytics so it will install the YOLO V8 package source
on your basic notebook as well.
So YOLO V8 is the first iteration of YOLO which has its own official package like YOLO, V7, YOLO v6
don't have their own official package, so your here is basically the first iteration of YOLO, which
had its own option package.
So basically if you want to train your model and test your model and you don't need to, for example,
I will use.
I will just go on the GitHub repo.
In that case, we need to change the or make some changes in that train.
Dot py or prediction.py file.
For example, I want to add the speed estimation or if I want to add the distance calculation or if
we want to add the counting object counting.
So here I will use a git clone method or the implementation of YOLO v8.
So basically git clone method is used for development, but if you want to simply train your model test
and validate your model, then it's always better to use the PIP install Ultralytics method or the PIP
methods because it is easy and very efficient way to do it.
Okay.
And each one here you will use the command line interface to do the implementation and it's always easy
to implement through it.
Okay, so here and here, as I'm just training, testing and validation or validating my model, so
I'm just choosing PIP install ultralytics to install or YOLO V8 into my Google colab notebook.
So just clicking on PIP install ultralytics now.
So it will install yolo V8 into my google colab notebook.
So it might take a few seconds to install.
So let's see.
But it will be installed in a minute or so.
Okay.
So we have installed it into our Google CoLab notebook.
So let's check.
It's going to be installed and it's working fine, so we'll just run this to import Ultralytics Ultralytics
dot check.
So in this way we can check whether YOLO V8 is installed and it's working fine or not.
So you can see that YOLO v8 install and setup complete and it's working fine as well.
So now we will create a folder over here by the name of datasets where we will download our dataset
from the roboflow into this Google CoLab notebook folder.
Okay.
So mkdir basically creates a directory by the name of datasets so you can create a folder by any other
name as well.
So here I'm just creating a folder into my home directory by the name of datasets like you can see there
here.
Now we have a we have created a folder dataset, so it's currently empty.
So.
Here.
I will download my dataset from Google dataset from Roboflow into this folder.
So let's just see what is our present working directory and just set the current directory as this dataset.
So I'm just setting the current directory as this datasets folder.
And now I will install the dataset from roboflow.
So basically I have just you.
If you want to download the dataset from Roboflow into your Google CoLab notebook, just click on,
download this dataset and just show download select the format as GitHub V5 or YOLO format.
So Ultralytics has also released the YOLO V5, PyTorch and YOLO V8 so you can either select YOLO V8
or YOLO V5 PyTorch and click on Continue.
And just copy this from here.
Click on copy and just copy this from here and just we need to paste this over here.
So in this way you can simply download the data set from the Roboflow into your Google CoLab notebook.
So that data set is downloaded in the zip format and in the Google CoLab it is automatically unzipped.
Like you can see that downloading dataset version zip.
So it will be automatically unzipped as well.
So it might take few seconds.
So let's wait until our dataset gets unzipped and download it.
So it might take a few seconds.
So please wait with me until the dataset gets completely downloaded over here.
Okay.
So you can see that the dataset is downloaded and it's unzipped as well.
So if we go over here, so here we can see our dataset here, track two, and here are our training
test and validation folders, and here is our data.one file which contains the the classes number of
classes.
We have only one class which is of track and a training and validation paths as well.
Okay, so one thing I will do is I will just rename this folder to track and I will just go to the data
dot Yml file and update the training and validation images path.
So we will use the test data, set images to test our model when the model is trained.
Okay.
And validation dataset is used.
Images are used to validate our trained model, which will be on the train model will be on the track,
segmentation data set images.
Okay.
So here is just we are updating the file paths and just click on control s and just.
Uh, we are just setting what is the data set?
Location.
Okay, So what we have is just give me a minute and see what error we're getting.
No such file that that location.
Okay.
So basically we have update the name of the tag to.
So that's why we are getting this error.
But that is not the issue.
We will just go to.
Okay, we will just go to here.
Data dot HTML.
And just add this path over here.
So this is not the issue if we are not getting over here.
So just control V over here.
So we have added the dataset path over here.
So now just run the script for the training.
So the training will take very much time.
So as the training starts, I will just pause the video and when the training completes, I will be
back.
Okay, so here we are, just downloading the YOLO Pre-trained model from the YOLO GitHub repo and we
are fine tuning the YOLO V8 model on the track segmentation dataset.
So you can see that the training is about to start.
As the training starts.
I will just pause this video and then when the training complete I will be back.
So the training will take around 3 to 3.5 hours.
So this is very much time.
So I will be pausing the video when the training starts.
So just to show you that the training has started, then I will pause this video.
So the training is about to start in a few seconds.
Okay.
So just see when the training so the training will start is taking time to start.
Okay.
So as the training completes, I will be back and then we will discuss about the results.
Okay.
Well, guys, I am back.
I was able to train my YOLO V8 model for the drag segmentation and detection, so I have trained my
V8 model on 50 epochs and the image size was set to 640.
And you can see that the task is segmentation and the model is trained.
And the model we used, the V8 model we use to fine tune on the track segmentation data is YOLO V8 and
the segmentation.
I did not use the the large V8 model because it will take the more training time and did not use the
YOLO V8 Nano because it will be less accurate.
But it would be.
But it was fast so I just took a medium model YOLO V8 and the segmentation speed to fine tune on the
track segmentation dataset.
So here are the results.
As I told you that we have trained our model on 40 epochs, so it took around three hours for the training
because segmentation takes more time than in training than detection.
Then the mean average precision we obtained with 50 is 0.6.
Nine is around 70% and the mean average precision with 5295 is 0.235.
So the results are fine.
And here is the model summary which we obtained that you can see that the mean average precision with
50 is around 70% and with 50 to 95 is 0.212%.
Okay, 21.2% and it is 70%.
And so our is all the results are saved in the runs segmentation and the train folder.
Now let's see what different files we have to check this out.
Just run this cell so it will print all the different files we have over here.
As you can see that we have the confusion matrix.
We have the results CSV file which contains the model performance metrics or the model performance after
each of the epoch.
The result dot PNG contain the training and validation loss and the here we have also the confusion
matrix which shows how our model handles the classes.
And then we have the model predictions on the validation batch.
So we have not used that validation images for that training.
So it's always better to have a look.
And here are the model predictions on the training batches as well.
Thus here we have a great folder which contains the best words.
Unlike the best words mean the the words on which the model has given us the best results.
And the last word is the words file of the last epoch, which is put here, the epoch.
Okay, so here we have the confusion matrix.
So a confusion matrix is basically a chart that shows how our model handles different classes.
Okay.
So from here we can see that 86% of the time our model detected correctly that there is a tag, while
14% of the times when the crack was there, our model was unable to detect or segmentation.
So 14% of the times when the crack was there, the model failed to detect or either segment it.
Okay.
While 86% of the time our model was able to successfully detect and segment the crack.
Okay, so let's see the training and validation losses.
I have only trained.
I have trained the model only on 4D Box.
Okay.
So you can see that the training loss is validation losses and the decreasing.
So it means if we train our model on the higher epochs, we can say that the like for example, 150
box or 200 epochs, we can see that our loss will further decrease.
While you can see that mean average precision is increasing over here, you can see that.
So it means if we train our model on higher epochs, we will get some better mean average precision.
Like currently we have 70% mean average precision.
So it may goes to 8,580%.
Okay.
So here are the model predictions on the validation batch.
So as validation images are not used for the training, so it's always better to take a look and see
how our model is performing.
Okay.
So we can see that the model is able to successfully detect the track and was able to do the segmentation
on it as well.
You can see the red line, the segmentation is done with the red line.
You can see over here and you can see the bounding box around the track as well, which shows the model
was able to detect the track successfully.
Now in the next step, I will validate the custom model using the best weights we have got.
And I'm just passing the data dot yml file path.
So here I'm doing the validation and you can see that we have a mean average precision with 5,070% and
with IOU 5295 is 23.8%.
Okay, so now let's test our model on some test images and see what results do we get.
Okay, so I'm just testing my model on the test dataset images.
Let me show you.
So if we go to dataset, Greg and here the test data set images on which we are just testing our model's
performance by passing the best words file over here and setting the confidence value to 0.25.
Okay, so they are around 112 images on the test dataset, so I will not be displaying all the predictions
on the test images.
Instead, I will be displaying the predictions on the top five images over here.
So you can see that the model was able to successfully detect the crack and do the segmentation here
as well.
Here, the model was also able to detect the crack and do the segmentation here as well.
Here, the model is also able to detect this crack and do the segmentation as well.
And here the model is was also able to detect the crack and do the segmentation as well.
So our results are very promising.
So the model gives very good results.
Now let's test that model on a demo video.
So I have just downloaded a demo video from the pixel side and just test that demo video on the drive
and I am downloading the demo video from drive into my Google CoLab notebook.
So let's just run this cell so that video will be downloaded into my Google CoLab notebook.
So here we have that crack video.
So I will test my model on this video and see what results do we get.
So yolo.is equal to segment because I'm performing segmentation and I'm doing prediction.
So mod is equal to predict.
And here I'm just defining setting the path for the best words file which we have over here runs a segment.
Bringing.
Bierdz.
Vestbygd is the best way to file.
And here is our confidence score.
And here the path for our demo video.
Okay, let's test our model on the demo video and see what results do we get.
So.
Or it started.
So it might take some time.
Let's see how many frames our video gets divided first.
So let's see.
So our model has divided our complete video into 723 frames.
You can see over here.
So it will do this segmentation or it will do the predictions or prediction and segmentation on each
frame separately.
Like we can see that the crack is being detected.
So this might take some time.
So as this process completes, the prediction is complete.
Let me get back and show you the results we got.
Well, guys, the prediction on that crack video has been completed and our results are saved in run
segment.
Predict three.
So if you go over here, run segment, predict three.
So here we have the output video of the our after doing the predictions on the demo video.
So due to large font size, I was not able to display the output video over here.
So I've just downloaded the compressed file, basically just converted this video into a compressed
file so the size of the video has been reduced.
So let's download this compressed video file and let's display the output.
Let me show you the output results and see what results do we get.
So it might take few seconds.
So let me down it download.
So let then I can show you the results and we can see that.
What results do we actually get?
Okay, so let me play this video up and show you what results do actually we get.
Okay.
So now you can see the results on your screen.
I think it might take a few seconds more.
Okay.
So now you can see the results on your screen.
So you can see that the model was able to detect the crack as well as water was able to do the segmentation
on the crack as well.
So the results are very promising, which we got after implementing your crack segmentation on a custom
dataset of crack.
So you can see that the results are very good.
So this is all from this video tutorial.
In this video tutorial, we have trained our model on the crack dataset.
So in the output we are able to detect the cracks as well as able to perform the segmentation on the
cracks as well.
See you all in the next video with some new projects.
Till then, bye bye.