Flower detection & classification using CNN
Flower Recognition System
CNN (Convolutional Neural Networks)
Convolutional Neural Network is a Machine Learning algorithm that will take an image and will predict the object in the image based on how it is trained.
Let’s talk about the working of CNN
CNN is created using four layers. The first layer is the input layer. The second layer is the Convolution layer. The third layer is the Pool layer.
As you can see in the above image. There is a 6×6 matrix M1. There is a 3×3 matrix M2 which is called the filter. We will slide the M2 matrix over the M1 matrix until every pixel is scanned at least once. As we slide the M2 over M1, we will have a 3X3 scanned matrix M3 which is just a patch of M1. Now from this M3 matrix, our convolutional layer will find important features from the M3 matrix in numeric form. It is calculated by multiplying the weight of the filter with the M3 matrix. The result is stored in matrix M4 as seen in the picture.This is how the Convolutional layer works.
3. Pooling layer: The pooling layer is used to downsample the matrix of the data. It is used to reduce the number of parameters in the matrix. It is done to save memory and computation power and also to prevent overfitting.
Pooling is done in the same way as convolution is done. The only difference is here we will either take the maximum value in the patch or we will take the average value of the patch. Based on this, there are two types of pooling –
- MAX_POOLING – In this, we take the maximum value of the patch matrix.
- AVERAGE_POOLING – In this, we take the average value of the patch matrix.
4. Fully Connected layer: This layer flattens the matrix obtained in the pooling layer and obtains the class score. This flattened matrix is a m x 1 matrix that is passed through hidden layers of the neural network and finally, the result is predicted.
Model Architecture
This neural network consists of hidden layers. These hidden layers contain some weights on which our activation function performs some calculations. Then the result is predicted. If the predicted result is correct, then it’s okay. Otherwise, it calculates error and will backtrack and will update the weights of filters of hidden layers accordingly. This process is repeated until our error gets so minimum that it can be ignored i.e. our model starts predicting the correct results.
After that, we pass our test dataset and check if our model is predicting correct results by checking errors.
At last, for testing our model, we pass a random image to our neural network and see if it is predicting it correctly or not.
Project Prerequisites
The prerequisites for this project are – Python and Google Colab.
The other libraries that we will use are-
- Numpy – pip install numpy
- Keras – pip install keras
- Tensorflow – pip install tensorflow
Make sure that these libraries are installed in your system.
Flower Recognition using Machine Learning Project
For your convenience, we will provide the dataset to you. We split the dataset into training, testing, and Prediction folders. This dataset is divided into three parts. The first folder contains training images. These images will be passed during training. The second folder contains test images that will be passed during testing. The third folder is the prediction folder. The images in these folders will be used for final testing.
Please download the machine learning flower recognition project & dataset from the following link: https://www.kaggle.com/datasets/alxmamaev/flowers-recognition
Steps to Implement:
Below are the steps to implement flower recognition project using machine learning:
1. Import all the libraries
2. Here we are loading our datasets. We are using the flow_from_directory function of keras which loads the dataset from the folder and will also categorize it into respective categories.
3. Here we are doing the same thing for our test dataset.
4. In this, we are creating our cnn variable which is also our CNN model. This is the first step of building it and here we are defining it that we are using a sequential model of Keras.
5. Here we are doing the first step of our four steps discussed above. The first step is to pass our images through the convolutional layer. After that, it passes to the pooling layer. Here we are using Max_pool.
6. Here we are doing the same thing as above. This is done because there is a possibility that some important features might be left in the first iteration. So, we are performing it twice to get good results.
7. Here are dropping out some units in our network. We are using the dropout layer for that purpose. This layer is an important step for building our neural network because it prevents overfitting by dropping some of the units.
8. After pooling and dropping, the next step is to flatten the matrix obtained. This step is already explained above. Here we are using Flatten function in Keras for flattening.
9. Here we are making our hidden layer. As explained above, our flattened neuron layer has to pass through hidden layers where some computations occur which ultimately produces results.
10. Here we are using again the same action but with a different activation function. Here we changed the activation function because different activation function gives different results. You can try using different functions and see which one gives the best result. In our case, the ‘softmax’ function gives the best result.
11. Here are compiling our final results. For this, we have to pass an optimizer. There are a lot of optimizers. You can try them out.
12. After performing all the steps, we have finally built our Neural Network. Now we have to provide training and test datasets to train into this neural network and we are done.
13. After fine- tuning
14. Here we are providing an image to our neural network to predict the output to check if it is working correctly. So, we provide an image from the prediction folder. For testing, we provided an image of a daisy flower.
15. This code is to classify the image as to whether the flower daisy or rose or dandelion or sunflower or tulip. Because we are gonna get our answer in form of 1, 2, 3, 4, 5.
16. Here we are just printing the result.
Website that will help you a lot: https://keras.io/ (keras documentation)
Here , in this website , you will get all basic code template , and all optimizers are listed here.
Well explained!
ReplyDelete