Loading in your own data - Deep Learning basics with Python, TensorFlow and Keras p.2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody and welcome to part 2 of our deep learning with Python tensorflow in Karros tutorial in this tutorial what we're going to be talking about is how to load in an outside data set the outside data set we're going to use is this cats and dogs data set from Microsoft it was for initially a kaggle challenge and the idea is to take pictures of cats and dogs and then identify them by feeding them through a neural network and having the neural network say whether or not that's a cat or a dog so go ahead and download that data set and then once you have that data set let me pull up an example here what you should see is like this so you should get two directories these two I've added these two things I've added in but what you should have is cat and dog and then in here you should have some like images of cats and dogs in this case bunch of dogs so each one has like 12,500 samples so you should have plenty of examples to teach a model what's a cat and what's a dog so go ahead and download those extract those and then we'll come over here and we will get to work so we're gonna import first of all numpy is and P we're gonna import Metz plot Lib dots pie plot as p LT we're going to import OS we're gonna import cv to and from and actually it's just CB - if you don't have numpy pip install numpy if you don't have an apple lib pip install matplotlib but if you don't have cv to you will need to do a pip install OpenCV - python alright once we have those basically I'm gonna use matplotlib just to show the image we're going to use OS to iterate through directories and join paths CB - tis show or I'm sorry to do some image operations and then numpy to do various array operations so the first thing we're gonna do is specify a data directory my data is located in my X files under data sets and pip images then we're going to have categories that two categories we've got to deal with here are dog and cat I can't leave those different and in fact we should probably double quote since we double quoted the other ones so now what we want to do is iterate through all of the are examples of dog and cat so the way we're going to do that is for category in categories what do we want to do well we're gonna say path is OS not path that join dated dirt and whatever that category is so basically this just gets our us into our basically the path to cats or dogs Durer now we want to say is for image in OS dot lister that path so it'll just be a bunch of these images basically whether it are named by just number once we have that we can just iterate through all of those images so the images are we can convert them immediately to an array with CV to dot M read and we can read those with OS that path join and we're going to join path and image now and that's the full path to that image then what we're gonna do is we're gonna convert it to grayscale so we read in that image and then we say C V to C V to M read underscore grayscale so we're gonna convert it to grayscale because one you know RGB data is three times the size of grayscale data and I just don't think that color is that essential in this specific task and a lot of identifying tasks it is but at least in the you know the difference between a cat and a dog yes they have a lot of color but does the is the color the differentiating factor we're doing cats and dogs I don't think so if it was like you know dog versus some reptile or something like that then yeah probably you'd want to bring in color okay so once we've done that the next thing that we want to do is like we can at least we can graph this and look at what we're dealing with just to make sure it's what we expect so peel t2m show image array c map equals gray again the only reason I'm using matplotlib here is I don't know how to do like inline in Jupiter notebook with CV - I'm sure there's a way if somebody knows it go ahead and leave it below then I'm just gonna throw a break here in a break just so we can look at this picture real quick so as you can see it's a grayscale image of a dog no surprise there it's kind of what we expected also our data this is what our data looks like image array okay just a bunch of numbers now what if we didn't convert it to grayscale so in this case you can see it's just a bunch of number you know pixel values here in this 2d array what if we didn't do grayscale though and what if we just did this blue dog now no longer is it 2d because these are actual RGB actually probably BG are I believe is how cv2 reads things and that's why this is all why the dog is a blue in the photo anyway we're gonna we are going to keep grayscale though and just take a note you know like what how that changes your your actual data so for example we could say image ray dot shape for example there and it's a 398 by 500 which brings us to the next problem we have which is you can even tell like some of the all these dogs are like different shaped photos some are landscapes some are portraits some are square and we really need everything to be normalized at all you know if at all possible there are ways to have variable sized images to make classifications on but in the interest of keeping things as simple as possible we'd like to make everything the same shape so that's what we're going to do next now we have to decide on a shape so for example what if we say image size 50 so maybe we're gonna try every image is a 50 by 50 so the way we would do that is just new array equals CB to re size and we resize the image array and then we do image size image size and that's our new array and we really should look at it so em show new array and then see map for the color map oh my gosh can we get this please gray and then PLT not show so I can still tell that's a dog but eventually we can't write if I we do 10 make it a 10 by 10 I can't tell that's a dog I don't think anybody can we go with 20 still probably not maybe though you might be able to get away with it at 30 it's still pretty hard but now you can start to see like you know like the for armor like it's the wrist you know the area after the wrist the hand of a dog I guess I don't know anyways that's usually longer in dogs so I could make this classification at this point but you know at 50 I'm pretty comfortable but you do have to be careful because this dog takes up quite a bit of the image whereas some of these might not like for example I'm sure I can find one eventually this ones are pretty small he blends into the bench quite a bit most of these are pretty pretty well done actually but sometimes you know it won't it'll be smaller part of the image and it'll be a little harder when we make it a small image look at this camo dog anyway so keep that in mind I'm gonna I guess I'll stick with 50 we'll see how that goes so once you decide on the size that you want to go with let's go ahead and create our training data set so I'm gonna say training data equals an empty list and then I'm gonna start this function that I'm gonna call create training data and I'm gonna give myself I'm just gonna say pass I'm just gonna get myself some space here there you go okay create training data and what we want to do is now iterate through everything and build the data set so I'm just gonna take this copy and then come down here paste it in tab this over now the next thing we need to do is we are norm that we're kind of like in our Emnes dataset right did we we we have to map basically we got the features as numbers but our label or classification is not yet a number right we can't map things to a string dog or a string cat right we need to map things to some sort of numerical values so we're gonna say 0 is a dog 1 is a cat and the way we can do that is we could just get the index value of dog and cat and make that index value in that list the arbitrary classification it doesn't matter which one is the one in which one is 0 it's just you have to convert to a number so the we're gonna do that is I'm just gonna say class underscore num is going to be equal to categories dot index index lowercase whatever that category is so zero for a dog 1 for a cat now we're going to iterate over the images I we don't need to show them we don't need to break any more and all I want to do now is resize with this new array so we'll come down here we'll perform that resizing operation and I think that's about it so now we just want to append this to our training data list above there so training underscore data dot of hand and we want to append our new underscore array and whatever that classification is so class underscore num okay so we want to do that for everything the other thing we probably should do is for image we price it in case this and a try and except I've been through this data set before and some of the images are like broken so accept exception e and you know what I'm gonna do I am gonna I'm just gonna pass actually I already know that there's some that are broken you normally you would throw the exception so you could read it and figure out what's going wrong but I'm gonna go ahead and just pass but there's like three will get like an OS error and some other warning information and all that fun stuff but I'll just pass for now create training data and then running that and then whenever that's done let's go ahead and print print the Len of training data now a couple things we should talk about is the balance of your training data so it's really important that your training data especially like in the case of classification is properly balanced so in the case of a binary choice like we have cats and dogs you want to make sure you have fifty fifty right just as many cats in just as many dogs now sometimes you can have different numbers and then when you train the model you can inform the model and say hey these are our class weights so they have a weights that you can pass in the way this will work is it will eight the you know the loss a little little differently in an attempt to handle for your imbalance data set but if at all possible you definitely want to balance the data set instead so but sometimes like let's say you had so let's say you had a day so there was 75% dog 25% cat you feed that through the neural network in the neural networks gonna learn really quickly just always predict dog and you'll be 75 percent right and then when it tries to learn from there it's gonna have a really really hard time so so if you balance it so it's a perfect 50/50 you'll be better the next thing you want to do is shuffle the data so we've got the training data but as you can see the first thing we did was iterate over the category then go from there so everything's dot all dogs and then all cats well if you feed that to the neural network it's gonna learn okay only predict dog and then it switches over to cats and it's like I'm wrong I'm wrong I'm wrong okay cat cat everything's a cat and then it just keeps going back and forth it's probably not gonna learn very well that way either so we definitely want to shuffle the data so we can import random and then we can do a random not full there we go training data sense training is a list mutable there it is it's already shuffled at this point so for example we could now for I don't know sample in training data we we can check that our labels are correct by doing print sample one so this will be the label so sample zero with would be a whole we went through them all anyways sample zero would be unless there's two of to ten here would be the actual image array itself I don't want to run the whole thing all over again well we'll just wait for that that's fine so now let's do the let's take this data now and now that it's shuffled let's pack it into the variables that we're going to use right before we feed it into our neural network so that's going to be an empty list for X and an empty list for wine general capital X is your feature set lowercase Y is your labels sometimes you'll see a tray next sy and like all these kinds of things and we could we can split those up but we can still we can actually specify a validation set so you don't have to split them up sometimes you may do that anyways but you don't really have to do that so you can use built-in methods to to properly do an out-of-sample test okay so now we're going to say four features label in training data we're going to X dot append features y dot a pen dot append label so we build those out into lists and we really can't pass especially for the features we can't pass a list to the neural network I wish we could I think we should be able to especially if we're using a high level API like Kaos it's kind of silly that we still got to do this hopefully soon one day this will change but we need to convert both things well actually Y can stay lists but X has to be first of all an umpire array so let's go ahead and do that so x equals NP array X but also we need to reshape it to be negative 1 by the shape of each X so negative 1 is just how many features do we have well you can say negative 1 and that's kind of a catch-all it's anything any number and then we can say we know the shape of the data to be image size by image size you could also do like X 1 0 X 1 1 and so on but anyways we'll just go with this so image size by image size and then by 1 so this one is because it is a grayscale and just as a bit of a homework challenge for you guys I encourage you to attempt to make a confident to work rather than with grayscale work with color images so see if you can convert what we're going to build here from a greyscale convolutional neural network to B color so just as an aside that might be the hardest thing to remember to do is change that one to a three because then it would be three values so that's what we have to do again I think this reshape is kind of silly I wish somebody from hopefully Karros eventually would make this not be a requirement we should not have to deal with that shape it should just intelligently shavings there's always the same kind of thing like you always have to do this same step and it's silly ok so once we have our data uh-oh EXO when did we convert it first of all when do we define an x max not a pen when did it get converted when did we use an X I'm just tripping out I guess cuz I never ran this I don't know when we had an X so why how when was X a thing oh man someone's got to help me out with that one when did X become an umpire a before this was run did we ever use an X variable or maybe it's cuz I'm redoing this and X already did have a yeah so the first recording got screwed up that's what happened man that was killing me I was like wait a second so X I already was if I should have restarted the kernel anyway moving along okay the last thing we want to do is we want to save these so you don't want to generate your data like you don't want to like do all this crunching and reshaping all that every single time because chances are you're gonna be like tweaking your model so the reality of neural networks is that you don't have the answer right away or you're not working on a problem that's as easy as the one that we're about to be working on so it's gonna be it's not gonna be as simple as like oh just throw them all together use exactly these you know features and this many nodes this many layers you're not gonna know you're gonna be tweaking so you don't want to have to be rebuilding your data set every time so the last thing we're gonna do is we're gonna once we've created our training data we're just gonna import Pikul and then in some way you don't have to use pickle we could mm PI not save or whatever somehow save your data so you don't to redo it every time so pickle out equals open and I'm gonna open X dot pickle X down pick W be pickled dump we want to dump X we're to pickle out and then pickle pickle out dot close and then we want to do the same thing for the Y so I'm just gonna do why come down why and then later if we want to read it back in you can do something like this like pickle underscore in equals open X dot pickle RB and then or really know what kind of did an extra step there but anyway x equals pickled lode pickle in okay and then x1 for example is our image and then x so it'll be our feature basically so that would be our image and then Y one would be the label for x1 anyways that's all for now what we're gonna do is in the next tutorial we're gonna take this data set that we've you know compiled and then we're gonna feed it through our convolutional neural network after going over some of the basics of confidence and all that so if you've got questions comments concerns whatever something you think could be done better whatever feel free to leave them below otherwise I will see you guys in the next tutorial where we are gonna feed it through a neural network and hopefully get our correct classifications so till next time
Info
Channel: sentdex
Views: 657,973
Rating: undefined out of 5
Keywords: TensorFlow, Keras, Deep Learning, tutorial, neural network, machine learning
Id: j-3vuBynnOE
Channel Id: undefined
Length: 18min 50sec (1130 seconds)
Published: Sat Aug 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.