Building and Deploying a Flower Classification Model with Keras and Gradio

Imagine you‘re walking through a beautiful garden filled with colorful flowers – vibrant red roses, delicate white daisies, bright yellow sunflowers, and more. Could a computer learn to recognize and name all these different flowers just like a knowledgeable gardener? It sure can! In this article, we‘ll walk through how to train a deep learning model that can classify images of flowers into different categories like roses, daisies, dahlias, and tulips. Then we‘ll use a simple tool called Gradio to turn our trained model into an interactive web application that anyone can use to identify flowers from their own photos. Along the way, we‘ll break down the key concepts and code. By the end, you‘ll understand how to build your own image classifiers and deploy them as web apps!

The Power of Deep Learning for Image Classification

In recent years, deep learning has achieved remarkable results in computer vision tasks like image classification. Deep convolutional neural networks (CNNs) in particular have proven very effective at analyzing the content of images and recognizing the objects in them.

At a high level, a CNN works by learning a hierarchy of visual features and patterns in images. The early layers of the network detect simple features like edges and textures. The middle layers combine these into more complex shapes and patterns. And the last layers piece together the activations from previous layers to recognize high-level objects and structures, like a flower. During training, the CNN automatically learns which features are most useful for the classification task by tuning the numerical weights on the connections between its many neurons.

While the inner workings of CNNs may seem complex, modern deep learning libraries like Keras make it simple to define the architecture of a CNN and train it on an image dataset. Let‘s see how we can use Keras to build a CNN for categorizing photos of flowers.

Training a Flower Classification Model in Keras

The first step is to obtain a labeled dataset of flower images to train and evaluate our model on. Luckily, the TensorFlow team provides a dataset of 3670 photos of flowers from 5 different species that we can download:

import tensorflow as tf
import pathlib

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file(‘flower_photos‘, origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

The images are organized into subdirectories by flower type: daisy, dandelion, roses, sunflowers, and tulips. We can read them into a tf.data.Dataset object using the image_dataset_from_directory utility:

batch_size = 32
img_height = 180 
img_width = 180

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

This splits the data into a training set with 80% of the images and a validation set with 20%. The images are also resized to a standard size and grouped into batches.

Next, we can define a CNN model using the Keras Sequential API. Here‘s an example architecture:

from tensorflow import keras 
from tensorflow.keras import layers

num_classes = 5

model = keras.Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
  layers.Conv2D(16, 3, padding=‘same‘, activation=‘relu‘),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding=‘same‘, activation=‘relu‘),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding=‘same‘, activation=‘relu‘),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation=‘relu‘),
  layers.Dense(num_classes)
])

This model has three convolutional layers that learn 16, 32, and 64 filters respectively, each followed by a max pooling layer to downsample the feature maps. The final feature maps are flattened and passed through two fully connected dense layers to produce the output scores for the 5 flower classes.

Before training, we compile the model with an optimizer, loss function, and metrics:

model.compile(
  optimizer=‘adam‘,
  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
  metrics=[‘accuracy‘])

SparseCategoricalCrossentropy is used as the loss since the labels are integers representing the flower classes. The popular Adam optimizer tunes the model‘s weights to minimize the loss.

We‘re now ready to train the CNN on our flower dataset:

epochs = 10
history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

Over 10 epochs, the model will iterate through the training set, use the current weights to make predictions on each batch, compare those predictions to the true labels to calculate the loss, and finally update the weights to reduce the loss. After each epoch, it will evaluate on the validation set to measure generalization performance.

On this small dataset, the CNN should achieve around 70-80% validation accuracy after training, enough to produce reasonable flower identifications. The model‘s performance could be improved further with a more complex architecture, data augmentation to expand the training set, and additional epochs of training.

Deploying the Keras Model with Gradio

Once we have a trained flower classification model, how can we easily deploy it as a web application for others to try? That‘s where Gradio comes in. Gradio is a Python library that provides a simple way to create web interfaces for interacting with machine learning models. Users can upload images to the interface and see the model‘s predictions with just a few lines of Python code.

First, make sure you have Gradio installed:

pip install gradio

Next, we need a function that takes an input image and returns the model‘s top predicted flower classes and their probabilities. Here‘s one way to implement that:

import numpy as np

class_names = [‘daisy‘, ‘dandelion‘, ‘roses‘, ‘sunflowers‘, ‘tulips‘]

def predict_image(img):
  img_array = tf.keras.preprocessing.image.img_to_array(img)
  img_array = tf.expand_dims(img_array, 0)

  predictions = model.predict(img_array)

  predicted_class = class_names[np.argmax(predictions[0])]
  confidence = round(100 * np.max(predictions[0]), 2)
  return {predicted_class: confidence}  

This function preprocesses the input image into the format expected by the model, runs the model‘s predict method, and returns the name of the top predicted class and its confidence score as a percentage.

Now we can create a Gradio interface that accepts a flower image upload and displays the model‘s top prediction:

import gradio as gr

image = gr.inputs.Image(shape=(180,180))
label = gr.outputs.Label(num_top_classes=1)

gr.Interface(fn=predict_image, inputs=image, outputs=label).launch()

Running this code will start the Gradio web server and open the interface in a new browser tab. Try uploading your own photos of flowers and see how well the model identifies them! The interface also provides options to share a public link to your app.

Budding Potential and Room to Grow

We‘ve seen how straightforward it is to train an image classifier in Keras and deploy it for anyone to use with Gradio‘s delightful interfaces. But this is just the beginning – there are so many exciting directions to take these techniques!

A flower classification model like this one could form the basis of a useful mobile app for gardeners and nature lovers to quickly identify plants they encounter. With a larger dataset covering more species, the model could be extended to recognize all kinds of flowers, trees, mushrooms, birds and other wildlife.

Image classifiers also have important applications in agriculture, such as detecting weeds, diseases, and pests in crops. Similar models can be used for medical image diagnosis, industrial defect detection, and categorizing objects and scenes for self-driving vehicles.

To improve the flower classifier, we could experiment with state-of-the-art CNN architectures like EfficientNet, Vision Transformers, or ConvNeXt. Newer techniques like few-shot learning could help the model adapt to recognizing novel flower species from just a handful of examples. Explaining the model‘s reasoning with salience maps or class activation heatmaps could make its predictions more interpretable and trustworthy.

I encourage you to try training your own image classification models on different datasets and deploy them with Gradio. You‘ll gain hands-on experience with the full deep learning workflow and contribute to a blossoming ecosystem of machine learning web apps!

Planting Seeds and Pollinating Knowledge

To learn more about image classification and deep learning, check out these informative resources:

Feel free to leave a comment if you have any questions! I‘d love to hear about the fascinating image recognition models you build and the creative interfaces you deploy. Happy classifying!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts