Deploying Image Classification Model on Streamlit

Nwosu Rosemary
Analytics Vidhya
Published in
2 min readMar 29, 2021

This is the second phase of my image classification model. The article on how was done can be found here.

For the second phase, I deployed the model to streamlit. To make this possible, you will need to apply for an invite and once you have gotten it, you can start your deployment.

Install Package:

First, you will need to install streamlit into your local machine using

pip install streamlit

Then run => streamlit hello

This will open up the web page on your localhost. For more information, you can check out their documentation .

App.py:

For my project, I already saved my model as a .hdf5 file. I created a new file called app.py, then imported libraries which included streamlit. Since my model involved transfer learning, I had to import tensorflow hub.

For the UI, I created a custom css file and and opened it as a file in my app.py. Then I created the title and markdown which explained what the web app does.

In my def main(), I added file uploading functionality, and a button while pointing to the prediction model to predict the class of images uploaded.

In the def predict(image), I called my classifier model which I already saved, defined image shape and model variables. The model variable, I had to use the custom objects function from Keras since the model wasn’t from the hub. Though this process was made possible by importing the Tensorflow hub library.

Next, I created the test image variable, I resized it, changed it to an array, scaled it and used numpy library to expand dimensions. Then I created the class names which will act as the label for the predicted image.

For prediction, I used the model.predict function for the test image, the scores using softmax and numpy and finally the result which had the class names listed.

Deploying model:

To deploy the model, I had to install the library that will create the requirements.txt file. This file is what will enable your model to work on another machine when your code is ran.

pip install pipreqs

Then => pipreqs /path/to/project.

Live Project:

Bag classifier is the link to the live project.

Demo:

Conclusion:

This is a simple streamlit project for image classification, everything you will need is found here . You can connect with me on LinkedIn for suggestions or corrections. Thank you for reading.

--

--

Nwosu Rosemary
Analytics Vidhya

Data Scientist || Machine Learning enthusiast and hobbyist