Deploying simple image classifier to Heroku (tensorflow, streamlit and heroku)
Github repository for this post:
So everybody was talking about streamlit and how easy it was to create an “app” (more like a simple front end) for data science projects, so I wanted to test it. Before, usually I would do that using flask or dash and I got to say… Streamlit is waaaay simpler and straight to the point.
In this project, I used a pre-trained convolutional neural network to create a image classifier using tensorflow. Then, using streamlit, created a simple web front-end where user can input a image url. The model will classify the image in one of the imagenet categories and display it. Finally, deployed it on Heroku server, open to public.
About the code
Nothing too fancy here:
- Function to download image from url and resize it to a target size
- Pre process image according to the convolutional neural net used
- Decode result from index to imagenet category name
- Made a function to load the model just to be able to cache it
- Use simple st.write to make the streamlit output
About deploying
A couple of tricks:
- I used
tensorflow-cpu
on the requirements to reduce slug size (free tier limits it to around 500MB) - When creating the app on heroku, I connect it to a github branch, in this case the
heroku-deploy
branch of my repo, and opted to automatic deploy.
- So every time I push to this branch, it automatically triggers a deployments
- I created a
setup.sh
with streamlit configuration. I got this from the streamlit docs - On the
Procfile
, I run thesetup.sh
and then the streamlit app
Well, that’s basically it!
Closing thoughts
I can totally understand all the hype around streamlit, since it makes building a web front end to showcase your model results incredibly easy, and usually this is an area where a lot of data scientists have trouble, specially the ones coming from a statistics/maths background. I’ll probably be using it to prototype simple apps quick and easy!
See you around!