How to Train a Custom Model for Object Detection (Local and Google Colab!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back spent again today we're going to learn how to train our custom model using our own PC or using Google collaborates free GPUs so since last time I finished annotating all the images we got of our masks so you can see here I went through all of these it was about like 220 something images and I did the annotations for each one and that worked out pretty well that took a little bit of time but you know take some time to pre-process things so we have this folder all set up and we also have our models folder all set up our options detection folder so now the next step in the process is going to be getting a model to take from the model directory and using it to train our model so we can do that by going to the tensorflow one detection models ooh so tensorflow support for under detection tensorflow to actually just came out but this tutorial is gonna still follow tensorflow one hopefully I'll make an update video at some point about 1002 but for now we're just gonna follow the way we've been doing it so if we go to this model zoo it gives you a whole list of different models you can use to train your your own model so they're all classified by different speeds and performance so the one that most people use is this one right here the SSD mobile net be one this is kinda like the basic one so it's pretty fast and it's decently accurate which is nice to see so generally the faster something is the less accurate it is as you can see here like 26 seconds goes down to 1829 1839 16 so this one right here is 30 milliseconds and we have accuracy of 21 so there are other models that are a lot more accurate but are a lot slower like for instance we have this one down here 620 milliseconds which is a lot but it's 37 so it's gonna be really accurate even this one that's 1,800 milliseconds that's almost 2 seconds of delay so but for our purposes we're just gonna use this one right here so I already have it downloaded but if you download this one you're gonna get this tower folder right here which an archive so along with that we also need to go here which is our samples and configs if your model or the repository that you cloned at the beginning of this is the most up-to-date it'll have all of the configs you need in it already under here let me show you if we open up or them out of detection so if we go so for in our normal object section folder we can go to models and I'll have all the config files you already need but if for some reason the config you want isn't in here you can just go to this website and grab it yourself so the one that we're gonna get this one it's v1 Coco so we're gonna go look for that Stevi one Coco that's v2 here it is so this is the one we want download all right you know instead of download what do I want to download it because I don't wanna copy the whole thing again alright you can either use this one or you can use this one right here the mobile net via on pets config I'm just saying this one because I already have this one on my PC and it does the same thing this one was just trained for detecting pets rather than detecting whatever the other one was I think the other one was like common objects so I already have this one of my PC so we're gonna use that one and just edit it to make sure that it works correctly so to do that first let's see let's get the downloaded file I'm just gonna open up downloads we're gonna pull it out here actually I might need to download it I don't remember that's not okay yeah let's download it I lied let's go get this one we're gonna put it downloads so then alright so that should be downloading and this has opened up our mask so we're just gonna put it in here for now move that you're just gonna extract it and then we get this file right here which has everything that we need actually this pipeline config may we maybe we use this not sure but anyways once we have that let's get the config file I should have a copy of it over here to take a look at it so I'm just gonna use notepad plus plus to edit this okay so yeah so this is going to look like this for most of them so the things will have to change are the number of classes which is right here so generally it'll say 90 or 37 or whatever this one was previously trained on so just change it to how many you have so our case with several one if we change that to 1 and then down here there's a whole bunch of things we have to change well for me there probably updated though you oughta most likely have to change them so for a fine-tuned checkpoint I'll just make it so that it's the name of the folder slash model dot checkpoint so for this one we're using the 28 Ino 128 so that I'll be that right there again if you're using a different one just go to the folder just do rename copy that and then just paste it right here on the full on the file that you would normally download there's usually at the longer extension I could probably show you so this is what like the normal one looks like yes I'm gonna see here path to be configured basically everywhere where you see path to be configured to get you got to change it up so that's the first one we got to do we just pull this up over here and then we have to change the input path and label map path under train import Reader so for the input path this is gonna be where our labels and where we have our audio detection pptx file is we haven't made that yet we're gonna make that in a minute so just don't worry about that again just change these to where your files are so these are where our TF records are so we put that in data if you're somewhere else just change it to be somewhere else again on the default they have all this extra stuff in here but we don't need that for what we're doing so just do train that record or whatever your TF record is so for a label map path this is a a file we're going to add or you can make this training wherever you want it to be I might change it to training later actually we'll see and then down here we have input path so again this is gonna be for test and then this is gonna be training yes I might try to change this up later but that's all we really need to do right here for patch size I'm actually gonna change this to 24 this was basically to be how many how many images and samples is gonna throw into the model at once to train this does take up a lot of memory on your GPU or system memory so if you're getting memory like out-of-bounds errors or not off memory change this to a smaller back side or if you have a lot of memory change it bigger if you want and it'll go faster so we're gonna save that that should all be good make sure everything's in the right place so we have this and here so I'm actually gonna move this into training so once that's in training let's move this is actually the wrong folder over here for models let me just move real quick this is the one that we were working out of before I just had to made a copy for something else I was doing the same exact thing don't worry so we ran object detection and alright so we still have our files we did the last time so this is everything that we had before so I'm gonna do is actually going to delete the data folder that's in here because what we want to do is copy all of these folders or files I'm going to copy those and we're gonna paste them right into here that might take a second so basically we're gonna just be putting in our own data into here alright so we got all of that oops I forgot to make the file that we need so we're gonna go into this wooden training actually it might be the wrong place we can change in a middle minute so we're gonna do we're gonna create a new text document and we're and this is really simple this just tells the actual camera if we're using the camera or image detection what to call it like to put the the name on the boxes so we're doing cloth mass so we're just gonna go like this and then ID one name we call it a cloth mass like that and then like that I'm gonna make sure it is formatted that correctly yep looks like okay so now all you I do is save as change this down here to all files and then we want to call this object detection make sure it's a hyphen and not an underscore dot PB txt so that's gonna change it to a PB txt file so let's close this and we can see right here it created a new file for us so we can get rid of that original one and that should be all set all right so now let me just make sure we're on track for our cheat sheet okay so all files so now removed everything we have so now we're gonna start our training fun ok so right now I'm in actually the wrong folder I need to move back a couple so I need to go into what did I call this folder I just i only renamed it it's our our same models folder there we guides this one so we need to go into research and then we're gonna go into object well actually first let's do research and then we're going to let these commands we use from last time this set Python path it's good to do it every time you run this and then we're gonna move into object detection so now we're going to run our training so actually I'm going to delete this out of here again all post these commands in the description below because they're kind of long so actually we'll go over this command real quick so basically we're is calling the trained app hi script and we're setting flags for the training directory which is where all of our files are going to be saved and for our pipeline config is basically the configuration file that we're using so if you have a different configuration file or you need to set it up you put the name with a file right here and then just op config and then we do log the standard error so let's copy this and let's run it let's see if we get in here can't open the file oh whoops I forgot a step I did forget something alright so inside of our audio detection we need to go into the folder legacy and we need to take these files we're just gonna copy them we're gonna paste them out here we're gonna replace that file so the reason why those were in legacy is that those are kind of older files since we're doing this sort of in the older way it's not really the most up-to-date but it's the way I learned to do it so it's the way I do it and I'm sure you could change something else somewhere else to change the scripts up to make it work for legacy that folder but I like to just move them all in here and it makes life a little bit easier for a moment so let's see if that's gonna work now how it does know module name net okay so if we're getting this error no module name net so we have a pretty simple fix we're gonna go in the folder back one so weird and go back into research we're going to take this folder call it slim we're gonna copy that and then we're gonna go back into object detection and paste that and to slim or pay slim in to avi detection let's try it again see what its gonna say no module name that's okay we can fix that so if you get no module name Nets all you do is take the Nets folder inside of here let's add a slim and just paste it inside inside of Audry's detection so let's run it again you might get another error let's see no module name deployment okay yep so that means we can also go into slim and we're gonna take deployment and right here sometimes I have to do with the path setup if the paths are a little mixed up if not you can just do this and it should make things happier better maybe what can it find now failed to create open data obvious tection dot PB TX to you okay so looks like we need to take our yeah cause in that config file I kind of mixed it up so we're just gonna copy it instead of changing the file cuz I'm a little lazy we're just gonna paste it in here hopefully that'll work so let's run this again alright looks like we might get it going this time I'm actually gonna open up my rise I down clock to my computer for a moment though we're gonna get the full performance out of it so I don't have a compatible GPU for training models so to do that you'd have to get tensorflow GPU and you need a CUDA enable GPU which is generally well not generally this is nvidia cards so I have an AMD card I rise and rise in our rx 5700 XT which is really nice graphics card but unfortunately does not work for what we're doing so if you don't have a compatible graphics card you can use your CPU although it can be pretty slow depending on what you're doing so we'll see here how fast we're going so we can see what's going on I'll show you in a minute so we're saving checkpoints to training slash model so checkpoints it's going to be what saves all of our progress that we've made we're gonna use those checkpoint files in a minute later so we can see here we're starting to get steps in it takes a little bit to start at the beginning you'll probably see a bigger jump right here so basically we're going to do is going to go through all these steps and is gonna go step after step after step and it's going to try to decrease this loss century here we want the lost percent to be as low as we can get it generally around 1% you hope to get underneath 1% but if you can't but it takes a long time to get there if you don't have a good system so this may take a while to start running again because I'm on CPU so I'll show you where it's gonna go though so if we go into training you can see we start to get some more files into here so these aren't the files we generally need but what you'll start to see once this starts going and we'll get a ton of files ton of steps going you'll see the lost steps going and every once in a while let's say like saving checkpoint or summarizing and so you'll get more checkpoint files in here that'll have numbers on the end of them and those are the files we're gonna look for later so we'll see if that's running so we're gonna let those run in the background and what we're gonna do is actually show how to do this do the same process using Google Collaborate which if you if you've never used Google collab it's kind of like it's like all of other Google services it's open so not open source but you can share it with other people and you can edit code in this one and it's built for using tensorflow which is really handy so let's go over there so this is a file that i've been using that i put together so what we're gonna do delete that that's not meant to be there I'll edit this file up to clean it up a little bit and put it on the github also if you want to use it so I'll walk a little bit through what we're gonna do so the first step we're basically it's gonna run these one after each other one after another so we're gonna student do pip install we're gonna upgrade pip and upgrade protobuf sometimes there's a problem where protobuf isn't working correctly and doing this step right here fixes it so let's just do that so this next one just sets the tensorflow version and we're importing tensorflow and installing numpy I got some extra stuff in here that we don't really need that's gonna go so this right here just checks your GPU status oh and to make sure that you're using a GPU go up here on runtime and do change run time type and make sure you're using a GPU all right so we can check GPU status and we run this one also this means that we are connected to a GPU which is what we want well right for this tour is going to show us some more statistics about our GPU not really necessary but let's see so it's going to show us how much RAM is free so we have 12 and a half gigs and because you share GPUs on Google collabs so if you get like a high utilization you might want to try restarting it and getting a different connection because you want as much RAM as you can get so this is where the interesting part comes in so to get our data into Google collab we actually going to use Google Drive so what we have is that this is just my Google Drive and I actually copied and pasted the models folder right here so you can see it's everything that we had before so go into research and then object detection is right here and these are all of our files that we had so doing that we can so that's the setup only I do is Mount the Google Drive so we can do is drive dump mount and then we're gonna just change the directory and you'll see it's gonna ask me to log in so right here it says go to this URL in a browser so I'm probably gonna cut this so you can't see all my emails but his business and I asked me for our login code hurry up so this is the code that we need so we can copy this and then paste in the authorization code close that out which takes moment so there we go mounted at content drive so just make sure that if you call it something else just make sure you're here in the models directory so right here we're gonna install and compile protobuf we're gonna get psyphon and we're going to go into the research file and then we're gonna compile pro talk right here and this is sets up our Python path and then we're gonna build and install just like we did in that first video with setup dot pi so this file usually takes a long time to run because it up high takes a long time to install over the cloud so this is gonna go for a while so don't be afraid of this takes up to like five minutes sometimes it takes a long time if you're running it for the first time if you run it multiple times it's usually faster afterwards but for the first time be prepared for it to take a while alright so setup dot pi has finished installing so we can move on let's have another GPU right here so you always have a session with cool collaborate and it tells you how much time you have so usually it starts at about 12 hours so it's usually plenty for what we're gonna do for what we're going to do with 12 is plenty so right here is where it gets a bit more interested we're gonna start training so we're just going to do Pitman stall TF slim and we're just going to CD into the correct directory we set up some Python paths again these are some commands that we don't need I'll clean up this is the main command right here it's the same one that we ran on the command prompt does a strained up high and then training so it'll actually go into our Google Drive and save the directory or save our files into training so let me take a look in here I did some practice training before this you'll probably see some files in here already yep so we'll go back into here and we'll run this again it's probably taking me to start and if you're doing this on the cloud you might also get those errors that we were getting before we're just can't find nets can't find slim deployment whatever just do the same thing where you move those folders up into object detection and it should fix it for you if you do it while you're in Google Drive so say so I think you added something in your Google Drive and then you want to go over here it won't it doesn't update at the same time so what you'll probably have to do is click on run time and then where is it runtime then factory reset run time so that I'll unmount your drives that'll reset all the variables so everything can update in the back end again which may be important I had to deal with that a few times so I was wondering like why isn't this updating or it's working so I should work it can save files to your drive automatically but reading from it takes some time for it to like readjust and everything so again this does take some time to load so I did so I pulled up the command prompt again while we were waiting for that and I ended up stopping it because I didn't want to eating up all my cpu power well let's try and record the video so you can see this is where it starts to look like you'll start to get these steps right here and the loss will eventually go down slowly slowly so I'm getting like three point five three three-and-a-half seconds per step which is pretty terrible actually because again I'm on CPU of these things are really meant to run on GPUs so and when I didn't get it far enough to show any summaries but it'll say like some receive - blah blah wherever so we'll start to get those I can see in the background we're starting to get this to run so I'll show you how to save the graph on either one now don't worry so alright again we can see down here saving checkpoint to training slash model so that's in here this training I lost it over here so we will be updating live to this folder sorry and clear all this junk out of it before but you'll get the idea so there we go string this step so these steps are much faster this doing point three seconds of Step four as mine was three and a half seconds of step so you can see how much faster this will record or not record a little train so when again sometimes it does start a lot like with the loss well this loss is probably because I was training it before already and I think about you can see yeah I'm on step like 65,000 or was it 66,000 yeah so for you it'd be on like step zero and they would look a little bit more like minded with like percent like loss would be like 10% 11% so just let those run for a while so once you start to see that you're getting down to like one like one point two point eight point nine like in that area and that error consistently then you can just use control-c to stop it or you can go up here or you can click this to end it or you can do runtime interrupt execution before you do that make sure you go into training and you look to make sure that the checkpoint files have saved you want to make sure all three files I've saved every checkpoint has three of them you get a data index and a dot meta so you need all three files so make sure that so cuz sometimes like you'll see like recording summary for stab like six thousand but you won't see that step recorded in training for a long time so make sure you wait until you've gotten to a point where it is in your folder and you feel comfortable ending it so I am going to end this one right here because I already have some files we can use so I'm just gonna interrupt execution so there we go so again I'm just gonna use some files we already have in here I'm actually going to delete some other stuff so you can see how it looks like delete some files so the next step is to export our inference graph so this is going to be the graph for the neural network and the model really that our tensorflow is going to use or oppy detection excuse me sorry so we're gonna do so it says Python export inference graph which is a script we have and then we have the pipeline so again it's just the name of the configuration we're using and then the train checkpoint prefix is the checkpoint file that we most recently have so all you have to do is you point to where it is so training is where we have it slash them all that checkpoint and then this number right here is going to be changed every time you run a model it's gonna be the most recent one that you have so illustration one I had was 65 37 I believe that was yeah so 65 37 is the last set of information that we got so that's what we're going to use so and then this one right here is going to be the name of the folder that we're going to save that graph to so let's see I said they're just deleted stuff I'm not sure if it's gonna update and you don't have to have a folder named new graph or whatever it's called already it'll create that file for you so it takes usually a couple seconds it looks like that's done let's see we got it over here so there we go we got the new file new graph so we got that which is good that we're almost there so now to download it off of Google Drive it's a good idea to zip the file so you can just run this it's just a model graph dot zip we're gonna words ipping this folder and we're gonna call it model graph that zip from the folder new graph pretty simple just run that takes a second again it's really faster to zip it this way than it is to go into here and zip it for some reason I tried both and so much faster does it fit on here so we'll see it's probably update in a moment but we should get up there it is our model graph dot zip is right there so perfect but if you wanted to do this if you were doing this on the command prompt instead like I was doing it's clear the screen real quick so if we were doing it on herobrine another command is really similar it's actually the same command it's just the export graph so you can just run it's the same exact file or full of not file command so again make sure you just update that checkpoint file and you can name the graph whatever you want to do it's the same exact thing and so we don't have to zip it if we're on the local PC because it's already just in a four we can use already so now I got a reorient myself so let's download it I probably actually have it downloaded already maybe not okay let's download it then some model graph let's download it that's just saying it's too big to scan for viruses which is fine cooking once that means I do have it let's just move it to where we were in this model is such a big directory object detection so then we can just paste it right into here or say to bring got save and we can go into detection so then we have a model graph right here move on do would you just want to extract it so then we're put it so then we have the folder right here in new graph which is what we want it's good so that's everything we need right there so there you go we trained our model and now we're ready to use it for our webcam if that's what you want to do so let's pull that up hopefully it works on here because it's always fun everything goes to plan so there are some things we're going to need to update in our file actually so that's open this file on a project so this is the same file that we used last time when we use the original cocoa data step so there are some things we can edit actually I'm thinking about making a new file um yeah let's let's make a new file why don't we I'm gonna copy this actually might have one on No okay so we're isn't make a copy of it so let's just rename it custom model webcam so something like that I'll put it in the github and I'll make a note if I end up changing the name of it because I that sounds kind of dumb not gonna lie so actually let me pull it up here my charm so custom model webcam okay so what we're gonna change is right here so model name so this is what you would normally use so instead of this we want to change it to the name of our folder that we have which in our case is new graph so we don't need these two lines right here because this is used to download the files download the model sorry this we need this we need but we're going to change this so we're going to change to where that label is where we have our PB txt so we did put it in data so we can keep it like that so we call it object oops detection dot PB txt again this is just a folder name of where we kept it so you can change that number of classes again how many classes we have we just have one so we're not downloading our model so we don't need any of this and let's see is there anything else we gotta change don't think so this is if you want images let's see if it'll run hopefully don't run it's my webcam it's plugged in see what happens right so hopefully it'll go I had to have a face mask next to me that we can try to write so there we go there's me so you can probably see two of me right now we should see the Lighting's kind of bad in here but you should be able to see it there it is look at that cloth mask look we did it can lights kind of bad sorry but you can see that's the math might be kind of hard to see me now yeah we have our mask that's great that's basically all there is to it and you can set this up to work with the image detection also it's really similar I might add another file to do that later but that's basically the whole setup congratulations you've trained your own model so again tensorflow to does added support for object xyn so it does work a little bit different than this there's some different files so I'm gonna try working on that myself and if I can make some progress I'll make an update video on here too but other than that that's gonna be it for me I hope you found this tutorial Cirie's helpful you can leave a like and subscribe and I'll see you next time [Music]
Info
Channel: Lazy Tech
Views: 70,315
Rating: undefined out of 5
Keywords: object detection, tensorflow, tensorflow object detection, tutorial, object detection 2020, python, cython, tf_slim, pycocotools, google, protoc, protobuf, matplotlib, webcam detection, AI, machine learning, deep learning, CNN, custom model, training custom model, artificial intelligence, custom object detection, google colab
Id: _gGI91BmIdk
Channel Id: undefined
Length: 29min 32sec (1772 seconds)
Published: Wed Jul 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.