Building a Deep Learning Flower Classifier

How I built a Web App that can classify from five different flowers based on the uploaded image.

Rupak Karki
The Startup
4 min readOct 10, 2020

--

Built using Streamlit and Python.

Photo by Colter Olmstead on Unsplash

Introduction

After spending some time looking at Deep Learning with TensorFlow and training some models on my own, I decided to make the model meaningful. Making a model and training it is one thing, but deploying it and creating something that even people with no experience can work with is another.

That’s when I discovered Streamlit, it is an open source framework that let’s you create data apps in pure Python. I had little to no experience in creating Web Apps using JavaScript frameworks, but using Streamlit, I could do that in Python.

You can learn more about Streamlit on their website.

You can find the source code on my GitHub. Any feedback and suggestions are welcome.

Libraries used

tensorflow==2.2.0                       
streamlit==0.65.2
numpy==1.19.1
opencv_python==4.4.0.42
Pillow==7.2.0

The Model

Due to the computation power required by Deep Learning, creating and training a new model from scratch would be hard and time consuming. So I used a concept called Transfer Learning.

Transfer Learning

TL is a machine learning method where a model built and trained for one task is reused for another task. We use a pre-trained model as a starting point for our required task and re-train the model with our datasets. Transfer Learning saves model training time and can help us achieve better accuracy than we would on a model built from scratch.

I like to do the data generation and model training in Jupyter Notebook.

For the base model, I used the Xception model from Keras.

base_model = keras.applications.Xception(
include_top=False,
weights="imagenet",
input_shape=IMG_SIZE, pooling="avg")

On top of this base model, I added a Dense Layer with 5 neurons and a softmax activation. The base model was made non-trainable.

The Dataset

Directory Structure for the datasets

To generate the datasets, we need to find different pictures for our classes and group them into separate directories accordingly. You can use the above image for reference. I downloaded a flower photos dataset and then created the validation directory myself.

Generating the Train and Validation Data

For generating dataset from directory, we will use the ImageDataGenerator available in the TensorFlow library.

Generate Dataset from directory

We first create objects of this class for both training and validation data. Then we can use the method flow_from_directory of these objects to generate our datasets. You should get something like this when you run the code.

Found 3070 images belonging to 5 classes.
Found 600 images belonging to 5 classes.

Training the Model

my_model.compile(loss="categorical_crossentropy",
optimizer="adam",metrics=["accuracy"])
history = my_model.fit(train_data, epochs=10,
validation_data=val_data)

After training, we can save the model to use in our application.

keras.models.save_model(model=my_model, 
filepath="flower_classifier.hdf5")

Creating the Application

This code creates the UI for the App. We can make the UI in any ways we desire. Streamlit provides plethora of options to create the application, you can read the docs to customize the app in your own style. I chose to create a simple app with an Image chooser, the image chosen is displayed and during prediction, a progress bar is shown. Then the flower class is displayed with the accuracy.

Pre-processing

We also need to pre-process the uploaded image to the requirements of the model and also parse the predictions to display.

The two functions here pre-process the image to be fed into the model and also makes the predictions usable in the UI to display.

Running the application

We can run the app by navigating to the directory where the files are located and running a command.

$ streamlit run <APP_NAME>.py

Your browser should open automatically and the app should be displayed. Now you can choose an image and the app will predict it for you.

Result

Final Look at our app

Conclusion

Streamlit is a really helpful and easy library to quickly prototype and make data driven applications without the need for HTML, CSS, JS or Flask and Django. But it is not the all-in-one solution for all your application deployment needs. The biggest downside is that it can be slow when doing heavy tasks, the whole code is re-run every time an image is uploaded. Of course, we can use the cache function but it may not be applicable in all of the use cases.

Streamlit allows us to display images, plots, graphs and so many things on the UI for quick prototyping and proof of concept apps. It is a great tool and I recommend anyone looking for a quick and easy tool to try this out.

You can find me at my site here.

--

--