Train Neural Network by loading your images |TensorFlow, CNN, Keras tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my channel today's video is pretty special I am going to load some of my previous images from Google photos and we are going to train a neural network for my mood either I'm happy or not happy the guys after completing this video you will be able to train a neural network with help of your own images and predict the mood like you are happy not happy smiling not smiling so was the video till the end so that you will have complete idea about neural networks and image processing and if your first time visiting to my channel don't forget to press the bell icon and the subscribe button so first of all let's create a folder for this project so I'm going to write computer vision so this is our folder inside this one we will create some of the folders like base data so inside this base data we will keep three folders one for training other for testing and one for validation inside this training folder let's create two folders where we will keep my happy photos and another one where I will keep not happy photos and I'm going to copy and paste these two folders both in my training and in my validation so I'm going to jump to my google photos and collect some of my photos from there and save it over here [Music] so basically why I have created these happy and not happy folder because I'll be utilizing a Karass library which can generate labeled based on the folder name so if whatever image I will save inside this happy folder those all images will be labeled as happy and other images will be labeled as not happy going forward in this video I will explain these all things in the code I have copied all the photos and this is how the happy photos looks like okay guys so so I don't want to invest too much time to collect a lot of photos so what I will do I will train my model and then I will give all my other photos through this model to classify happy and unhappy photos okay in that way whatever photos will not work well or strongly classified we will push those photos to our training so that our model will become more robust okay so these all are happy photos and these all are unhappy photos let's jumped over Jupiter notebook let's import the basic libraries and a matplotlib see we do who is one specific library I wanted to discuss in depth is our image data generator so guys this is the library which is utilized to generate the labels for these images so whatever images are saved inside happy folder it will automatically give a label to them ok so I will explain all those things going forward in the code meanwhile I will also import image as an alone so let's do some beautification of the code so that long lines I am going to keep on top and short one on the bottom so first of all let's see how our image looks like so I'm going to load my image now I have to provide the part of this image let's let's call this image so this is the image so actually this is an RGB image guys and you can see that 3 dimensional matrix with help of CV 2 dot image rate and inside that you can give this path okay this is how the 3 dimensional matrix if you want to see the shape of this matrix you can just write down shape so guys we have 430 points as in height and 333 388 point 7 width and RGB 3 colours that makes this matrix 3 dimensional okay let's generate our training and validation data set with help of image data generator so you have to initiate this class so train I will instead train is equal to this image data generator we see this matrix these values of RGB ranges from 0 to 255 okay so I want these ranges to be 0 to 1 so I will divide every single element inside these matrix by 255 okay so let's do it so you can give the rescale over here so it will automatically divide your images by 255 now you also want to initiate one for your validation okay we have initialized two classes for your training now let's convert your training images to a data set that can be fitted to your neural network so this object only you have to give so for generating your labels from directory you have to build flow from directory function inside this class okay so so you can give flow from directory so now inside this you have to give the path of your training giving this relative path over here since you cannot give multiple sized matrix to your neural network so you have to resize your images so this function can also resize your images to a given size so we have to just give target size and you have to give the target so I wanted to make this 200 cross 200 okay so all these images will be resized to this 200 cross 200 cross 3 matrix so after that you have to get the batch size so these many images will be given to neural network at first and in the batches your neural network will train up so batch size since we have very few number of images and we are just experimenting so I'm going to give our knit 3 as on that side so after that you have to get the class mode so guys we can have multiple type of modes but here you have only two labels like happy and not happy so you are giving binary so you can see that it has already found out your 37 images and it has two classes I am going to paste this code and generate the same thing your validation data said you have to give the same path for your validation so we will go back and we will take this one if you want to see the labels and the data generated with these function you can easily do it with help of this so this class indices will give that you are not happy got converted to zero and happy got converted to one so guys neural network cannot take like string type of things as an Y or the prediction so it converts all the classes available inside your data set to a numerical value so you can see that all those images which were not happy have a label 1 and all other images have a label 0 so we have 11 images in our validation data set and this is guys very few number of images to train you in a neural network actually you need huge amount of images but we are just experimenting so I thought to like start with this one only once you have defined your training and validation data set you have to define your model so we will be utilizing a convolutional neural network over here with max pooling or we have to give TF dot there us third models dot sequential and all the layers are given in a form of list guys so we will give a list over here one by one we will provide our layers let's define a conversation layer over here so we'll give Conn 2d and after that you need to provide how many filters you wanted to keep inside this layer so I will keep around 16 filters now and after that you need to provide the size of your filters and then we have to provide the activation function so I will be utilizing value then you have to provide the input shape we have defined 200 cross 200 so that's what is our input shape 200 cause 200 and we have RGB so it's a three dimensional matrix okay so is a simple convolution layer so in this way you define a convolution layer after that I'm adding a max pooling layer and this will have a size of 2 cross 2 we know that max willing just takes the max Excel out of given number of pixels let's define another condition layer so I'm just going to copy it from here to here I'm going to increase the number of filters to 32 you do not have to provide the input shape so this is OK and then we are all again going to provide another convolutional layer so guys if you see over here I'm increasing the number of filters going downward Network oh I am doing it to increase the number of channels so if you are not familiar with what our number of channels so you can check out the video in the description you will get complete information about number of channels after that we will flatten this out and we will apply to dense layer over here and we'll keep activation is equal to Sigma so if you are going for a multi class type of classification you can give soft mix since we have only binary though I am just giving a sigmoid okay it have worked and these models is perfectly working now let's compile our model so since this is the classification type of so we'll keep los as in binary cross-entropy and we have already imported the rmsprop optimizer sorry I should put a comma so optimizer you need to provide over here you can put atom also so I have tested it with rmsprop I am utilizing this one then you have to provide the matrix on which you want to optimize it and that is your accuracy and so that is because of these brackets that's correcting this one awesome so our model compilation is good now let's fit up the model model fit is equal to model thought fit you have to give your train so you have these as an training data set train data set you need to provide then you have to provide within one epoch how many step you want to keep so I will just keep very less number of step and how many pock you want to keep so let's keep ten epochs so epochs are like how many iterations you want to give to your optimizer you can have something called as callback function with help of these callback you can stop your I trations once you will reach to the desired accuracy like I can say my model stopped at 90% accuracy so that can be done the callback now you need to provide your validation data so it's running and you can see you are getting good accuracy and it's running pretty fast because we have very less number of images and right now we are being able to get only 66% accuracy so it it means we need to increase the box I will give 30 box right now we are getting good accuracy over here you are reaching to 90 percent of validation accuracy and 61 93 okay this is going pretty good let's see how much better we can preach oh this is awesome you have been able to classify every single image guys you have like hundred percent accuracy this is pretty awesome guys so what I will do like this is this is getting 100% accuracy guys because we have very less number of data points okay so both validation and in the training data set is a hundred percent so let's check how good this optimizer works on the testing data set okay so I'm going to give the first create something some function to test out all those images so let's do this di path is equal to so these are all the images I kept for tests and this was our tested a set path after that we will read every single image from this folder and then we will check how my model is classifying those images okay before I in who is thoughtless dir and let's see it is printing up your various okay awesome so first load our image and you have to give your pod plus your eye and there should be some slashes because you want to read those files now let's show these images PLT dot I am show so this is a single image actually showing up okay I need to keep LT dot show so that it will show up all the images so these are the images guys these are my images as you can see this is where I am looking up happy this is sad this is sad this is okay okay this is happy let's see how model gets these images so first of all we need to convert these images to our array ms2 array and there you need to provide your image so this is your X since you are not utilizing that data generator over here so you need to provide an additional dimension to your data set otherwise it will throw up error so we can do it with help of explained dimension of numpy so if you have multiple images you want to vertically join all those images so that can be done with help of NV dot we stick you have to just provide acts or here you have to give model dot predict and inside that you have to pass on your image so we have not happier than zero and happy as in one so let's take a native way model dot predict and save this as inside something some value of VL and if value equal equals to 0 it means print you are not happy else print you are happy ok let's see ok so it has thrown out some error if you will be wrong dimension so it will throw up an error so we have to resize your image over here that can be done by a simple target image target size that is by which you have trained it and that one is 200 comma 200 comma 3 so what do you have to give only 200 comma 200 awesome so as you can see that it is printing up you are happy you are not happy you are not happy over here you're not happy over here not happy over here you're happy over here you are not happy you're not happy you're not happy you are happy you're not happy you are happy so whereas you see that this is able to classify all my images died wherever I am happy and not happy so try this out with yourself guys so if you guys are new to my channel don't forget to press the bell icon in the subscribe button and I'll meet you in the next lecture bye bye guys take care have fun
Info
Channel: When Maths Meet Coding
Views: 115,700
Rating: undefined out of 5
Keywords:
Id: uqomO_BZ44g
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Sat May 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.