Complete YOLO v8 Custom Object Detection Tutorial | Windows & Linux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to the channel in today's video we are going to do custom object detection using YOLO V8 this is going to be a two class object detection and here are the steps that we are going to follow first we will download images of our interest from Google using a small script then we will annotate those images in Yolo format after that we will set up YOLO V8 on our local machine then we will train the YOLO V8 object detection model and finally we will run custom object detection on images videos and webcams the timestamps would be in the description below let's get started let's open Anaconda prompt or terminal if you are using native Python and create a virtual environment I am using Anaconda so I am going to issue conda create minus n YOLO V8 underscore custom that is the name of the environment and python equals 3.9 hit enter Then type y hit enter again once the installation is done activate the environment by command conda activate YOLO V8 underscore custom we can see that our environment is activated now install a library to download images from Google with command pip install simple underscore image underscore download equals equals 0.4 install this specific version as the new versions do not work once the installation is complete let's create a new empty file called download underscore images dot pi open that file in any text editor and then let's import the library from simple underscore image underscore download import simple underscore image underscore download as Sim then let's initialize downloader response equals Sim dot simple underscore image underscore download now we need to define the keywords against which we want to download images from Google so keywords equals this is going to be a list and let's just use one keyword building workers you can use multiple keywords if you want now for each keyword we will call response dot download method pass that keyword and let's define the number of images we want to download against that keyword let's do 200 for this tutorial now go to Anaconda prompt and run this file with command python download underscore images dot Pi hit enter and you will see that a new folder is created called Simple underscore images and once all the images are downloaded let's go to this folder and you will see a subfolder against each one of your keywords and if we open that here are all the images that are downloaded some are just garbage so I'm just going to select around 50 good images from here cut them and then create a new folder called images and paste all the selected images here we can delete this old folder now to annotate the images let's install another Library by command pip install label IMG notice that the I is capital here once installation is done let's run this tool by command label IMG and hit enter you will see this interface now click on file and then open dir this is the directory that contains all the images that we want to label then again click on file and then change save dir this is the directory that will contain all the labels once we annotate the images let's create a new directory called labels and select that now make sure this is set to YOLO and not to Pascal VOC now let's start annotations by clicking on create Redbox create a rectangle around the object that you want to annotate and then type the label I'm going to label worker hat but that level is too long so let's just call it hat and then create another box around jacket and label it as jacket similarly annotate rest of the objects in this image and assign the respective label to them once done click on the next image and repeat the process I'm going to label all the images and get back to you once I'm done all right all the images are labeled and you can see a text file in the labels folder for each of the images now let's create a directory called train and another directory called well open well directory and create a subdirectory called images and another subdirectory called labels now I am going to move last six images from Main images folder to Val slash images and from Main labels folder just move classes.txt to the root directory as we will need it later now from Main levels folder move last six annotations to the valve slash labels folder now move the main images and labels folder inside train folder that's it our data set is now ready to train YOLO V8 on this custom data set let's create an empty file called Data underscore custom.yaml open it in any text editor let's define train directory which is the directory over here and then Val directory for NC which is the number of classes the value would be 2 as we have two classes for each class we need to define a name so this is going to be a list now which name would be the first index and which will go to the second well open this classes dot txt that we move to the root directory earlier and notice that the hat is the first class name so type it in the first index of the names list and then jacket would go to the second index so that's how we know the order of the class names now save this file finally we need to set up YOLO V8 just open Anaconda prompt and type pip install ultralytics hit enter and it will install all the libraries required to run YOLO V8 this installs Pi torch CPU version so if you want to use CPU for training which would be painfully slow you are all set we can verify by running python then import torch then type torch dot underscore underscore version underscore underscore and you can see it's 1.13.1 CPU version let's exit Python and clear the screen to install GPU version just go to the official Pi torch website click on get started scroll down and here select the same version that you just saw earlier select your operating system I'm gonna go with Windows with Pip for Python and Cuda would be 11.7 then copy this command at the end paste it in the terminal add minus minus upgrade flag and then wait for it to download and install GPU version of pytorch the older CPU version would automatically be uninstalled once the installation is finished let's run python import torch and then type torch dot underscore underscore version underscore underscore and now we can see it is installed with Cuda we can also verify if the GPU is detected by command torch dot Cuda dot is underscore available and we can see it prints true alright now let's train YOLO V8 on custom data set go to official Ultra analytics GitHub repository and scroll down to detection section there are five yellow V8 model variations and of course the smaller model would have lower performance while the larger model would perform better but would also be slower I'm going with yolo V8 medium just right click on it and copy this link and paste anywhere we just need to copy this file name at the end now go to Anaconda prompt and issue the command YOLO task equals detect mode equals train epochs let's set it to 100 data equals which is the yml file so type data underscore custom.yml and then model equals and paste the model file name that we just copied let me extend this window a little bit then set IMG SZ equal 640 it's the image size on which the model was drained if we hit enter it will download the pre-trained YOLO V8 media model automatically and would try to start training but you encountered this error that file is not found this means that you will have to give absolute Paths of your train and valve folders so open data underscore custom.yml let's type complete path of the folder which is D drive then the coding bug then YOLO V8 underscore custom and then train again copy this path and paste for Val folder as well now if we issue the same command the training would start but depending on the available memory of the GPU you can also encounter Cuda out of memory error to resolve that just add another parameter batch equals and set it to some low value like four or eight I'm gonna go with 8 that's it the training would start and it's gonna take some time I will be back once it's done all right once the training is done you will see that the results are saved in runs and detect and train folder here you can also see performance of the model on validation data set as performance was not improving for 50 epochs the training is stopped early if you want to change this threshold you can set another parameter in the command patience equals and then set to the number of epochs after which you want early stopping alright inside runs slash detect and then train we have multiple files results dot CSV has performance metrics after each Epoch and results.png has graphs for training and validation loss and map values what we are interested in is the best weight file just copy this best DOT PT file and paste it in the root folder I'm gonna rename it to YOLO V8 M underscore custom.pd by the way you can also use Python script to train the model and model dot train takes all the parameters that I showed you in the command line and if you want to retrain the model after adding more images delete labels dot cache file in val folder and labels.cache file in the train folder all right to test the custom object detection module I'm gonna copy any image from the Val folder and paste it in the root folder let's rename it to 1.jpeg I also have copied these two videos of construction workers called one.mp4 and 2.mp4 to run prediction just issue this command YOLO task equals detect mode equals predict model which is the file YOLO V8 M underscore custom.pt show equals true conf equals 0.5 Source equals one dot jpeg if we hit enter we are gonna see the results for a moment and output is stored in runs detect and predict folder if we open that we can see the output there is a false positive though maybe using more data can improve that now if you want to save the bounding box information you can set save underscore txt equals true and this time the prediction image is the same but we also have one dot txt with bounding box information in Yolo format now to run object detection on videos just replace the source with your video name which is one dot MP4 in my case and here we can see the output in real time the results are stored in predict three so if we open it we can see the video is stored here and all the predictions are also there if you do not like the size of the font or the bounding box line you can set another parameter line underscore thickness let's set it to 1 and this time font and bounding box lines are smaller if you want to save cropped objects let me change the source back to one dot jpeg add another parameter save underscore crop equals true and now if you go to predict folder we can see that all the object crops are stored in subdirectory if you do not want the labels and the confidences set height underscore labels equals true and hide underscore conf equals true and now if we run it the bounding box would not have any labels or confidences if you want to detect a specific class you can set classes equals and then Define a list of classes let's only detect hats which is at index 0. and now if we run it we can see that the only hacks are detected while the jackets are ignored similarly you can set the classes to be detected to 0 and 1 and so on for all of your classes now to run on the local webcams just set the source to zero or for the IP webcam just set the source to the URL for detections on all the images and videos in a directory set the source to that directory if you want to export your custom object detection model to any other format just change the command a bit remove source line thickness conf and set the mode equals export then format equals 1 and X if we hit enter this will export the model in on NX format if the package is not installed it will install it automatically and here you can see that the model is exported in on an experiment to export in TF Lite just change the format to tflight at the moment tflight is not implemented maybe in the future versions it will be available by the way you can run all of these predictions using python script as well let me just copy this train dot pi just change this model to YOLO v8m underscore custom.pt and then call model.predict method here you can set all the parameters that we used in the command line so just set Source equals 1.jpg show equals true save equals true conf equals 0.5 and run the code python test dot pi and here you have the same result with that I think we have covered everything let me know if I have missed anything in the comments down below if you have learned something of value today hit like And subscribe to the channel consider support on the patreon to help the channel out I will see you next time [Music] thank you
Info
Channel: TheCodingBug
Views: 132,097
Rating: undefined out of 5
Keywords: yolov8, yolo v8, yolov8 custom object detection, yolo v8 custom object detection, yolo, yolov8 tutorial, yolo v8 tutorial, yolov8 custom dataset, official yolov8, official yolo v8, yolov8 windows, yolov8 custom, yolo object detection, YOLOv8, YOLO v8, yolo v8 object detection, yolov8 linux, yolo v8 windows, yolo v8 linux, custom yolov8, yolo v8 custom dataset, yolo v8 custom, custom yolo v8, yolo v8 custom data, yolo v8 custom training, yolov8 custom training
Id: gRAyOPjQ9_s
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Fri Feb 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.