Awesome
stylize-datasets
This repository contains code for stylizing arbitrary image datasets using AdaIN. The code is a generalization of Robert Geirhos' Stylized-ImageNet code, which is tailored to stylizing ImageNet. Everything in this repository is based on naoto0804's pytorch-AdaIN implementation.
Given an image dataset, the script creates the specified number of stylized versions of every image while keeping the directory structure and naming scheme intact (usefull for existing data loaders or if directory names include class annotations).
Feel free to open an issue in case there is any question.
Usage
-
Dependencies:
- python >= 3.6
- Pillow
- torch
- torchvision
- tqdm
-
Download the models:
- download the models (vgg/decoder) manually from pytorch-AdaIN and move both files to the
models/
directory - Get style images: Download train.zip from Kaggle's painter-by-numbers dataset
- download the models (vgg/decoder) manually from pytorch-AdaIN and move both files to the
-
To stylize a dataset, run
python stylize.py
.Arguments:
--content-dir <CONTENT>
the top-level directory of the content image dataset (mandatory)--style-dir <STLYE>
the top-level directory of the style images (mandatory)--output-dir <OUTPUT>
the directory where the stylized dataset will be stored (optional, default:output/
)--num-styles <N>
number of stylizations to create for each content image (optional, default:1
)--style-map <STYLE_MAP>
an explicit content style map json. If provided, num-styles will be ignored (optional, default:None
)--alpha <A>
Weight that controls the strength of stylization, should be between 0 and 1 (optional, default:1
)--extensions <EX0> <EX1> ...
list of image extensions to scan style and content directory for (optional, default:png, jpeg, jpg
). Note: this is case sensitive,--extensions jpg
will not scan for files ending on.JPG
. Image types must be compatible with PIL'sImage.open()
(Documentation)--content-size <N>
Minimum size for content images, resulting in scaling of the shorter side of the content image toN
(optional, default:0
). Set this to 0 to keep the original image dimensions.--style-size <N>
Minimum size for style images, resulting in scaling of the shorter side of the style image toN
(optional, default:512
). Set this to 0 to keep the original image dimensions (for large style images, this will result in high (GPU) memory consumption).--crop <N>
Size for the center crop applied to the content image in order to create a squared image (optional, default 0). Setting this to 0 will disable the cropping.
The chosen styles per content image will be saved in a content_style_map.json
. This file can be used with the --style-map
argument to reproduce a specific dataset or to create an explicit content style mapping manually. Keys and values must be existing file names in the --content-dir
and --style-dir
respectively.
{
"content_image_1.jpg": ["style_1.jpg"],
"content_image_2.jpg": ["style_2.jpg", "style_3.jpg"]
}
Here is an example call:
python3 stylize.py --content-dir '/home/username/stylize-datasets/images/' --style-dir '/home/username/stylize-datasets/train/' --num-styles 10 --content_size 0 --style_size 256