Image Style Transfer Tutorial

GitHub Repository

Introduction

In this tutorial, we demonstrate the workings of an iterative procedure that transfers the style from one image to the contents of another image, example below:

Style Transfer Sample

Style Transfer works by starting with an array which is just random noise. The algorithm then applies two loss functions to it:

  • Style Loss Function that gets the output image closer and closer in "Style" (as shown above) to the image whose style we want to match
  • Content Loss Function that gets the ouput image closer and closer to an image whose "Content" or looks we want to match

As we minimize these joint loss functions, the result is an image which has the content of the "Content" image and "Style" of the Style image. For more details refer the Jupyter Notebook in the repo.

Get ready for the tutorial

Make sure you've signed up and created a developer account.

You can also use the JupyterLab IDE and work through tutorial completely in the web browser. All the steps remain same, but there is no CLI installation required.

Install roro CLI tool for python3 using pip. Ensure that your are working in a isolated virtual environment.

$ pip install roro

Note, we only support python 3.5

In this tutorial you will do the following:

  1. Clone the project repo from GitHub
  2. Create a new project on rorodata platform
  3. Deploy your model in production as a RESTful API
  4. Consume your new style transfer API

Step 1: Clone the project repo from GitHub

Clone the code repository rorodata/credit-scoring-demo (manually or using git) and download the files to a local directory.

$ git clone https://github.com/rorodata/style-transfer-demo.git
$ cd style-transfer-demo

Now login into your rorodata account using roro login. Enter your login credentials to connect with your cloud account. Send us an email or ping us on our slack if you run into any issues and we shall help you out quickly.

$ roro login
Email: <your-registered-email>
Password:
Login successful.

Step 2: Create a new project on rorodata platform

Create a new project using roro create. Pick a unique name for the project. It is a good practice to keep the project name short, readable without spaces or special characters (hyphen is ok.)

$ roro create <your-project-name>
Created project: <your-project-name>

Step 3: Deploy your model in production as a RESTful API

We are almost ready to deploy our style transfer API. Before deploy, we need to edit the roro.yml configuration file. In the roro.yml file change the project label to your-project-name.

project: <your-project-name> 
runtime: rorodata/sandbox

services:
    - name: default
      function: StyleTransfer.style_transfer

StyleTransfer.style_transfer is just a reference to the model code.

Let's deploy! All we need now is a run roro deploy. Ensure that roro deploy is run the same directory that contains the roro.yml file.

$ roro deploy
Deploying project <your-project-name>. This may take a few moments ...
Building docker image ...done
Updating scheduled jobs ...done
Restarted one service.

        default: https://<your-project-name>.rorocloud.io/

Deployed version 1 of <your-project-name>

This will take a couple of minutes as the machine loads all dependencies and readies the selected hardware instances

Awesome! your style transfer API is now live.

Step 4: Consume your new style transfer API

The easiest way to test this is through our roro client or our open-source firefly library. Below we will use our roro client. Go to your python shell before executing the below code.

>> api = roro.Client("https://<your-project-name>.rorocloud.io/")

# content file is an image you want to style
>> content_file=open('path-to-content-file.jpg', "rb"), format='jpg'))

# style file is an image whose style you want to copy 
>> style_file=open('path-to-style-file.jpg', "rb"), format='jpg'))


>> result_image = api.style_transfer(content_fileobj = content_file, style_fileobj=style_file)

That's it. You have deployed your first machine learning API with rorodata.

Feedback

Help us improve this tutorial. Flag errors, issues or request how-tos, guides and tutorials on our #documentation channel on our Slack