Streamlit — use data apps to better test your model | by Dipam Vasani | Towards Data Science

We use all kinds of techniques from creating a very reliable validation set to using k-fold cross-validation or coming up with all sorts of fancy metrics to determine how good our model performs. However, nothing beats looking at the raw output. When you look at sample output, you figure out things that none of the other methods can tell you.
For example, if you are performing object detection on crops, you might see that when the crop is aligned in a certain way because of the wind, our bounding boxes don’t encapsulate the crop properly.
However, looking at sample outputs is a tedious task. Say we want to test various NMS values, and we also want to test it on a bunch of images. We can always write a function for this, but running it, again and again, is boring.
Wouldn’t it be nice to have an app where we can upload an image and use a slider to adjust the NMS value? Welcome, Streamlit.
According to their website, Streamlit is an open-source web framework for data scientists and machine learning engineers to create beautiful, performant apps in just a few hours, all in pure Python.
pip install streamlit
For this demo, I will be using the model I trained for wheat object detection using a FasterRCNN. You can go back to that article, train or, download my model and then follow along.
Streamlit is just a standard python file run from top to bottom. We will start by creating a file called app.py, and writing the following code in it.
We will run this as follows:
streamlit run app.py
It will open up our browser with the following output.
Notice that we used a # inside st.write(). This indicates that we are writing markdown inside it. The first thing we need is a file uploader to upload images to Streamlit. Again, this is as simple as doing:
We pass the type of files allowed (jpg and png in this case), and when we do attach one, we read it using PIL. When we save app.py now:
we see that Streamlit identifies that the source file has changed and asks if we want to rerun. And we do. Hence, we select “always rerun” for change to reflect automatically. Now our browser looks like this:
To display the image, we can use st.image(). However, after generating predictions, we want to replace the input image with our predicted image. To do so, we create an empty container and display everything in it.
Another thing I like about using a container is that you can set use_column_width = True, and you won’t have to worry about image resizing. Now we can drag and drop images into our app.
Finally, we can convert our image to a tensor, load our model, generate outputs, and write it to our container.
To vary NMS values, we can either use a slider or an input box to type the value or use the ‘+’ and ‘-’ buttons to increase or decrease the value. I’m going to go with the slider. Our code changes to.
And that’s it. In about just 50 lines of code, we can create this super useful app to test our model. You can also create templates for various problems like image classification, segmentation, object detection, and so on and use them every time you train a new model.
Finally, when we change the NMS values using the slider, if we’ve already generated predictions for a particular value, we don’t want to generate them again. Hence you can stick a simple decorator on top of the function:
and it will cache the results for you. This way it won’t rerun the same threshold every time but just used the cached result. Cool, isn’t it?
That will be it for this article. I would really recommend everyone to use Streamlit and create data apps. It works well for all kinds of data and caching helps with expensive operations like working with large data frames. Give it a try.
If you want to learn more about deep learning check out my deep learning series.