1. 程式人生 > >Cool Factor: How to Steal Styles with Machine Learning, Turi Create, and ResNet

Cool Factor: How to Steal Styles with Machine Learning, Turi Create, and ResNet

Turi Style Transfer

First of all, follow the Turi Create installation instructions on GitHub. It’s imperative to create a Python 2.7 environment with the specific dependencies required, or you’ll be in version hell. Trust me, I’ve spent time there! ?

And use Anaconda to make life easier! ?

Here’s my repository

for a pre-compiled Jupyter notebook and source images. From here, open Jupyter and let’s jump into code:

import turicreate as tc# load style and content sources
styles = tc.load_images('styles/')content = tc.load_images('content/')

Content matters, because the closer you train on examples of what your target images will look like, the more robust your results!

# create Style Transfer modelmodel = tc.style_transfer.create(styles, content, max_iterations=100)

More iterations = more better, with diminishing returns. Unless you’re on a machine with a dedicated GPU — or better, using AWS, Azure, Google, etc. — this will take a while… and you might want to run overnight with 1–5k iterations.

From here, save that model ASAP! We can easily save and load models in Turi, but note the class switches to tc for generic loads of any model type.

# save modelmodel.save('dog_style_model3.model')
# load modeltc.load_model('dog_style_model.model')

Next, let’s explore our model:

# get model characteristicsmodel
Class : StyleTransfer
Schema — — — Model : resnet-16Number of unique styles : 3Input image shape : (256, 256)
Training summary — — — — — — — — Training time : 58m 27sTraining epochs : 200Training iterations : 100Number of style images : 3Number of content images : 3Final loss : 15.9675

If you, by default, ran a verbose=True model while fitting, each iteration will have provided its loss. On my run, the final loss of 15.9675 is hovering around the lowest loss seen, so I can be “somewhat” confident that the model is good. Loss will depend on things like the complexity of your styles and the variance of your content files.

Again, you’ll need to run many iterations if you have lots of styles and/or content.

You should also give your styles descriptive filenames; the peek below will provide a good reference table.

# peek at style sourcesstyles
# outputs a reference table to filenames
Monet’s Impressionist Style