Algorithmia Blog

Getting Started with Neural Style Transfer from the Command Line


Want a fun way to quickly convert photos and images into the style of masterpiece paintings and drawings?

The deep learning technique called style transfer is a fun way of reimagining one image in the style of another.

In this post we’re going to show you how to use the command line to apply style transfer filters to your images using the Deep Filter algorithm.

Confused? Check out our demo of using deep learning for artistic style transfer.

Prerequisite:

You will need two things to complete this recipe:

Step 1: Install the Command Line Tool

The latest releases & changelog can be found here.

Linux and OS X

Install is by running:

curl -sSf https://raw.githubusercontent.com/algorithmiaio/algorithmia-cli/master/install.sh | sh

Windows

Download and extract the latest Windows x86_64 zip file. We recommend putting it in your PATH.

Find both MSVC and GNU builds for x86 and x86_64 on the CLI release page.

Step 2: Configure Authentication

Run algo auth from the command line, and then enter your Algorithmia API key.

$ algo auth

Configuring authentication for 'default' profile

Enter API Key (prefixed with 'sim'): [YOUR API KEY]

Profile is ready to use. Test with 'algo ls'

Alright, let’s confirm our setup and call an algorithm from the command line.

Step 3: Confirm Setup

To call any algorithm from the CLI, the syntax is algo run followed by the algorithm’s username and algorithm name (kenny/factor), the data options (-d), and finally the input (10).

$algo run kenny/factor -d 10

Completed in 0.0 seconds

[2,5]

In this case, we called the algorithm Factor by user kenny to make sure everything is working properly.

Step 4: Upload Images

The Algorithmia CLI makes it easy to work with files in the Data API. The first thing to do is create a new folder:

Diegos-MacBook-Pro:pics diegooppenheimer$ algo mkdir .my/PicsToFilter

Created directory: data://.my/PicsToFilter

The CLI will automatically recognize .my as a folder in your Data Collection.

Second I will copy all files from my local directory into my new PicsToFilter folder:

Diegos-MacBook-Pro:pics diegooppenheimer$ algo cp * data://.my/PicsToFilter

Uploaded data://diego/PicsToFilter/momo_mishka.jpg

Uploaded data://diego/PicsToFilter/loowit.jpg

Uploaded data://diego/PicsToFilter/momo.jpg

Finished uploading 3 file(s)

We can quickly verify everything is there:

Diegos-MacBook-Pro:pics diegooppenheimer$ algo ls data://diego/PicsToFilter

loowit.jpg       momo.jpg         momo_mishka.jpg

Step 5: Apply Style Transfer

Now that all our images are in a folder, I can easily filter all of them.

Let’s first create a folder for the filtered images:

algo mkdir data://diego/FilteredPics

I then run a bash loop to apply a style transfer filter to each image in my PicsToFilter folder then save the result to my FilteredPics folder:

Diegos-MacBook-Pro:Dev diegooppenheimer$ for file in $(algo ls data://diego/PicsToFilter); do algo run deeplearning/DeepFilter -d "{\"images\":[\"data://diego/PicsToFilter/$file\"],\"savePaths\":[\"data://diego/FilteredPics/$file\"],\"filterName\":\"post_modern\"}";done;

Completed in 7.7 seconds

{"savePaths":["data://diego/FilteredPics/loowit.jpg"]}

Completed in 8.9 seconds

{"savePaths":["data://diego/FilteredPics/momo.jpg"]}

Completed in 5.4 seconds

{"savePaths":["data://diego/FilteredPics/momo_mishka.jpg"]}

Completed in 7.7 seconds

Let’s explain whats going on here: First, we are looping around all the files in the PicsToFilter directory "for file in $(algo ls....".

Then, once we are aware of all the files in the directory we are calling DeepFilter on each image (algo run deeplearning/DeepFilter...), and then saving it to our FilteredPics folder.

Step 6: View Your Neural Style Art

Head to your Algorithmia Hosted Data (if you’re logged in, the root of the folder can be found here https://algorithmia.com/data/hosted).

You should find all your filtered pictures under My Collections/FilteredPics (sign-in required).

Conclusion

In this post we showed you how to easily transform images using the Deep Filter algorithm and the Algorithmia CLI.

Here is the complete set of instructions to try yourself after installing the CLI and configuring the authentication:

$ algo mkdir .my/PicsToFilter

$ algo cp * data://.my/PicsToFilter

$ algo mkdir data://diego/FilteredPics

$ for file in $(algo ls data://diego/PicsToFilter); do algo run deeplearning/DeepFilter -d "{\"images\":[\"data://diego/PicsToFilter/$file\"],\"savePaths\":[\"data://diego/FilteredPics/$file\"],\"filterName\":\"post_modern\"}";done;

Get this recipe and more in the Algorithmia sample apps repo on Github.

Deep Learning on Algorithmia: