API Reference

stylish

stylish.BATCH_SIZE = 4

Default batch size used for training.

stylish.EPOCHS_NUMBER = 2

Default epoch number used for training.

stylish.CONTENT_WEIGHT = 7.5

Default weight of the content for the loss computation.

stylish.STYLE_WEIGHT = 100.0

Default weight of the style for the loss computation.

stylish.TV_WEIGHT = 200.0

Default weight of the total variation term for the loss computation.

stylish.LEARNING_RATE = 0.001

Default Learning Rate.

stylish.LAYER_WEIGHTS = (1.0, 1.0, 1.0, 1.0, 1.0)

Default weights for each layer used for style features extraction.

stylish.train_model(style_path, training_path, output_path, vgg_path, learning_rate=0.001, batch_size=4, epoch_number=2, content_weight=7.5, style_weight=100.0, tv_weight=200.0, layer_weights=(1.0, 1.0, 1.0, 1.0, 1.0), limit_training=None)[source]

Train a style generator model for style_path on training_path.

The training duration can vary depending on the Hyperparameters specified (epoch number, batch size, etc.), the power of your workstation and the number of images in the training data.

Usage example:

>>> train_model(
...    "/path/to/style_image.jpg",
...    "/path/to/training_data/",
...    "/path/to/output_model/",
...    "/path/to/vgg_model.mat"
... )

style_path should be the path to an image from which the style features will be extracted.

training_path should be the training dataset folder.

output_path should be the path where the trained model should be saved.

vgg_path should be the path to the Vgg19 pre-trained model in the MatConvNet data format.

learning_rate should indicate the Learning Rate to minimize the loss. Default is LEARNING_RATE.

batch_size should indicate the number of training examples utilized in one iteration. Default is BATCH_SIZE.

epoch_number should indicate the number of time that the training data should be trained. Default is EPOCHS_NUMBER.

content_weight should indicate the weight of the content for the loss computation. Default is CONTENT_WEIGHT.

style_weight should indicate the weight of the style for the loss computation. Default is STYLE_WEIGHT.

tv_weight should indicate the weight of the total variation term for the loss computation. Default is TV_WEIGHT.

layer_weights should indicate a list of 5 values for each layer used for style features extraction. Default is LAYER_WEIGHTS.

limit_training should be the maximum number of files to use from the training dataset folder. By default, all files from the training dataset folder are used.

stylish.apply_model(model_path, input_path, output_path)[source]

Apply style generator model_path for input image.

Return path to image generated.

Usage example:

>>> apply_model(
...    "/path/to/saved_model/",
...    "/path/to/input_image.jpg",
...    "/path/to/output/"
... )

model_path should be the path to a Tensorflow model path that has been trained on an other image to extract its style.

input_path should be the path to an image to apply the model_path to.

output_path should be the folder where the output image should be saved.

stylish.create_session()[source]

Create a Tensorflow session and reset the default graph.

Should be used as follows:

>>> with create_session() as session:
    ...
stylish.compute_style_feature(session, path, vgg_mapping, layer_weights=(1.0, 1.0, 1.0, 1.0, 1.0))[source]

Return computed style features mapping from image path.

The style feature map will be used to penalize the predicted image when it deviates from the style (colors, textures, common patterns, etc.).

Usage example:

>>> compute_style_feature(session, path, vgg_mapping)

{
    "conv1_1": numpy.array([...]),
    "conv2_1": numpy.array([...]),
    "conv3_1": numpy.array([...]),
    "conv4_1": numpy.array([...]),
    "conv5_1": numpy.array([...])
}

session should be a Tensorflow session.

path should be the path to an image from which the style features will be extracted.

vgg_mapping should gather all weight and bias matrices extracted from a pre-trained Vgg19 model (e.g. extract_mapping()).

layer_weights should indicate a list of 5 values for each layer used for style features extraction. Default is LAYER_WEIGHTS.

stylish.compute_loss(session, input_node, style_features, vgg_mapping, batch_size=4, content_weight=7.5, style_weight=100.0, tv_weight=200.0)[source]

Create loss network from input_node.

Return a mapping with the content loss, the style loss, the total variation loss and the total loss nodes.

Usage example:

>>> compute_loss(session, input_node, style_features, vgg_mapping)

{
    "total": tf.Tensor(...),
    "content": tf.Tensor(...),
    "style": tf.Tensor(...),
    "total_variation": tf.Tensor(...)
}

session should be a Tensorflow session.

input_node should be the output tensor of the main graph.

style_features should be the style features map extracted.

vgg_mapping should gather all weight and bias matrices extracted from a pre-trained Vgg19 model (e.g. extract_mapping()).

batch_size should indicate the number of training examples utilized in one iteration. Default is BATCH_SIZE.

content_weight should indicate the weight of the content. Default is CONTENT_WEIGHT.

style_weight should indicate the weight of the style. Default is STYLE_WEIGHT.

tv_weight should indicate the weight of the total variation term. Default is TV_WEIGHT.

stylish.optimize(session, training_node, training_data, input_node, loss_mapping, output_checkpoint, writer, batch_size=4, epoch_number=2)[source]

Optimize the loss for training_node.

session should be a Tensorflow session.

training_node should be the optimizer node that should be executed.

training_data should be a list containing all training images to feed to the input_node.

input_node should be the placeholder node in which should be feed each image from training_data to train the model.

loss_mapping should be a mapping of all loss nodes as returned by compute_loss().

output_checkpoint should be the path to export each checkpoints to resume the training at any time. A checkpoint will be saved after each epoch and at each 500 batches.

writer is a FileWriter instance to record training data.

batch_size should indicate the number of training examples utilized in one iteration. Default is BATCH_SIZE.

epoch_number should indicate the number of time that the training data should be trained. Default is EPOCHS_NUMBER.

stylish.get_next_batch(iteration, content_targets, batch_size, batch_shape)[source]

Return array with image matrices according to iteration index.

iteration should be an integer specifying the current portion of the images to return.

content_targets should be the list of image paths from which the content features should be extracted.

batch_size should be the size of the image list to return.

batch_shape should be indicate the dimensions in which each image should be resized to.