Custom TensorFlow Model for Image Classification App | #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this exciting tutorial we will be coding our own tensorflow image classification model in python specifically a convolutional neural network for fruit classification and then in the next tutorial we'll actually deploy our neural network model that we coded in an android app for image classification please give the video a like and subscribe if you find it helpful there's a lot of fun ahead so let's get started we're going to be writing all of our code in google colab so we can go ahead and search for google collab and visit the website collab provides us with free jupyter notebook environments that run entirely in the cloud and they even provide us with gpu instances so that we can make our training faster i'm going to go ahead and click on new notebook to create a new notebook and now i have a place where i can write some code i'm going to call this notebook fruit classification tensorflow model for android app the first thing that we're going to have to do is get our data set of fruit images for specifically apples oranges and bananas into our google colab fortunately i made all this easy all we have to do is use the wget command so type exclamation mark wget and if you go to the video description i provided a link to this bitbucket fruits zip file where it contains our data set so running this command by clicking the run button over here will allow us to have our data set downloaded into google colab that way we don't have to upload it or do anything like that and once again this link is in the video description to make it easy and as always all the code for this tutorial both the starter code as well as the complete and finished app is provided in the video description if you find the repositories useful please make sure to give it a start now you can see that it's downloaded our zip file which contains our data set fruits.zip and if we go over to the files section over here you can see we have fruits.zip over here the next step is to actually unzip this dataset that way we can take a look at the images inside so once again we're going to type exclamation mark unzip and fruits dot zip and you can press the run button over here or hold shift and press enter and this will run the cell you can see that it's unzipping all of these files and soon over here once we press the refresh button this refresh button here you can see that we have a fruits folder inside of this roots folder i've already prepared the data set for us so we have training testing and validation folders for our neural network about 70 percent of the images for apples bananas and oranges are in the training folder and we can go ahead and look at how this is organized inside our trading folder we have apple folder and then we can click on these images to see pictures of apples so this is how our data set is organized uh similarly if i go inside the test folder we can have apple banana and orange folders inside the orange folder we have some images and this is what our dataset looks like so now that we have our dataset prepared or at least the folders organized we're ready to go ahead and start doing our imports the first thing that we're going to do is import tensorflow as tf and we're also going to import matplotlib which will allow us to view our images inside our actual notebook rather than going to the folder over here i'm going to import this as plt that way we don't have to do as much typing i'm also going to import i think this is good for now so we'll go ahead and run this cell and now i'm going to use this function tf.keras.utils.imagedataset from directory to load these images into a variable that we can use for training our models so first i'm going to start by creating a variable for our training folder and inside over here inside of this function i have to provide some parameters the first parameter i'm going to provide is the path to our training folder because what i want to do is load all of these images inside our training folder into this variable here and that's why i'm going to provide fruits slash train as the path i also have to provide the image size so i'm going to go ahead and create some variables for my image height and image width image height and image width we're going to have these images whatever right now they're in all sorts of different sizes we want to resize all of them into 32 by 32 images that are all the same so our neural network can train on the images of the same input size and dimension so now that i've provided the width and height over here i'm going to type image underscore size equals image height comma image width and for my next parameter i can specify the batch size so this is how many uh images that our renewal network will train on before it updates its parameters and i'll just specify 20 for the batch size for right now so i'll say batch size equals batch size and now i have my variable for my training data set so let's go ahead and run this and see what happens so you can see it said that it found 460 files belonging to three classes our three classes are the apple banana and orange and if you were to count all the number of images in each of these folders for apple banana and orange it'd be 416. let's go ahead and do the same exact things for our validation set as well as our test set so i'm going to copy and paste this i'm going to rename these variables valid ds i'll call it val underscore ds and test underscore ds we've got to change the path so food slash validation for valdius and fruitslash test for test ds now let's run this and see what happens so you can see it's printed out three lines on the first line print out 460 files belong to three classes that's for a training set next it printed out 65 66 files belong to three classes that's for our validation set and finally 130 files belonging to three classes for our test set and if you're new to machine learning typically what this what this means is that our machine learning model is going to train on our training data set and while it's training on these images it's learning from these images we're going to evaluate it on our validation set and this is basically to see how our neural network does on images it hasn't seen before while it's going through the training process and finally we're going to evaluate our models once our model is completely trained we're going to evaluate it on our test set so that's why we have these three different data sets approximately 70 for training 10 for validation and 20 for our testing so uh enough talking let's go ahead and take a look at what our images look like so i'm going to create um i'm going to create an array for my class names first off we have apple then we have banana and then we have orange and using the matplotlib python library plt what we're going to do is create a figure and i'll say fixed size equals 10 comma 10. now what we're going to do is we're going to iterate over some images we're going to take in order to actually get a batch of images from our training data set so remember our batch size is 20. in order to get a batch of 20 images what we're going to do is say train ds.take one and we can actually iterate over this so i'm going to say four images comma labels in train ds.take one and um this is going to give me a list of images and a list of their corresponding labels so what uh what fold they belong to i'm going to iterate i'm going to display nine of them in a three by three um grid so that's what i'm going to say for i in range nine ax equals plt dot subplot three comma three comma i plus one and now to actually show the image i'm gonna use plt.i am show that function and we have our images list over here so i'm gonna say images of i then i'm gonna cast i'm going to make it get a numpy array from that and say as type uint 8 i'm also going to print a title for each of these images so this is actually displaying the image but let's provide a title as well let's display what let's display the name of the fruit above the image so that's we're going to say plt.title we have our class names array over here so i'm going to say class names and then labels is a list of the labels it's a list of zero one and two zeros ones and twos telling us what fruit this is so that's what i'm going to say class names of labels of i and i can just say plt.axis off and now when we run this there we go you can see that it's displaying a three by three grid of nine fruits and that's pretty cool we can see oranges bananas and even though all these images started off at different sizes we've rescaled them to 32 by 32 images so you can go ahead and run this cell a couple of times if you want to see what the data set looks like you can see that it's displaying different images now now that we have our data set ready to go the next step is to create our model so i'm going to create a variable called model and then i'm going to say tf.keras that's sequential and what this allows us to do is add layers to our neural network and we can provide a list of layers so that's why i'm going to type parentheses followed by square brackets and inside over here is where i can start providing my layers but the first thing that we want to do is actually a bit of pre-processing right now we have these images and each of these pixel values for rg and b go from 0 to 255 we're going to rescale that to to be between 0 to 1. that way there's a smaller range of values for our network to learn so that's what i'm going to say tf.keras.layers dot rescaling 1 point and then divided by 255 and this will handle doing all that division for us so once again we'll do is we'll go through each of these pixels and divide it by 255 so instead of being from zero to 255 it will be from zero to one and this is our basic pre-processing layer and then now we're actually ready to start adding the main layers of renewal network the first layer that we're going to add is a convolutional layer and if you're unfamiliar with convolutional neural networks i'll provide a link in the video description where you can read more about them because the focus of this tutorial is how to create a machine learning model in python and then use it in your android app but essentially what the convolutional layer does is apply filters to images to learn certain features and in this case i'm going to have it apply 32 filters with a kernel size of three by three and my activation going to be the relu our model is going to have several convolutional layers that ray can learn to extract features and distinguish between apples oranges and bananas in between these convolutional layers we're going to have max pooling 2d layers and what these will do is they'll down sample the size of these feature maps specifically the max pooling 2d layer looks at every 2x2 patch of pixels in our image and or in our feature map and it takes the largest of those pixel values and that's what's going to be chosen so essentially it's cutting down our feature map size by four times each dimension is being cut down by half so we're going to have this convolutional layer here i mean our pooling layer and let's just go ahead and copy this a couple of times that way we can have three convolutional layers what these convolutional layers do i forgot to mention is they also love our model to do this learning process while retaining the 2d structure of these images so right now these images are still in that 2d structure 3d if you want to count the number of feature maps and after having learned these features from the convolutional layers we're going to have uh we're going to flatten out um all of these inputs in our model so that's we're going to use the flattened layer so now it's one dimensional and with the flattened layer after have after flattening it out we're going to create a layer so layers that is dense or just has neurons it's going to have 128 neurons and once again we're going to use a rayleigh activation function and finally after having these 108 128 neurons in our last layer we're going to have the number of classes that our model is trying to classify which in our case is going to be three so this is what our neural network will look like it's a basic neural network you can make it more complex and play around with it if you want but essentially to recap we have this rescaling layer to pre-process our images and get it to be each pixel will be from zero to one we have three convolutional layers and three max pooling layers that retained a 2d structure learn features and down sample the feature maps then we flatten it all out have a dense layer with 128 neurons and finally our last layer in the neural network has three neurons one for each of the classes so let's run this cell and now we have to compile our uh model we have to compile it with an optimizer so i'm going to use that atom optimizer the way that neural networks work is as they're learning they're trying to minimize a loss function the last function that we're going to choose is because we have three neurons in our last layer we're going to use a loss function called sparse categorical cross entropy so i'm going to type dot losses and then this the loss i want is sparse category cross entropy and we're going to say from logits equals true in addition to computing this loss at every uh after every epoch or iteration over our data set i also want our um while our model is training i also want to receive the accuracy so that's what i'm going to add another parameter here called metrics inside the square brackets we can provide a list of metrics we want our neural network to calculate while it's training and i'm going to specify accuracy we're ready to compile our neural network everything's good after a model is compiled we're actually ready to train it so the way that we do that is by using the fit function so fit we're going to fit it or train it on our data so remember we have that variable train ds i'm just going to scroll up really quickly remember trained yes contains all of our images from the fruits and then training folder which is about 460 images so modeler fit trained yes i'm also going to provide it with validation data so it's not going to learn from this data instead it's going to be evaluated on this validation data that way we can see how it does on this data set while it is learning so we can say wow ds and for the number of epochs or iteration over our data set i'll specify 10. so it'll go over this whole data set over the whole training data set 10 times and learn from those images so let's go ahead and run this and let's see our neural network learn so you can say epoc 1 out of 10 it has an accuracy of 59.13 on the training set this is the accuracy over here on the validation set you can also see that our validation loss is decreasing once again our loss function is the sparse category categorical cross entropy and one thing that can help you help this code run faster is if you go to runtime or here in the menu select change runtime type and i should have selected gpu accelerator from the beginning this would have made the whole training process faster unfortunately i forgot to do that so if it's not too late for you i would select gpu click save and it would just make this whole process faster but now you can see that our model has finally finished uh training its validation loss decreased increased a little bit but now it finished at 0.10 and we have an accuracy of 97.83 on the training set and 96.97 on a validation set so it looks like our model hasn't over fit too much on the data set which means that it's like pretty much memorized with the training data as it looks like and this is great these are great results great accuracy for a fairly basic convolutional neural network let's go ahead and evaluate our convolutional neural network now on the test set so it's never seen these images before it hasn't trained on them it has it hasn't been evaluated on any of these images let's go ahead and see how it does so we'll type model that evaluate and inside we'll pass in our test data set variable which contains all of our test images let's run this a 93.85 accuracy that's pretty good uh let's see how many images we had we had 130 images so it was able to out of 130 images it correctly classified 93.85 percent of them now that we have a fully trained model the last thing that i'm going to show you is how to actually use it to make predictions on images and receive those predictions so over here copied some code that we had earlier all this does is it's going to take one batch from our test data set and get us the image and labels from the test data set and all that we have to do to get a prediction from our model is just a model and pass in the list of images so in our case we passed in a list of 20 images to our models we got the classifications and you can see that i'm printing it out for every single image that it needs to predict it's going to give us an array of size 3. and array tells us the likelihood that it's an apple a banana and then an orange and the index with the largest number is what our model thinks that that image is so in this case this first image had the biggest number um 4.04 so i thought the first image was an apple over here i thought the second image was going to be an orange the third it also thought the third image was going to be an orange and the reason for this is because if you look at our neural network the last layer is a dense layer with three neurons so that's why we're getting three outputs the likelihood of it being an apple banana and orange and now copied over this code that's going to display the images in a 3x3 grid along with their predictions and actual labels and this is really cool you can see that our model thought that this was an apple and actually was an apple it thought that this was an orange and it actually wasn't orange and the way that this works is once again we're feeding these images into our model and getting the classifications numpy is a library that provides a function called arg max that basically gives us the index with the biggest number and remember our classifications has that array of size three with the likelihoods so that's why using numpy.org max to get the index with the biggest likelihood and then all we're doing is just printing that out so to conclude in this tutorial you learned how to create your own convolutional neural network in python for image classification specifically for classifying between apples bananas and oranges in the next tutorial we're going to go ahead and actually take this neural network that we coded over here in python and we're going to deploy it in an android app that way users can actually take pictures on their phone or upload pictures and have the neural network running on their phone identify this as an apple an orange or banana there's a lot of exciting stuff ahead so please make sure to subscribe and get notified when that tutorial comes out i put a lot of effort into these tutorials so please make sure to give them a like and share them with friends as always both the code to get you started with this tutorial and the code for the complete finished app will be linked in the video description until the next tutorial happy developing from ij apps i hope to see you there
Info
Channel: IJ Apps
Views: 33,072
Rating: undefined out of 5
Keywords: IJ Apps, android, machine learning, tensorflow, tensorflow lite, tensorflow android, image classification, tensorflow image classification, android image classification, image classification android, android tensorflow lite, tflite, tflite image classification, tflite tutorial, tflite app, python, custom TensorFlow model, python machine learning, computer vision, tensorflow convolutional neural network, google colab, jupyter notebook, convolutional neural network, neural network
Id: ba42uYJd8nc
Channel Id: undefined
Length: 20min 39sec (1239 seconds)
Published: Mon Jan 31 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.