API

Shell Command

$ style-transfer --help
usage: style-transfer [-h] [--artwork <path>] [--init_img <path>]
                      [--init_random] [--area <int>] [--iter <int>]
                      [--lr <float>] [--content_weight <int>]
                      [--style_weight <int>] [--content_weights <str>]
                      [--style_weights <str>] [--avg_pool] [--no_feature_norm]
                      [--preserve_color {content,style,none}]
                      [--weights {original,normalized}]
                      [--device {cpu,cuda,auto}] [--use_amp] [--use_adam]
                      [--optim_cpu] [--quality <int>] [--logging <int>]
                      [--seed <int>]
                      <content path> <style path>

Creates artwork from content and style image.

positional arguments:
  <content path>        Content image.
  <style path>          Style image.

optional arguments:
  -h, --help            show this help message and exit
  --artwork <path>      Save artwork as <path>. (default: artwork.png)
  --init_img <path>     Initialize artwork with image at <path> instead of
                        content image. (default: None)
  --init_random         Initialize artwork with random image instead of
                        content image. (default: False)
  --area <int>          Content and style are scaled such that their area is
                        <int> * <int>. Artwork has the same shape as content.
                        (default: 512)
  --iter <int>          Number of iterations. (default: 500)
  --lr <float>          Learning rate of the optimizer. (default: 1)
  --content_weight <int>
                        Weight for content loss. (default: 1)
  --style_weight <int>  Weight for style loss. (default: 1000)
  --content_weights <str>
                        Weights of content loss for each layer. Put the
                        dictionary inside quotation marks. (default:
                        {'relu_4_2':1})
  --style_weights <str>
                        Weights of style loss for each layer. Put the
                        dictionary inside quotation marks. (default: {'relu_1_
                        1':1,'relu_2_1':1,'relu_3_1':1,'relu_4_1':1,'relu_5_1'
                        :1})
  --avg_pool            Replace max-pooling by average-pooling. (default:
                        False)
  --no_feature_norm     Don't divide each style_weight by the square of the
                        number of feature maps in the corresponding layer.
                        (default: True)
  --preserve_color {content,style,none}
                        If 'style', change content to match style color. If
                        'content', vice versa. If 'none', don't change content
                        or style. (default: style)
  --weights {original,normalized}
                        Weights of VGG19 Network. Either 'original' or
                        'normalized' weights. (default: original)
  --device {cpu,cuda,auto}
                        Device used for training. (default: auto)
  --use_amp             Use automatic mixed precision for training. (default:
                        False)
  --use_adam            Use Adam instead of LBFGS optimizer. (default: False)
  --optim_cpu           Optimize artwork on CPU to move some data from GPU
                        memory to working memory. (default: False)
  --quality <int>       JPEG image quality of artwork, on a scale from 1 to
                        95. (default: 95)
  --logging <int>       Number of iterations between logs. If 0, no logs.
                        (default: 50)
  --seed <int>          Seed for random number generators. (default: random)

Python Object

class style_transfer.learn.StyleTransfer(lr=1, content_weight=1, style_weight=1000.0, content_weights="{'relu_4_2':1}", style_weights="{'relu_1_1':1,'relu_2_1':1,'relu_3_1':1,'relu_4_1':1,'relu_5_1':1}", avg_pool=False, feature_norm=True, weights='original', preserve_color='style', device='auto', use_amp=False, adam=False, optim_cpu=False, logging=50)

The central object that performs style transfer when called.

Parameters
  • lr (float) – Learning rate of the optimizer.

  • content_weight (int) – Weight for content loss.

  • style_weight (int) – Weight for style loss.

  • content_weights (str) – Weights of content loss for each layer.

  • style_weights (str) – Weights of style loss for each layer.

  • avg_pool (bool) – If True, replaces max-pooling by average-pooling.

  • feature_norm (bool) – If True, divides each style_weight by the square of the number of feature maps in the corresponding layer.

  • weights (str) – Weights of the VGG19 Network. Either 'original' weights or 'normalized' weights that are scaled such that the mean activation of each convolutional filter over images and positions is equal to one.

  • preserve_color (Union[str, None]) – If set to 'style', changes color of content image to color of style image. If set to 'content', changes color of style image to color of content image. If set to None, does not change any color.

  • device (str) – Set to 'cpu' to use CPU, 'cuda' to use GPU or 'auto' to automatically choose device.

  • use_amp (bool) – If True, uses automatic mixed precision for training.

  • adam (bool) – If True, uses Adam instead of LBFGS optimizer.

  • optim_cpu (bool) – If True, optimizes artwork on CPU, but can calculate gradients on GPU. This moves some data from GPU memory to working memory, but increases training time.

  • logging (int) – Number of iterations between logs. If set to 0, does not log.

__call__(content, style, area=512, init_random=False, init_img=None, iter=500)

Returns artwork created from content and style image.

Parameters
  • content (PIL.Image.Image) – Content image.

  • style (PIL.Image.Image) – Style image.

  • area (int) – content and style are scaled such that their area is area * area. artwork has the same shape as content.

  • init_random (bool) – If True, initializes artwork with random image.

  • init_img (Union[PIL.Image.Image, None]) – Image with which artwork is initialized. If set to None, initializes artwork either with content or randomly.

  • iter (int) – Number of iterations.

Returns

artwork that matches content of content and style of style.

Return type

PIL.Image.Image