Analytics Vidhya
Published in

Analytics Vidhya

Create Your Own DeepArt Clone: Pytorch Neural Style Transfer Web App Using Streamlit (With Code and Colab Demo)

Neural Style Transfer on Nikola Tesla

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.

The App

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:

Took me longer to edit this collage than to run the A.I.

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.

The Code

All the relevant code can be found on my github:

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.

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!

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Parallel Processing using the Virtru Python SDK

Open Service Mesh — Microsoft’s SMI based Open Source Service Mesh Implementation

Terraform — Iterative Subnet Module — AWS

Version Of Java Should I Use As A Beginner

learn java with practice test

What do you mean by Numerical Integration?

Spring Boot Configuration Documentation – Two ways with IntelliJ Idea

Wrapping up Buffer

Certification helper GCP — BigQuery

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
George Davila Durendal

George Davila Durendal

AI Engineer & Founder, CEO @ HyperHelper.ai

More from Medium

Wikipedia and irregular data: How much can you fetch in one expression?

How I programmed AI which writes pop songs about AI

First steps on Text Augmentation in a non-English dataset

Technology you should know about