Flowers Recognition and Classification Using Deep Learning Model

saheed adeyemi
3 min readFeb 1, 2021
The image is gotten from my model

How perfect are you when it comes to Identifying species of flowers? In this article I have built a deep learning model with PyTorch to Identify the specie of some flowers with very high accuracy. I got the Dataset I used in this work from Kaggle.

Let’s begin by importing the necessary libraries

Like I said earlier the dataset was downloaded from (kaggle.com). It contains 4242 images of flowers, The pictures are divided into five classes(species): daisy, tulip, rose, sunflower, dandelion. For each class there are about 800 photos. Photos are not high resolution, about 320x240 pixels. Photos are not reduced to a single size, they have different proportions!

Now, Lets download the dataset

The dataset has been downloaded, lets look into the dataset to see what it contains

from the output above, we can see that the dataset contains ‘sunflower’, ‘rose’, ‘daisy’, ‘tulip’, ‘flowers’, ‘dandelion’.

NOTE: look carefully at the output(species) above, ‘flowers’ is not a specie of flower, therefore we need to remove ‘flowers’.

with the code above, the data has been clean. Now lets check the number of pictures of flowers in each species.

from the output above we have about 734 images of sunflowers, 784 rose flowers, 769 daisy, 984 tulip and 1055 dandelion.

Now, lets import the dataset into pytorch since we are using pytorch in this work

using the ImageFolder class from torchvision

the dataset has been move to pytorch. lets check the species once again

lets check the total number of flowers available

we have about 4323 flowers available

lets have a view of one of the flowers

lets try to transform all the images into tensor because pytorch doesn’t understand images but tensor.

The images will not be clear has before the transformation because i have resize it to fit into a GPU which I will use later in this work. lets have a view of an image after transformation.

Now, Lets Split our dataset into 2 sets: TRAINING DATASET AND VALIDATION DATASET

Training dataset is part of the datasets to train our deep learning model to recognize the images it usually take about 90% of the dataset while Validation dataset is the remaining part(10%) of the dataset to test(like exam) the model if it has learn properly.

the following code are the process to split the dataset

from above we have about 3891 training dataset and 432 validation dataset

lets select the batch in which we are to train and validate the model

Let’s take a look at some sample images from the training dataloader. To display the images, we’ll need to denormalize the pixels values to bring them back into the range (0,1).

Lets use GPU(graphic processing unit) in other to make our model to work faster

transferring into GPU

Preparing the Model for training

Training the model

lets train the model by using the following code

Now that the model has been trained lets check the accuracy of the model in identifying the flowers

from the above output the validation accuracy is 0.2044270783662796(20.4%) which is very poor.

we are going to continue training the model using Epoch and choosing Hyperparameters. this can be done by the following

from the output above the accuracy has move 0.7721 about 77.21% as we can see from the last epoch

lets continue training and see if we can get a better accuracy

from the above output we are able to get up to 79% accuracy

Prediction and testing of MODEL

lets use our model to predict some random flower

WOW! our model recognize the flower above correctly. the name of the flower is tulip and the model got it right

lets try another one

our model got this right too

lets try another one

OH OOO! our model got this wrong , the correct answer is daisy our model predicted dandelion

lets try another one

our model got this right.

As you can see our Deep learning model was able to recognize 3 out of 4 flowers correctly.

Thanks very much for reading, Iam looking forward to your comments

--

--