Hello everyone.
In this video tutorial we will run YOLO V8 in Google CoLab.
Yolo V8 is faster and more accurate than YOLO.
V7 YOLO V8 is trained for object detection and image segmentation and image classification tasks.
We will run object detection and segmentation tasks in this tutorial.
So here are different models of YOLO V8.
You can see over here.
This snapshot is taken from the GitHub repository of YOLO V8.
So we have around five different model.
The first model is YOLO, V8 and YOLO V8 and is smallest in size and faster in speed, but it is less
accurate.
While YOLO v8 X is larger in size.
Not as fast as YOLO V8 and is, but it is more accurate than all the other YOLO V8 models.
Okay, so before we run this script, please make sure you have select the runtime as GPU.
To check this, click on runtime.
Go to change runtime and make sure that the hardware accelerator is set as GPU.
If it is set as None or TPU, please make sure it is set to selected GPU and click on Save.
Now we will first import all the required libraries.
So first of all will import from IPython dot display import image.
We need this library if we because we need to display any output, image or any input image into our
Google CoLab notebook to display any input or output image into the Google CoLab notebook, we require
this library from ipython dot display import image.
So let's run this cell to import this library.
Now to run object detection, image classification and image segmentation tasks we will install in ultralytics.
So here I'm doing PIP install ultralytics.
So it might take a few seconds to install.
This yellow year is the first iteration of YOLO to have an official package where we can run YOLO V8
used by PIP, install ultralytics or cloning the GitHub repo.
Okay.
But here we would prefer to do PIP install ultralytics.
Now we will check whether the GPU is available or not.
To check this.
Their son.
Import torch and torch dot dot dot is available.
So if we have the GPU available, it will print true.
If the GPU is not available, it will print false.
Now torch dot which we are checking the torch version which is one point 13.1, the latest PyTorch version
and 116.
So first of all, we will perform or implement the object detection task.
We will start with object detection for the sample image.
Okay.
To just run object detection on the sample image, I will I will use command CLI command, which is
YOLO.
Torch is equal to detect mode is equal to predict.
If we are predicting because we are using a pre-trained model yolo v s dot over here.
So if we want to fine tune our YOLO vitiate model on some data set, then we will use task as train.
But here we are only doing the predictions, so we will choose mode as predict and currently we are
performing detection.
So we will choose task is equal to detect.
If we are performing segmentation, then the task is equal to segmentation.
If we are performing classification, then task is equal to classification.
And we have and we are using YOLO model test our model on a sample image.
And this is the sample image which I am passing over here.
So I have imported a sample, image and video tutorial from my system onto the Google CoLab notebook
so I can test your model on a sample image, which is this and on a demo video, which is of course.
Okay, so this is the sample which I am using.
So let's run this cell first.
Or that it might take a few seconds, so it will run soon.
Okay.
The model has downloaded from here.
And.
Only clapped out work in just a few seconds.
So we are our model has detected 11 person, one bicycle, one motorcycle, one bus route, traffic
lights, two backpacks, three hand bags in 15.5 millisecond.
It is fast.
Okay, so our output is image is saved in run detect predict.
So this is our runs folder inside detect inside predict we have our image dot jpg to display this image,
click on here copy part and just paste it over here.
Okay, So in the start, you know that we have imported the image library to display the image or input
or output image into the Google CoLab notebook.
So I'm displaying the output image using the image library into the Google CoLab notebook.
So just run this cell.
This is our output image.
Our model has successfully detected persons here.
This is the wrong prediction model has made.
This is the backpack.
This is the bus.
These are the traffic lights.
These are all the persons.
So the.
Yes, the wrong prediction over here.
Over here as well.
These are the errors which he has made.
Back and back like this.
So your predictions are wrong by majority of the predictions are correct.
Okay.
So if we want to save this bounding box coordinates information like which are the coordinates of this
bounding box, what are the coordinates of this bounding box?
So to save the bounding box information, like coordinates of all the bounding box which we have here,
we will set save as text is equal to true by setting.
Save text is equal to true will save the bounding box coordinate information with that output image
as well.
So let's set save dash text is equal to true and just run this set.
Now.
It will save the bounding box information as well.
Okay.
So our our output is saved in predict two labels folder.
Let's see.
Runs detect predict to and here this is the output image.
So just go over here copy path.
And just paste it over here.
Now just run this cell and see our output image.
So this is our output image.
But we have used save text is equal to true over here, which will save the bounding box coordinates.
Or you can see the information.
So here we have the bounding box information here, 21 different objects are detected in this image.
You can see the app, 21 different objects in this image.
And here are all the coordinates information of them.
So 24 310 represent the classes like the first term.
This first term in all the lines represent the class like 24 represent the class three represent the
class one represent the class zero represent the person class.
So the first term represent the classes.
This one represent the X1Y.
This.
This one represent the x one.
This represents the Y one though the second term is basically x one.
The third term is y one, so x one and y one are these points.
And let me draw over here.
Just I think I have the hair option over here.
Okay.
Just give me a minute.
I will draw it.
Set the.
Better explain.
I think I can explain it in a much.
Okay.
Just give me a minute, guys.
Okay.
So like, you can see this blue line over here.
So X1 are these are these coordinates over here?
These are the x1 coordinates over here.
Now just there, all these.
So if you can see this blue line.
So these are the x1 y1 coordinates.
And the last two are.
Just on selected.
Okay.
I'm just trying to isolate it now.
Okay, so this the last two are.
This is x two and this is the y two coordinate x2 and y2 coordinate.
So where are the x2 and y2 points?
These are the.
These are the x2 and Y2 points.
Over here you can see these are the x2 and Y2 points.
So in this way the bounding box coordinate information is saved in that text file with respect to each
of the class.
Okay.
So.
But if we want to prop each of the.
And Egypt up.
For example, if you want to drop each of the detected object.
So for this we can just write save dash crop is equal to true.
So here I have written.
Just remove this thing.
These are not required.
So here I've written save dash prop.
Is equal to true, so it will crop each of the detected object.
Okay, so let's run this cell.
It would drop each of the detected objects separately and save in a separate image file.
Okay, let me show you.
So our results are saved in runs detect period three.
So just here and predict three and crop images over here so you can see that we have.
Air bags.
Bicycle.
Bicycle bags.
Motorcycle.
Personal traffic lights.
So you can see over here.
So each person, each directed person bounding box is saved over here, like you can see over here,
we have the separate image for each of the person, like you can see over here.
He rejected person bounding box and the image is saved separate separately in a jpg file.
To do this we just write save dash prop is equal to true which saves the each of the bounding box separately.
Okay.
So let me display this over here as well.
Okay.
So in the next step, if we want if we want to.
Remove this label like this is the label person is the label, and 0.89 is the confidence.
So if I want to remove the label of person and the 0.89, which is the confidence.
So what I need to do is.
So high Dash label will remove the label and hide dash.
Confidence will hide the confidence value.
Dash label will hide the label, which is person bus or a traffic light.
Dash Confidence means how much model is shown that this prediction is correct.
So if write height dash confidence is equal to true, it will hide the confidence value, which is varies
from 0 to 1.
So if the confidence value is 0.50, it means model is very much sure that this is predictions are correct.
While if the confidence value is 0.10, it means the model is not sure that whether the prediction is
correct or not.
Okay.
So I wonder if I run this cell.
Uh.
Here, you can see that it might take a few seconds.
Run.
So our results are saving prediction for.
So if I go to this over here.
And just copy this.
And just paste it over here and display this output image.
So we can see that our predictions, our class labels and the confidence value is not not either.
We cannot see it.
So we have to hide the class label and the corporate values.
Okay, so now we can run object detection on videos as well.
So here is a demo video which I have uploaded over here.
You can upload any of the video of your choice by clicking.
Right clicking over here and clicking on the upload option will upload any image or video of your choice
into your local directory.
Okay, so let's run this cell.
For in the source.
You just need to pass the video path of your video so we know how to get the video path.
You will just click on this.
He points over here and just copy path and just.
Is this part over here and just run all this so you can see that each we get the predictions in each
of the frames.
These are the predictions in each of the frames, like in some frame, which is detecting three cars
in some frame, obstructing five cars in some frame.
It's selecting seven cars.
So it's not a very big video.
So it will just be complete very soon in a few seconds now.
So our script has run and our output is saved and runs detect predict file.
So here, over here our output is saved.
So just copy this over here.
Copy path and just paste this over here.
And just run this.
I will have the output video over here.
Or just checking it.
And now we get just get the output of over here.
So it might take a few seconds more to run, but it will not take more than a few seconds.
So just wait.
And as we get the output demo, we will download it and show it to you.
Then we'll move towards the image segmentation task.
So just a few minutes more it will be downloading and will be can be seen over here.
So here we have our output demo video.
Let's download it over here and see what results did we get from this.
So let's let we have downloaded.
Let me just play this.
I do love going to the spaceship.
Okay.
So you can see that detections are we have we are able to implement detections.
You can see that the car is detected, bus is detected and proc are this is the wrong direction basically
as we are using a small model.
So detections are not very accurate.
But if we use the YOLO last YOLO model, the detections will be very good, but an accuracy will also
be very good.
But we will have a compromise in terms of speed.
So.
So these are the results of our detection.
Now let's move towards the segmentation part.
So here we have the image segmentation.
Now we will implement the image segmentation.
So just we will make one change into the command line CLI, which is DOS is instead of setting dos is
equal to detect.
Now we'll select DOS is equal to segment for segmentation and all the everything will be saved mod is
equal to predict model is equal to s.
Now we will just write yolo v s dot, but here we will just as dash seg for the segmentation.
So instead, in case of classification we just set dash cls.
So in simple case for detection we just write.
We ate escaped, but in case of segmentation we write dash sac for segmentation for classification,
we do dash CLS and in the source we pass our input image path which is over here which we have passed.
Image one dot JPG.
Now just run this cell and see what output image we get.
So it might take few seconds.
So let's see what do we get in the output?
So the fusing layers and here our output image is saved.
So I have already set this path over here.
And if we run this cell so we get our output image, which is like this, now we can see the segmentation
is implemented.
So this is basically semantic segmentation over here.
Like you can see the person's all the persons have a same mask, same color mask, which is of maroon,
while the bus, the traffic lights have some different color masks.
But for the same object, the mask color is same.
Okay, so we can also hide labels and confidence.
Whether you are here by setting height dash labels equal true and hide dash confidence is equal to true.
So just run this cell and display this output image over here.
So it might take a few seconds.
Then let's see what the results do we get.
But you can see now we don't have any label or neither the confidence value.
Now we can run segmentation on video as well.
So now let's run the segmentation on video.
So the script is running.
It might take a few seconds to display, but let's see.
So these are the detections in each of the frame where the model is doing the detections in each of
the frame, like six cars, five trucks, and the time taken to detect this object is 11.4 millisecond,
13.3 milliseconds.
So the time taken to reach in each frame.
So the timing can also be seen over here.
So the process is going on.
Let's see, as it finishes, we will see a display.
Our output video is also over here as well.
So it might take a few more seconds to complete.
But let's wait and see what the results.
So our results are saved in result runs.
Predict segment.
Predict three.
So I've already saved this set, this part, and my video name was demo MP4, so my output video name
is also demo dot mp4.
Okay, so let's see the results over here.
What kind of results do we get?
But here is our output demo video.
Let's just play it so you can see that the segmentation is implemented.
Like we can see the mask over each of the object, The for example, the objects which are same like
cars, they have the same color mask which is of orange, while the truck has a light green color mask
and all the trucks have this light green color mask.
So we are able to implement segmentation on video as well.
Now export the model.
Now let's see how we can export any model PRE-TRAINED or a fine tuned model in any of the format.
For example, I want to export model into the onnx format.
So to this I will just write format is equal to Onyx and all the other things will remain the same.
I will just make format is equal to or double and onyx.
And here I will write my model name.
It can be a pre-trained model or a fine tuned model so you can write your own model name and just set
the format is equal to double X on format.
So our model will be converted into onnx format.
So just run this cell over here.
And see what results do we get?
Might.
It might take a few seconds.
So just.
So currently we are converting a detection model into the Onnx format.
In the next part we will see how we can convert segmentation model into the onnx format.
So let's say we have.
So here you can see that we have passed YOLO, V8, SP2 over here and it is converted into DOT or an
x or X format over here.
Okay, let's convert the segmentation model into dot onex format.
So here I have written the name of the segmentation model and format is equal to on double or x format
and see whether it is converted into onnx format or not.
It might take a few seconds and let's see so we can see that our segmentation model, which is YOLO,
V8 dash segmentation, is converted into the ONNX format.
So now we have done all this using command line interface.
But if I want to do all this in Python, we can also do this.
But let's start with image.
For image we first type parameter.
Let's import YOLO and.
So here I will initialize YOLO by this variable name which I've written model.
So you can write any of the variable name here.
You can write your own name or here I can write my own name, which is Moinard here as well.
So you can initialize YOLO with any of the variable name here.
I've initialize YOLO with the model name.
Then I have used the predict method that basically take all the parameters of this command line interface
like model, board and task.
So it takes all the parameters of the command line interface and in the model dot predict function,
I just pass the source in which is my input image path and set save is equal to true, which will save
the my output image and set the confidence value 0.5.
And if I want to save the bounding box coordinates I will write save dash text is equal to true, which
will save the bounding box coordinates and just run this cell.
So here we will get the output image.
It might take few seconds, but we will get the output image.
So let's see, what else do we get over here?
So our output image is saved in runs.
Detect predict six.
So if I pass this path over here, so just run this and see using this method, do we are we able to
get the results?
So you can see that my results are here and the detections are quite fine.
So now we can use the same method for video as well.
So here I'm just passing the path of my input video and setting the confidence value.
Now I just don't want the coordinates.
So let say text is equal to false and just run this cell.
So this will print the output video, the detections on the output video.
So let's see.
What do we get over here?
So here we are just for performing detections.
You can also perform segmentation as well.
So it's your or your choice.
So.
It will take for winners and to run.
So let's wait for the few minutes until it finishes.
Currently, it's where it is, so it's about to finish.
So as it finish, we will see the output results.
What results do we get over here?
And then we will move towards the export section, how we can support our model into the Onnx format
so it has run successfully.
So I have already run this cell and here is the output video for the detections.
You can see that the detections are working quite fine and the results are very good.
Now we can see how we can export the model into the Onnx format as well.
So export the model into the Onnx format.
I will just remove the model dot, predict line and add the model dot export line.
So let me show you what it is.
So to convert the model into the onnx format, just remove this line and instead of this line, add
the model dot export line.
So let me say so instead of Model.predict, I have added model dot export and the format I want to convert
on my model is on Onnx format.
So just writing the name of the format and here is my model name and just converting into an export.
So just run this and see what desserts do we get.
So just let me tell you, I will upload this sample image video as well as this CoLab file into the
attachment, so you can just download it from there and run the script and see what results do you get.
So now it is done.
So you can see that here is my model V8.
For next fight.
So the model successfully exported into the Onnx format.
So this is all from this video.
See you in the next video.
Till then, bye bye.