Implementation of the neural style transfer algorithm (Gatys, Ecker, Bethge) using PyTorch. See accompanying blog post.
Generally follows the PyTorch tutorial, with some modifications.
Style transfer can run either on a single content image, or a directory of content images.
Style image + Content image -> Result
The style image used here is Nocturne in Black and Gold, the Falling Rocket by James Abbott McNeill Whistler.
Create and activate a new virtual environment:
$ conda create -n style_transfer python=3
$ conda activate style_transfer
Install dependencies:
$ pip install -r requirements.txt
$ python single_neural_style_transfer.py
In single_neural_style_transfer.py
:
-
Setting the style image
- On line 258:
style_path = 'style_imgs/marbled_paint_1.jpeg'
- Replace this path with the path to the image you want to use as style input.
- On line 258:
-
Setting the content image
- On line 259:
content_path = 'content_imgs/clouds/clouds12.jpg'
- Replace this path with the path to the image you want to use as content input.
- On line 259:
-
Setting the output path
- On line 275:
torchvision.utils.save_image(output, 'output.png')
- By default, the result will save to a file named
output.png
in the same directory. - If you want, replace this with the filepath where you would like the output image to go.
- On line 275:
$ python batch_neural_style_transfer.py
In batch_neural_style_transfer.py
:
-
Setting the style image
- On line 249:
style_path = 'style_imgs/turner_sunset.jpg'
- Replace this path with the path to the image you want to use as style input.
- On line 249:
-
Setting the content image directory
- On line 250:
content_paths = 'content_imgs/*/*'
- Replace this path with the directory of images you'd like to use as content input.
content_images/*/*
will grab nested directories in thecontent_images
directory.content_images/*
will grab all images inside thecontent_images
directory (unnested).
- On line 250:
-
Setting the output path
- On line 251:
output_folder = 'results/turner_sunset/'
- Replace this with the directory you would like the output image results to go.
- On line 251:
- Image size:
imsize = 256
- By default, the resulting images will be 256 x 256 pixels.
- Style and content weights, number of iterations
- Can tweak style_weight, content_weight, and num_steps.
- More steps -> more stylized.
def run_style_transfer(cnn, norm_mean, norm_std,
content_img, style_img, input_img,
num_steps=300, style_weight=1000000, content_weight=1):
single_neural_style_transfer.py
: Run neural style transfer on a single content image.batch_neural_style_transfer.py
: Run neural style transfer on a directory of content images.requirements.txt
: Contains dependencies for project to be installed.
Optional:
style_imgs/
: Directory containing images I thought would be nice to use as style input.content_images/
: Directory containing images I thought would be nice to use as content input.results/
: Directory containing results from previous runs.