Image Preparation for Convolutional Neural Networks with TensorFlow's Keras API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey I'm Andy from beep lizard in this episode we'll go through all the necessary image preparation and processing steps needed to train our first convolutional neural network our goal over the next few episodes will be to build and train a convolutional neural network that can classify images as cats or dogs the first thing that we need to do is get and prepare our data set for which we'll be training our model we're going to work with the data set from the cackle cats versus dogs competition and you can find a link to download the data set in the corresponding blog for this episode on V Blizzard comm so we're mostly going to organize our data on disk programmatically but there's a couple of manual steps that we'll go through first so after you've downloaded the data set from cackled you will have this zip folder here and if we look inside then this is the contents we have a zipped train folder and a zip test folder along with this sample submission CSV so we actually are not going to be working with this or this so you can delete the test zip as well as the CSV file we're going to only be working with this train zip so we want to extract the high-level cats versus our dogs vs. cats at first and then extract the train zip so because that takes a while I went ahead and did that here so now I have just this train directory because I moved the test directory elsewhere and deleted the CSV file so now I have this extract a train folder and I go in here and I have a nested train folder as you can see I'm already in train once now I'm in train again and in here we have all of these images of cats and dogs ok so that is the way that the data will come downloaded the first step or the next step that we need to do now is to come into here and grab all of these images and we're going to ctrl X or cut all of these and bring them up to the first train directory so we don't want this nested directory structure instead we're going to place them directly within the first train directory all right now all of our images have been copied into our base train directory here so this nested train directory that the images previously belong to is now empty so we can just go ahead and delete this one all right so now our directory structure is just this dogs vs. cat top directory within we have a train directory and then we have all of the images within our train directory both of cats and dogs the last step is to just move the dogs versus cats directory that has all the data in it to be in the place on disk where you're going to be working so for me I am in relative to my Jupiter notebook I am within a directory called data and I have placed dogs versus cats here all right so that's it for the manual labor now everything else that we'll do to organize the data and then later process the data will be programmatically through code all right so we're now here within our Jupiter notebook and first things first we need to import all of the packages that we'll be making use of and all the packages here are not just for this specific episode on processing the image data but actually these are all of the packages that we'll be making use of over the next several episodes as we're working with CNN's all right so we'll get that taken care of and now just this cell here is making sure if we are using a GPU that tensorflow is able to identify it correctly and we are enabling memory growth on the GPU as well if you're not using a GPU then no worries as mentioned earlier you are completely fine to follow this course with a CPU only alright so now we're going to pick back up with organizing our data on disk so assuming that we've gone through the first steps from the beginning of this episode we're now going to organize the data into trained valid and test ders which correspond to our training validation and test sets so this script here is going to first change directories into our dog vs cat directory and then it's going to check to make sure that the directory structure that we're about to make does not already exist and if it doesn't it's going to proceed with the rest of this script so the first thing that it's doing as long as the directory structure is not already in place is making the following directories so we already have a train directory so it's going to make a nested dog and cat directory within train and then additionally it's going to make a valid directory that contains dog and cat directories and a test directory which also contains dog and cat directories now this particular data set contains 25,000 images of dogs and cats and that is pretty much overkill for the tests that we will be using these images for in the upcoming episodes we're actually only going to use a small subset of this data you're free to work with all the data if you'd like but it would take a lot longer to train our networks and work with the data in general if we were using the entire set of it so we are going to be working with a subset consisting of 1,000 images in our training set 200 in our validation set and 100 in our test set and each of those sets are going to be split evenly among cats and dogs so that's exactly what this block of code here is doing it's going into the images that are in our dog vs cat directory and moving 500 randomly moving 500 cat images into our trained cat directory 500 dog images into our trained dog directory and then similarly doing the same thing for our valid our valid our validation set both for cat and dogs and then our test set both for cat and dogs with just these quantities differing regarding the amounts that I stated earlier for each of the sets and we're able to understand which images are cats and which are dogs based on the names of the files so if you saw earlier the cat images actually had the word cat in the file names and then the dog images had the word dog in the file names so that's how we're able to select dog images and cat images here with this script alright so after this script runs we can pull up our file explorer and look at the directory structure and make sure it is what we expect so we have our dog vs. cat directory within our data directory here so if we enter then we have test trained and valid directories inside test we have cat that has cat images and inside dog we have dog images if we back out and go to train we can see similarly and if we go into valid we can see similarly and you can select one of the folders and look at the properties too to see how many files exist within the directory to make sure that is it is the amount that we chose to put in from our script and that we didn't accidentally make any type of error so if we go back to the dog sources cats the root directory here you can see we have all of these cat and dog images left over these were the remaining 23,000 or so that were left over after we moved our subset into our trained valid and test directories so you're free to make use of these in any way that you want or delete them or move them to another location all of what we'll be working with are in these three directories here all right so at this point we have obtained the data and we have organized the data now it's time to move on to processing the data so if we scroll down first we are just creating these variables here where we have assigned our trained valid and test paths so this is just pointing to the location on disk where our different datasets reside now recall earlier in the course we talked about that whenever we train a model that we need to put the data into a format that the model expects and we know that when we train a Karass sequential model that the model receives the data whenever we call the fit function so we are going to put our images into the format of a Karass generator and we're doing that in this cell here we're creating these trained valid and test batches and setting them equal to image data generator dot flow from directory which is going to return a directory iterator basically it's going to create batches of data from the directories where our data sets aside and these batches of data will be able to be passed to the sequential model using the fit function so now let's look exactly at how we are defining these variables so let's focus just on train batches for now so we're setting train batches equal to image data generator and dot flow from directory but first to image data generator we are specifying this pre-processing function and setting that equal to TF Kerris applications wgg 16 dot pre process input so I'm just going to tell you for now that this is a function that is going to apply some type of pre-processing on the images before they get passed to the network that we'll be using and we're processing them in such a way that is equivalent to the way that a very popular model known as vgg 16 we're processing our images in the same format as which images that get passed to this vgg 16 model our process and we want to talk about more about this in a future episode so don't let it confuse you now just know that this is causing some type of processing to occur on our images and we'll talk more about it in a future episode and not stress on it now because it's not necessarily very important for us right at this moment the technical details of that is at least so besides that when we call flow from directory this is where we are passing in our actual data and specifying how we want this data to be processed so we are setting the directory equal to train path which up here we defined the location on disk where our training set was under the Train path variable and then we're setting target size equal to 224 by 224 so this is the height and width that we the cat and dog images to be resized - so if you're working with an image data set that has images of varying sizes or you just want to scale them up or scale them down it this is how you can specify that to happen and this will resize all images in your data set to be of this heightened width before passing them to our network now we are specifying our classes which are just the classes for the potential labels of our data set so cat or dog and we are setting our batch size to ten we do the exact same thing for the validation set and the test set everything is the exact same for both of them except for where each of these sets live on disk as being specified here under the directory parameter and then the only other difference is here for our test batches we are specifying this shuffle equals false parameter now this is because whenever we use our test batches later for inference to get our model to predict on images of cats and dogs after training and validation has been completed we're going to want to look at our prediction results in a confusion matrix like we did in a previous video for a separate data set and in order to do that we need to be able to access the unshelled labels for our test set so that's why we set shuffle equals false for only this set for both validation and training sets we do want the data sets to be shuffled all right so we run this and we get the output of found at 1,000 images belonging to two classes and that is corresponding to our train batches found 200 images belonging to two classes which corresponds to valid batches and then the 100 belonging to two classes corresponding to our test batches so that is the output that you want to see for yourself that's letting you know that it found the images on disk that belong to both the cat and dog classes that you have specified here so if you are not getting this at this point if you get found at 0 images then perhaps you're pointing to the wrong place on this you just need to make sure that it's able to find all the images that you set up previously alright and here we are just verifying that that is indeed the case now next we are going to just grab a single batch of images and the corresponding labels from our train batches and remember our batch size is 10 so this should be 10 images along with the 10 corresponding labels next we're introducing this function plot images that we're going to use to plot the images from our train batches that we just obtained above and this function is directly from tensorflow website so check the link in the corresponding blog for this episode on D poster comm to see to be able to get to a tensor flows site where exactly I pulled this off of so we will define this function here alright so now we're just going to use this function to plot our images from our test batches here and we're going to print the corresponding labels for those images so if we scroll down we can see this is what a batch of training data looks like so this might be a little bit different than what you expected given the fact that it looks like the color data has been a little bit distorted and that's due to the pre-processing function that we called to pre-process the images in such a way that in the same type of way that images get pre processed for the famous bgg 16 model so like I said we're going to discuss in detail what exactly that pre processing function is doing technically as well as why we're using it in a later video but for now just know that it's skewing the RGB data in some way so we can still make out the fact that this is a cat and this looks like a cat this is a dog dog dog dog cat yes so we can still kind of generally make out what these images are but the color data is skewed but don't worry too much about the technical details behind that for right now just know that this is what the data looks like before we pass it to the model and here are the corresponding labels for the data so we have these one hot encoded vectors that represent either cat or dog so a one zero represents a cat and a zero one represents a dog ok so I guess I was wrong earlier with thinking that this one was the dog this one is a cat because as we can see it maps to the one zero one honk Oh ting and if you don't know what I mean by one hot encoding then check out the corresponding video for that in the deep learning fundamentals course on beep lasercom but yeah we can see that zero one is the vector used to represent the label of a dog so this one's a dog and the next two are dogs as well this one and this one now just a quick note about everything that we've discussed up to this point sometimes we do not have the corresponding labels for our test set so in the examples that we've done so far in this course we've always had the corresponding labels for our test set but in practice a lot of times you may not have those labels and in fact if we were to have used the downloaded test directory that came from the cackled download then we would see that that test directory does not have the images labeled with cat or dog so in this case we do have the test labels for the cat and dog images since we pulled them from the original training set from catechol that did have the corresponding labels but if you don't have access to the test labels and you are wondering how to process your test data accordingly then check the blog for this episode on the poster com I have a section there that demonstrates what you need to do differently from what we showed in this video if you do not have access to the labels for your test set all right so now we have obtained our image data organize it on disk and it processed it accordingly for our convolutional neural network so now in the next episode we are going to get set up to start building and training our first CNN by the way we are currently in Vietnam filming this episode if you didn't know we also have a vlog channel where we document our travels and share bit more about ourselves so check that out at people's our vlog on YouTube also be sure to check out the corresponding blog for this episode along with other resources available on deep loser comm and check out the people's or type mine where you can gain exclusive access to perks and rewards thanks for contributing to collective intelligence I'll see you next time [Music] [Music]
Info
Channel: deeplizard
Views: 68,868
Rating: undefined out of 5
Keywords: deep learning, pytorch, cuda, gpu, cudnn, nvidia, training, train, activation function, AI, artificial intelligence, artificial neural network, autoencoders, batch normalization, clustering, CNN, convolutional neural network, data augmentation, education, Tensorflow.js, fine-tune, image classification, Keras, learning, machine learning, neural net, neural network, Python, relu, Sequential model, SGD, supervised learning, Tensorflow, transfer learning, tutorial, unsupervised learning, TFJS
Id: _L2uYfVV48I
Channel Id: undefined
Length: 18min 31sec (1111 seconds)
Published: Thu Jul 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.