Implementation of neural style transfer from Gatys et al. (2015). Support for VGG19 only.
app.py
is code for a Streamlit app that provides simple UI interface for running style transfer on images of the user's choice.style_transfer.py
is the main Python code.style_transfer.ipynb
is the Jupyter Notebook version.
The entrypoint for running a style transfer is the style_transfer
function from style_transfer.py
.
It takes the following parameters:
content_im
: bytes-like object representing the content imagestyle_im
: bytes-like object representing the style imageim_size
: tuple representing the dimensions of the final output image or number representing the max. side length of the final output image. Content and style images get resized according to this parameter.cb
: callback function which is run every 10 iterations.
The signature of thecb
function is as follows:
cb(current_iteration_number, num_steps, style_loss, content_loss)
num_steps
: number of iterations algorithm takes to generate the final image.content_weight
: weight that the algorithm gives to preserving the content of the content image. Defaults to 10.style_weight
: weight given to preserve the style of the style image. Defaults to 9000.content_layers
: comma-separated list of layer names used to obtain content loss. Defaults to['r4_2']
.*style_layers
: comma-separated list of layer names used to obtain style loss.
Defaults to['r1_1','r2_1','r3_1','r4_1','r5_1']
.*
* Refer to the representation of VGG19 layers below to understand which layers to pass to content_layers
and style_layers
. Simply add r
before the layer you want to target (e.g. r1_1
for the first layer with 64
output channels):
1_1 1_2 2_1. 2_2. 3_1. 3_2. 3_3. 3_4. 4_1. 4_2 4_3. 4_4. 5_1. 5_2. 5_3. 5_4. 64, 64, 'P', 128, 128, 'P', 256, 256, 256, 256, 'P', 512, 512, 512, 512, 'P', 512, 512, 512, 512, 'P'
P stands for pooling layers.
Content Image | Style Image | Result |
---|---|---|
![]() |
![]() |
![]() |