Official YOLO v7 Custom Object Detection Tutorial | Windows & Linux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to my channel in today's video we will be doing custom object detection using yellow v7 here are the steps that we are going to follow the timestamps would be in the description first we will create a virtual environment i will show you how to download images from google using a small script then we will annotate the images after that we will install libraries required to train yellow v7 object detection models finally when everything is ready we will train yellow v7 model and then test it on images and videos let's get started to create a virtual environment open anaconda prompt and use command conda create minus n then environment name which is yolo v7 underscore custom and python equals 3.9 hit enter type y and then hit enter again once the environment is created activate it by command conda activate yolo v7 underscore custom we can see that base environment is now changed to yolo b7 underscore custom now to download images from google we are going to install a library by command pip install simple underscore image underscore download equals 0.4 i'm installing this specific version because the latest version does not work with google properly after installation is finished create an empty script called download underscore images let's write couple of lines of code from simple underscore image underscore download import simple underscore image underscore download as same then response equals same dot simple underscore image underscore download now we will define the keywords against which we want to download images let's define johnnydepp jack and jack sparrow now for each keyword invoke response dot download and limit the number of images to let's say 300 save the file then run the script by command python download underscore images dot pi you can see a new folder is created and inside that folder there would be subfolders against each keyword and images would be downloaded in respective folders after this process is finished i am going to merge images of both folders then let's create a new folder called images and move all the images there as you can see there are a lot of repeated images and some garbage images i will select 60 or so images for labeling and delete rest of the files now we are ready for annotations for that let's install another library by command pip install label img note that i is capital in the command once the installation is finished type label img and hit enter you would be presented with this gui click on open dir and point to the images directory that has all the selected images then click on change save dir and point it to a new directory called labels make sure this is set to yellow as some time it is set to pascal voc now click on create checkbox and draw a bounding box around your object as accurately as possible type the label for the object and click ok click on save and then on next image this way annotate all your images and if any image has multiple objects draw multiple bounding boxes once you are done with all the images head to the labels directory and you will see that there is a text file against each image having annotations there is classes.txt file just delete that as we do not need it now you have images and their corresponding labels in a separate folder create a new folder called train another folder called validation inside validation create a subfolder called images and another folder called labels it is important that you give exactly same names to these two folders now from main images folder i'm going to move last 12 images to val slash images similarly from main labels folder am going to move corresponding 12 annotations to val labels and now move main images and labels folders to the train folder that's it our train and validation sets are ready now we need to download official yolo v7 repository download it as a zip file in our yolo v7 underscore custom folder once the file is downloaded extract the file let's rename it to yolo v7 dash custom then cut the folder from here and move to the main folder delete the empty leftover directory inside the extracted repository there are a bunch of files and folders open requirements.txt with your favorite text editor and note the torch version it says the torch version should be greater than or equal to 1.7 but not 1.12 similarly torch vision version should be greater than or equal to 0.8.1 but not 0.13 note this down and go to pi torch official website we want to install pytorch with cuda support to utilize gpu during training yellow v7 but we do not want to install the latest version instead click on previous fire torch versions and scroll down to version 1.11 which is below 1.12 that we just noted in requirements.txt delete torch and torch vision from requirements.txt and save the file create a new text file and copy this pip installation command and paste in this new text file type minus i and then space and copy this url here then keep torch on first line and torch vision on the second delete everything else save the file as requirements underscore gpu.txt now in anaconda prompt go to yolo v7 dash custom directory and execute the command pip install minus r requirements.txt this will install all the libraries that are part of requirements.txt once it is done execute another command pip install minus r requirements underscore gpu.txt this will install pi torch with cuda support all prereqs are now set let's move our train and validation folders to yellow v7 dash custom repository inside data folder now we need to modify some configuration files let's make a copy of coco.yaml file in the data folder and rename it to custom underscore data dot yaml open it and delete first four lines as we do not need them now we need to define train and validation folder paths train folder is inside data and then train we do not need the test folder so just delete this then validation folder is inside data and val nc is number of classes which is one in our case and now we need to define the name of that class which is jack sparrow save this file now find the cfg folder from yolo v7 dash custom repository and then go to training there are seven configuration files for all variations of yolo v7 models and you can train any of them on the custom data set for demonstration in this tutorial i will train base yolo b7 make a copy of yolo v7 dot yml and rename it to yolo v7 dash custom dot yml open this file in text editor and just change nc which is number of classes from 80 to 1. save the file now we need to download weights for the base yolo v7 from official repository by the way if you plan to train tiny version of yellow v7 its weights file is hidden here in releases and then in assets there it is i have downloaded both yellow v7 dot pt and yellow v7 dash tiny dot pd already so i will just copy them in our yolo v7 dash custom directory now we are all set to start training let's go to anaconda prompt make sure you are in yolo v7 dash custom directory let's run the command python train dot pi minus minus workers 1 minus minus device 0 minus minus batch dash size let's do it if you encounter cuda memory error you can reduce it to four or if you have a high end gpu you can increase it to 16 or even 32 then epochs let's do training for 100 epochs then minus minus img which is training image size as we are doing base yolo v7 it's trained on 640 by 640 resolution so let's write 640 640 here then minus minus data this is that custom underscore data dot yml file inside data folder which we created then minus minus hyp which is hyper parameter file already available in the data folder so just type data slash hype.scratch dot custom dot yml then minus minus cfg this is the yolo v7 dash custom dot yml file inside cfg slash training which we modified and minus minus name which is the name of the model output after training is done let's call it yolo v7 dash custom and finally minus minus weights which is the path of the weight file that we downloaded it's yolo v7 dot pt hit enter the training is started we can see that it has found 52 images and labels for training and 12 for validation and none of the labels are missing for any image by the way if you decide to add more images and labels after running this command make sure to delete label cache files from data train and data val folders okay so training is running i will get back to you when this is done the yellow v7 custom model training is finished we have trained weights files at this location let's go to runs train yellow v7 dash custom and we can see a bunch of files here including training log graphs etc but for now we are just interested in the weights folder we will copy best dot pt file from here which represents the best weights based on validation loss and paste it in the main yolo v7 dash custom folder let's rename it to yolo v7 underscore custom dot pt and now we are ready to run predictions let's copy an image from validation data and paste it in the main folder let's rename it to one dot jpg so that it's easier for us to use it in the command now in the command prompt run python detect dot pi minus minus weights yellow v7 underscore custom dot pd minus minus conf 0.5 minus minus img dash size 640 minus minus source one dot jpg minus minus view dash image minus minus no dash trace hit enter the processing is done the output is inside runs slash detect slash exp folder there we have it it's able to detect jack sparrow with a very high confidence now let's do it for the video i have this savvy.mp4 which i will copy in the main folder for videos exactly same command is used except the path to the source is now savvy.mp4 there we have it the output is again in the runs slash detect and in another subfolder so does that mean that yolo v7 can also do face recognition maybe we will explore that further in one of our future videos by the way if you find the bounding box font too big or want to show fps on the video check out this tutorial where i show you how to make these edits in the code i will leave the link in the description if you have learned something of value today leave a like and subscribe to the channel consider support on patreon to help the channel out i will see you next time [Music] [Music]
Info
Channel: TheCodingBug
Views: 102,396
Rating: undefined out of 5
Keywords: yolo v7, yolov7, yolo, yolo object detection, YOLOv7, Official YOLOv7, yolov7 tutorial, train yolov7, yolo v7 tutorial, yolo v7 object detection, yolo v7 linux, yolov7 windows, yolov7 linux, yolov7 python, yolo v7 python, yolov7 object detection, yolo7, yolo v7 official, official yolov7, official yolo v7, yolo v7 custom dataset, yolo v7 custom, custom yolo v7, yolo v7 custom object detection, custom yolov7, yolov7 custom dataset, yolov7 custom object detection
Id: -QWxJ0j9EY8
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Mon Aug 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.