Installing Neural-Style On A Mac

Eric
5 min readJul 7, 2016

--

Want your computer to make art for you? Check out Neural-Style. It can do some pretty incredible things, but holy cow is it a pain to install. Here’s a rundown on how it goes on OS X.

First read all of the neural-style installation guide. Some of it works for OS X, some doesn’t. It’s good background material.

Open a terminal window. I like iTerm2. It’s better than the plain old OS X terminal app. Everyone who does terminal things uses iTerm2. Install it.

Install torch7

Copy and paste the code from the installation guide into iTerm2. You might get a prompt about pasting multiple lines of code. It’s OK. Do it.

Lots of stuff will happen in the terminal window. You may get prompted about allowing keychain access to your Git credentials. I hit allow.

At some point, you’ll be prompted for a password. I put in my OS X password, and nothing happened. So I quit (control-c). Then did the whole thing again, and it finished doing what it does. It takes like 3 or 4 minutes.

The end of the torch install

If you see the “Not updating your shell profile.” message, then do this next:

echo “. /Users/yourusernamehere/torch/install/bin/torch-activate” \
>> .bash_profile
source ~/.bash_profile

You may get a prompt about editing bashrc or .zshrc. Type yes and hit enter.

Test the torch installation by typing th in the command line. You should see a Torch ascii art title. Type exit to return to the normal command prompt.

Install loadcaffe

Do NOT follow the instruction guide for this one. Instead, make sure you have homebrew installed. If/when you have homebrew installed, all you need to do is type the following in the command line:

brew install protobuf

Then install loadcaffe:

luarocks install loadcaffe

Install Neural-Style

The instruction guide creates a neural-style folder right in your home directory (like torch did). I put it elsewhere. It’s fine. Copy the commands from the guide, but replace cd ~/ with where ever you want neural-style installed.

Finally, do:

sh models/download_models.sh

Installing the NIN model

For people with sub-par gfx cards (like me), the “NIN Model” moves a lot faster (but takes a crapload of fiddling). Go to Google Drive to get it, and download the files into the models folder of your neural-style installation.

Have a GPU

In order to make this program bearable, a GPU is needed. Higher end Macbook Pros have nVidia GPUs, and iMacs can have either nVidia or AMD. Install CUDA for nVidia, and OpenCL for AMD. Only do one or the other. Not both.

Install OpenCL (AMD)

A brand new iMac with an AMD R9 GPU already has OpenCL installed. To install the OpenCL backend (clnn), go to the OpenCL Torch distro-cl repo and follow the instructions.

Install CUDA (nVidia)

This was a pain to install. Go to the nVidia website and download the CUDA SDK for OS X. Install the Driver, Toolkit, AND Samples. Follow the directions on the nVidia site, but replace any mention of CUDA-7.0 with CUDA-7.5 (or whatever version you installed).

READ THE INSTRUCTIONS CAREFULLY! There’s a lot to miss.

I couldn’t find the cuda-install-samples-7.0.sh script, so I had to change the permissions of the nVidia folder with:

sudo chown -R ericterpstra /Developer/NVIDIA

Below is the output of the deviceQuery sample program. If you’ve gotten this far, you’re golden.

Then continue with the steps from the neural-style instructions…

luarocks install cutorch
luarocks install cunn

BOOM! Done! I did manage to continue to Step 6 and get cuDNN installed, but I’ll leave that as an exercise for the reader. (Hints: Use version 5, symlink to /usr/local/bin).

Try It

Close iTerm2, and re-open it, and go to the neural-style folder (this ensures all the new drivers and libraries and such are available in the command window). Try the following command on for size:

th neural_style.lua \
-content_image examples/inputs/brad_pitt.jpg \
-style_image examples/inputs/picasso_selfport1907.jpg \
-output_image out.png \
-gpu 0 \
-print_iter 1 \
-seed 123 \
-backend nn \
-optimizer adam \
-model_file models/nin_imagenet_conv.caffemodel \
-proto_file models/train_val.prototxt \
-content_layers relu0,relu3,relu7,relu12 \
-style_layers relu0,relu3,relu7,relu12 \
-image_size 512 \
-init image \
-tv_weight .0001 \
-num_iterations 100 \
-content_weight 10 \
-style_weight 1000

The command line output should start like this:

Successfully loaded models/nin_imagenet_conv.caffemodel
MODULE data UNDEFINED
warning: module ‘data [type 5]’ not found
conv1: 96 3 11 11
...

And end like this:

Iteration 100 / 100
Content 1 loss: 37206.174316
Content 2 loss: 128888.750000
Content 3 loss: 59730.058594
Content 4 loss: 1006.701431
Style 1 loss: 11546.215057
Style 2 loss: 38181.282043
Style 3 loss: 31794.481277
Style 4 loss: 2.701067
Total loss: 308356.363787
A self portrait ;)

Oh, and don’t forget to read the wiki. And tell Justin Johnson that he’s awesome for making this:

@misc{Johnson2015,
author = {Johnson, Justin},
title = {neural-style},
year = {2015},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/jcjohnson/neural-style}},
}

-init image \

-tv_weight .0001 \

-num_iterations 100 \

-content_weight 10 \

-style_weight 1000

--

--