Hello Python programmers, this is AK. In this video we are going to do one deep learning project which is able to classify the fruit images. I used one popular dataset for this problem that is named as fruits360.
It is one of the popular dataset in Kaggle. Why I selected this dataset means it has wide range of fruit variants. So when you include high variants of fruits in your deep learning model, you will get good results at the end of testing your model.
The dataset that I used here is a very heavy weighted one. because its size is about 750 megabytes it means we need a high amount of GPU and processing power for training and testing this entire data set. I already did this project it nearly takes 1.5 hours for training and the accuracy that I got from this project is 85%. I made totally 50 approaches and relatively I got good accuracy in my initial attempt.
If you want good accuracy you need to perform the hyper parameter tuning and do some adjustments in my current parameters. and you will get definitely good accuracy than me okay so this is the process we are going to do in this video watch this video fully otherwise you won't understand the concepts that i'm explaining in this video okay so before learning something from this video do like share comment and subscribe these things will help me to produce more useful contents in python and if you want to reach me out personally then this is my instagram id you can ask any doubts about python and if you need any assistance for your project you can ping me on instagram i will definitely help you okay So let's start this video. First of all, this project is a kind of big scale project.
We are going to load our custom dataset from the Kaggle and after that we need to make some adjustment on that. So if you are a beginner in deep learning, I recommend you to watch the previous video that I made which is based on the deep learning. It will help you to understand the basic things in the deep learning.
So I already did this project. So I'm not going to type any code. It takes so much time to type everything. Rather than I will try to explain all the stuffs clearly with some cool real-time examples.
With these examples, you will easily understand what I did in this project. Okay, so if you are doing any project in deep learning, you need to follow this template. Or you can consider this methodology as a flow chart for creating a deep learning model.
The first step you need to import the record libraries for your model. Generally in python, There are two possible ways of doing deep learning. The both two are very popular approaches. The first one is the TensorFlow and Keras libraries approach and second one is the PyTorch approach. Basically the TensorFlow is a Google product and it has large community support than PyTorch.
For example, if you face any kind of error in this project, what will you do? You just go to Google and search about the particular error that has occurred in your program. Mostly in Stack Overflow or GitHub, you will easily find the solution. if you use the tensorflow approach.
If you are using any other libraries in deep learning, it is very hard to find the syntaxes and solutions on other approaches. That's why I'm saying most people are using the tensorflow and Keras for doing the deep learning models. First process is you need to import all the necessary libraries for doing this project.
So look at this. Here I imported some libraries. Probably if you're a new viewer of my channel, you don't know about some libraries that I used here.
And in this video, I'm going to use the convolutional neural network for this classification project. And if you don't know about this, here is the pre-requested video that I made for you. I clearly explained the CNN and its parameters. So it will be very helpful for you to understand this video.
So after importing the libraries, we need to provide the dataset to our deep learning model. For that you need dataset. As I said, I already downloaded the dataset from the Kaggle.
I will provide the link in the description so you can easily find the dataset through my link. So after downloaded the dataset, you can see here it is our dataset. So it is in the folder of archive and in the archive folder, you can see other two subfolders which are train and test.
So in the dataset, we are having two folders. The train folder purposes, we only use that data for training the deep learning model and the test folder purposes for checking and evaluating the results of our deep learning model. So in the train folder, we are having 131 categories of fruit variants.
Each folder contains the different type of images of a particular fruit. Now in the test folder have the same replica of the train folder. Both data has 131 categories but the difference is in the train folder has high amount of data.
The train folder has 67000 number of images which belongs to 131 categories and the test folder has 22000 number of images which belongs to the same 131 categories. So in the previous video of convolutional neural network, I used the binary classification technique which means our model will predict the given input image is masked image or not masked image. So in that video I told you I will do the project on multi-class classification using the convolutional neural network.
So in this video we are going to deal with 131 classes of data. So this is called the multi-class classification. Now what we are going to do is we are going to create two variables one is train path and other one is test path.
So look at my code I created those two variables and I gave the path of the respective two data folders. So we finished the two blocks of this template and third one is the data insights. So in this part we are going to check how the data is presented and its dimensions.
So I just visualized one of the images from the training part. So this is how the data looks like. Next we need to find out the dimensions of the every image in the data set.
Most probably all the data will be on the same size. So I cross checked the data while downloading from the Kaggle. They clearly mentioned everything is in the same size.
So for checking the dimensions of the image you need to type image to array method and you have to pass the image variable in this method. So when you run this pot of code you can get the dimensions of the data. So this image contains 100 cross 100 ratio that means 100 width and 100 height values and it also provides another value that is the 3. The 3 indicates that this particular image is a three channel type of image.
Here the channel means the color combination. So this image contains the combination of red, green and blue colors. Okay so now we are going to build our neural network layers.
As I said I already completed all the coding parts so look at my code. I already had a CNN structure. Behind the each code there will be a lot of mathematics are present in that.
I already explained it on this video. So you can get all the mathematical explanations on that video. So in this CNN structure you may notice one thing which is differ from the previous CNN video.
That is before the step of fully connected layer I added one code which is the model.drawport. So if you have the dataset right we are having two folders train and test dataset. Sometimes the neuron extract the same number of features from the multiple data. So this will happen when the multiple neurons faces identical data patterns.
So in that time it generates same results and it will cause too much of computation for calculating the output and it leads to wastage of machine resources. So if many neurons extracting the same feature it reduces the quality of the feature and that will bring you the overfitting problems. So when you add this dropout layer it will randomly shut down some neuron values.
It means this method will divide the value 0 with the selected random neuron values. After you divide with zero, the rest of the neurons value will be multiplied by this equation. By multiplying with this equation, the overall sum of neurons will be protected and its value never change.
So this is the simple thing about the dropout method. So if you have any doubts about the mathematical backgrounds behind this code, ping me on Instagram. Otherwise, feel free to comment it down. I will definitely help you.
So this is a simple concept about the dropout method in this neural network structure. So the next process is the compilation of our model. So look at my code.
You can see here I just added some parameters in the model.compile method. I just explained some parameters during the model compilation of previous CNN video. So I'm going to explain this part in a theoretical manner because I'm working on a new script which I'm going to explain very precisely about the mathematical approaches in deep learning.
I want to cover these things also. I already told this in the previous video of CNN. You can expect a new video about the mathematical formulas and approaches that we used behind the course.
Generally if I want to explain anything related to max I do some high research about that and I take some time to make it as a perfect video. So the scripting is going on I will try to upload that video as soon as possible. Okay so in the model.compile method first I added the categorical cross entropy to the loss parameter.
We are using the multi-class classification so only we are using that. categorical cross entropy. So in the previous CNN video we want only the two results either the given input image is masked image or not masked image. So in that time we applied the binary cross entropy.
Now the second parameter is about the optimizer. There are two popular approaches in the parameter that are Adam and SGD. Here we applied the SGD value to the parameter so this parameter will select only the minimal random samples from the data set and start to analyze the learning rate of the algorithm. The final parameter is the matrix that will provide the accuracy of each step of CNN algorithm.
Okay, so that's all about the compilation method. Let's move to the next step. So next step I added the data normalization steps. So in this step, I added few more parameters like shear range, zoom range, and horizontal flip and vertical flip.
The shear range will tilt the image to the value that we are given and zoom range will zoom out the images with the amount of value that we are given. Why we are adding these parameters means we are augmenting the data. The data augmentation is the process of adding the slight modifications to your original data samples that helps you to reduce the overfitting while training the deep learning model so look at this we are augmenting the data only for the training samples so in the test samples we don't add those augmentations in the test data okay next i created two variables the train data generator and your test data generator so in the train data generator i added a few parameters like target size batch size and class mode and color mode So in the target size, I added the image dimensions and in the batch size, I added the value 32. So the deep learning model take the first 32 data for processing and it is an iterative process. First it takes 32 images and next it takes another 32 images.
So this process is going on until the end of the data gets exhausted. Next one is the color mode. Our images has the RGB colors. So only we are using the RGB in the color mode parameter. And the final parameter is the class mode.
You already knew about this. We are calculating the multi-class classification so only we are using the categorical value to the parameter. You need to replicate this step for the test data generator variable also.
Okay, so that's all. Let's train this model. Our model trained successfully. Now we need to start the prediction. First of all, this process takes me 1.5 hours of training.
I'm not going to spend another 1.5 hours for re-execution of this code. Here I just saved the model. So in case of prediction you just use this model for doing the predictions. Here I declared another variable called probability. This variable is used for giving the probabilities codes of our results.
So only I used the softmax method in the variable. Now we have to start the prediction against the test data. So I'm giving here a random number in the range of 0 to 22000 of images.
I don't know which image this random number represents but we will find out the image category with the help of this model. You can see here it predicted this image is number 26 category. We don't know what fruit is present in the 26th category. For identifying the fruit in the 26th category, I just wrote this code. So when you type 26 in this code, it will get the respective fruit category for you.
So you can see here this program predicts The 26th image belongs to the cherry category. So this means our program works perfectly. And that's all about this video.
If you like this video, give a like and subscribe and share for more interesting scripts in Python. Thanks for watching and thank you. See you on next another video.