Classify Flower Images Using Machine Learning On Google Colab
Image classification is a process in computer vision that can classify an image according to its visual content. In this article I will show you how to classify different species of flowers.
The first thing that I like to do before writing a single line of code is to put in a description in comments of what the code does. This way I can look back on my code and know exactly what it does.
#Description: This program classifies flower images
Setup / Loading The Images
A link to another web page should pop up. Without that web page you should see the authorization code or be asked to sign in to your Google account to get the authorization code.
Once you get the authorization code, then enter the code in the text box that should also be displayed after running the code above and then press enter.
The drive should now be mounted.
You can look over to the left of the Google website and see your drive. In my drive I have already downloaded and unzipped the flower photos. Be sure to do the same within your Google Drive.
In my flower_photos folder, you can see I have folders (daisy, dandelion, roses, sunflowers, & tulips) that contain images of a specific flower.
Now time to come up with a smart way to store all of these files into a feature and target data set. The feature data set will contain a specific flower image and the target data set at the same corresponding row or index, will contain the label for that specific image.
In the below code, I am reading from the specific folder location and
Prepare The Data For The Model
Split the data
Take a look at the first image.
Show the image.
Print the label of the image above. The image above is a picture of tulips. It’s pretty hard to see after resizing the picture to be 32 x 32.
Convert all the labels to numerical values. The labels will be values between [0,4] inclusive.
Note: daisy = 0, dandelion = 1, rose = 2, sunflower = 3, and tulip = 4.
Convert the labels into a set of 5 numbers to input into the neural network.
Print all of the new labels.
Print the new label of the first image (the tulip picture).
Normalize the pixels in the images to be values between 0 and 1, remember that the pixel values have a range from 0 to 255.
Create And Build The Model
To build the model we need to create the architecture using
import tensorflow as tffrom tensorflow import kerasfrom keras.models import Sequentialfrom keras.layers import Dense, Flatten, Conv2D, MaxPooling2Dfrom tensorflow.keras import layersmodel = tf.keras.Sequential()
Next we add the first layer, a convolution layer to extract features from the input image, and create 32 5 x 5 ReLu convoluted features also known as feature maps. Since this is the first layer we must input the dimension shape which is a 32 x 32 pixel image with depth = 3 (RGB).
model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(32,32,3)))
The next layer will be a pooling layer with a 2 x 2 pixel filter to get the max element from the feature maps. This reduces the dimension of the feature maps by half and is also known as sub sampling.
Create one more convolution layer and pooling layer like before, but without the
model.add(layers.Conv2D(64, (5, 5), activation='relu'))model.add(layers.MaxPooling2D(pool_size=(2, 2)))
Add a flattening layer, to reduce the image to a linear array also known as a one 1-Dimension vector to feed into and connect with the neural network.
Now create a neural network where the first layer has 64 neurons and the activation function ReLu.
Create the last layer of this neural network with 5 neurons (one for each label) using the softmax function.
So the CNN should look like below when put together.
Compile the model. Give it the
categorical_crossentropy loss function which is used for classes greater than 2, the adam optimizer, and the accuracy of the model.
Train the model using the
fit() method, which is another word for train. We will train the model on the training data with batch size =25, epochs =1000, and split the data into training on 70% of the data and using the other 30% as validation. Training may take some time to finish.
Batch: Total number of training examples present in a single batch
Epoch:The number of iterations when an ENTIRE dataset is passed forward and backward through the neural network only ONCE.
Fit: Another word for train
Evaluate The Model
Evaluate the model and get the models accuracy on the test data. Accuracy is one metric for evaluating classification models. Informally, accuracy is the fraction of predictions our model got right.-Google Machine Learning Crash Course
Next, visualize the models accuracy and check for overfitting.
Overfitting is caused by the model memorizing the training data instead of learning the more-general mapping from features to labels. Overfitting is easy to diagnose with the accuracy visualizations you have available. If “Accuracy” (measured against the training set) is very good and “Validation Accuracy” (measured against a validation set) is not as good, then your model is overfitting.-datascience.stackexchange.com
The model looks like it is overfitting, so the model may not be that useful.
Visualize the models loss. Loss is the penalty for a bad prediction. That is, loss is a number indicating how bad the model’s prediction was on a single example. If the model’s prediction is perfect, the loss is zero; otherwise, the loss is greater. -Google Machine Learning Crash Course
Let’s test the model by loading a new image.
Resize the image.
Show the prediction probabilities for this image.
Add the label of the classes to an array in the index of the labels corresponding equivalent number. For example ‘daisy’ will be located at index=0 , because the corresponding label as a number is 0.
Sort the probabilities from least to greatest such that the highest probability is at index=4 and the lowest probability is at index = 0.
Print the likely class.
Looks like this model was able to accurately classify the image with a 26.07% accuracy score.
Save the model.
To load the model.
With some more testing and changing of the parameters and data it may be possible to get a better accuracy score for the model on the test data. But that is all for now ! Thank you for reading this article and I hope it was informative.
If you are also interested in reading more on machine learning to immediately get started with problems and examples then I strongly recommend you check out Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems. It is a great book for helping beginners learn how to write machine learning programs, and understanding machine learning concepts.
Thanks for reading this article I hope it’s helpful to you all! If you enjoyed this article and found it helpful please leave some claps to show your appreciation. Keep up the learning, and if you like machine learning, mathematics, computer science, programming or algorithm analysis, please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 ).