Create Your Own DeepArt Clone: Pytorch Neural Style Transfer Web App Using Streamlit (With Code and Colab Demo)
Follow me on twitter: @GDurendal
Neural Style transfer is one of those fun yet generally impractical domains of modern machine learning. Its usually hardly worth the logistical effort of setting up the environment given the relative complexity of the algorithms. Then once you do its just console commands, the process of creating images is hardly as fun or easy as it should be. There are of course sites like deepart and plenty of apps on the topic. But nothing that lets A.I. engineers insert their own models or deploy the frontend themselves. And one also gets the added latency of the App/Web Queue when using those. What if we could create our own that was a lot quicker and infinitely more customizable?
One day I found myself wanting to take on a custom Streamlit project to better understand the framework beyond simple buttons and tables. Luckily I also came across this pytorch neural style transfer demo around the same time: https://github.com/pytorch/examples/tree/master/fast_neural_style. This particular implementation gives strikingly good results (in my opinion). So I decided to create my own streamlit style transfer web app based on it.
Lets take a look at the app itself. Here’s what you’ll see when you first open it:
As you can see it is totally intuitive. Just pick a style based on the paintings given and upload your image. Lets try one of Einstein in the Mosaic style:
Now click the ‘Create Style Transfer Image’ button to run the algorithm!
And that’s it! Super easy! Speed will vary according to image size but any image under 2MB should be done in under a minute, depending on the power of your computer/internet speed if you’re using the colab. This one ran in a few seconds for me, as did most. Certainly far faster than any publicly available style transfer app.
Its so easy to use that I had time to play around with it. You can paint your dogs:
I especially like the results on old images. Sure, there are frameworks like deoldify that can restore old photos, but thats hardly any fun. Check out these results:
Can certainly see how one might prefer having paintings like this hanging up in the house over a plain old image.
Code wise the algorithm stores these images in the directory images/output-images and names them solely according to the style used so as to not clog up the directory. Can just right click and do ‘save image as’ to save the images straight from the streamlit app. But if you want to save them images in folder make sure to copy them before you use the same style again.
All the relevant code can be found on my github:
Based off of fast_neural_style example at https://github.com/pytorch/examples To run use the following commands in…
A colab demo was included for ease but it works best in a dedicated virtual environment. Open command prompt on a computer with Python 3.7+ installed and create a virtual environment:
py -m venv streamlitstyle
Then navigate to that directory: cd streamlitstyle and activate the venv using .\scripts\activate
Then simply download the repo and install the requirements using the requiremnts.txt I provided: pip install -r requirements.txt
A pytorch 1.7.1 download is also required. This can be done fairly simply following the instructions on https://pytorch.org/
Once all that is done you’re ready to run the program! Just use streamlit run streamlit_main.py and the app should open up in your browser at http://localhost:8501
Now just its just a simple interface!
And here’s a colab demo for ease of access for those that don’t have the requisite hardware.
Pytorch Neural Style Transfer algorithm on Streamlit-built UI — GeorgeDavila/PytorchNeuralStyleStreamlit
The colab demo is much more hackish since Colab and IPython shells do not support localhosting. So we have to tunnel the app through ngrok and use a corresponding ngrok.io link, follow the instructions I wrote in the colab notebook. It can be a bit convoluted but follow those instructions and you should be able to get an active tunnel up. More active usage will require opening an ngrok account, so I suggest using a local virtual environment if you want to play around with it for longer.
I even made a way to edit the streamlit code within the colab itself. Streamlit typically can only be called from console commands using streamlit run, so you can’t just run it right from a IPython shell, have to call it using console commands. So we simply write the streamlit code to a python file as text (see the text variable my_st_code). Try some basic edits yourself (if you know basic streamlit), you can edit it straight from that notebook.
And that’s it! We have a beautiful and easy to use streamlit web app! One could even host it on Heroku if so inclined (although this particular algorithm can use up a lot of compute power). Enjoy!