TFLite Object Detection Android App Tutorial Using YOLOv5 | Yolov5 to tflite conversion

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello Everyone, this is Aarohi and welcome to  my channel. So guys, In this video i'll show you   how to develop your own mobile application object  detection mobile application using yolo 5 so the   topics which we are going to cover in our today's  class on first we train our yellow v5 model after   that we will convert that yolo v5 model into a  tensorflow model and once we have that tensorflow   model then we will convert that tensorflow model  into a ta flight model and this tf light model   we will use in our mobile application android  application to make it as a object detection app   right so let's begin so first let's discuss about  the data set so the data set which i'm using uh   for today's classes that data set have 12 classes  and some of the classes are apple banana cucumber   eggplant kitkat maggie mushroom orange these  are few classes of the dataset let me show you   that data set first so this is the data set  here so guys what i have done is i've created   one separate folder just create one folder  under that folder you will put place all your   code which is related to our today's class  okay so i have created this folder yolo v5   pattos to ta flight android app under that  i have one folder with the name of dataset   this dataset folder when you will open it you will  get two folders train and well train is used to   train a model and on val folder we will  do the testing okay so when you open the   screen folder you will see two folders again  one is images and the other one is labels   we if you open this images folder you will  see the images all the images are here okay   and when you will open the labels folder you will  get the labels corresponding to each you know   image which we have in our images folder right so  this is the annotation file basically if you will   see these are the annotation file i have opened  the first text file and you will see like this   this is the class name and these are the bounding  box coordinates of it okay and guys if you don't   have a data set and if you want to prepare your  data set in the format which yolo v5 accepts   then you can use label image tool you can simply  install this tool by writing pip install label emg   and you can start annotating your data set and  you'll get your own data set okay and you have   to paste it like this under train these two  folder images will have all the images labels   will have all the text file um annotation files  okay in the same way if we open this well valid   folder again images and labels labels have  all the labels and under images we have the   images on which you want to perform the testing  so this is my data set on which i'm working   okay now you know about the data set now let's  see our code how to work on it okay so for that   this is the jupiter notebook i'm using i'll  share the link in my description section   you can take this code from there okay so  starting i'm starting with importing the   torch torch so why i'm importing torch because  yolo v5 runs on pi dots so you need to install the   spy torch so the fighter version which i'm using  is you can see over here i'm using 1.8.1 by torch   okay with cuda 11.1 so i'm using um gpu rtx 3090  so with my gpu this is the compatible version in   the same way you if you're working on cpu then  install the pytorch version which works with cpu   and in the same way if you're using different cuda  versions or um whatever your pc um resources are   according to that choose the python version okay  so once you install the pytosh version you need to   clone this github repo this is the yolo v5 github  rapper from ultralitx we are installing this okay   once you have this clone this github repo you  will see one folder like this see yolo v5 folder   again okay so data set folder the  another folder is yellow v5 folder   and under yellow v5 folder you will see these  files and folders okay now the next step is   once you enter into that clone github repo  the yolo v5 folder you need to install the   requirements.txt file this file is already  present in that folder you can see here okay here okay so you need to install it and  after that once you install the requirement i'm   importing all the required modules and this is the  command for training our algorithm okay so what   we are doing is python train dot py okay and then  image size you can change the image size over here   as per your data set right and the batches so guys  you can change the batch size sometime you face   memory issue then you can change the batch size  as per that number of epochs you can define as per   your need and then we have this dataset dot yaml  file this file you have to create okay so how to   create this file just go there so let me open this  dataset dot yml file for you so this is my file   train and well over here you have to define  the images which are present in train folder   right which path you have to give here the images  of the train folder and in this well you have to   give the path till the images of well folder and  number of classes of your data set so in my case   i have 12 classes and then you have to define the  name of these classes and it should be a dataset   dot y aml file okay so this is the file so that  file we are giving over here and then this waits   this way you have to download these weights  from internet you can get this weight from   this repo only from here only you can get  this weight file okay so now my algorithm   is getting trained and you can see after  50 epochs let me scroll down directly   after 50 epochs you will have runs folder under  your yolo e5 folder under that you will have   train folder and then exp folder there you will  get the saved weights now let's open it and see   so this is the run folder open it train  folder and then we have exp folder okay so   this so my latest code is in  this folder so i'm opening this   under this you can see you have one weights folder  when you will open this weight folder you will get   two weight files last dot pt and best dot pt  this pt simply means pi torch model okay so   and over here you can see the different uh  other things like the related confusion matrix   and you know the other results everything  will get stored over here all right and let me   show you the kind of results we are  getting so this is the one of the   some of the images from the valve folder you can  see over here right so our algorithm is detecting   now if you want to perform the testing  of this yolo v5 model how we perform that   testing for testing we use this detect dot py  file okay and we want to use which weights now   the weights on which the weights created just  right now after performing the training we get   one weight file right so now we want to use  those weights files so it is in run train   exp2 under that we have weights file and  i'm using best.pt from there remember this   from here right run train exp 5 weights  and then best dot p ui pg awaits okay and   confidence score you can change it over here and  then you have to give the path of the images where   you want to perform the testing so i want  to perform the testing on the test images   folder which is present in exp 2 folder okay  so now let me open this folder and show you   exp2 right run train exp2 test images on  these images i want to perform the testing   okay so we are performing the testing after  performing the testing results got stored in   this folder runs under run detect folder  and exp8 folder now let's open that   runs detects exp8 and let me show you the  results okay just give me a second see   so these are the few results from our uh algorithm  this is how our algorithm is performing testing   right so now our algorithm is ready yolo  v5 algorithm is ready and we have trained a   model and we have used the test command also to  check if that model is working correctly or not   now the next step is to convert that dot pt  model the python model into a tensorflow model   tensorflow model means dot pb model okay so dot  pb simply means it is a uh when we say this dot   pb model so dot pb basically stands for protobuf  and in tensorflow now this protobuf file contains   the graph definition as well as the weight of the  model so what our task is to convert the dot pt   model into a dot pb model and this dot pb model  simply stores the graph definition and weights of   the tensorflow model okay so now let's do that  so for that you have to execute this command   python export dot py this export dot py file is  already present in your yolo v5 folder this so   we are using this file okay now using this file  what we want to do is we want to what weight we   wants to convert the weights this best dot  pt weights which is present in exp2 folder   and if you want to convert it into a tf light  then you have to mention like this include   tf light and then the image size just execute this  command once you execute this command you will get   a tf light weight and it is stored  in over here you can see yolo v5 runs   train exp 2 weights under this weights folder  you will get a tf lightweight let me show you   yolo v5 runs train exp2 weights and now you  can see we have this tf lights weight over   here okay and now we have the ta flight model now  okay sorry guys before that i have to show you   this thing just give me a second let  me show you so what we are doing is   we are converting it into a ta flight  and here you can see the tf light weights   it's saved over here here okay and this folder  will get created itself and under this you will   see this pb also okay so what i have told you  just now that first we will convert the dot pb   right we will convert our uh yellow v5 weight into  our tensorflow words so by executing that command   it will execute this step by default okay you will  get this dot pv file and from dot pv file it will   give you this dot ta flight weights also right  by just one command by running this one command   you will get the dot pb file also and after  from that pb file from the tensorflow model   you will get the tf light weights also so now we  have a ta flight with us okay now let's test our   this tf lightweights okay so for that again what  we are using is we are using the detect dot py   file which is present in our yolo v5 folder  detect dot py okay we are using this file   and what weights we are using this time we  are using the ta flight weights okay and again   on which folder you want to perform the testing  the folder which is present over here so this is   the same folder we have performed the testing on  the images which are present in test images folder   when we test our yolo v5 model see we  are performing the test on that only   and with tf light model also we are performing  the test on that only and the results will get   stored in exp 9 when you execute this command  you will get another folder with the name of exp9   under this detect folder and there you will  get the results let's see this exp9 folder now   runs detect exp9 and you can see our results  of df like model now our tf light model is   also working fine okay so our model is ready  now the next step is to use this model into a   mobile application to make it a object detection  mobile app okay so for that you need a android   studio so guys you can install download and  install android studio and once you install the   android studio then okay let me show you one  more thing after that you will have one folder   this android folder okay i'll give you this  android folder you have to place this enjoy folder   in your yolo v5 folder okay the github wrapper  which we have cloned and we are working on it   under that folder uh inside that yolo v5 only just  copy it from here and place it under your yolo v5 okay we will have this android folder is  containing a code of mobile application so   again i have given the link in my description uh  section so from that github repo you can take this   android folder okay so now what you need to do  in this android folder open this android folder   under that you have app open that and then open  this source under source click on this main and   under main go to this assets folder first okay  when you open this assets folder here you have   to place your tf lightweights right the tf  light weight which we have created you have   to place that tf light weight over here and one  more thing you need to do this custom classes   okay so let me open this custom classes  text file this is a text file and here   you have to write down the name of your custom  classes which you have in your data set and   one class in one line okay so these are the two  things you need to do at this part under assets   your weight file the tf lightweight file and  the custom classes this file will have all your   custom classes this is the first change you need  to do after that go to this mains folder again and   go under this java org tensorflow lite examples  detection you have to come here and you have to   open this detector activity dot java file okay  when you open this detector activity.java file   here you have to make a few changes okay so  those changes are give me a second please just uh ctrl f and find this label  file name okay so just the second all right let's go back and see if i'm working  on a correct okay just go in this ta flight okay   guys you have to go under detection you have to  go in this tf light okay over here you will get   detector factory.java so this is a file open this  file okay over here see first of all what you need   to do you can see here you have to define the path  of the custom classes.txt remember so this is the   file which we have created which have the list of  all the classes which are present in your data set   right and it is an asset folder just write the  name of that file in which you have defined your   custom classes okay over here also here and  here and just give the path of your ta flight   model over here that's it guys these are the  few changes which you need to do once you do   these changes after that your android application  object detection android application will work   right now let me show you how to test your android  application and for that you need android studio   i have told you already that you need to install  the android studio once you install the android   studio right so this is this kind of screen you'll  get and after that see over here i have configured   my device over here in the same way you have  to so this is the device which i'm using right   so you have to connect your device over here right  so my device is this and after that you need to   build the app and then you have to run the  app right so over here from here process my   app is already running so i'm not terminating the  process right otherwise if your app is not running   you need to click the button over here which is  over here and your app will run and you can see   that application on your mobile application now  let me show you so guys you can see here this   is my android application and it is detecting a  kitkat kitkat is one of the class which we are   detecting using this object detector so in the  same way you can use other classes and this is   how this object detection app work right so i have  given the link in my github description check in   the github link right so you can get the android  folder from there and you can train your own model   and then you have to do just few changes  in that android folder which i've shown you   and you will be having your own object detection  mobile application thank you for watching you
Info
Channel: Code With Aarohi
Views: 53,034
Rating: undefined out of 5
Keywords: Object Detection, Yolov5, TFLite, Object Detection Mobile App, Object Detection App, AI, Artificial Intelligence, Deep Learning, YOLO, Machine Learning, Neural Networks, Tensorflow, Piford Technologies, Akminder, Aarohi Singla
Id: ROn1_O2zEtk
Channel Id: undefined
Length: 20min 38sec (1238 seconds)
Published: Tue May 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.