Classify Flower Images Using Machine Learning & Python

Classify Flower Images Using Machine Learning On Google Colab

Image for post
Image for post

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.

Programming:

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

Since I am using Google Colab for this program, I need a way to upload the flower images to the site. So, I will upload the images to my Google Drive and then mount the drive.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

The drive should now be mounted.

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

Prepare The Data For The Model

Split the data

Image for post
Image for post

Take a look at the first image.

Image for post
Image for post
Image for post
Image for post

Show the image.

Image for post
Image for post
Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

Convert the labels into a set of 5 numbers to input into the neural network.

Image for post
Image for post

Print all of the new labels.

Image for post
Image for post
Image for post
Image for post

Print the new label of the first image (the tulip picture).

Image for post
Image for post

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.

Image for post
Image for post

Create And Build The Model

To build the model we need to create the architecture using Sequential() .

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.

model.add(layers.MaxPooling2D(pool_size=(2, 2)))

Create one more convolution layer and pooling layer like before, but without the input_shape.

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.

model.add(layers.Flatten())

Now create a neural network where the first layer has 64 neurons and the activation function ReLu.

model.add(layers.Dense(64, activation='relu'))

Create the last layer of this neural network with 5 neurons (one for each label) using the softmax function.

model.add(layers.Dense(5,  activation='softmax'))

So the CNN should look like below when put together.

Image for post
Image for post
Image for post
Image for post
Convolutional Neural Network (CNN) that was built above as an image. Image taken from https://adventuresinmachinelearning.com/keras-tutorial-cnn-11-lines/

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.

Image for post
Image for post

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

Image for post
Image for post
Image for post
Image for post

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

Image for post
Image for post
Image for post
Image for post
The models accuracy is 52.65% on the test data.

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

Image for post
Image for post

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

Image for post
Image for post

Model Test

Let’s test the model by loading a new image.

Image for post
Image for post
Image for post
Image for post

Resize the image.

Image for post
Image for post

Show the prediction probabilities for this image.

Image for post
Image for post

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.

Image for post
Image for post

Sort the probabilities from least to greatest such that the highest probability is at index=4 and the lowest probability is at index = 0.

Image for post
Image for post

Print the likely class.

Image for post
Image for post
Image for post
Image for post

Looks like this model was able to accurately classify the image with a 26.07% accuracy score.

Save the model.

Image for post
Image for post

To load the model.

Image for post
Image for post

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.

Image for post
Image for post

Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems

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 ).

Resources:

[1] Keras tutorial — build a convolutional neural network in 11 lines

[2] Build your first Convolutional Neural Network to recognize images

[3] Create your first Image Recognition Classifier using CNN, Keras and Tensorflow backend

[4] Building a Convolutional Neural Network (CNN) in Keras

[5] Machine Learning A-Z: Download Practice Datasets

[6] Building A Deep Learning Model using Keras

[7] Python | Image Classification using keras GeeksforGeeks

[8] Building powerful image classification models using very little data

[9] Image classification with Keras and deep learning

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store