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.
You can learn more about Streamlit on their website.
You can find the source code on my GitHub. Any feedback and suggestions are welcome.
This is a flower classification web app where you upload the flower's picture and the model predicts the class of that…
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.
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(
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.
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.
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
history = my_model.fit(train_data, epochs=10,
After training, we can save the model to use in our application.
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.
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.
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.