Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIMachine Learning Model Deployment using Streamlit

Machine Learning Model Deployment using Streamlit

This article was published as a part of theĀ Data Science Blogathon

Overview of Streamlit

If you are someone who has built ML models for real-time predictions and wondering how to deploy models in the form of web applications, to increase theirĀ  accessibility. You are at the right place as in this article you will be seeing how to deploy models that are already built on Machine Learning or Deep Learning.

Article overview:

  • Understand the concept of Model Deployment.
  • Perform Model deployment using Streamlit for the dog-breed classifier.

Once you are done training the model you have several options of deploying a project on the web that areĀ Flask, Django, Streamlit.

Flask and Django are somewhat heavy so it takes more than one article and time to understand them better (we would be focusing on this one also), but for now, we would be discussing Streamlit. So letā€™s start with a question.

Why Streamlit?

  • Streamlit lets you create apps for your Machine Learning project using simple code.
  • It also supports hot-reloading that lets your app update live as you edit and save your file.
  • Using streamlit creating an app is very easy, adding a widget is as simple as declaring a variable.
  • No need to write a backend, No need to define different routes or handle HTTP requests.
  • More information can be found on their website ā€” ā€” ā€“https://www.streamlit.io/.

We would be discussing how you can deploy a Deep Learning Classifier using Streamlit. For this article letā€™s take a Dog Bread Classifier, you can check how to create a Dog Bread classifier in the given article link.

Once you have gone through the above link itā€™s time to start the deployment part.Ā we
will use the model which is trained on different dog breeds. Go through the above article link,
train your model and save feature_extractor.h5,
dog_breed.h5, dog_breeds_category.pickle.

  • feature_extractor.h5 is a saved model which will extract features from images,
  • dog_breed.h5 is another saved model which will be used for prediction.
  • dog_breeds_category.pickle the file will be used to covert class_num to class_label.

Model Deployment Using Streamlit

Once you have all the required files letā€™s start with Streamlit installation procedure and build a web application.

Installing Streamlit

Running the following command will install all the dependencies and will set Streamlit in your python environment.

pip install streamlit

Setting up the Project Structure for Model Deployment using Streamlit

Creating a Directory tree is not required but it is a good practice to organize your files and folders.

Model Deployment using Streamlit | project structure
Source: Local

Start by creating a project_folder, inside the project folder create another folderĀ static and put all the downloaded files inside static and also create folder imagesĀ inside static. now create an empty main.py and helper.py file and place it in the project directory.

Create prediction Pipeline

Creating a predictor function that will take an uploaded pictureā€™s path as input and give different dog breed classes as output.

  • The predictor function will handle all the image processing, model loading require for the prediction.
  • The predictor function will be coded in helper.py to keep our structure look ordered.

Letā€™s begin by loading all of the required libraries:

import cv2

import os

import numpy as np

import pickle

import tensorflow as tf

from tensorflow.keras import layers

from tensorflow.keras import models,utils

import pandas as pd

from tensorflow.keras.models import load_model

from tensorflow.keras.preprocessing.image import load_img,img_to_array

from tensorflow.python.keras import utils

Loading the saved model from the directory:

current_path = os.getcwd()

# getting the current path

dog_breeds_category_path = os.path.join(current_path, 'static\dog_breeds_category.pickle')

# loading class_to_num_category

predictor_model = load_model(r'static\dogbreed.h5')

with open(dog_breeds_category_path, 'rb') as handle:

Ā Ā Ā Ā dog_breeds = pickle.load(handle)

# loading the feature extractor model

feature_extractor = load_model(r'static\feature_extractor.h5')

In the above code chunk, we are loading the different categories of Dog Breeds using the pickle file and then we are loading the weights file (.h5 file) that has training weights. Now you will be defining a predictor functionĀ that takes the imageā€™s path as input and returns prediction.

def predictor(img_path): # here image is file nameĀ 

Ā Ā Ā Ā img = load_img(img_path, target_size=(331,331))

Ā Ā Ā Ā img = img_to_array(img)

Ā Ā Ā Ā img = np.expand_dims(img,axis = 0)

Ā Ā Ā Ā features = feature_extractor.predict(img)

Ā Ā Ā Ā prediction = predictor_model.predict(features)*100

Ā Ā Ā Ā prediction = pd.DataFrame(np.round(prediction,1),columns = dog_breeds).transpose()

Ā Ā Ā Ā prediction.columns = ['values']

Ā Ā Ā Ā predictionĀ  = prediction.nlargest(5, 'values')

Ā Ā Ā Ā prediction = prediction.reset_index()

Ā Ā Ā Ā prediction.columns = ['name', 'values']

Ā Ā Ā Ā return(prediction)

In the above block of code, we have performed the following operations:-

  1. First passing image path to predictor function.
  2. The function reads the image in (331,331) and converts it into an array.
  3. Then it converts the image array into tensors (4-d) array for prediction.
  4. Then it passes tensors toĀ feature_extractor function to get extracted features for prediction, now our extracted features will become input for predictor_model
  5. And finally, it passes extracted features to predictor_model and gets final prediction then converts into data frame to get our prediction in the desired format

The predictor_function returns the top 5 detected dog breeds with their prediction confidence in a data frame.

Now you have a function ready that takes image path and gives prediction which we will call from our web app.

Creating Frontend

Our goal is to create a web app where we can upload a picture and then save that picture in theĀ static/images directory for the prediction part.

Pipeline

  1. Create an upload button and save uploaded pics in the directory.
  2. The function predictor will take an uploaded imageā€™s path as input and would give the output.
  3. Show the uploaded image.
  4. Show the top-5 predictions with their confidence percentage in a barplot.
  5. After prediction, delete the uploaded picture from the directory

Frontend Streamlit code will be written inĀ main.py andĀ to make use of the predictor function created in helper.py we need to import theĀ predictor function in main.py file. Letā€™s check the code part for the given pipeline.

from helper import *

#importing all the helper fxn from helper.py which we will create later

import streamlit as st

import os

import matplotlib.pyplot as plt

import seaborn as sns

sns.set_theme(style="darkgrid")

sns.set()

from PIL import Image

st.title('Dog Breed Classifier')

In the above code, we have first imported all the dependencies and then created an app with the title ā€œDog Breed Classifierā€. Itā€™s time to define a function to save uploaded images.

def save_uploaded_file(uploaded_file):

Ā Ā Ā Ā try:

Ā Ā Ā Ā Ā Ā Ā Ā with open(os.path.join('static/images',uploaded_file.name),'wb') as f:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā f.write(uploaded_file.getbuffer())

Ā Ā Ā Ā Ā Ā Ā Ā return 1Ā Ā Ā Ā 

Ā Ā Ā Ā except:

Ā Ā Ā Ā Ā Ā Ā Ā return 0

This function saves the uploaded pics to theĀ static/images folder.

Creating Upload button, display uploaded image on the app, and call the predictor function which we had just created.

uploaded_file = st.file_uploader("Upload Image")

# text over upload button "Upload Image"

if uploaded_file is not None:

Ā Ā Ā Ā if save_uploaded_file(uploaded_file):Ā 

Ā Ā Ā Ā Ā Ā Ā Ā # display the image

Ā Ā Ā Ā Ā Ā Ā Ā display_image = Image.open(uploaded_file)

Ā Ā Ā Ā Ā Ā Ā Ā st.image(display_image)

Ā Ā Ā Ā Ā Ā Ā Ā prediction = predictor(os.path.join('static/images',uploaded_file.name))

Ā Ā Ā Ā Ā Ā Ā Ā os.remove('static/images/'+uploaded_file.name)

Ā Ā Ā Ā Ā Ā Ā Ā # deleting uploaded saved picture after prediction

Ā Ā Ā Ā Ā Ā Ā Ā # drawing graphs

Ā Ā Ā Ā Ā Ā Ā Ā st.text('Predictions :-')

Ā Ā Ā Ā Ā Ā Ā Ā fig, ax = plt.subplots()

Ā Ā Ā Ā Ā Ā Ā Ā axĀ  = sns.barplot(y = 'name',x='values', data = prediction,order = prediction.sort_values('values',ascending=False).name)

Ā Ā Ā Ā Ā Ā Ā Ā ax.set(xlabel='Confidence %', ylabel='Breed')

Ā Ā Ā Ā Ā Ā Ā Ā st.pyplot(fig)

Letā€™s discuss the above code:

  • You can write text anywhere in the program using the st.write() method.
  • The Image.open()Ā reads image and st.image() method display the image in the application.
  • os.remove() removes the uploaded file after prediction.
  • st.file_uploader(ā€˜Upload Imageā€™) creates an upload button.
  • Whatever file will be uploaded will be passed to save_uploaed function in order to save the uploaded file.
  • For plotting the prediction bar-plot we are using seaborn in Streamlit.
  • sns.barplot() creates barplot.
  • Plotted bar-plot will be sorted according to their confidence percentage.

Ā 

Model Deployment using Streamlit web app 1
Source: Local

Run Web App

Run the web app on your browser by running the command:-

streamlit run main.py

HereĀ main.py is the file containing all the frontend code.

main.py | Model Deployment using Streamlit
Source: Local

So Far we build the web app using Streamlit and it runs as a website on your local computer.

Model Deployment using Streamlit Over Internet

Deploying over the internet increases the accessibility of your application. After deployment app can be accessed from mobile, computer from anywhere in the world.

Streamlit gives you aĀ Streamlit Share feature to deploy your Streamlit web app for free on the internet.

  • Once
    your account is created, you simply need to push the deployment folder
    (local repo) to ā€˜Githubā€™ (remote repo) and from here Streamlit takes
    care of the rest. as you can see down below the deployment page on streamlit.io.
  • I personlally would not suggest deploying the model on Streamlit share as itĀ is not much flexible.

You have multiple choices when it comes to hosting your model on the cloud. AWS, GCD, AZURE CLOUD are some popular services nowadays.

Heroku is a free online model hosting service. you can host the model on the cloud for free. Deploying apps on Heroku is more flexible, Managing your app, package versions, storage is a lot easier with Heroku.

Conclusion

Creating a Machine Learning model is not enough until you make it available to general use or to a specific client. If you are working for a client you probably need to deploy the model in the clientā€™s environment, but if you are working on a project that needs to be publicly available you should use technology to deploy it on the web. Streamlit is the best lightweight technology for web deployment.

So by this, we come to an end to the article.Ā I hope you enjoyed it and now can start creating beautiful apps yourself.

The whole code and architecture can be downloaded from this link.

Thank you for reading this article! There is actually a lot more to learn about Streamlit, I hope you can give me some claps and ask a lot
of questions in the comment box!

The media shown in this article are not owned by Analytics Vidhya and are used at the Authorā€™s discretion.
Thetechwriters

15 Oct 2021

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?